Showing posts with label PEG. Show all posts
Showing posts with label PEG. Show all posts

Intersection of a CFL with the Prefixes of a CFL

2008/07/16

Just a quick note on a decision problem: a classical result from the theory of context-free languages is the undecidability of the emptiness of the intersection of two CFLs. Put more concretely, given L1 and L2 two CFLs, we cannot decide whether L1∩L2=∅.

In the context of PEGs (Ford, 2004), the disjointness of two expressions can be ensured by the emptiness of the intersection of the set of prefixes of the context-free approximation of the first expression with the context-free approximation of the second: given e1 and e2 two parsing expressions, we consider two context-free approximations L1 and L2 with ℒ(e1)⊆L1 and ℒ(e2)⊆L2, and we want to check that Pre(L1)∩L2=∅, where Pre(L)={x|∃y, xy∈L}.

Goofing Around

At first sight, there is little indication whether the problem is decidable or not. The set of prefixes of a context-free language is in general context-free (for instance Pre({anbn|n≥0})={anbm| 0≤m≤n}), and its intersection with a context-free language is in general context-sensitive (for instance Pre({anbncm| m, n≥0})∩{ambncn|m, n≥0}=a*∪{anbncn| n≥0}).

On the other hand, this is hardly conclusive, and the hypothesis that one of the languages is closed under the Pre operation might be just enough to make the problem decidable. For instance, couldn't we encode this intersection in a "higher-order" grammatical formalism with decidable emptiness, like indexed grammars (Aho, 1968)?

The Hard Way

It turns out that this slight variation of the emptiness of intersection problem is not decidable either. Considering the reduction of the Post correspondence problem given by Harrison (1978, pages 254–258), an instance of PCP

x=(x1, x2, ..., xn)  y=(y1, y2, ..., yn)  xi, yi∈{a,b}+

is associated with the languages

L(x,y) = {baik···bai1cxi1···xikcyRjl···yRj1caj1b···ajlbc | k,l≥1, 1≤ip,jq≤n, 1≤p≤k, 1≤q≤l}
Ls = {w1cw2cwR2cwR1c | w1, w2 ∈ {a,b}+}

with intersection

L(x,y) ∩ Ls = {t1ct2ctR2tR1c | t1=baik···bai1, t2=xi1···xik=yi1···yik}

which is nonempty if and only if there is a solution to the PCP instance (x,y).

Considering now the intersection Pre(L(x,y))∩Ls, observe that the four c symbols of Ls reject any proper prefix of L(x,y). Thus the emptiness of the intersection of the set of prefixes of a CFL with a CFL is not decidable.

The Easy Way

Sometimes it takes me a little while to figure something very simple. Consider two context-free languages L1 and L2 over an alphabet Σ, and consider the intersection Pre(L1·{a})∩(L2·{a}) with a not in Σ. This intersection is empty if and only if L1∩L2 is.

More on Parsing Expression Languages

2008/03/13

Back to PELs (Ford, 2004) with a small comparison with Church Rosser languages (McNaughton et al., 1988) this time. Both enjoy a linear time membership problem, include some context-sensitive languages and the full class of DCFLs, and are closed under complement.

I had wondered for some time whether all PELs were Church Rosser, but I just found out that the following Gladkij language was not growing context-sensitive and thus not Church Rosser (Buntrock and Otto, 1998):

ℒGladkij = { wcwRcw | w ∈ {a,b}* }.

We can however recognize ℒGladkij with a PEG since it is the intersection of two deterministic context-free languages

ℒ1 = { wcwRcw' | w,w' ∈ {a,b}* }
ℒ2 = { w'cwcwR | w,w' ∈ {a,b}* }.

Since PELs are closed under intersection and include all the DCFLs, it follows that ℒGladkij is a parsing expression language. The following PEG recognizes ℒGladkij (I'm not too fond of predicates, so I prefer to stick with the ordered choice operator):

S ← ((W' c W / ε) Z / ε) W c W'
W' ← a W' / b W' / ε
W ← a W a / b W b / c
T ← a / b / c
Z ← T Z / T

The expression W' c W recognizes ℒ2, and the expression W c W' recognizes ℒ1. If an input string is in ℒ2, then the expression (W' c W / ε) Z fails, thus the next alternative ε is used and the input string also has to be in ℒ1 in order to be accepted by the grammar. If an input string is not in ℒ2, then (W' c W / ε) Z succeeds and consumes the whole input through Z, therefore the grammar cannot accept the input.

I have currently no clue whether an inclusion of the Church Rosser languages in the class of parsing expression languages would hold.

A Note on Parsing Expression Languages

2008/03/11

Thanks to a recent excursion into the realm of RCGs, I have found the answer to another comparison with the boolean closure of some context-free languages: the comparison with PEL (Ford, 2004).

Parsing expression languages are closed under union, intersection, and complement, and properly include DCFLs (Birman and Ullman, 1973). Thus a comparison with the boolean closure of DCFLs is of some interest (on the other hand, it is widely conjectured that they do not capture all the CFLs). Using again the results of Kutrib et al. (2004, Theorem 17), we know that the language

ℒexp = { a2n | n ≥ 1 },

which is not semilinear, is not in Bool(CFL) and thus not in Bool(DCFL).

But we can generate this language with the PEG

S ← a S a / a a

(see Exercise 6.1.1 by Aho and Ullman, 1972). Thus the inclusion of Bool(DCFL) in the set of parsing expression languages is proper.