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. is any grammar, with empty productions and cycles of unit productions allowed. A unit production is with . Write for one step with a unit production, and for zero or more such steps; is a unit pair when . is the set built by line L6.
Lemma 1: the search finds the unit pairs and stops
Fix . Each iteration of lines L4 and L5 pops one element of , and an element is pushed only when it is added to , which happens at most once for each nonterminal. So there are at most iterations, each of which looks at the unit productions of one nonterminal, and the loop stops.
Every element of forms a unit pair with . by zero steps. If is added while is popped, then , so by induction on the order of insertion, and gives .
Every unit pair has . By induction on the number of steps of . For , . For , . By induction, ; every element of is pushed on once and popped once before the loop ends, and when is popped, line L5 adds unless it is already in .
Lemma 2: no unit productions
Line L6 adds only for a production of that is not a unit production. So no production of is a unit production.
Lemma 3: derivations in are derivations in
If , it was added for some with , and by Lemma 1, in . So . Replacing each step of a derivation in by the steps gives a derivation in with the same start and end. So for every nonterminal .
Lemma 4: derivations of terminal strings in have counterparts in
Claim. If with and , then .
Proof. By strong induction on . Write the derivation as a leftmost one; this does not change . Its first steps rewrite only the leftmost nonterminal, so it starts with some number of unit steps followed by a step 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 , is a unit pair, and by Lemma 1 and line L6, .
The rest of the derivation is . By the splitting lemma of derivations, write and with and . For a terminal , . For a nonterminal , and , so by the induction hypothesis. Then . If , then and the single step is enough.
Theorem
For every , ; in particular . has no unit production, and ELIMINATE-UNITS stops after searches, each of at most iterations, and line L6, which writes at most 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.
Remark on the parse trees
The proof of Lemma 4 maps each parse tree of to a parse tree of with the same yield, by collapsing each maximal chain of unit steps into the first node of the chain. Every tree of is the image of some tree of , by Lemma 3 applied node by node. So each sentence has at most as many parse trees in as in .