Pumping Lemma for CFG By Mr R Karunamoorthi
Pumping Lemma for CFG By, Mr. R. Karunamoorthi, AP / CSE
Pumping Lemma for CFG Think of languages that cannot be CFL == think of languages for which a stack will not be enough e. g. , the language of strings of the form ww
Why pumping lemma? • A result that will be useful in proving languages that are not CFLs – (just like we did for regular languages) • But before we prove the pumping lemma for CFLs …. – Let us first prove an important property about parse trees 3
Observe that any parse tree generated by a CNF will be a binary tree, where all internal nodes have exactly two children (except those nodes connected to the leaves). A BC; B a The “parse tree theorem” Given: – Suppose we have a parse tree for a string w, according to a CNF grammar, G=(V, T, P, S) Parse tree for w S = A 0 A 1 – Let h be the height of the parse tree Implies: – |w| ≤ 2 h-1 A 2 . . . h = tree height Ah-1 A CNF parse tree’s string yield (w) can no longer be 2 h-1 a w 4
To show: |w| ≤ 2 h-1 Proof…The size of parse trees Proof: (using induction on h) Basis: h = 1 Derivation will have to be “S a” |w|= 1 = 21 -1. Ind. Hyp: h = k-1 |w|≤ 2 k-2 Ind. Step: h = k S will have exactly two children: S AB Heights of A & B subtrees are at most h-1 w = w. A w. B, where |w. A| ≤ 2 k-2 and |w. B| ≤ 2 k-2 Each NT will lead to one leaf terminal |w| ≤ 2 k-1 Parse tree for w S = A 0 A B h = height w. B w. A w 5
Implication of the Parse Tree Theorem (assuming CNF) Fact: – If the height of a parse tree is h, then • ==> |w| ≤ 2 h-1 Implication: – If |w| ≥ 2 h, then • Its parse tree’s height is at least h+1 6
The Pumping Lemma for CFLs Let L be a CFL. Then there exists a constant N, s. t. , – if z L s. t. |z|≥N, then we can write z=uvwxy, such that: 1. |vwx| ≤ N – middle portion is not too long (u v is not empty) 2. vx≠ - v and x are places to be pumped. Atleast one should not be empty. 3. For all k≥ 0: uvkwxky L. v and x can be pumped any no. of times and resulting string is still in L Note: we are pumping in two places (v & x) 7
Proof: Pumping Lemma for CFL • If L=Φ or contains only , then the lemma is trivially satisfied (as it cannot be violated). Here |z|= N = 0. • For any other L which is a CFL: – – Let G be a CNF grammar for L Let m = number of variables in G Choose N=2 m. Pick any z L s. t. |z|≥ N the parse tree for z should have a height ≥ m+1 (by the parse tree theorem) 8
Parse tree for z condn 2 Meaning: Repetition in the last m+1 variables (Ai=Aj) Atleast m+1 occurrences of variables A 0 to Ah Only m diff variables in G So atleast 2 of last m+1 variables are same S = A 0 levels m > , s le b ia m var A 1 A 2. . . z h-m≤ i < j ≤ h S = A 0 + Ai = Aj Ai h ≥ m+1 Aj Ah-1 Ah=a u v m+1 x w y z = uvwxy Therefore, vx≠ (No unit prod) 9
Extending the parse tree… condn 3 S = A 0 Replacing Aj with Ai (k times) Or, replacing Ai with Aj Ai=Aj Aj h ≥ m+1 Ai Ai v v x u y … w z = uvkwxky y z = uwy x … u w ==> For all k≥ 0: uvkwxky L 10
Proof condn 1 • Also, since Ai’s subtree no taller than m+1 ==> the string generated under Ai‘s subtree, which is vwx, cannot be longer than 2 m (=N) But, 2 m =N ==> |vwx| ≤ N This completes the proof for the pumping lemma. 11
Example 1 • • • Let L be the language { 0 n 1 n 2 n | n 1 }. Show that this language is not a CFL. Suppose that L is a CFL. Then some integer p exists and we pick z = 0 p 1 p 2 p. Since z=uvwxy and |vwx| p, we know that the string vwx must consist of either: – – – • • all zeros all ones all twos a combination of 0’s and 1’s a combination of 1’s and 2’s The string vwx cannot contain 0’s, 1’s, and 2’s because the string is not large enough to span all three symbols. Now “pump down” where i=0. This results in the string uwy and can no longer contain an equal number of 0’s, 1’s, and 2’s because the strings v and x contains at most two of these three symbols. Therefore the result is not in L and therefore L is not a CFL.
Example 2 • Let L be the language { aibjck | 0 i j k }. Show that this language is not a CFL. This language is similar to the previous one, except proving that it is not context free requires the examination of more cases. • Suppose that L is a CFL. • Pick z = apbpcp as we did with the previous language. • As before, the string vwx cannot contain a’s, b’s, and c’s. We then pump the string depending on the string vwx as follows: – There are no a’s. Then we try pumping down to obtain the string uv 0 wx 0 y to get uwy. This contains the same number of a’s, but fewer b’c or c’s. Therefore it is not in L. – There are no b’s but there a’s. Then we pump up to obtain the string uv 2 wx 2 y to give us more a’s than b’s and this is not in L. – There are no b’s but there are c’s. Then we pump down to obtain the string uwy. This string contains the same number of b’s but fewer c’s, therefore this is not in C. – There are no c’s. Then we pump up to obtain the string uv 2 wx 2 y to give us more b’s or more a’s than there are c’s, so this is not in C. • Since we can come up with a contradiction for any case, this language is not a CFL language.
Example 3 • Let L be the language {ww | w {0, 1}*}. Show that this language is not a CFL. • As before, assume that L is context-free and let p be the pumping length. • This time choosing the string z is less obvious. One possibility is the string: 0 p 1. It is in L and has length greater than p, so it appears to be a good candidate. • But this string can be pumped as follows so it is not adequate for our purposes: 0 p 1 000… 000 0 1 0 000… 0001 u v w x y
Example 3 • This time lets try z=0 p 1 p instead. We can show that this string cannot be pumped. • We know that |vwx| p. – Let’s say that the string |vwx| consists of the first p 0’s. If so, then if we pump this string to uv 2 wx 2 y then we’ll have introduced more 0’s in the first half and this is not in L. – We get a similar result if |vwx| consists of all 0’s or all 1’s in either the first or second half. – If the string |vwx| matches some sequence of 0’s and 1’s in the first half of z, then if we pump this string to uv 2 wx 2 y then we will have introduced more 1’s on the left that move into the second half, so it cannot be of the form ww and be in L. Similarly, if |vwx| occurs in the second half of z, them pumping z to uv 2 wx 2 y moves a 0 into the last position of the first half, so it cannot be of the form ww either. – This only leaves the possibility that |vwx| somehow straddles the midpoint of z. But if this is the case, we can now try pumping the string down. uv 0 wx 0 y = uwy has the form of 0 p 1 i 0 j 1 p where i and j cannot both equal p. This string is not of the form ww and therefore the string cannot be pumped and L is therefore not a CFL.
- Slides: 15