Download presentation
Presentation is loading. Please wait.
Published byHomer Caldwell Modified over 9 years ago
1
Non-CF Languages The language L = { a n b n c n | n 0 } does not appear to be context-free. Informal: A PDA can compare #a’s with #b’s. But by the time b’s are processed, the stack is empty. Not possible to count a’s with c’s. The problem of A * vAy : If S * uAz * uvAyz * uvxyz L, then S * uAz * uvAyz * … * uv i Ay i z * uv i xy i z L as well, for all i=0,1,2,…
2
Pumping Lemma for CFLs Idea: If we can prove the existence of derivations for elements of the CFL L that use the step A * vAy, then a new form of ‘v-y pumping’ holds: A * vAy * v 2 Ay 2 * v 3 Ay 3 * …) Observation: We can prove this existence if the parse- tree is tall enough.
3
Recall Parse Trees Parse tree for S AbbcBa * cbbccccaBca cbbccccacca S b b ac A c c ca A c c c B B
4
Pumping a Parse Tree A A u vxy z If s = uvxyz L is long, then its parse-tree is tall. Hence, there is a path on which a variable A repeats itself. We can pump this A–A part. S
5
A Tree Tall Enough Let L be a context-free language, and let G be its grammar with maximal b symbols on the right side of the rules: A X 1 …X b A parse tree of depth h produces a string with maximum length of b h. Long strings implies tall trees. Let |V| be the number of variables of G. If h = |V|+2 or bigger, then there is a variable on a ‘top-down path’ that occurs more than once.
6
uvxyz L A A u vxy z By repeating the A–A part we get… S
7
uv 2 xy 2 z L A u v x y z R A A v x y … while removing the A–-A gives… S
8
uxz L A uz In general uv i xy i z L for all i=0,1,2,… S x
9
Pumping Lemma for CFL For every context-free language L, there is a pumping length p, such that for every string s L and |s| p, we can write s = uvxyz with 1) uv i xy i z L for every i {0,1,2,…} 2) |vy| 1 3) |vxy| p Note that 1) implies that uxz L 2) says that v and y cannot be both empty strings Condition 3) is not always used. (It is not crucial part of pumping lemma, but helps to reduce the number of cases.)
10
Formal Proof of Pumping Lemma Let G=(V, ,R,S) be the grammar of a CFL. Maximum size of rules is b 2: A X 1 …X b A string s requires a minimum tree-depth log b |s|. If |s| p=b |V|+2, then tree-depth |V|+2, hence there is a path and variable A where A repeats itself: S * uAz * uvAyz * uvxyz It follows that uv i xy i z L for all i=0,1,2,… Furthermore: |vy| 1 because tree is minimal |vxy| p because bottom tree with p leaves has a ‘repeating path’
11
Pumping lemma for {a n b n c n | n >= 0} Assume that B = {a n b n c n | n 0} is CFL Let p be the pumping length, and s = a p b p c p B P.L.: s = uvxyz = a p b p c p, with uv i xy i z B for all i 0 Options for vxy: 1) The strings v and y are uniform (v=a…a and y=c…c, for example). Then uv 2 xy 2 z will not contain the same number of a’s, b’s and c’s, hence uv 2 xy 2 z B 2) At least one of v or y is not uniform. (i.e., it has at least two different symbols occurring in it). Then uv 2 xy 2 z will not be a…ab…bc…c Hence uv 2 xy 2 z B
12
Pumping lemma applied to {a n b n c n } continued Assume that B = {a n b n c n | n 0} is CFL Let p be the pumping length, and s = a p b p c p B P.L.: s = uvxyz = a p b p c p, with uv i xy i z B for all i 0 We showed: For every way of partitioning s into uvxyz, there is an i such that uv i xy i z is not in B. Contradiction. B is not a context-free language.
13
Another example Proof that C = {a i b j c k | 0 i j k } is not context-free. Let p be the pumping length, and s = a p b p c p C P.L.: s = uvxyz, such that uv i xy i z C for every i 0 vxy can’t have a’s and c’s. Why? So only two options for vxy: 1) vxy belongs to a*b*, then the string uv 2 xy 2 z has not enough c’s, hence uv 2 xy 2 z C 2) vxy belongs to b*c*, then the string uv 0 xy 0 z = uxz has too many a’s, hence uv 0 xy 0 z C Contradiction: C is not a context-free language.
14
D = { ww | w {0,1}* } (Ex. 2.22) Carefully take the strings s D. Let p be the pumping length, take s=0 p 1 p 0 p 1 p. Three options for s=uvxyz with 1 |vxy| p: 1)If a part of y is to the left of | in 0 p 1 p |0 p 1 p, then second half of uv 2 xy 2 z starts with “1” 2) Same reasoning if a part of v is to the right of middle of 0 p 1 p |0 p 1 p, hence uv 2 xy 2 z D 3) If x is in the middle of 0 p 1 p |0 p 1 p, then uxz equals 0 p 1 i 0 j 1 p D (because i or j < p) Contradiction: D is not context-free.
15
Pumping lemma for CFG - remarks Using the CFL pumping lemma is more difficult than the pumping lemma for regular languages. You have to choose the string s carefully, and divide the options efficiently. Additional CFL properties would be helpful (like we had for regular languages). What about closure under standard operations?
16
Union Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1 A 2 is context free as well. Proof: Assume that the two grammars are G 1 =(V 1, ,R 1,S 1 ) and G 2 =(V 2, ,R 2,S 2 ). Construct a third grammar G 3 =(V 3, ,R 3,S 3 ) by: V 3 = V 1 V 2 { S 3 } (new start variable) with R 3 = R 1 R 2 { S 3 S 1 | S 2 }. It follows that L(G 3 ) = L(G 1 ) L(G 2 ).
17
Intersection, Complement? Let again A 1 and A 2 be two CF languages. One can prove that, in general, the intersection A 1 A 2, and the complement Ā 1 = * \ A 1 are not context free languages.
18
Intersection, Complement? Proof for complement: Recall that a problem in HW 5 shows that L = { x#y | x, y are in {a, b}*, x != y} IS context-free. Complement of this language is L’ = { w | w has no # symbol} U { w | w has two or more # symbols} U { w#w | w is in {a,b}* }. We can show that L’ is NOT context-free.
19
Context-free languages are NOT closed under intersection Proof by counterexample: Recall that in an earlier slide in this lecture, we showed that L = {a n b n c n | n >= 0} is NOT context-free. Let A = {a n b n c m | n, m >= 0} and B = L = {a n b m c m | n, m >= 0}. It is easy to see that both A and B are context-free. (Design CFG’s.) This shows that CFG’s are not closed under intersection.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.