Algorithm Search⌘K

Proofs: Greibach normal form conversion

These are the human proofs of Greibach normal form conversion, at full length.

The classical conversion gives an equivalent grammar in Greibach normal form full

This proof shows: correctness.

We use the pseudocode of the page and the notation of the grammar pages: productions A→α∣βA \to \alpha \mid \beta, ε\varepsilon for the empty string, SS the start symbol. The input is G=(V,Σ,P,S)G = (V, \Sigma, P, S). A production is in form if it is A→aB1⋯BkA \to a B_1 \cdots B_k with a∈Σa \in \Sigma, k≥0k \ge 0 and all BiB_i nonterminals. For a grammar HH and a nonterminal XX of HH write LH(X)={w∈Σ∗:X⇒H∗w}L_H(X) = \{w \in \Sigma^* : X \Rightarrow_H^* w\}.

Theorem. The algorithm ends and returns a grammar G′G' with L(G′)=L(G)L(G') = L(G) in which every production is in form, except S0→εS_0 \to \varepsilon when ε∈L(G)\varepsilon \in L(G), and then S0S_0 is on no right side.

The proof has three parts: the steps that keep the language, the shape of the productions, and the end of the loops.

Part 1: the steps keep the language

Line L2. Let G1G_1 be the grammar after line L2. The three removals are the standard ones, done in the order ε\varepsilon-productions, unit productions, useless symbols.

  • ε\varepsilon-productions. Let N\mathcal{N} be the set of nullable nonterminals. Replace each production A→X1⋯XkA \to X_1 \cdots X_k by all productions A→αA \to \alpha where α≠ε\alpha \ne \varepsilon comes from X1⋯XkX_1 \cdots X_k by deleting some of the XiX_i that are in N\mathcal{N}. Each new production A→αA \to \alpha is simulated by the old production and derivations Xi⇒∗εX_i \Rightarrow^* \varepsilon for the deleted XiX_i, so no new nonempty string is derived. Conversely, take a parse tree of a nonempty ww from AA in the old grammar. Delete every subtree whose frontier is empty; its root is nullable. A node keeps at least one child, because its frontier is nonempty. The node A→X1⋯XkA \to X_1 \cdots X_k with some children deleted is a new production A→αA \to \alpha with α≠ε\alpha \ne \varepsilon. So L(A)L(A) is the same up to ε\varepsilon for every AA, and the new grammar has no ε\varepsilon-production.
  • Unit productions. For each AA, let U(A)U(A) be the set of BB with A⇒∗BA \Rightarrow^* B by unit productions only. Replace all productions by A→αA \to \alpha for B∈U(A)B \in U(A) and B→αB \to \alpha a non-unit production. A new production is simulated by a chain of unit steps and one old production. Conversely, in a parse tree, each maximal chain of nodes joined by unit productions ends at a node with a non-unit production; contract the chain into one node. So the languages do not change, and no unit production remains. No ε\varepsilon-production appears, because only non-unit productions are copied.
  • Useless symbols. First delete the nonterminals that derive no terminal string, and every production that contains one. Then delete the nonterminals not reachable from SS. A parse tree of a terminal string uses only symbols that derive terminal strings and that are reachable from its root, so L(S)L(S) does not change. If SS derives no terminal string, L(G)∖{ε}=∅L(G) \setminus \{\varepsilon\} = \emptyset and G1G_1 is empty.

So L(G1)=L(G)∖{ε}L(G_1) = L(G) \setminus \{\varepsilon\}. In G1G_1 every right side is nonempty, a right side of length 1 is a terminal, and every nonterminal derives some terminal string.

Line L4. Replacing a terminal aa in a non-first position by TaT_a and adding Ta→aT_a \to a keeps every L(A)L(A), because TaT_a derives exactly aa.

