Introduction

Before algorithms such as CYK or proofs such as the CFL pumping lemma, grammars are cleaned: remove useless symbols, ε\varepsilon-productions (when ε∉L\varepsilon \notin L), and unit productions, then convert to Chomsky normal form (CNF) or Greibach normal form (GNF). The skills that matter are the mechanical steps and the shape of CNF productions.

Prerequisites

Core concepts

Useless symbols

A symbol XX is useful if some derivation S⇒∗αXβ⇒∗wS \Rightarrow^* \alpha X \beta \Rightarrow^* w with ww terminal. Equivalently: XX is generating and reachable from SS in the grammar after removing nongenerating symbols and productions containing them; reachability in the original grammar alone is insufficient. Algorithm: compute generating symbols bottom-up; then reachable symbols by forward search from SS using only generating symbols; delete the rest.

Eliminating ε\varepsilon-productions

If ε∉L(G)\varepsilon \notin L(G), remove all A→εA \to \varepsilon. First compute nullable variables (those that derive ε\varepsilon). For every production with nullable symbols on the right, add variants with those symbols deleted (but do not add a new B→εB \to \varepsilon). If ε∈L(G)\varepsilon \in L(G), keep a fresh start S′→S∣εS' \to S \mid \varepsilon and eliminate ε\varepsilon from the rest.

Eliminating unit productions

A unit production is A→BA \to B with BB a variable. Compute unit pairs (A,B)(A,B) with A⇒∗BA \Rightarrow^* B by only units. For every such pair and every non-unit production B→γB \to \gamma, add A→γA \to \gamma. Then delete all units.

Chomsky normal form

Every production is A→BCA \to BC (two variables) or A→aA \to a (one terminal), except possibly S→εS \to \varepsilon if ε∈L\varepsilon \in L. Any CFG converts to CNF. Doing the binarisation (START, BIN) before ε\varepsilon-removal keeps the growth polynomial; removing ε\varepsilon first can blow up exponentially, since a body of length mm with mm nullable symbols yields up to 2m−12^m - 1 productions. CYK needs CNF.

Greibach normal form

Every production is A→aαA \to a \alpha with aa terminal and α\alpha a (possibly empty) string of variables (plus S→εS \to \varepsilon if ε∈L\varepsilon \in L). Every CFL without ε\varepsilon has a GNF grammar. Conversion first removes left recursion and then substitutes leading variables until every right-hand side starts with a terminal. The form explains one-terminal-per-step derivations and the absence of left recursion.

Consequences of GNF. Each derivation step emits exactly one terminal, so a string of length n≥1n\ge1 has a derivation of exactly nn steps. A GNF grammar gives a PDA with one state that reads one input symbol on every move (no ε\varepsilon-moves except possibly for ε\varepsilon itself). A GNF grammar can never be left-recursive.

Removing immediate left recursion

A→Aα1∣⋯∣Aαm∣β1∣⋯∣βnA \to A\alpha_1 \mid \cdots \mid A\alpha_m \mid \beta_1 \mid \cdots \mid \beta_n (no βj\beta_j starts with AA) becomes

A→β1A′∣⋯∣βnA′,A′→α1A′∣⋯∣αmA′∣ε.A \to \beta_1 A' \mid \cdots \mid \beta_n A', \qquad A' \to \alpha_1 A' \mid \cdots \mid \alpha_m A' \mid \varepsilon .

Both generate β(α)∗\beta (\alpha)^*, with β\beta any βj\beta_j and α\alpha any αi\alpha_i. Example: E→E+a∣aE \to E + a \mid a becomes E→aE′E \to a E', E′→+aE′∣εE' \to + a E' \mid \varepsilon; both generate a(+a)∗a(+a)^*. The same transformation is used in top-down parsing.

How it works

  1. Eliminate ε\varepsilon-productions (after arranging start if needed).
  2. Eliminate unit productions.
  3. Eliminate useless symbols (generating then reachable), or eliminate useless both before and after as a safety pass.
  4. Convert to CNF: replace terminals inside long right-hand sides by fresh variables Ua→aU_a \to a; break long variable strings into binary trees of fresh variables.

Algorithms and pseudocode

Text
NULLABLE(G)
1  N <- { A | A -> eps in R }
2  repeat
3      add A if A -> X1...Xk and every Xi in N
4  until stable
5  return N

TO-CNF(G)
1  G <- ELIM-EPS(G); G <- ELIM-UNIT(G); G <- ELIM-USELESS(G)
2  replace terminals in long RHS by fresh U_a
3  binarize RHS longer than 2 with fresh variables
4  return G

