250 likes | 416 Views
CS 3240: Languages and Computation. Properties of Context-Free Languages. Properties of CFGs. Chomsky Normal Form. In Summary. Conversion to Chomsky Normal Form. q j. q 1. q k. Recall: Pumping Lemma for Regular Languages.
E N D
CS 3240: Languages and Computation Properties of Context-Free Languages
qj q1 qk Recall: Pumping Lemma for Regular Languages For every regular language L, there is a finite pumping length p, such that for any string sL and |s|p, we can write s=xyz with:1) x yi z L for every i{0,1,2,…}2) |y| 13) |xy| p
Pumping Lemma for CFL Theorem: For every context-free language L, there is a pumping length p, such that for any string sL and |s|p, we can write s=uvxyz with1) u vi x yi z L for every i{0,1,2,…}2) |vy| 13) |vxy| p Note that 1) implies that uxz L (take i=0), requirement2) says that v,y cannot be the empty strings ε and condition 3) is useful in proving non-CFL.
Pumping a Parse Tree S A A u v x y 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.
uvxyz L S A A u v x y z By repeating the A–A part we get…
uv2xy2z L S A A A R y v u x z y x v … while removing the A–A gives…
uxz L S A u z x In general: uvixyiz L for all i=0,1,2,…
Using Pumping Lemma “Proof by contradiction. Assume that L is context free.Let p be the pumping length of L and take sL. By the pumping lemma it is possible to write s=uvxyz(with |vy| 1 and |vxy| p) such that uvixyiz L for all i0. However… Thus the pumping lemma does not hold for s. Contradiction. The language L is not context-free.” Here you have to be clever in picking the right s. Show that pumping up or down s gives strings that are not in L. Be careful to consider all v,x,y possibilities.
Example I: Pumping anbncn Proof by contradiction; assume L = {anbncn | n0} is CF. According to the pumping lemma there is a pumping length p such that for s = apbpcp L, we can write s = uvxyz = apbpcp,with uvixyiz L for all i0. Two options for 1 |vxy| p: 1) vxy = a*b*, then the string uv2xy2z has not enough letters c, hence uv2xy2zL 2) vxy = b*c*, then the string uv2xy2z has not enough letters a, hence uv2xy2zL Contradiction: the pumping lemma does not hold, hence L is not context free.
Example II: (Pumping down) Prove that C = {aibjck | 0ijk } is not context-free. Let p be the pumping length, and s = apbpcp C. Pumping lemma: s = uvxyz, such that uvixyiz C for every i0. Two options for 1 |vxy| p: 1) vxy = a*b*, then the string uv2xy2z has not enough letters c, hence uv2xy2zC 2) vxy = b*c*, then the string uv0xy0z = uxz has too many letters a, hence uv0xy0zC Contradiction: The pumping lemma does not hold forthis string apbpcp, hence C is not context-free.
Example III: ww Prove that the language D = { ww : w{0,1}* } is not CF. You must be careful when picking the strings sD… Let p be the pumping length, take s=0p1p0p1p. Options for s=uvxyz with 1 |vxy| p: • If vxy is to the left of the middle of 0p1p 0p1p,then second half of uv2xy2z starts with a “1”. • If vxy is to the right of the middle of 0p1p 0p1p, then first half of hence uv2xy2z ends with a “0”. • If x is in the middle of 0p1p 0p1p, then pumped down uxz equals 0p1i 0j1p D(because i or j < p)
Additional Notes on CFL • Let A1 and A2 be two context-free languages,then the union A1 A2 is also context free. • However, the intersectionA1A2 is not necessarily context free • The complementĀ1= *–A1 are not necessarily context free either
Make a chain of everywhere a variable occurs From each production send a link to a counter counting how many variables in the prod have not yet been found to produce When a variable is found to be generating decrement Counter accordingly When counter reaches 0 the head variable is generating