Substitution (lines L7, L9 and L10). Let A→XγA \to X \gamma be a production with XX a nonterminal different from AA, and let X→δ1∣⋯∣δrX \to \delta_1 \mid \cdots \mid \delta_r be all productions of XX. Replacing A→XγA \to X \gamma by A→δ1γ∣⋯∣δrγA \to \delta_1 \gamma \mid \cdots \mid \delta_r \gamma keeps L(Y)L(Y) for every nonterminal YY. Each new production is simulated by two old ones. Conversely, in a tree of the old grammar, a node that uses A→XγA \to X \gamma has a first child XX that uses some X→δtX \to \delta_t; merge the two nodes into one node that uses A→δtγA \to \delta_t \gamma. Repeat until no node uses A→XγA \to X \gamma; each merge removes one node, so this ends. The productions of XX do not change, because X≠AX \ne A. Removing a duplicate production does not change the grammar as a set of productions.

Immediate left recursion (line L8). Let the productions of AA be A→Aα1∣⋯∣Aαp∣β1∣⋯∣βqA \to A \alpha_1 \mid \cdots \mid A \alpha_p \mid \beta_1 \mid \cdots \mid \beta_q, where no βk\beta_k starts with AA, and let BB be a new nonterminal. The new productions are A→βk∣βkBA \to \beta_k \mid \beta_k B and B→αt∣αtBB \to \alpha_t \mid \alpha_t B. In the old grammar, a node AA of a parse tree starts a maximal chain of nodes along first children that use the productions A→Aαt1A \to A \alpha_{t_1}, A→Aαt2A \to A \alpha_{t_2}, …, A→AαtrA \to A \alpha_{t_r} and ends at a node that uses A→βkA \to \beta_k; the chain derives βkαtr⋯αt1\beta_k \alpha_{t_r} \cdots \alpha_{t_1}. In the new grammar, A⇒βkB⇒βkαtrB⇒⋯⇒βkαtr⋯αt1A \Rightarrow \beta_k B \Rightarrow \beta_k \alpha_{t_r} B \Rightarrow \cdots \Rightarrow \beta_k \alpha_{t_r} \cdots \alpha_{t_1} (or A⇒βkA \Rightarrow \beta_k when r=0r = 0), and every use of AA and BB in a new tree has this shape. So the two grammars derive the same sentential forms from AA by these blocks, and replacing each block by the other one maps trees to trees in both directions. Every L(Y)L(Y) for an old YY is kept. We need p≥1p \ge 1 only to create BB, and q≥1q \ge 1 holds because AA derives a terminal string, which a tree can only do by ending its chain at some βk\beta_k.

Line L11. EE is computed on the input grammar, so EE is true if and only if ε∈L(G)\varepsilon \in L(G). If EE is true, the new S0S_0 has the productions S0→εS_0 \to \varepsilon and S0→αS_0 \to \alpha for S→αS \to \alpha, so L(S0)=L(S)∪{ε}=L(G)L(S_0) = L(S) \cup \{\varepsilon\} = L(G). If EE is false, the start symbol stays SS and L(S)=L(G)∖{ε}=L(G)L(S) = L(G) \setminus \{\varepsilon\} = L(G).