Mathematical explanation

Why CNF still generates the same language

Each transformation preserves the terminal yield: nullable expansion simulates deleting nullable variables; unit elimination short-circuits unit chains; binarization introduces fresh variables that expand uniquely to the old flat RHS.

Parse-tree height in CNF

A CNF parse tree for a string of length n≥1n \ge 1 has exactly 2n−12n-1 variable nodes: nn of them use A→aA \to a and n−1n-1 use A→BCA \to BC. Because the tree above the terminal level is binary, a tree whose longest root-to-leaf path has hh variables yields a string of length at most 2h−12^{h-1}. So if ∣w∣>2∣V∣−1|w| > 2^{|V|-1}, some path repeats a variable, which is exactly what the CFL pumping lemma pumps; this gives the pumping length p=2∣V∣p = 2^{|V|}.

Worked examples

Example 1: remove useless symbols

S→AB∣aS \to AB \mid a, A→bA \to b, B→BB \to B, C→cC \to c.

  1. Generating symbols: AA (A→bA \to b), CC, SS (S→aS \to a). BB is not generating: its only rule is B→BB \to B. Delete BB and every production that mentions it, here S→ABS \to AB and B→BB \to B.
  2. Reachable from SS in what is left (S→aS \to a, A→bA \to b, C→cC \to c): only SS. Delete AA and CC.

Final grammar: S→aS \to a.

Order matters. Doing reachability first finds S,A,BS, A, B reachable (only CC is removed); deleting non-generating BB afterwards removes S→ABS \to AB and leaves A→bA \to b behind, a useless production that the wrong order fails to delete. Always remove non-generating symbols first, then unreachable ones.

Example 2: nullable and ε\varepsilon-removal

