Handout 8: Context-free Closure Properties

Advanced Topics in Theoretical Computer Science
Fall 2012
Handout 8: Context-free Closure Properties
c Laura Kovács
8.1 Closure under union. The context-free languages are closed under union. Given two CFGs G1 =
(V1 , Σ, R1 , S1 ) and G2 = (V2 , Σ, R2 , S2 ), we can construct a CFG G = (V, Σ, R, S) such that L(G) = L(G1 ) ∪
L(G2 ):
V = V1 ∪ V2 ∪ {S}
R = R1 ∪ R2 ∪ {S → S1 | S2 }
Given two PDAs M1 and M2 , we can construct a PDA M such that L(M ) = L(M1 ) ∪ L(M2 ) as we did for
NFAs.
This result can be useful to show that a language is context-free. For example, the language
L7 = {0i 1j 2k | i 6= j or j 6= k}
is context-free, because it is the union of the two context-free languages {0i 1j 2k | i 6= j} and {0i 1j 2k | j 6= k}.
8.2 Closure under concatenation. The context-free languages are closed under concatenation. Given
two CFGs G1 = (V1 , Σ, R1 , S1 ) and G2 = (V2 , Σ, R2 , S2 ), we can construct a CFG G = (V, Σ, R, S) such that
L(G) = L(G1 ) ◦ L(G2 ):
V = V1 ∪ V2 ∪ {S}
R = R1 ∪ R2 ∪ {S → S1 S2 }
Given two PDAs M1 and M2 , we can construct a PDA M such that L(M ) = L(M1 ) ◦ L(M2 ) as we did for
NFAs.
This result can be useful to show that a language is context-free. For example, the language
L8 = {0i 1j 2k | j = i + k}
is context-free, because it is the concatenation of the two context-free languages {0i 1i } and {1k 2k }.
8.3 Closure under iteration. The context-free languages are closed under iteration. Given a CFGs
G = (V, Σ, R, S), we can construct a CFG G0 = (V 0 , Σ, R0 , S 0 ) such that L(G0 ) = L(G)∗ :
V 0 = V ∪ {S 0 }
R = R ∪ {S 0 → ε | SS 0 }
Given a PDA M , we can construct a PDA M 0 such that L(M 0 ) = L(M )∗ as we did for NFAs.
8.4 Closure under intersection with a regular language. If A is a context-free language and B
is regular, then A ∩ B is context-free. Given a PDA M1 = (Q1 , Σ, Γ, δ1 , q01 , F1 ) and an NFA M2 =
(Q2 , Σ, δ2 , q02 , F2 ), using the product construction we can construct a PDA M = (Q, Σ, Γ, δ, q0 , F ) such
that L(M ) = L(M1 ) ∩ L(M2 ):
Q = Q1 × Q2
((p1 , p2 ), γ 0 ) ∈ δ((q1 , q2 ), σ, γ) iff (p1 , γ 0 ) ∈ δ1 (q1 , σ, γ) and p2 ∈ δ2 (q2 , σ)
q0 = (q01 , q02 )
F = F1 × F2
This result can be useful to show that a language is not context-free. For example, the language
1
L8 = {w ∈ {0, 1, 2}∗ | #(w, 0) = #(w, 1) = #(w, 2)}
is not context-free, because the non-context-free language is L5 is the intersection of L8 and the regular
language 0∗ 1∗ 2∗ .
8.5 Nonclosure under intersection. The product of two PDAs would result in two stacks. Indeed, there
are context-free languages whose intersection is not context-free. For example, L5 is the intersection of the
two context-free languages {0i 1j 2k | i = j} and {0i 1j 2k | j = k}.
8.6 Nonclosure under complementation. Using de Morgan’s law, from Sections 8.1 and 8.5 it follows
that there are context-free languages whose complement is not context-free. For example, the complement
of L7 is not context-free, because L5 = L7 ∩ 0∗ 1∗ 2∗ .
A PDA M = (Q, Σ, Γ, δ, q0 , F ) is deterministic if for all q ∈ Q, σ ∈ Σ, and γ ∈ Γ,
|δ(q, σ, γ) ∪ δ(q, σ, ε) ∪ δ(q, ε, γ) ∪ δ(q, ε, ε)| = 1;
that is, in all situations M can proceed in exactly one way. If M is deterministic and M 0 = (Q, Σ, Γ, δ, q0 , Q\F ),
then L(M 0 ) = L(M ). It follows that context-free language L7 cannot be recognized by a deterministic PDA;
that is, unlike for finite automata, the deterministic variety of push-down automata is strictly less powerful
than the nondeterministic variety.
2