So L(G′)=L(G)L(G') = L(G) if the algorithm ends.

Part 2: the shape of the productions

Let the order after line L4 be A1,…,AmA_1, \ldots, A_m, with the TaT_a at the end. Call a production regular if it is a single terminal, or if it has length at least 2 and every symbol after the first is a nonterminal.

Claim 1. After line L4 every production is regular, and every later step keeps this.

After line L2 a right side of length 1 is a terminal, and line L4 makes every non-first symbol a nonterminal. A substitution replaces A→XγA \to X \gamma, with ∣Xγ∣≥2|X \gamma| \ge 2, by A→δγA \to \delta \gamma with δ\delta regular and γ\gamma a nonempty string of nonterminals; the result has length at least 2, and its symbols after the first are those of δ\delta after the first and those of γ\gamma, all nonterminals. In line L8, a production Ai→AiαtA_i \to A_i \alpha_t has length at least 2, so αt\alpha_t is a nonempty string of nonterminals, and so are αtBi\alpha_t B_i. The productions βk\beta_k are regular, and βkBi\beta_k B_i is regular because βk\beta_k is nonempty. □\square

So a regular production that starts with a terminal is in form. It remains to show that every production starts with a terminal at the end.

Claim 2. After iteration ii of the loop of line L5, every production of AiA_i starts with a terminal or with some AjA_j with j>ij > i, and the productions of A1,…,AiA_1, \ldots, A_i do not change in later iterations.

By induction on ii. In iteration ii, the inner loop goes through j=1,…,i−1j = 1, \ldots, i - 1. Before step jj, by the induction hypothesis for j′<jj' < j, no production of AiA_i starts with Aj′A_{j'} for j′<jj' < j: such a production was replaced at step j′j' by productions that start with the first symbol of a production of Aj′A_{j'}, which is a terminal or an AkA_k with k>j′k > j'; if k<jk < j that production was in turn replaced at step kk, since the steps go in increasing order. After step jj no production starts with AjA_j, by the same argument. So after the inner loop, every production of AiA_i starts with a terminal or with AkA_k, k≥ik \ge i. Line L8 removes those with k=ik = i: the productions βk\beta_k and βkBi\beta_k B_i start with the first symbol of βk\beta_k, which is a terminal or AkA_k with k>ik > i. Later iterations change only Ai′A_{i'} with i′>ii' > i and the new Bi′B_{i'}. □\square

Claim 3. After line L9, every production of every AiA_i starts with a terminal.

By Claim 2, the productions of AmA_m start with terminals. Line L9 goes from i=m−1i = m - 1 down to 1. When it handles AiA_i, the productions of all AjA_j with j>ij > i already start with terminals, and every production of AiA_i that starts with a nonterminal starts with such an AjA_j, by Claim 2. Substituting AjA_j gives productions that start with the first symbol of a production of AjA_j, a terminal. □\square

Claim 4. Every production of BiB_i starts with some AkA_k or with some BjB_j with j<ij < i.

The productions of BiB_i are αt\alpha_t and αtBi\alpha_t B_i, where Ai→AiαtA_i \to A_i \alpha_t was a production when line L8 handled AiA_i. By Claim 1, αt\alpha_t is a nonempty string of nonterminals, and the nonterminals that existed at that time were the AkA_k and the BjB_j created at an earlier iteration, so with j<ij < i. Lines L9 and L10 do not change which nonterminal starts a production of BiB_i before line L10 handles BiB_i. □\square

Part 3: the loops end and finish the form

The loops of lines L5, L6 and L9 are bounded. Each substitution step is finite, because XX has finitely many productions.

For line L10, the implementation keeps the BiB_i in a queue. It takes the first BiB_i; if a production of BiB_i starts with a BjB_j that is still in the queue, it puts BiB_i back at the end, and otherwise it substitutes the leading nonterminal of each production of BiB_i and removes BiB_i from the queue.

Claim 5. The queue becomes empty, and then every production of every BiB_i starts with a terminal.

While the queue is not empty, let BiB_i be the element with the smallest index. By Claim 4 its productions start with an AkA_k or with a BjB_j with j<ij < i, and no such BjB_j is in the queue. So within one pass through the queue, BiB_i is handled and removed. The queue has at most mm elements, so it is empty after at most mm passes. When BiB_i is handled, each production starts with an AkA_k, whose productions start with terminals by Claim 3, or with a BjB_j that has left the queue, whose productions start with terminals by induction on the order of removal. One substitution therefore makes every production of BiB_i start with a terminal, and BiB_i does not change afterwards. □\square

By Claims 1, 3 and 5 every production of A1,…,AmA_1, \ldots, A_m (including the TaT_a) and of every BiB_i is regular and starts with a terminal, so it is in form. Line L11 adds S0→εS_0 \to \varepsilon and copies of the productions of SS, which are in form; S0S_0 is a fresh name and occurs on no right side. With Part 1, this proves the theorem. ■\blacksquare

Back to Greibach normal form conversion