Algorithm Search⌘K

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 (x,y)(x, y) be an instance of Post's correspondence problem with lists x=(x1,…,xm)x = (x_1, \ldots, x_m) and y=(y1,…,ym)y = (y_1, \ldots, y_m) over Σ\Sigma, let I={1,…,m}I = \{1, \ldots, m\} be new symbols, and for a sequence s=(i1,…,ik)s = (i_1, \ldots, i_k) write xs=xi1⋯xikx_s = x_{i_1} \cdots x_{i_k}, ys=yi1⋯yiky_s = y_{i_1} \cdots y_{i_k} and s←=ik⋯i1\overleftarrow{s} = i_k \cdots i_1. Let Gx,yG_{x,y} have start symbol SS and the productions

S→A∣B,A→xi A i∣xi i,B→yi B i∣yi i(i∈I).S \to A \mid B, \qquad A \to x_i\, A\, i \mid x_i\, i, \qquad B \to y_i\, B\, i \mid y_i\, i \qquad (i \in I).

Lemma 1. For every string ww, there is at most one parse tree of ww with root AA, and at most one with root BB. The strings derived from AA are the strings xss←x_s \overleftarrow{s} with ss nonempty, and those derived from BB are the strings yss←y_s \overleftarrow{s}.

Proof. By induction on the number of steps, every sentential form derived from AA is xi1⋯xijA ij⋯i1x_{i_1} \cdots x_{i_j} A\, i_j \cdots i_1, or, after a step A→xi iA \to x_i\, i, a string xss←x_s \overleftarrow{s}; and every nonempty ss is reached by the obvious derivation. A parse tree with root AA is a chain: each node AA has the children xix_i, AA, ii, or xix_i, ii. The sequence of indices ii along the chain, read from the bottom up, is the suffix of the yield in I∗I^*. Since the yield xss←x_s \overleftarrow{s} has s←\overleftarrow{s} as its longest suffix in I∗I^* (the symbol before it, if any, is in Σ\Sigma), the yield determines the sequence of indices, and the sequence determines the tree. The same holds for BB. □\square

Lemma 2. Gx,yG_{x,y} is ambiguous if and only if the instance has a solution.

Proof. Suppose ww has two different parse trees T1,T2T_1, T_2 with root SS. The root of each uses S→AS \to A or S→BS \to B. If both used S→AS \to A, their subtrees at AA would be two different trees of ww with root AA, which Lemma 1 rules out; the same for BB. So one tree derives ww from AA and the other from BB, and by Lemma 1, w=xss←=ytt←w = x_s \overleftarrow{s} = y_t \overleftarrow{t} for nonempty s,ts, t. Both s←\overleftarrow{s} and t←\overleftarrow{t} are the longest suffix of ww in I∗I^*, so s=ts = t, and the parts before the suffix give xs=ysx_s = y_s. So ss is a solution.

Conversely, if ss is a solution, the string w=xss←=yss←w = x_s \overleftarrow{s} = y_s \overleftarrow{s} has a tree that starts with S→AS \to A and one that starts with S→BS \to B, by Lemma 1, and these are different. □\square

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 GG with root SS 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 GG is ambiguous. So the set is recursively enumerable.

Hardness. The map (x,y)↦Gx,y(x, y) \mapsto G_{x,y} 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. Gx,yG_{x,y} is linear. PCP remains undecidable for lists over {a,b}\{a, b\}. Replacing each index symbol ii by the string d cid\, c^i, with new letters cc and dd, keeps Lemma 1 true: the longest suffix of the yield in {c,d}∗\{c, d\}^* is the coded index sequence, and the coding i↦dcii \mapsto d c^i is uniquely decodable because each code word starts with dd and contains no other dd. So the reduction produces linear grammars over {a,b,c,d}\{a, b, c, d\}. ■\blacksquare

Back to Undecidability of CFG ambiguity