Proof of the closure properties of context-free languages full
This proof shows: statement.
Throughout, G=(V,Σ,R,S), G1=(V1,Σ,R1,S1) and G2=(V2,Σ,R2,S2) are grammars for L, L1 and L2, renamed so that their nonterminal sets are disjoint from each other and from every new symbol. We use one fact about grammars many times.
Fact (context-freeness). If X1⋯Xk⇒∗w, then w=w1⋯wk with Xi⇒∗wi for each i, and each derivation Xi⇒∗wi uses at most as many steps as the whole. Conversely, derivations Xi⇒∗wi combine into X1⋯Xk⇒∗w1⋯wk. This holds because a step rewrites one nonterminal and does not look at its neighbours, so the steps can be sorted by the symbol Xi they descend from.
1. Union, concatenation and star
Let S be a new start symbol.
- Union. Add S→S1∣S2 to R1∪R2. A derivation from S starts with one of the two productions and then uses only the productions of that grammar, because the nonterminal sets are disjoint. So the language is L1∪L2.
- Concatenation. Add S→S1S2. By the Fact, S1S2⇒∗w exactly when w=w1w2 with S1⇒∗w1 and S2⇒∗w2. The language is L1L2.
- Star. Add S→S1S∣ε to R1. By induction on k, S⇒∗S1kS, so S derives every string of L1k for every k≥0. Conversely, a derivation from S uses S→S1S some k times and S→ε once, since that is the only way to remove S, so by the Fact the string is in L1k. The language is L1∗.
2. Reversal
Replace each production A→X1⋯Xk by A→Xk⋯X1. By induction on the length of the derivation, A⇒∗w in the old grammar exactly when A⇒∗wR in the new one: the first step A→X1⋯Xk with Xi⇒∗wi becomes A→Xk⋯X1 with Xi⇒∗wiR, and wkR⋯w1R=(w1⋯wk)R. The language is LR.
3. Substitution and homomorphism
Let s map each a∈Σ to a context-free language s(a)⊆Δ∗ with grammar Ga=(Va,Δ,Ra,Sa), and extend it by s(ε)={ε}, s(xy)=s(x)s(y) and s(L)=⋃x∈Ls(x). The grammar Gs has nonterminals V∪⋃aVa, start symbol S, the productions of every Ga, and each production of G with every terminal a replaced by Sa.
If x=a1⋯ak∈L and yi∈s(ai), then S⇒∗Sa1⋯Sak in Gs, by the derivation of x with the replaced productions, and then Sai⇒∗yi, so y1⋯yk is derived. Conversely, take a parse tree of y in Gs. The nodes labelled by nonterminals of V form a tree at the top whose leaves are labelled by symbols Sa1,…,Sak in order, because the productions of V only produce symbols of V and start symbols Sa, and the productions of each Ga never produce a symbol of V. Replacing each Sai by ai turns the top tree into a parse tree of x=a1⋯ak in G, and the subtree below each Sai shows yi∈s(ai), with y=y1⋯yk. So L(Gs)=s(L).
A homomorphism h is the substitution with s(a)={h(a)}, and {h(a)} has the grammar Sa→h(a). So h(L) is context-free.
4. Intersection with a regular language
Let D=(Q,Σ,δ,q0,F) be a DFA for R, with δ a partial function. The grammar G∩ has a new start symbol S′ and the nonterminals [pXq] for p,q∈Q and X∈V∪Σ, with the productions
- S′→[q0Sf] for each f∈F;
- [paq]→a whenever δ(p,a)=q;
- [pAp]→ε for each A→ε in R and each p∈Q;
- [p0Apk]→[p0X1p1][p1X2p2]⋯[pk−1Xkpk] for each A→X1⋯Xk in R with k≥1 and all states p0,…,pk.
Claim. [pXq]⇒∗w in G∩ if and only if X⇒∗w in G (or X=w for a terminal X) and δ∗(p,w)=q.
Proof. "Only if", by induction on the length of the derivation. For a terminal X=a, the only production is [paq]→a with δ(p,a)=q. For X=A, the first step uses a production that comes from some A→X1⋯Xk. If k=0 then w=ε, q=p, and δ∗(p,ε)=p. Otherwise, by the Fact, w=w1⋯wk with [pi−1Xipi]⇒∗wi by shorter derivations, so by induction Xi⇒∗wi and δ∗(pi−1,wi)=pi. Then A⇒X1⋯Xk⇒∗w and δ∗(p,w)=pk=q.
"If", by induction on the length of the derivation X⇒∗w in G, with terminals as the base case. If X=A and the first step is A→X1⋯Xk, then w=w1⋯wk with Xi⇒∗wi by shorter derivations. Let p0=p and pi=δ∗(pi−1,wi), which is defined because δ∗(p,w) is, and pk=q. By induction [pi−1Xipi]⇒∗wi, and the production [pAq]→[p0X1p1]⋯[pk−1Xkpk], or [pAp]→ε when k=0, finishes. □
With X=S, p=q0 and q∈F, the claim gives L(G∩)=L∩R.
5. Inverse homomorphism
Let g:Δ∗→Σ∗ be a homomorphism, and let P=(Q,Σ,Γ,δ,q0,Z0,F) be a PDA with L(P)=L, which exists by the equivalence of grammars and pushdown automata. Let B be the finite set of suffixes of the strings g(a), a∈Δ, including ε. The PDA P′ has states Q×B, start state (q0,ε), start symbol Z0, final states F×{ε}, and three kinds of moves, for all X∈Γ:
- (load) ((q,g(a)),X)∈δ′((q,ε),a,X) for each q∈Q and a∈Δ;
- (step) ((p,u),γ)∈δ′((q,bu),ε,X) whenever (p,γ)∈δ(q,b,X) with b∈Σ and bu∈B;
- (silent) ((p,u),γ)∈δ′((q,u),ε,X) whenever (p,γ)∈δ(q,ε,X), for each u∈B.
The second component is a buffer that holds the part of g(a) that P has not read yet.
Claim. P′ accepts x∈Δ∗ if and only if P accepts g(x).
Proof. Given an accepting run of P′ on x=a1⋯ak, delete the load moves and drop the buffer from each state. A step move becomes the move of P that reads the first symbol b of the buffer, and a silent move becomes the same silent move of P. The buffer is loaded with g(ai) only when it is empty, and its symbols are removed in order, so the moves of P read g(a1)g(a2)⋯g(ak)=g(x). The run ends in a state (q,ε) with q∈F, so all of g(x) was read and P accepts.
Conversely, take an accepting run of P on g(x). Build a run of P′ by induction along it, keeping the invariant: after the moves of P that have read g(a1)⋯g(ai−1)v, where v is a prefix of g(ai) and g(ai)=vu, the run of P′ has read a1⋯ai (or a1⋯ai−1 with an empty buffer if v=ε and ai is not loaded yet), its state is (q,u) for the current state q of P, and its stack is that of P. A silent move of P is copied as a silent move. Before a move of P that reads a symbol b, while the buffer is empty, load the next symbols ai of x; loads of symbols with g(ai)=ε keep the buffer empty, and the first load with g(ai)=ε puts g(ai), which starts with b, into the buffer; then copy the move as a step move. At the end of the run of P, load the remaining symbols of x, whose images are all ε, since P has read all of g(x). The final state is (q,ε) with q∈F, so P′ accepts x. □
So g−1(L)=L(P′) is context-free.
6. Failure for intersection, complement and difference
Let L1={aibicj:i,j≥0}, with the grammar S→XC, X→aXb∣ε, C→cC∣ε, and L2={aibjcj:i,j≥0}, with the grammar S→AY, A→aA∣ε, Y→bYc∣ε. Both are context-free. Their intersection is {anbncn:n≥0}, which is not context-free by the pumping lemma. So the class is not closed under intersection.
If it were closed under complement, then by part 1 it would contain L1∪L2=L1∩L2, a contradiction. And L=Σ∗∖L with Σ∗ context-free, so closure under difference would give closure under complement. ■