These are the human proofs of Undecidability of CFG equivalence, at full length.
Proof that equivalence of context-free grammars is undecidable full
This proof shows: statement.
Let (x,y) be an instance of Post's correspondence problem with lists x=(x1,…,xm) and y=(y1,…,ym) of nonempty strings over Σ. Let I={1,…,m} be new symbols and Δ=Σ∪I. For a sequence s=(i1,…,ik) write xs=xi1⋯xik and s=ik⋯i1. Let
Lx={xss:s∈Ik,k≥1},
and Ly likewise. By Lemma 2 of the proof for intersections, Lx∩Ly=∅ if and only if the instance has a solution. We write L=Δ∗∖L.
The grammar for the complement. Let Gx have start symbol C and the productions
C→Q∣D,D→xiDi∣Y∣Zii(i∈I),
where Q generates the regular language Δ∗∖Σ∗I+, Y generates Σ+, and Zi generates the regular language (Σ∗∖xiΣ∗)I∗, each by a right-linear grammar built from a finite automaton. The construction is computable from x.
Proof. By induction on the number of uses of D→xiDi: each wraps the current string in xi…i, and the last step uses D→Y or D→Zii. □
Lemma 2.L(Gx)=Lx.
Proof. (⊆) Strings from Q are not in Σ∗I+⊇Lx. Let w∈L(D), written as in Lemma 1 with j=(j1,…,jr), and suppose w∈Lx, say w=xss. The longest suffix of w in I∗ is s.
If z∈Σ+, then w=xjzj, and its longest suffix in I∗ is j, because z ends with a symbol of Σ. So s=j and xs=xjz, which gives z=ε, a contradiction. (If r=0, w=z has no index symbol and is not in Lx.)
If z=uvi with u∈Σ∗∖xiΣ∗ and v∈I∗, then w=xjuvij, and its longest suffix in I∗ is vij. So s=vij, that is, s=(j1,…,jr,i,…) with the reverse of v at the end, and xs=xjxixt for the sequence t read from v. Comparing with w=xss gives u=xixt∈xiΣ∗, a contradiction.
(⊇) Let w∈/Lx. If w∈/Σ∗I+, then w∈L(Q). Otherwise w=us with u∈Σ∗ and s=(i1,…,ik), k≥1, and u=xs. Let r be the largest number with 0≤r≤k such that xi1⋯xir is a prefix of u, and write u=xi1⋯xiru′.
If r=k, then u′=ε because u=xs, and w=xi1⋯xiku′ik⋯i1 is derived by k uses of D→xiDi and then D→Y with Y⇒∗u′.
If r<k, then u′ does not start with xir+1, by the choice of r. So w=xi1⋯xir(u′ik⋯ir+2)ir+1ir⋯i1, and the part in parentheses is in (Σ∗∖xir+1Σ∗)I∗, the language of Zir+1. So w is derived by r uses of D→xiDi and then D→Zir+1ir+1. □
Theorem. Universality, equivalence, inclusion, equality with a regular language and inclusion of a regular language in a context-free one are undecidable, and each is complete for the co-recursively-enumerable sets.
Proof.The reduction. Given (x,y), compute Gx and Gy with disjoint nonterminals and let G have the new start symbol S with S→Cx∣Cy. By Lemma 2,
L(G)=Lx∪Ly=Lx∩Ly,
so L(G)=Δ∗ if and only if Lx∩Ly=∅, if and only if the instance has no solution. This is a many-one reduction from the complement of PCP to universality. The complement of PCP is complete for the co-recursively-enumerable sets, since PCP is complete for the recursively enumerable sets. So universality is hard for that class.
The other problems. Let GΔ be the grammar S→aS∣ε (a∈Δ) and EΔ a regular expression for Δ∗. Then L(G)=Δ∗ if and only if L(G)=L(GΔ), if and only if L(GΔ)⊆L(G), if and only if L(G)=L(EΔ), if and only if L(EΔ)⊆L(G). So universality reduces to each of the problems 2 to 5, and each is hard.
Membership in the class. For each problem, a negative answer has a finite witness: a string in one language and not in the other. Membership of a string in a context-free or regular language is decidable, so listing all strings of Δ∗ and testing each finds a witness exactly when the answer is negative. So the complement of each problem is recursively enumerable.
Fixed alphabet. PCP remains undecidable for lists over Σ={a,b}. Replace each index symbol i by the code dci with new letters c,d. Lemma 2 holds with I∗ and I+ replaced by the regular sets {dci:i∈I}∗ and its nonempty version, and with "longest suffix in I∗" replaced by the longest suffix in {c,d}∗, which decodes uniquely because every code starts with d. One also adds to Q the strings whose suffix in {c,d}∗ is not a sequence of codes, which is regular. So universality is undecidable over {a,b,c,d}, and equivalence is undecidable with G2 fixed to the grammar for {a,b,c,d}∗. ■