6.2 Closure Properties of CFL's Theorem 1 Context-free languages are closed under union, concatenation and Kleene closure. Proof Let L1= L(G1) and L2 = L(G2) be CFL’s, where G1 = (V1, T1, P1, S1) and G2 = (V2, T2, P2, S2) 1. For union, L1L2, Construct G = (V, T, P, S), where P = P1P2 {SS1, SS2}, V = V1 V2 {S} 2. For concatenation, L1.L2, Construct G = (V, T, P, S), where P = P1P2 {SS1S2}, V = V1 V2 {S} 3. For Kleene closure, L1*, Construct G = (V, T, P, S), where P = P1{SS1S, S}, V = V1 {S} Theorem 2 Context-free languages are closed under substitution. Proof Let L be a CFL, L *, and for each a in let La be a CFL. Let L = L(G), G = (V, T, P, S), and for each a in let La = L(Ga), where Ga=(Va, Ta, Pa, Sa). Assume that VVa = , for all a in . And Va Vb = , for all a≠ b in . Construct G’=(V’, T’, P’, S), where V’ = a in Va V, T’ = a in Ta , P’ = a in Pa {Af() | A is in P, and f(a) = Sa for each a in } Example 6 L = {0n1m | n ≠m } is a CFL. Let L = L(G), G = ({S, A, B}, {0, 1}, P, S), where P contains the following production rules: S 0S1 | 0A | 1B, A 0A | , B 1B | . Let L0 = L(G0), G = ({S0}, { (, ), [, ]}, P, S0), where P contains the following production rules: S0 S0S0 | [S0] | (S0) | [] | () L0 can be defined recursively as follows: 1. () and [] are in L0. 2. If R and S are in L0, then (R), [R], and RS are in L0. And L0 contains only the strings described in 1 and 2. Let L1 = L(G1), G = ({S1, C, D}, {c, d}, P, S1), where P contains the following production rules: S1 dC | cD C dCC | cS | c D cDD | dS | d Let L1 consists of strings over {c, d} with the property that the number of c’s is equal to the number of d’s in the strings. If f is the substitution f(0) = L0 and f(1) = L1, then f(L) is generated by the grammar S S0SS1 | S0A | S1B, A S0A | , B S1B | . S0 S0S0 | [S0] | (S0) | [] | () S1 dC | cD C dCC | cS | c D cDD | dS | d Corollary Context-free languages are closed under homomorphism. Proof homomorphism is a special case of substitution. Theorem 3 Context-free languages are closed under inverse homomorphism. Proof Let h : Δ* be a homomorphism and L be a CFL.. Let L = L(M), PDA M = (Q, Δ, Γ, , q0, Z0, F). An idea of constructing a PDA M’ to accept h-1(L) is as follows: What M’ does is the following: On reading an input symbol a, M’ generates the string h(a) and stores h(a) in a buffer, then simulates M on h(a). Only when the buffer is empty, M’ is allowed to read in next input. And the buffer holds only a suffix of h(a) for some input a at all time. M’ accepts an input w if the buffer is empty and M enters a final state. Construct a PDA M’ = (Q’, , Γ, ’, [q0, ], Z0, F{}) to accept h-1(L) as follows: Q’ = { [q, x] | q Q and x is a suffix of some h(a) for a .} ’ is defined as follows: 1. ’ ([q, x], , Y) contains all ([p, x], γ) if (q, , Y) contains (p, γ). The move M’ simulates -move of M ignoring the buffer. 2. ’ ([q, ax], , Y) contains all ([p, x], γ) if (q, a, Y) contains (p, γ). M’ simulates moves of M on reading a Δ, then removes a from the front of the buffer. 3. ’ ([q, ], a, Y) contains all ([q, h(a)], γ) for all a and Y Γ. When the buffer is empty, M’ reads an input a and loads the buffer with h(a). (q, h(a), ) *(p, , ) ([q, ,], a, ) (q, h(a), ) * ([p, ], , ). M M' M' If M accepts h(w), i.e., (q0, h(w), Z0) for some p in F and in Γ*, then ([q0, ], w, Z0) *(p, , ) M *([p, ], , ). So M’ accepts w. M' We have that h-1(L(M)) L(M’) If M’ accepts w = a1a2 … an. ([q0, ], a1a2 … an, Z0) *([p1, ], a2 … an, 1), M' *([p1, h(a1)], a2a3 … an, 1), M' *([p2, ], a2a3 … an, 2), M' *([p2, h(a2)], a3 … an, 2), M' . . . *([pn, ], , n+1), M' where pn is a final state. For all i, we have (pi, h(ai), i) (pi+1, , i+1). M So, for these moves altogether we have (q0, h(a1a2 …an), Z0) (pn, , n+1). M Hence, h(a1a2…an) is in L(M). And L(M’) h-1(L(M)}. End of Theorem 3. Theorem 4 Context-free languages are not closed under intersection. Proof L1 = {0n1n0m | n, m = 0, 1, 2, …} and L2 = {0n1m0m | n, m = 0, 1, 2, …} are CFL’s. L1L2 = {0n1n0n | n = 0, 1, 2, …} is not CFL. Corollary Context-free languages are not closed under complementation. L1 L2 = (L1cL2c)c , where c stands for complement. If CFL’s are closed under complement, then CFL’s are closed under intersection. Theorem 5 The intersection of a context-free language and a regular language is context-free. Proof Let L be a CFL, and let R be a regular language. L = L(ML) for PDA ML = (QL, , Γ, L, q0, Z0, FL), and R = L(MR) for DFA MR = (QR, , R, p0, FR). Construct a PDA M’ for L R by simulating ML and MR in parallel. M = (QLQR, , Γ, , [p0, q0], Z0, FLFR), where ([p, q], a, X) contains ([p’, q’], ) if and only if R(p, a) = p’, L(q, a, X) contains (q’, ).