S→ABS \to AB, A→aA∣εA \to aA \mid \varepsilon, B→bB∣εB \to bB \mid \varepsilon. Nullable: A,B,SA,B,S. After removal (keeping ε\varepsilon in language via S′→S∣εS' \to S \mid \varepsilon): productions such as S→AB∣A∣BS \to AB \mid A \mid B, A→aA∣aA \to aA \mid a, B→bB∣bB \to bB \mid b.

Example 3: unit elimination

S→AS \to A, A→BA \to B, B→aB∣bB \to aB \mid b. Unit pairs involving S,A,B. Add S→aB∣bS \to aB \mid b, A→aB∣bA \to aB \mid b; delete units.

Example 4: to CNF

S→aSb∣abS \to aSb \mid ab. After cleanup: introduce A→aA \to a, B→bB \to b, then S→ASB∣ABS \to A S B \mid A B; binarize S→AXS \to A X, X→SBX \to S B, and S→ABS \to A B. The CNF grammar has 4 variables and 5 productions and still generates {anbn∣n≥1}\{a^n b^n \mid n \ge 1\}.

Derivation length in CNF. A leftmost derivation of aabb is S⇒AX⇒aX⇒aSB⇒aABB⇒aaBB⇒aabB⇒aabbS \Rightarrow AX \Rightarrow aX \Rightarrow aSB \Rightarrow aABB \Rightarrow aaBB \Rightarrow aabB \Rightarrow aabb: 7 steps =2⋅4−1= 2 \cdot 4 - 1. In general a string of length n≥1n \ge 1 needs exactly nn terminal steps (A→aA \to a) and n−1n - 1 binary steps (each A→BCA \to BC adds one leaf position), so every derivation in CNF has exactly 2n−12n - 1 steps, and every parse tree has 2n−12n - 1 variable nodes.

Example 5: a GNF grammar and its derivation length

S→aSB∣aBS \to aSB \mid aB, B→bB \to b is in GNF and generates {anbn∣n≥1}\{a^n b^n \mid n \ge 1\}. aabb: S⇒aSB⇒aaBB⇒aabB⇒aabbS \Rightarrow aSB \Rightarrow aaBB \Rightarrow aabB \Rightarrow aabb, 4 steps for 4 symbols. Compare 7 steps in CNF: a derivation of a string of length nn takes exactly nn steps in GNF and 2n−12n - 1 in CNF.

Example 6: removing ε\varepsilon and units together

S→aSb∣AS \to aSb \mid A, A→cA∣εA \to cA \mid \varepsilon; language {ancmbn}\{a^n c^m b^n\}, which contains ε\varepsilon.

  1. Nullable: AA, then SS (via S→AS \to A).
  2. Remove ε\varepsilon: S→aSb∣ab∣AS \to aSb \mid ab \mid A, A→cA∣cA \to cA \mid c, and a new start S0→S∣εS_0 \to S \mid \varepsilon.
  3. Unit pairs: (S0,S)(S_0, S), (S0,A)(S_0, A), (S,A)(S, A). Replace units: S0→aSb∣ab∣cA∣c∣εS_0 \to aSb \mid ab \mid cA \mid c \mid \varepsilon, S→aSb∣ab∣cA∣cS \to aSb \mid ab \mid cA \mid c, A→cA∣cA \to cA \mid c.
  4. CNF: Ua→aU_a \to a, Ub→bU_b \to b, Uc→cU_c \to c, X→SUbX \to S U_b; then S→UaX∣UaUb∣UcA∣cS \to U_a X \mid U_a U_b \mid U_c A \mid c, and A→UcA∣cA \to U_c A \mid c, and the same right-hand sides as SS for S0S_0 plus S0→εS_0 \to \varepsilon.

Check: acb derives as S0⇒UaX⇒aX⇒aSUb⇒acUb⇒acbS_0 \Rightarrow U_a X \Rightarrow a X \Rightarrow a S U_b \Rightarrow a c U_b \Rightarrow a c b, 5 steps =2⋅3−1= 2 \cdot 3 - 1.

Example 7: CYK needs CNF

CYK decides membership by filling a table of substrings bottom-up, and each cell combines exactly two smaller cells, which is possible only because every rule has the shape A→BCA \to BC or A→aA \to a. For the CNF grammar S→AB∣BCS \to AB \mid BC, A→BA∣aA \to BA \mid a, B→CC∣bB \to CC \mid b, C→AB∣aC \to AB \mid a, the top cell for baaba contains SS, so the string is in the language (the full table is in Closure and Decision Properties of CFLs).

Important properties and observations

  • Order matters: remove ε\varepsilon-productions before unit productions, because ε\varepsilon-removal can create new units (in Example 6, ε\varepsilon-removal introduced the unit S0→SS_0 \to S); remove useless symbols last, non-generating before unreachable.
  • A unit cycle such as A→BA \to B, B→AB \to A is harmless: both pairs (A,B)(A,B) and (B,A)(B,A) are unit pairs, and both variables receive each other's non-unit rules.
  • CNF never has mixed terminal/variable RHS of length greater than 1.
  • GNF starts every RHS with a terminal.
  • Simplification does not remove ambiguity by itself.

Common misconceptions.

  • Deleting all ε\varepsilon-productions when ε∈L\varepsilon \in L without a new start.
  • Calling A→aBA \to aB a CNF production (it is GNF-like, not CNF).
  • Forgetting reachability after deleting non-generating symbols.

GATE examination perspective

  • EXECUTE STEPS IN ORDER. Show nullable set, then new productions, then deletions.
  • CNF SHAPE CHECK. Only A→BCA\to BC and A→aA\to a (plus optional S→εS\to\varepsilon).
  • COUNT NEW VARIABLES. Binarization of length-kk RHS adds k−2k-2 fresh variables.
  • USELESS SYMBOLS. Always both generating and reachable; test generating first.
  • DERIVATION LENGTH. For n≥1n\ge1, CNF: 2n−12n-1 steps; GNF: nn steps. If the start-to-epsilon exception is present, the empty string takes one step.
  • TREE HEIGHT. CNF yield length at most 2h−12^{h-1} for hh variables on the longest path; the pumping length is 2∣V∣2^{|V|}.
  • ε\varepsilon IN THE LANGUAGE. Neither CNF nor GNF can produce ε\varepsilon except through S→εS \to \varepsilon with SS never on a right-hand side.
  • LEFT RECURSION. A→Aα∣βA \to A\alpha \mid \beta becomes A→βA′A \to \beta A', A′→αA′∣εA' \to \alpha A' \mid \varepsilon.

Summary

Sources

  1. J. E. Hopcroft, R. Motwani, J. D. Ullman, Introduction to Automata Theory, Languages, and Computation, 3rd ed., Pearson Addison-Wesley, Section 7.1 (normal forms: eliminating useless symbols, computing generating and reachable symbols, eliminating epsilon-productions and unit productions, Chomsky normal form), Section 7.2.1 (the size of parse trees).
  2. M. Sipser, Introduction to the Theory of Computation, 3rd ed., Cengage Learning: Section 2.1 (Chomsky normal form).
  3. P. Linz, An Introduction to Formal Languages and Automata, 6th ed., Jones & Bartlett Learning: Section 6.1 (removing useless productions, lambda-productions and unit-productions) and Section 6.2 (Chomsky normal form, Greibach normal form).

Next steps