Algorithm Search⌘K

Proofs: Chomsky normal form conversion

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

The Chomsky normal form conversion keeps the language and produces Chomsky normal form full

This proof shows: correctness.

Let G=(V,Σ,R,S)G = (V, \Sigma, R, S) with V∩Σ=∅V \cap \Sigma = \emptyset. Write G1,…,G6G_1, \ldots, G_6 for the grammars after START (L1), TERM (L2, L3), BIN (L4, L5), DEL (L6 to L8), UNIT (L9, L10) and USELESS (L11). For a grammar HH and a symbol XX let LH(X)={w∈Σ∗:X⇒H∗w}L_H(X) = \{ w \in \Sigma^* : X \Rightarrow^*_H w \}, with LH(a)={a}L_H(a) = \{a\} for a terminal aa. We use parse trees throughout: w∈LH(X)w \in L_H(X) exactly when there is a parse tree of HH with root XX and yield ww. All new nonterminals are fresh, so they differ from each other and from every symbol of GG.

Invariant 0. S0S_0 is on no right side of any GiG_i. It is fresh, L1 puts it only on a left side, TERM and BIN rewrite right sides of G1G_1 without adding S0S_0, and DEL and UNIT only build right sides from parts of existing right sides.

Step 1 (START). LG1(S0)=LG(S)L_{G_1}(S_0) = L_G(S), and LG1(A)=LG(A)L_{G_1}(A) = L_G(A) for A∈VA \in V.

Proof. By Invariant 0, a parse tree of G1G_1 contains S0S_0 at most at the root. A tree with root A∈VA \in V is a tree of GG. A tree with root S0S_0 is S0→SS_0 \to S above a tree of GG with root SS. □\square

Step 2 (TERM). LG2(A)=LG1(A)L_{G_2}(A) = L_{G_1}(A) for every nonterminal AA of G1G_1.

Proof. Let hh replace each new TaT_a by aa. Every production of G2G_2 other than Ta→aT_a \to a is A→α′A \to \alpha' with A→h(α′)A \to h(\alpha') in G1G_1. Given a tree of G2G_2 with root A≠TaA \ne T_a, delete every node TaT_a and attach its leaf aa to its parent; the result is a tree of G1G_1 with the same root and yield. Conversely, in a tree of G1G_1, replace at each node that uses a production with ∣α∣≥2|\alpha| \ge 2 each terminal child aa by a node TaT_a with the leaf aa; the result is a tree of G2G_2. □\square

Step 3 (BIN). LG3(A)=LG2(A)L_{G_3}(A) = L_{G_2}(A) for every nonterminal AA of G2G_2.

Proof. The new nonterminals A1,…,Ak−2A_1, \ldots, A_{k-2} made for one production p=A→X1⋯Xkp = A \to X_1 \cdots X_k are fresh for pp, so each AiA_i has exactly one production, Ai→Xi+1Ai+1A_i \to X_{i+1} A_{i+1} (or Ak−2→Xk−1XkA_{k-2} \to X_{k-1} X_k), and appears on exactly one right side, that of Ai−1A_{i-1} (or of AA for i=1i = 1). In a tree of G3G_3 with an old root, every node labelled A1A_1 is therefore the right child of a node AA that uses A→X1A1A \to X_1 A_1, and below it the chain continues to Ak−2A_{k-2}. Contracting each such chain into one node AA with children X1,…,XkX_1, \ldots, X_k gives a tree of G2G_2 with the same yield. Expanding each node that uses pp into the chain turns a tree of G2G_2 into a tree of G3G_3. □\square

After BIN every right side has length at most 2, and every right side of length 2 consists of two nonterminals, by TERM.

Step 4 (DEL). Let NN be the set of nullable nonterminals of G3G_3. Then for every nonterminal AA of G3G_3 and every w≠εw \ne \varepsilon, w∈LG4(A)  ⟺  w∈LG3(A)w \in L_{G_4}(A) \iff w \in L_{G_3}(A). Moreover LG4(S0)=LG3(S0)L_{G_4}(S_0) = L_{G_3}(S_0).

Proof. (⇒\Rightarrow) Every production of G4G_4 other than S0→εS_0 \to \varepsilon is A→αA \to \alpha where α\alpha comes from a production A→βA \to \beta of G3G_3 by deleting symbols of NN, which derive ε\varepsilon in G3G_3. So A⇒G3∗αA \Rightarrow^*_{G_3} \alpha, and every derivation of G4G_4 that does not use S0→εS_0 \to \varepsilon is simulated in G3G_3. A derivation of w≠εw \ne \varepsilon from AA does not use S0→εS_0 \to \varepsilon: if A≠S0A \ne S_0 it never meets S0S_0, by Invariant 0, and if A=S0A = S_0 the production S0→εS_0 \to \varepsilon yields only ε\varepsilon.

