Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2004 SDU Lecture8 NON-Context-free languages.  2004 SDU 2 Are all languages context free? Ans: No. # of PDAs on  < # of languages on  Pumping lemma:

Similar presentations


Presentation on theme: " 2004 SDU Lecture8 NON-Context-free languages.  2004 SDU 2 Are all languages context free? Ans: No. # of PDAs on  < # of languages on  Pumping lemma:"— Presentation transcript:

1  2004 SDU Lecture8 NON-Context-free languages

2  2004 SDU 2 Are all languages context free? Ans: No. # of PDAs on  < # of languages on  Pumping lemma: A technique to show that specific given languages are not context free.

3  2004 SDU 3 Statement of Pumping Lemma Pumping lemma for context-free languages: If A is a context-free language, then there is a number p (the pumping length) where, if s is any string in A of length at least p, then s may be divided into five pieces s = uvxyz satisfying the conditions. 1.For each i  0, uv i xy i z  A, 2.|vy| > 0, and 3.|vxy|  p. uvxyzuvxyz uxzuxz uvvxyyz uvvvxyyyz uvvvvxyyyyz uvvvvvxyyyyyz

4  2004 SDU 4 Proof of pumping lemma Idea: If a sufficiently long string s is derived by a CFG, then there is a repeated variable on a path in the parse tree. We can substitute the smaller sub-tree rooted at the lower occurrence for the larger sub-tree rooted at the higher occurrence to build infinitely many longer strings (and one shorter string, i = 0 case) derived by the CFG. Think: why not use PDA to prove? T R R....... uxvyz

5 The pumping lemma in work: example S  “R” is a regular expression R  0 | ( R )* “0” is a regular expression “(0)*” is a regular expression “((0)*)*” is a regular expression “(((0)*)*)*” is a regular expression … “((0)*)*” is a regular expression u = “( v = ( x = 0 y = )* z = )*” is a regular expression uv 0 xy 0 z: “(0)*” is a regular expression uv 1 xy 1 z: “((0)*)*” is a regular expression uv 2 xy 2 z: “(((0)*)*)*” is a regular expression uv 3 xy 3 z: “((((0)*)*)*)*” is a regular expression s R R R 0 “ is a regular expression ” ( ) * ( ) *

6 The pumping lemma in work: example S  “R” is a regular expression R  0 | ( R )* “((0)*)*” is a regular expression u = “( v = ( x = 0 y = )* z = )*” is a regular expression s R R R 0 “ is a regular expression ” ( ) * () * ( R ) * uv 2 xy 2 z

7 The pumping lemma in work: example S  “R” is a regular expression R  0 | ( R )* “((0)*)*” is a regular expression u = “( v = ( x = 0 y = )* z = )*” is a regular expression s R 0 “ is a regular expression ” ( ) * R uxz

8  2004 SDU 8 Details of Proof of Pumping Lemma How long does s have to be?  Let b be the maximum number of symbols in the right-hand side of a rule.  Assume b  2. //why? –Each node in a parse tree using this grammar can have no more than b children. –At most b leaves are 1 step from the start variable; at most b 2 leaves are at most 2 steps from the start variable; at most b h leaves are at most h steps from the start variable.

9  2004 SDU 9 Details of Proof of Pumping Lemma (contd.) So, if the height of the parse tree is at most h, the length of the string generated is at most b h  Let |V | = number of variables in G Set p = b |V|+1  Because b  2, so a parse tree for any string in A of length at least p requires height at least |V | + 1. Therefore, let s in A be of length at least p.

10  2004 SDU 10 Details of Proof of Pumping Lemma (contd.) Choose a parse tree T of s with smallest number of nodes. The longest path of T must have length  |V| +1 Consider the lowest |V|+1 variables on this path. On this sub-path some variable R must repeat. T R R....... uxvyz We start with a smallest parse tree that derives s

11  2004 SDU 11 Details of Proof of Pumping Lemma (contd.) Replace the sub-tree under the 2 nd occurrence of R with the sub-tree under the 1 st occurrence of R and still get a legal parse tree. Therefore we may cut s into 5 pieces uvxyz as the figure indicates and we may repeat the 2 nd and 4 th pieces to obtain longer stings in the language. T R R....... uR vy z xvy

12  2004 SDU 12 Details of Proof of Pumping Lemma (contd.) In other words, uv i xy i z is in A for any i  0. even if i = 0. For i=0, replace the sub-tree under the 1 st occurrence of R with the sub-tree under the 2 nd occurrence of R u z T R.... x

13  2004 SDU 13 Details of Proof of Pumping Lemma (contd.) |vy|>0? T R R....... u xvyz R....... u x z T TT’ T’ has fewer nodes than T and also derives s

14  2004 SDU 14 Details of Proof of Pumping Lemma (contd.) |vxy|  p? Note that the sub-tree rooted at first R has height at most |V|+1, so, the string vxy has length at most p = b |V|+1.

15  2004 SDU 15 Application of pumping lemma A = {0 n 1 n 2 n | n  0} is not context free. Proof. Assume, on the contrary, that A is context free. By pumping lemma, there exists a constant p such that every w  A of length  p can be divided into w = uvxyz such that 1.|vxy|  p 2.|vy| > 0 3.For every i  0, uv i xy i z  A. Let w = 0 p 1 p 2 p. Since |vxy|  p, vxy is either in 0*1* or 1*2*. So, it is not the case that uv 2 xy 2 z has the same number of 0’s, 1’s as 2’s.

16  2004 SDU 16 Application of pumping lemma B = {ww | w  {0,1}*}is not context free. Proof. Assume B is context free. Let p be the constant from the pumping lemma for B. Let w = 0 p 1 p 0 p 1 p, which is in B. Then w can be written as w = uvxyz such that 1.|vxy|  p 2.|vy| > 0 3.For every i  0, uv i xy i z  B. If v contains a symbol from the first 0 p, then y cannot contain one from the second 0 p, so pumping does not work. If v contains a symbol from the first 1 p, then y cannot contain one from the second 1 p, so, pumping lemma does not work. If v contains only symbols from the second 0 p 1 p, then pumping lemma does not work.

17  2004 SDU 17 C={0 n 1 n 0 n 1 n | n  0} is not context free. The proof is similar to that of B. Does this contradict that Context free languages are closed under concatenation?  C 1 = {0 n 1 n | n  0}, C 2 = {0 n 1 n | n  0},  Is C = C 1  C 2 ? What happens if we change the stack to be first- in-first-out? Or use two first-in-last-out stacks?


Download ppt " 2004 SDU Lecture8 NON-Context-free languages.  2004 SDU 2 Are all languages context free? Ans: No. # of PDAs on  < # of languages on  Pumping lemma:"

Similar presentations


Ads by Google