Lecture 18 Properties of Context-free Languages COT 4420 Theory of Computation Section 8.2 Closure properties of Context-free languages • If L 1 and L2 are context-free languages, then we prove that: Union: L1 ∪ L2 Concatenation: L1L2 Star: L1* Are Contextfree languages Union • L1 is context-free therefore there exists a context-free grammar G1 = (V1, T1, S1, P1) for it. • L2 is context-free therefore there exists a context-free grammar G2 = (V2, T2, S2, P2) for it. Union Create a grammar G = (V, T, S, P) such that: V = V1∪ V2 ∪ S T = T1∪ T2 P = P1 ∪ P2 U { S S1 | S2 } We can show that: L (G) = L(G1) ∪ L(G2) Union - Example S1 → aS1b | λ S 2 → aS 2 a | bS 2b | λ L1 = {a b } n n L2 = {ww } R Union L = {a b } ∪ {ww } n n R S → S1 | S 2 S1 → aS1b | λ S 2 → aS 2 a | bS 2b | λ Concatenation • L1 is context-free therefore there exists a context-free grammar G1 = (V1, T1, S1, P1) for it. • L2 is context-free therefore there exists a context-free grammar G2 = (V2, T2, S2, P2) for it. Concatenation Create a grammar G = (V, T, S, P) such that: V = V1∪ V2 ∪ S T = T1∪ T2 P = P1 ∪ P2 U { S S1S2 } We can show that: L (G) = L(G1) . L(G2) Concatenation- Example S1 → aS1b | λ S 2 → aS 2 a | bS 2b | λ L1 = {a b } n n L2 = {ww } R Concatenation L = {a b }{ww } n n R S → S1S 2 S1 → aS1b | λ S 2 → aS 2 a | bS 2b | λ Star - Closure • L1 is context-free therefore there exists a context-free grammar G1 = (V1, T1, S1, P1) for it. Star Closure Create a grammar G = (V, T, S, P) such that: V = V1 ∪ S T = T1 P = P1 U { S S1S | λ } We can show that: L (G) = L(G1)* Star Closure- Example S1 → aS1b | λ L1 = {a b } n n Star L = {a b } * n n S1 → aS1b | λ Negative Properties of Contextfree Languages Negative properties of Context-free languages • Context-free languages are not closed under: Intersection : L1 ∩ L2 Complement: L1 Intersection - Example L1 = {a b c } n n m Context-free: S → AC A → aAb | λ C → cC | λ L2 = {a b c } n m m Context-free: S → AB A → aA | λ B → bBc | λ Intersection: We showed that this is n n n L1 ∩ L2 = {a b c } not context-free Complement • De Morgan’s Law: L1 ∩ L2 = L1 ∪ L2 If the family of context-free languages were closed under complement, then L1 ∪ L2 would be a context-free language. L1 ∩ L2 would be contextfree. However, we showed that the intersection of two context-free languages is not necessarily context-free. Closure under Regular Intersection Closure under Regular Intersection Theorem: If L1 is a context-free language and L2 is a regular language, then L1 ∩ L2 is a contextfree language. Machine NPDA for M1 L1 Machine M2 DFA for L2 Closure under Regular Intersection Proof Proof: Construct a new NPDA machine M that accepts L1 ∩ L2. M simulates in parallel M1 and M2. NPDA qi DFA M1 a, b x q k transition NPDA qi, pj a pj transition M2 M a, b x transition qk, pl pl NPDA qi DFA M1 λ, b x q k pj transition transition NPDA qi, pj M2 M λ, b x transition qk, pj NPDA DFA M1 M2 q0 p0 initial state initial state NPDA M q0 , p0 Initial state NPDA DFA M1 p1 q1 final state M2 p2 final states NPDA q1, p1 M q1, p2 final states Closure under Regular Intersection Proof M accepts w: ((q0, p0), w, z) ⊦* ((qr, ps), λ, x) qr ∈ F1 and ps ∈ F2 If and only if: (q0, w, z) ⊦*M1 (qr, λ, x) And δ* (p0, w) = ps Closure under Regular Intersection Proof L(M) = L(M1) ∩ L(M2) M is an NPDA L(M1) ∩ L(M2) is context-free Application of Closure under Regular Intersection Show that the language L = { anbn : n≥ 0 , n≠100} is a context-free language. L1 = {a100b100} is regular L1 is finite and therefore regular. L1 is regular L = { anbn : n≥ 0 , n≠100} L1 = {a100b100} It is easy to see that L = {anbn : n≥ 0 } ∩ L1 Since {anbn: n≥ 0 } is context-free and L1 is regular, L is context-free. Another Example • Show that the language L = { w: na = nb = nc } is not context-free. Suppose L was context-free. Then: L ∩ { a* b* c*} = { anbncn : n≥ 0} would also be context-free. Context-free Regular Context-free But we know that this language is not context-free. We conclude that L cannot be contxt-free. Decision Properties of Contextfree Languages Membership Problem Question: Is string w in context-free language L? Answer: Parsers. Exhaustive search parser / CYK parsing algorithm. The Emptiness Problem Question: Given context-free language L, does it contain any string at all? Is L empty? (L = ø). Answer: Use the algorithm to find useless variables. If start variable S is useless, L is empty. The Infiniteness Problem Question: Given context-free language L, Is it finite? Answer: Assume grammar G for L does not contain any λ-productions, unit productions or useless productions. Create dependency graph for variables. If there is a loop in the dependency graph then the language is infinite. Note: If there is a cycle in the dependency graph it means that the grammar has a repeating variable. Non-Decision Properties • Many questions that can be decided for regular sets cannot be decided for CFL’s. • Example: Are two CFL’s the same? • Example: Are two CFL’s disjoint? – How would you do that for regular languages? • At this point we do not have a way of proving that no algorithm exists.