1 / 15

Pumping Lemma for CFG

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?.

jamarbaker
Download Presentation

Pumping Lemma for CFG

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pumping Lemma for CFG By, Mr.R.Karunamoorthi, AP / CSE

  2. 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

  3. 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

  4. 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” Parse tree for w Given: • Suppose we have a parse tree for a string w, according to a CNF grammar, G=(V,T,P,S) • Let h be the height of the parse tree Implies: • |w| ≤ 2h-1 S = A0 A1 A2 . . . h = tree height Ah-1 A CNF parse tree’s string yield (w) can no longer be 2h-1 a w

  5. To show: |w| ≤ 2h-1 Proof…The size of parse trees Parse tree for w 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|≤ 2k-2 Ind. Step: h = k S will have exactly two children: SAB  Heights of A & B subtrees are at most h-1  w = wAwB, where |wA| ≤ 2k-2 and |wB| ≤ 2k-2  Each NT will lead to one leaf terminal  |w| ≤ 2k-1 S = A0 A B h= height wB wA w

  6. Implication of the Parse Tree Theorem (assuming CNF) Fact: • If the height of a parse tree is h, then • ==> |w| ≤ 2h-1 Implication: • If |w| ≥ 2h, then • Its parse tree’s height is at least h+1

  7. 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: • |vwx| ≤ N – middle portion is not too long (u v is not empty) • vx≠ - v and x are places to be pumped. Atleast one should not be empty. • 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)

  8. 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=2m. • Pick any z  L s.t. |z|≥ N  the parse tree for z should have a height ≥ m+1 (by the parse tree theorem)

  9. S = A0 Ai h ≥ m+1 Aj u v x y w z = uvwxy Meaning: Repetition in the last m+1 variables (Ai=Aj) Parse tree for z condn 2 Atleast m+1 occurrences of variables A0 to Ah Only m diff variables in G So atleast 2 of last m+1 variables are same h-m≤ i < j ≤ h S = A0 + A1 Ai = Aj A2 . . . h ≥ m+1 m variables, > m levels m+1 Ah-1 Ah=a z Therefore, vx≠ (No unit prod)

  10. S = A0 Or, replacing Ai with Aj Aj w u y z = uwy Extending the parse tree… condn 3 S = A0 Replacing Aj with Ai (k times) Ai=Aj h ≥ m+1 Ai Ai u v x y v x … … ==> For all k≥0: uvkwxky L w z = uvkwxky

  11. 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 2m (=N) But, 2m =N ==> |vwx| ≤ N This completes the proof for the pumping lemma.

  12. Example 1 • Let L be the language { 0n1n2n | 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 = 0p1p2p. • 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.

  13. 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 uv0wx0y 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 are a’s. Then we pump up to obtain the string uv2wx2y 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 uv2wx2y 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.

  14. 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: 0p10p1. 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: 0p1 0p1 000…000 0 1 0 000…0001 u v w x y

  15. Example 3 • This time lets try z=0p1p0p1p 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 uv2wx2y 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 uv2wx2y 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 uv2wx2y 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. uv0wx0y = uwy has the form of 0p1i0j1p 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.

More Related