Proofs: Undecidability of CFG ambiguity
These are the human proofs of Undecidability of CFG ambiguity, at full length.
Proof that ambiguity of context-free grammars is undecidable full
This proof shows: statement.
Let be an instance of Post's correspondence problem with lists and over , let be new symbols, and for a sequence write , and . Let have start symbol and the productions
Lemma 1. For every string , there is at most one parse tree of with root , and at most one with root . The strings derived from are the strings with nonempty, and those derived from are the strings .
Proof. By induction on the number of steps, every sentential form derived from is , or, after a step , a string ; and every nonempty is reached by the obvious derivation. A parse tree with root is a chain: each node has the children , , , or , . The sequence of indices along the chain, read from the bottom up, is the suffix of the yield in . Since the yield has as its longest suffix in (the symbol before it, if any, is in ), the yield determines the sequence of indices, and the sequence determines the tree. The same holds for .
Lemma 2. is ambiguous if and only if the instance has a solution.
Proof. Suppose has two different parse trees with root . The root of each uses or . If both used , their subtrees at would be two different trees of with root , which Lemma 1 rules out; the same for . So one tree derives from and the other from , and by Lemma 1, for nonempty . Both and are the longest suffix of in , so , and the parts before the suffix give . So is a solution.
Conversely, if is a solution, the string has a tree that starts with and one that starts with , by Lemma 1, and these are different.
Theorem. The set of ambiguous context-free grammars is recursively enumerable and complete for the recursively enumerable sets under many-one reductions. In particular it is undecidable, and the set of unambiguous grammars is not recursively enumerable.
Proof. Membership. The parse trees of with root can be listed effectively, ordered by size. A program that runs through all pairs of different trees in this list and halts when it finds two with the same yield halts exactly when is ambiguous. So the set is recursively enumerable.
Hardness. The map is computable, and by Lemma 2 it is a many-one reduction from PCP to ambiguity. PCP is complete for the recursively enumerable sets (Post 1946, and the standard reduction from the halting problem), so ambiguity is complete too. A complete recursively enumerable set is not decidable, and its complement is not recursively enumerable, since a set that is recursively enumerable with a recursively enumerable complement is decidable.
Restrictions. is linear. PCP remains undecidable for lists over . Replacing each index symbol by the string , with new letters and , keeps Lemma 1 true: the longest suffix of the yield in is the coded index sequence, and the coding is uniquely decodable because each code word starts with and contains no other . So the reduction produces linear grammars over .