Pumping Lemma for Context-Free Languages CSCI 3130 Formal Languages and Automata Theory Siu On CHAN Fall 2019 Chinese University of Hong Kong 1/18
regular context-free These languages are not regular Are they context-free? 2/18 L 1 = { a n b n | n � 0 } L 2 = { z | z has the same number of a’s and b’s } L 3 = { a n b n c n | n � 0 } L 4 = { zz R | z ∈ { a , b } ∗ } L 5 = { zz | z ∈ { a , b } ∗ }
An attempt Let’s try to design a CFG or PDA read a / push x read b / pop x ??? 3/18 L 3 = { a n b n c n | n � 0 } S → a B c | ε B → ???
Suppose we could construct some CFG G for L 3 How does a long e.g. derivation look like? 4/18 S ⇒ CC ⇒ SBC ⇒ SCSC ⇒ SSBSC S → CC | BC | a ⇒ SSBBCC B → CS | b ⇒ a SBBCC C → SB | c ⇒ aa BBCC ⇒ aab BCC ⇒ aabb CC ⇒ aabbc C ⇒ aabbcc
Repetition in long derivations a c C c C b B S b B a S C B S If a derivation is long enough, some variable must appear twice on C S the same root-to-leave path in a parse tree 5/18 S ⇒ CC ⇒ SBC ⇒ SCSC ⇒ SSBSC ⇒ SSBBCC ⇒ a SBBCC ⇒ aa BBCC ⇒ aab BCC ⇒ aabb CC ⇒ aabbc C ⇒ aabbcc
Pumping example b a B C S a B b S B C C c S B b C c C c aaabbcbcc S B Then we can “cut and paste” part of parse tree b S C S a B C S a B S a B b C c C c aabbcc S C S 6/18
Pumping example We can repeat this many times Every suffjciently large derivation will have a middle part that can be repeated indefjnitely 7/18 aabbcc ⇒ aaabbcbcc ⇒ aaaabbcbcbcc ⇒ . . . ⇒ ( a ) i ab ( bc ) i c
Pumping in general x A v A v A w x x A y uv 3 wx 3 y u A w y uwy v u u u A v A w x y uvwxy A uv 2 wx 2 y v A v A w x x y 8/18
Example uv 3 wx 3 y , … 9/18 L 3 = { a n b n c n | n � 0 } If L 3 has a context-free grammar G , then for any suffjciently long s ∈ L ( G ) s can be split into s = uvwxy such that L ( G ) also contains uv 2 wx 2 y , What happens if s = a m b m c m No matter how it is split, uv 2 wx 2 y / ∈ L 3
Pumping lemma for context-free languages w non-empty one is At least y x v For every context-free language L u There exists a number m such that for every long string s in L 10/18 ( | s | � m ), we can write s = uvwxy where 1. | vwx | � m 2. | vx | � 1 3. For every i � 0 , the string uv i wx i y is in L � m
Pumping lemma for context-free languages To prove L is not context-free, it is enough to show that 11/18 For every m there is a long string s ∈ L , | s | � m , such that for every way of writing s = uvwxy where 1. | vwx | � m 2. | vx | � 1 there is i � 0 such that uv i wx i y is not in L
Using the pumping lemma 1. for every m 3. no matter how the pumping lemma splits s into uvwxy 12/18 L 3 = { a n b n c n | n � 0 } 2. there is s = a m b m c m (at least m symbols) ( | vwx | � m , | vx | � 1 ) 4. uv 2 wx 2 y / ∈ L 3 (but why?)
Using the pumping lemma aaa Then uv 2 wx 2 y does not have the same number of a’s, b’s and c’s bcccc x b bb v Case 1: v or x contains two kinds of symbols a Case 2: v and x both contain (at most) one kind of symbol bbcccc v aa aabb 13/18 � �� � Then uv 2 wx 2 y / ∈ L 3 because the pattern is wrong ���� ���� Conclusion: uv 2 wx 2 y / ∈ L 3
14/18 Which is context-free? L 1 = { a n b n | n � 0 } ✓ L 2 = { z | z has the same number of a’s and b’s } ✓ L 3 = { a n b n c n | n � 0 } ✗ L 4 = { zz R | z ∈ { a , b } ∗ } ✓ L 5 = { zz | z ∈ { a , b } ∗ }
Example 1. for every m 3. no matter how the pumping lemma splits s into uvwxy aaa a v aba a x aaab 15/18 L 5 = { zz | z ∈ { a , b } ∗ } 2. there is s = a m ba m b (at least m symbols) ( | vwx | � m , | vx | � 1 ) 4. Is uv 2 wx 2 y / ∈ L 5 ?
Example aaa aaab x a aba v a 1. for every m 3. no matter how the pumping lemma splits s into uvwxy 15/18 L 5 = { zz | z ∈ { a , b } ∗ } 2. there is s = a m ba m b (at least m symbols) ( | vwx | � m , | vx | � 1 ) 4. Is uv 2 wx 2 y / ∈ L 5 ? ���� ����
Example 1. for every m 3. no matter how the pumping lemma splits s into uvwxy 16/18 L 5 = { zz | z ∈ { a , b } ∗ } 2. there is s = a m b m a m b m (at least m symbols) ( | vwx | � m , | vx | � 1 ) 4. Is uv i wx i y / ∈ L 5 for some i ? Recall that | vwx | � m
Example Three cases bb vwx aaaaabbbbbaaa aabbb Case 3 aaabbbbb vwx 17/18 aaaaabb bbbaa Case 2 bbaaaaabbbbb vwx aaa aabbb Case 1 � �� � vwx is in the fjrst half of a m b m a m b m � �� � vwx is in the middle part of a m b m a m b m � �� � vwx is in the second half of a m b m a m b m
Example aaaaabb bbbaa Not of the form zz bb vwx aaaaabbbbbaaa aabbb Case 3 aaabbbbb vwx Case 2 bbaaaaabbbbb vwx aaa aabbb Case 1 18/18 Apply pumping lemma with i = 0 � �� � uwy becomes a j b k a m b m , where j < m or k < m � �� � uwy becomes a m b j a k b m , where j < m or k < m � �� � uwy becomes a m b m a j b k , where j < m or k < m This covers all cases, so L 5 is not context-free
Recommend
More recommend