200 likes | 519 Views
Fall 2011. The Chinese University of Hong Kong. CSCI 3130: Formal languages and automata theory. Limitations of pushdown automata. Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130. Non context-free languages. ✔. L 1 = { a n b n : n ≥ 0}
E N D
Fall 2011 The Chinese University of Hong Kong CSCI 3130: Formal languages and automata theory Limitations of pushdown automata Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130
Non context-free languages ✔ L1 = {anbn: n ≥ 0} L2 = {s: s has same number of as and bs} L3 = {anbncn: n ≥ 0} L4 = {ssR: s {a, b}*} L5 = {ss: s {a, b}*} ✔ ? These are not regular Are they context-free?
An attempt • Let’s try to design a CFG or PDA L3 = {anbncn: n ≥ 0} read a / push x S → aBc | e B → ?? read b / pop x ???
What would happen if... • Suppose we could construct some CFG for L3, e.g. • Let’s do some long derivations • S BC CSC aSC aBCC abCC abaC abaSB • abaBCB ababCB ababaB • ababab S BC B CS | b C SB | a . . .
Repetition in long derivations • If derivation is long enough, some variable mustappear twice on same path in parse tree S • S BC CSC aSC aBCC abCC abaC abaSB • abaBCB ababCB ababaB • ababab B C C S S B B C B C b b b a a a
Pumping example • Then we can “cut and paste” part of parse tree S S ababbabb ababab ✗ C B B C C S B S a b C C B B C S S B b b a S B B C B C b B C a b a b a b a b
Pumping example • We can repeat this many times • Every sufficiently large derivation will have a middle part that can be repeated indefinitely ababbabb ababbbabbb ababab ✗ ✗ ababnabnbb
Pumping in general u u u u y y y y A A A A w v v v x x x A A A w v v x x A A w v x A w uv2wx2y uvwxy uv3wx3y uwy
Example L3 = {anbncn: n ≥ 0} • If L3 has a context-free grammar G, then • What happens for anbncn? • No matter how it is split, uv2wx2y∉L3! If uvwxy is in G, so are uv2wx2y, uv3wx3y, uwy, ... aaa ... aabbb ... bbccc ... cc x v w y u
Pumping lemma for context-free languages • Pumping lemma: For every context-free language L There exists a number nsuch that for every string z in L, we can write z = uvwxy where |vwx| ≤ n |vx| ≥ 1 For every i≥ 0, the string uviwxiy is in L. x v w y u
Pumping lemma for context-free languages • So to prove L is not context-free, it is enough that For every nthere exists z in L, such that forevery way of writing z = uvwxy where |vwx| ≤ n and |vx| ≥ 1, the string uviwxiy isnot in L for some i≥ 0. x v w y u
Proving language is not context-free • Like for regular languages, you need a strategy that always wins you this game Donald choose nwrite z = uvwxy(|vwx| ≤ n,|vx| ≥ 1) you choose zLchoose iyou win if uviwxiyL 1 2 At least oneis not empty x v w y u ≤ n
Example Donald choose nwrite z = uvwxy(|vwx| ≤ n,|vx| ≥ 1) you choose zLchoose iyou win if uviwxiyL 1 2 L3 = {anbncn: n ≥ 0} choose n z= anbncn 1 write z = uvwxy i = ? 2 aaa ... aabbb ... bbccc ... cc x v w y u
Example • Case 1:v or x contains two kinds of symbolsThen uv2wx2y not in L3 because pattern is wrong • Case 2: v and x both contain one kind of symbolThen uv2wx2y does not have same number of as, bs, cs aaa ... aabbb ... bbccc ... cc v x aaa ... aabbb ... bbccc ... cc v x
More examples ✔ L1 = {anbn: n ≥ 0} L2 = {s: s has same number of as and bs} L3 = {anbncn: n ≥ 0} L4 = {ssR: s∈ {a, b}*} L5 = {ss: s∈ {a, b}*} ✔ ✘ ✔ Which is context-free?
Example L5 = {ss: s∈ {a, b}*} choose n z= anbanb 1 write z = uvwxy i = ? 2 a a a a a a a a a b a a a a a a a a a b What if: a a a a a a a a a b a a a a a a a a a b x v w y u x v w y u
Example L5 = {ss: s {a, b}*} choose n z= anbnanbn 1 write z = uvwxy i = ? 2 Recall that |vwx| ≤ n a a a a a a b b b b b b a a a a a a b b b b b b x v w y u
Example Three cases a a a a a a b b b b b b a a a a a a b b b b b b Case 1: x v w vwx is in the first half of anbnanbn a a a a a a b b b b b b a a a a a a b b b b b b Case 2: x v w vwx is in the middle part of anbnanbn a a a a a a b b b b b b a a a a a a b b b b b b Case 3: x v w vwx is in the second half of anbnanbn
Example Apply pumping with i = 0 a a a a a a b b b b b b a a a a a a b b b b b b Case 1: x v w uwy looks like ajbkanbn, where j < n or k < n a a a a a a b b b b b b a a a a a a b b b b b b Case 2: x v w uwy looks like anbjakbn, where j < n or k < n a a a a a a b b b b b b a a a a a a b b b b b b Case 3: x v w uwy looks like anbnajbk, where j < n or k < n
Example Apply pumping with i = 0 L5 = {ss: s∈ {a, b}*} Case 1: uv0wx0y looks like ajbkanbn, where j < n or k < n Not of the form ss Case 2: uv0wx0y looks like anbjakbn, where j < n or k < n Not of the form ss uv0wx0y looks like anbnajbk, where j < n or k < n Case 3: Not of the form ss This covers all the cases, so L5 is not context-free