Introduction
Before algorithms such as CYK or proofs such as the CFL pumping lemma, grammars are cleaned: remove useless symbols, -productions (when ), 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
- Context-Free Grammars, Derivations and Ambiguity: CFG basics.
Core concepts
Useless symbols
A symbol is useful if some derivation with terminal. Equivalently: is generating and reachable from 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 using only generating symbols; delete the rest.
Eliminating -productions
If , remove all . First compute nullable variables (those that derive ). For every production with nullable symbols on the right, add variants with those symbols deleted (but do not add a new ). If , keep a fresh start and eliminate from the rest.
Eliminating unit productions
A unit production is with a variable. Compute unit pairs with by only units. For every such pair and every non-unit production , add . Then delete all units.
Chomsky normal form
Every production is (two variables) or (one terminal), except possibly if . Any CFG converts to CNF. Doing the binarisation (START, BIN) before -removal keeps the growth polynomial; removing first can blow up exponentially, since a body of length with nullable symbols yields up to productions. CYK needs CNF.
Greibach normal form
Every production is with terminal and a (possibly empty) string of variables (plus if ). Every CFL without 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 has a derivation of exactly steps. A GNF grammar gives a PDA with one state that reads one input symbol on every move (no -moves except possibly for itself). A GNF grammar can never be left-recursive.
Removing immediate left recursion
(no starts with ) becomes
Both generate , with any and any . Example: becomes , ; both generate . The same transformation is used in top-down parsing.
How it works
Recommended order (Hopcroft-Motwani-Ullman / Linz)
- Eliminate -productions (after arranging start if needed).
- Eliminate unit productions.
- Eliminate useless symbols (generating then reachable), or eliminate useless both before and after as a safety pass.
- Convert to CNF: replace terminals inside long right-hand sides by fresh variables ; break long variable strings into binary trees of fresh variables.
Algorithms and pseudocode
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 GMathematical 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 has exactly variable nodes: of them use and use . Because the tree above the terminal level is binary, a tree whose longest root-to-leaf path has variables yields a string of length at most . So if , some path repeats a variable, which is exactly what the CFL pumping lemma pumps; this gives the pumping length .
Worked examples
Example 1: remove useless symbols
, , , .
- Generating symbols: (), , (). is not generating: its only rule is . Delete and every production that mentions it, here and .
- Reachable from in what is left (, , ): only . Delete and .
Final grammar: .
Order matters. Doing reachability first finds reachable (only is removed); deleting non-generating afterwards removes and leaves behind, a useless production that the wrong order fails to delete. Always remove non-generating symbols first, then unreachable ones.
Example 2: nullable and -removal
, , . Nullable: . After removal (keeping in language via ): productions such as , , .
Example 3: unit elimination
, , . Unit pairs involving S,A,B. Add , ; delete units.
Example 4: to CNF
. After cleanup: introduce , , then ; binarize , , and . The CNF grammar has 4 variables and 5 productions and still generates .
Derivation length in CNF. A leftmost derivation of aabb is : 7 steps . In general a string of length needs exactly terminal steps () and binary steps (each adds one leaf position), so every derivation in CNF has exactly steps, and every parse tree has variable nodes.
Example 5: a GNF grammar and its derivation length
, is in GNF and generates . aabb: , 4 steps for 4 symbols. Compare 7 steps in CNF: a derivation of a string of length takes exactly steps in GNF and in CNF.
Example 6: removing and units together
, ; language , which contains .
- Nullable: , then (via ).
- Remove : , , and a new start .
- Unit pairs: , , . Replace units: , , .
- CNF: , , , ; then , and , and the same right-hand sides as for plus .
Check: acb derives as , 5 steps .
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 or . For the CNF grammar , , , , the top cell for baaba contains , 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 -productions before unit productions, because -removal can create new units (in Example 6, -removal introduced the unit ); remove useless symbols last, non-generating before unreachable.
- A unit cycle such as , is harmless: both pairs and 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 -productions when without a new start.
- Calling 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 and (plus optional ).
- COUNT NEW VARIABLES. Binarization of length- RHS adds fresh variables.
- USELESS SYMBOLS. Always both generating and reachable; test generating first.
- DERIVATION LENGTH. For , CNF: steps; GNF: steps. If the start-to-epsilon exception is present, the empty string takes one step.
- TREE HEIGHT. CNF yield length at most for variables on the longest path; the pumping length is .
- IN THE LANGUAGE. Neither CNF nor GNF can produce except through with never on a right-hand side.
- LEFT RECURSION. becomes , .
Summary
Sources
- 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).
- M. Sipser, Introduction to the Theory of Computation, 3rd ed., Cengage Learning: Section 2.1 (Chomsky normal form).
- 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).