Proofs: Epsilon-production elimination
These are the human proofs of Epsilon-production elimination, at full length.
Epsilon-production elimination keeps every nonempty string full
This proof shows: correctness.
We use the pseudocode of the page. is the input, the set computed by line L1, the set built by lines L2 to L6, and the output. For a production , the worklist keeps a counter , initially .
Lemma 1 (line L1). When the worklist is empty, .
Proof. Invariant: equals minus the number of positions such that has already been taken off the worklist. Each nonterminal enters the worklist at most once, when it enters , and is later taken off and decreases once for each of its occurrences in . So at the end, is the number of positions of that hold a terminal or a nonterminal not in .
Every member of is nullable, by induction on the order of entry. A left side of an empty production derives in one step. Any other enters when for some , so every is a nonterminal already taken off the worklist, hence already in , hence nullable by induction; then .
Every nullable is in , by induction on the length of a derivation . Its first step uses some . If , is in the initial worklist. Otherwise every is a nonterminal, and by the splitting lemma with , so each is in by induction. Every member of is taken off the worklist before the end, so at the end . The counter reaches 0 at the moment the last of these occurrences is processed, and then enters if it was not there already.
Lemma 2 (soundness). If is in , then . So implies for every nonterminal of and every .
Proof. Line L6 adds only when comes from a production of by dropping some . Then , and each dropped derives by Lemma 1, so . A derivation in is a sequence of steps with productions of ; replacing each by the corresponding derivation in gives a derivation in .
Lemma 3 (completeness). If with , then .
Proof. Induction on . The first step uses some in , and by the splitting lemma with , where , so . Let ; it is not empty because . For , , so is a nonterminal in by Lemma 1. Hence the choice "keep exactly the positions in " is one of the choices of line L4, its right side is nonempty, and line L6 adds to . For , either is a terminal and , or is a nonterminal with , and , so derives with by induction. Then with .
Lemma 4 (cleanup). Removing a production that mentions a nonterminal of with no production left does not change, for any nonterminal , the set of terminal strings that derives.
Proof. Let be a nonterminal of with no production in the current set. No derivation of a terminal string can contain in a sentential form, because would have to be rewritten later and it has no production. So no production with on its right side occurs in a derivation of a terminal string, and removing such productions keeps every such derivation. Repeating the step keeps the sets as well, by induction on the number of rounds.
Lemma 5. No production in has an empty right side, and every string that a nonterminal of derives with is nonempty.
Proof. Line L6 adds only nonempty right sides. A derivation from a nonterminal starts with a nonempty right side, and each later step replaces a symbol by a nonempty string, so the length never drops below 1.
Theorem. Without line L7, has no empty production and . With line L7 applied (when keep_empty holds and ), the only empty production is , occurs on no right side, and .
Proof. By Lemmas 2, 3 and 4, for every nonempty , if and only if , and by Lemma 5, derives no empty string in . This gives without line L7. With line L7, is fresh, so it is on no right side, is its only empty production, and derives and every string of through (the production is omitted only when has no production left, in which case derives nothing and ). Since , by Lemma 1, so . If keep_empty holds but , then and .