Algorithm Search⌘K

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. G=(V,Σ,P,S)G = (V, \Sigma, P, S) is the input, NN the set computed by line L1, P′P' the set built by lines L2 to L6, and G′G' the output. For a production p=A→X1⋯Xkp = A \to X_1 \cdots X_k, the worklist keeps a counter cpc_p, initially kk.

Lemma 1 (line L1). When the worklist is empty, N={A∈V:A⇒∗ε}N = \{A \in V : A \Rightarrow^* \varepsilon\}.

Proof. Invariant: cpc_p equals kk minus the number of positions ii such that XiX_i has already been taken off the worklist. Each nonterminal enters the worklist at most once, when it enters NN, and is later taken off and decreases cpc_p once for each of its occurrences in pp. So at the end, cpc_p is the number of positions of pp that hold a terminal or a nonterminal not in NN.

Every member of NN is nullable, by induction on the order of entry. A left side of an empty production derives ε\varepsilon in one step. Any other AA enters when cp=0c_p = 0 for some p=A→X1⋯Xkp = A \to X_1 \cdots X_k, so every XiX_i is a nonterminal already taken off the worklist, hence already in NN, hence nullable by induction; then A⇒X1⋯Xk⇒∗εA \Rightarrow X_1 \cdots X_k \Rightarrow^* \varepsilon.

Every nullable AA is in NN, by induction on the length mm of a derivation A⇒mεA \Rightarrow^m \varepsilon. Its first step uses some p=A→X1⋯Xkp = A \to X_1 \cdots X_k. If k=0k = 0, AA is in the initial worklist. Otherwise every XiX_i is a nonterminal, and by the splitting lemma Xi⇒miεX_i \Rightarrow^{m_i} \varepsilon with mi<mm_i < m, so each XiX_i is in NN by induction. Every member of NN is taken off the worklist before the end, so at the end cp=0c_p = 0. The counter reaches 0 at the moment the last of these occurrences is processed, and then AA enters NN if it was not there already. □\square

Lemma 2 (soundness). If A→α′A \to \alpha' is in P′P', then A⇒G+α′A \Rightarrow^+_G \alpha'. So A⇒G′∗wA \Rightarrow^*_{G'} w implies A⇒G∗wA \Rightarrow^*_G w for every nonterminal AA of GG and every w∈Σ∗w \in \Sigma^*.

Proof. Line L6 adds A→α′A \to \alpha' only when α′\alpha' comes from a production A→X1⋯XkA \to X_1 \cdots X_k of PP by dropping some Xi∈NX_i \in N. Then A⇒X1⋯XkA \Rightarrow X_1 \cdots X_k, and each dropped XiX_i derives ε\varepsilon by Lemma 1, so A⇒+α′A \Rightarrow^+ \alpha'. A derivation in G′G' is a sequence of steps with productions of P′P'; replacing each by the corresponding derivation in GG gives a derivation in GG. □\square

Lemma 3 (completeness). If A⇒GmwA \Rightarrow^m_G w with w≠εw \neq \varepsilon, then A⇒(V,Σ,P′,S)∗wA \Rightarrow^*_{(V, \Sigma, P', S)} w.

Proof. Induction on m≥1m \ge 1. The first step uses some A→X1⋯XkA \to X_1 \cdots X_k in PP, and by the splitting lemma w=w1⋯wkw = w_1 \cdots w_k with Xi⇒miwiX_i \Rightarrow^{m_i} w_i, where ∑mi=m−1\sum m_i = m - 1, so mi<mm_i < m. Let K={i:wi≠ε}K = \{i : w_i \neq \varepsilon\}; it is not empty because w≠εw \neq \varepsilon. For i∉Ki \notin K, Xi⇒∗εX_i \Rightarrow^* \varepsilon, so XiX_i is a nonterminal in NN by Lemma 1. Hence the choice "keep exactly the positions in KK" is one of the choices of line L4, its right side α′=(Xi)i∈K\alpha' = (X_i)_{i \in K} is nonempty, and line L6 adds A→α′A \to \alpha' to P′P'. For i∈Ki \in K, either XiX_i is a terminal and Xi=wiX_i = w_i, or XiX_i is a nonterminal with Xi⇒GmiwiX_i \Rightarrow^{m_i}_G w_i, wi≠εw_i \neq \varepsilon and mi<mm_i < m, so XiX_i derives wiw_i with P′P' by induction. Then A⇒α′⇒∗wA \Rightarrow \alpha' \Rightarrow^* w with P′P'. □\square

Lemma 4 (cleanup). Removing a production that mentions a nonterminal of GG with no production left does not change, for any nonterminal BB, the set of terminal strings that BB derives.

Proof. Let XX be a nonterminal of GG with no production in the current set. No derivation of a terminal string can contain XX in a sentential form, because XX would have to be rewritten later and it has no production. So no production with XX 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. □\square

Lemma 5. No production in P′P' has an empty right side, and every string that a nonterminal of GG derives with P′P' 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. □\square

Theorem. Without line L7, G′G' has no empty production and L(G′)=L(G)∖{ε}L(G') = L(G) \setminus \{\varepsilon\}. With line L7 applied (when keep_empty holds and S∈NS \in N), the only empty production is S′→εS' \to \varepsilon, S′S' occurs on no right side, and L(G′)=L(G)L(G') = L(G).

Proof. By Lemmas 2, 3 and 4, for every nonempty ww, S⇒G′∗wS \Rightarrow^*_{G'} w if and only if S⇒G∗wS \Rightarrow^*_G w, and by Lemma 5, SS derives no empty string in G′G'. This gives L(G′)=L(G)∖{ε}L(G') = L(G) \setminus \{\varepsilon\} without line L7. With line L7, S′S' is fresh, so it is on no right side, S′→εS' \to \varepsilon is its only empty production, and S′S' derives ε\varepsilon and every string of L(G)∖{ε}L(G) \setminus \{\varepsilon\} through S′⇒SS' \Rightarrow S (the production S′→SS' \to S is omitted only when SS has no production left, in which case SS derives nothing and L(G)={ε}L(G) = \{\varepsilon\}). Since S∈NS \in N, ε∈L(G)\varepsilon \in L(G) by Lemma 1, so L(G′)=L(G)L(G') = L(G). If keep_empty holds but S∉NS \notin N, then ε∉L(G)\varepsilon \notin L(G) and L(G′)=L(G)∖{ε}=L(G)L(G') = L(G) \setminus \{\varepsilon\} = L(G). ■\blacksquare

Back to Epsilon-production elimination