Algorithm Search⌘K

Proofs: Unit-production elimination

These are the human proofs of Unit-production elimination, at full length.

ELIMINATE-UNITS returns a grammar without unit productions in which every nonterminal derives the same terminal strings, and it stops full

This proof shows: correctness, termination.

We use the notation of the context-free grammar page and the pseudocode ELIMINATE-UNITS of the page. G=(V,Σ,P,S)G = (V, \Sigma, P, S) is any grammar, with empty productions and cycles of unit productions allowed. A unit production is B→CB \to C with C∈VC \in V. Write B⇒uCB \Rightarrow_u C for one step with a unit production, and ⇒u∗\Rightarrow_u^* for zero or more such steps; (A,B)(A, B) is a unit pair when A⇒u∗BA \Rightarrow_u^* B. P′P' is the set built by line L6.

Lemma 1: the search finds the unit pairs and stops

Fix AA. Each iteration of lines L4 and L5 pops one element of WW, and an element is pushed only when it is added to UU, which happens at most once for each nonterminal. So there are at most ∣V∣|V| iterations, each of which looks at the unit productions of one nonterminal, and the loop stops.

Every element of UU forms a unit pair with AA. A⇒u∗AA \Rightarrow_u^* A by zero steps. If CC is added while BB is popped, then B∈UB \in U, so A⇒u∗BA \Rightarrow_u^* B by induction on the order of insertion, and B→C∈PB \to C \in P gives A⇒u∗CA \Rightarrow_u^* C.

Every unit pair (A,C)(A, C) has C∈UC \in U. By induction on the number mm of steps of A⇒umCA \Rightarrow_u^m C. For m=0m = 0, C=A∈UC = A \in U. For m>0m > 0, A⇒um−1B⇒uCA \Rightarrow_u^{m-1} B \Rightarrow_u C. By induction, B∈UB \in U; every element of UU is pushed on WW once and popped once before the loop ends, and when BB is popped, line L5 adds CC unless it is already in UU. □\square

Lemma 2: no unit productions

Line L6 adds A→αA \to \alpha only for a production B→αB \to \alpha of PP that is not a unit production. So no production of P′P' is a unit production. □\square

Lemma 3: derivations in G′G' are derivations in GG

If A→α∈P′A \to \alpha \in P', it was added for some B∈UB \in U with B→α∈PB \to \alpha \in P, and by Lemma 1, A⇒u∗BA \Rightarrow_u^* B in GG. So A⇒G∗B⇒GαA \Rightarrow^*_G B \Rightarrow_G \alpha. Replacing each step γAδ⇒G′γαδ\gamma A \delta \Rightarrow_{G'} \gamma \alpha \delta of a derivation in G′G' by the steps γAδ⇒G∗γBδ⇒Gγαδ\gamma A \delta \Rightarrow^*_G \gamma B \delta \Rightarrow_G \gamma \alpha \delta gives a derivation in GG with the same start and end. So LG′(X)⊆LG(X)L_{G'}(X) \subseteq L_G(X) for every nonterminal XX. □\square

Lemma 4: derivations of terminal strings in GG have counterparts in G′G'

Claim. If X⇒GmwX \Rightarrow^m_G w with X∈VX \in V and w∈Σ∗w \in \Sigma^*, then X⇒G′∗wX \Rightarrow^*_{G'} w.

Proof. By strong induction on m≥1m \ge 1. Write the derivation as a leftmost one; this does not change mm. Its first steps rewrite only the leftmost nonterminal, so it starts with some number k≥0k \ge 0 of unit steps X=X0⇒uX1⇒u⋯⇒uXkX = X_0 \Rightarrow_u X_1 \Rightarrow_u \cdots \Rightarrow_u X_k followed by a step Xk⇒αX_k \Rightarrow \alpha with a production that is not a unit production. Such a step exists: the derivation ends in a terminal string, and a unit step leaves a single nonterminal. So X⇒ukXkX \Rightarrow^k_u X_k, (X,Xk)(X, X_k) is a unit pair, and by Lemma 1 and line L6, X→α∈P′X \to \alpha \in P'.

The rest of the derivation is α⇒Gm−k−1w\alpha \Rightarrow^{m - k - 1}_G w. By the splitting lemma of derivations, write α=Y1⋯Yr\alpha = Y_1 \cdots Y_r and w=w1⋯wrw = w_1 \cdots w_r with Yi⇒GmiwiY_i \Rightarrow^{m_i}_G w_i and ∑mi=m−k−1<m\sum m_i = m - k - 1 < m. For a terminal YiY_i, wi=Yiw_i = Y_i. For a nonterminal YiY_i, mi≥1m_i \ge 1 and mi<mm_i < m, so Yi⇒G′∗wiY_i \Rightarrow^*_{G'} w_i by the induction hypothesis. Then X⇒G′Y1⋯Yr⇒G′∗w1⋯wr=wX \Rightarrow_{G'} Y_1 \cdots Y_r \Rightarrow^*_{G'} w_1 \cdots w_r = w. If r=0r = 0, then α=ε=w\alpha = \varepsilon = w and the single step is enough. □\square

Theorem

For every X∈VX \in V, LG′(X)=LG(X)L_{G'}(X) = L_G(X); in particular L(G′)=L(G)L(G') = L(G). G′G' has no unit production, and ELIMINATE-UNITS stops after ∣V∣|V| searches, each of at most ∣V∣|V| iterations, and line L6, which writes at most ∣U∣≤∣V∣|U| \le |V| copies of each non-unit production.

Proof. Lemmas 3 and 4 give the two inclusions, Lemma 2 the absence of unit productions, and Lemma 1 the bound on each search. □\square

Remark on the parse trees

The proof of Lemma 4 maps each parse tree of GG to a parse tree of G′G' with the same yield, by collapsing each maximal chain of unit steps into the first node of the chain. Every tree of G′G' is the image of some tree of GG, by Lemma 3 applied node by node. So each sentence has at most as many parse trees in G′G' as in GG.

Back to Unit-production elimination