160 likes | 350 Views
Properties of CFL’s. Reading: 8.1a and 8.2. Pumping Lemma for CFLs. Let L be a context-free language. Then there exists some positive integer m such that, if s is a string of length at least m, then s can be written as s = uvwxy such that: |vwx| <=m |vx| >0
E N D
Properties of CFL’s Reading: 8.1a and 8.2
Pumping Lemma for CFLs • Let L be a context-free language. Then there exists some positive integer m such that, if s is a string of length at least m, then s can be written as • s = uvwxy such that: • |vwx| <=m • |vx| >0 • uviwxiy is also in L for any i >=0
Why is it true? • If the language is finite, then it is regular and also context-free. • If it’s infinite, it still must have a finite number of non-terminals. • So, at least one non-terminal must be used more than once. • If A is used more than once, at least one derivation of it is recursive, and at least one is not (or it would be useless.)
So… • Suppose the rule is A -> vAx A *-> w Then if I can generate s = uvwxy I can also generate s’ = uv2wx2y and uv3wx3y… by using the recursive rule for A more times.
Using the Pumping Lemma to show a language is not CF. • Assume the language is context-free. • Show it does not satisfy the pumping lemma • Conclude (by contradiction) the language could not have been context-free.
Example: axbxcx : x>0 • Opponent chooses m. • I choose s = ambmcm. • Opponent must choose uvwxy so that |vwx| is no more than m but at least one symbol. • There is no way that vwx contains all three characters. It contains either one or two. • Pumping the string up adds more of one or two characters than the third.
Example: L = anbj j = n2 • Opponent chooses m. • I choose string ambm2 • Opponent chooses vwx. • If vwx contains a’s only, I can add to the number of a’s while the number of b’s stays the same. Likewise for b’s only. • If vwx contains a’s and b’s, only interesting case is v is some number of a’s (1<=k<=m) and x is some number of b’s (1<=p<=m). s’ = am+(i-1)k b m2 + (i-1)p • Choose i = 0; s’ = am-k b m2-p (m-k)2 <= (m-1)2 = m2 -2m +1 = m2 -(2m -1) < m2 -p
Closure for CFLs • The family of context-free languages is closed under union: • L1 is a CFL with grammar G1, start symbol S1 • L2 is a CFL with grammar G2, start symbol S2 • Let L3 = L1UL2 • L3(G3) has start symbol S -> S1 | S2
Closure for CFLs • The family of context-free languages is closed under star-closure. • Let L1 be a context-free language with grammar G1 and start symbol S1. • Let L be L1*. Then L(G) = S -> S S1 | λ
Closure for CFL’s • The family of context-free languages is closed under concatenation. • Let L1 be a CFL with grammar G1 and start symbol S1. • Let L2 be a CFL with grammar G2 and start symbol S2. • Let L3 = L1 L2. • Then L3(G3) has start symbol S -> S1 S2.
CFLs are not closed under intersection! • L1 = {anbncm: n,m >=0} • L2 = {anbmcm: n,m >=0} • L1 ∩ L2 = {anbncn: n >=0}
CFL’s are not closed under complementation! • Suppose that CFL’s are closed under complementation. • Let L1 and L2 be CFLs. • L1 ∩ L2 = L1 U L2 • By closure under union and complementation, we get closure under intersection. • But we just showed that CFL’s are not closed under intersection! • Contradiction! So, CFL’s are not closed under complementation.
Regular Intersection • The intersection of a regular language and a context-free language is context-free. • This is called closure under regular intersection. • Idea of proof: Make states equal to the states in the PDA x the states in the DFA.
Proofs Using Closure • Prove that the language L = {w in {a,b,c}* where na(w) = nb(w) = nc(w)} is not context-free. • Assume L is context free. • L∩L(a*b*c*) = anbncn must be context-free by closure under regular intersection. • But it’s not, so L must not be context-free.
Proofs Using Closure • Prove that the language L = {anbn; n>50} is context-free. • Idea: Don’t want to construct! Use closure. • Let L1 = {anbn} L1 is context-free. • Let L2 = {anbn; n<=50} L2 is Regular. • L = L1 L2, which is CF by closure under regular-intersection
Decidable Properties • A CFL is empty if its start symbol is found to be useless. • A CFL is finite if no variable can ever repeat. • Draw the dependency graph of the language. • The language is infinite if and only if the graph contains a cycle.