(⇐\Leftarrow) By induction on the height of a tree TT of G3G_3 with root AA and yield w≠εw \ne \varepsilon. The root uses A→Y1⋯YjA \to Y_1 \cdots Y_j with 1≤j≤21 \le j \le 2, since j=0j = 0 gives the yield ε\varepsilon. Let w=w1⋯wjw = w_1 \cdots w_j be the yields of the children, and let I={i:wi≠ε}I = \{ i : w_i \ne \varepsilon \}, which is nonempty. For i∉Ii \notin I, YiY_i derives ε\varepsilon, so it is a nonterminal in NN. For i∈Ii \in I, YiY_i is a terminal with wi=Yiw_i = Y_i, or a nonterminal with wi∈LG4(Yi)w_i \in L_{G_4}(Y_i) by induction. The production A→(Yi)i∈IA \to (Y_i)_{i \in I} is in G4G_4: if I={1,…,j}I = \{1, \ldots, j\} it is the original nonempty production, kept by L8; if j=2j = 2 and I={1}I = \{1\} it was added by L7 because Y2∈NY_2 \in N; if I={2}I = \{2\}, because Y1∈NY_1 \in N. Using it at the root gives a tree of G4G_4 with yield ww.

Finally, ε∈LG4(S0)\varepsilon \in L_{G_4}(S_0) exactly when L8 added S0→εS_0 \to \varepsilon, that is, when S0∈NS_0 \in N, that is, when ε∈LG3(S0)\varepsilon \in L_{G_3}(S_0): every other production of G4G_4 has a nonempty right side, so no other derivation can yield ε\varepsilon. □\square

After DEL the productions are A→BCA \to B C with B,CB, C nonterminals, A→aA \to a, unit productions A→BA \to B, and possibly S0→εS_0 \to \varepsilon. L7 adds only right sides of length 1, and L8 removes every other ε\varepsilon-production.

Step 5 (UNIT). For every nonterminal AA, LG5(A)=LG4(A)L_{G_5}(A) = L_{G_4}(A).

Proof. (⊆\subseteq) A production A→βA \to \beta of G5G_5 comes from some B∈U(A)B \in U(A) with B→βB \to \beta in G4G_4, so A⇒G4∗B⇒G4βA \Rightarrow^*_{G_4} B \Rightarrow_{G_4} \beta. Replacing each step of a derivation of G5G_5 by these steps gives a derivation of G4G_4.

(⊇\supseteq) By induction on the number of nodes of a tree TT of G4G_4 with root AA and yield ww. Starting at the root, follow the nodes that use unit productions: A=B0,B1,…,BmA = B_0, B_1, \ldots, B_m, where Bi−1→BiB_{i-1} \to B_i is a unit production and the node BmB_m uses a production Bm→βB_m \to \beta that is not a unit production. Then Bm∈U(A)B_m \in U(A), so A→βA \to \beta is in G5G_5 by L10. The children of the node BmB_m are terminals, or roots of subtrees with fewer nodes than TT whose yields are, by induction, derivable in G5G_5. Putting A→βA \to \beta at the root above these subtrees gives a tree of G5G_5 with yield ww. □\square

U(A)U(A) is computed by a graph search over the unit productions, which terminates even when they form cycles. L10 copies only productions that are not unit productions, so G5G_5 has only A→BCA \to BC, A→aA \to a and S0→εS_0 \to \varepsilon. Since S0S_0 is on no right side, S0∈U(A)S_0 \in U(A) only for A=S0A = S_0, so S0→εS_0 \to \varepsilon is not copied to any other nonterminal.

Step 6 (USELESS). LG6(S0)=LG5(S0)L_{G_6}(S_0) = L_{G_5}(S_0).

Proof. G6G_6 has a subset of the productions of G5G_5, which gives ⊆\subseteq. Conversely, let TT be a tree of G5G_5 with root S0S_0 and yield w∈Σ∗w \in \Sigma^*. Every node XX of TT is the root of a subtree with a terminal yield, so XX is generating, and every production used in TT has a generating left side and generating right-side symbols; so it survives the first part of L11. Every node is reached from the root along edges of TT, which use these surviving productions, so its label is reachable from S0S_0 in the grammar after the first part, and the production survives the second part. So TT is a tree of G6G_6. □\square

Theorem. G6G_6 is in Chomsky normal form with start symbol S0S_0, and L(G6)=L(G)L(G_6) = L(G).

Proof. The language: by Steps 1 to 3, LG3(S0)=LG(S)L_{G_3}(S_0) = L_G(S); by Step 4, LG4(S0)=LG3(S0)L_{G_4}(S_0) = L_{G_3}(S_0); by Steps 5 and 6, L(G6)=LG5(S0)=LG4(S0)L(G_6) = L_{G_5}(S_0) = L_{G_4}(S_0). The form: after Step 5 every production is A→BCA \to BC with nonterminals B,CB, C, A→aA \to a, or S0→εS_0 \to \varepsilon; Step 6 only removes productions; and by Invariant 0, B,C≠S0B, C \ne S_0. ■\blacksquare

Back to Chomsky normal form conversion