Simplification, CNF and GNF
Simplifying context-free grammars (removing epsilon-productions, unit productions and useless symbols, in the right order), Chomsky normal form (every production A -> BC or A -> a) and Greibach normal form (every production A -> a followed by variables), the conversion procedure with a stage-by-stage example, derivation lengths (2n - 1 steps in CNF, n in GNF), and the CYK membership algorithm with its table.
Why it exists
Arbitrary grammars are messy: empty right sides, chains of unit productions, variables that go nowhere. For proofs and algorithms one wants a standard shape. Chomsky normal form (CNF) makes every parse tree binary, which gives the CYK membership algorithm and the pumping lemma for context-free languages. Greibach normal form (GNF) makes every production start with a terminal, so a derivation of a string of length takes exactly steps and a pushdown automaton can run without -moves. GATE asks for the conversion steps, the number of productions or steps, and CYK tables.
Prerequisites
You need grammars, derivations and parse trees (context-free grammars).
Precise definitions
A grammar is in Chomsky normal form if every production has one of the forms
and, only if , the single production with not occurring on any right side. A grammar is in Greibach normal form if every production is with a terminal and a (possibly empty) string of variables.
Theorem. Every CFL has a grammar in CNF and a grammar in GNF (apart from the empty string). The conversion below produces grammars for ; if add a new start variable with .
Terms:
- a variable is nullable if ;
- a unit production is with a variable;
- a variable is generating if it derives some terminal string, and reachable if it occurs in a sentential form derivable from ; a variable that is not both is useless.
How it works
Simplification steps
- Eliminate -productions. Find the nullable variables (iterate: is nullable if it has a production whose right side consists only of nullable variables). For every production, add all variants obtained by deleting any subset of nullable symbols from its right side, and delete all (never add an empty right side). This can create unit productions.
- Eliminate unit productions. For every pair with using unit productions only, give every non-unit production of ; then delete all unit productions.
- Eliminate useless symbols, in this order: first remove variables that are not generating (and every production using them), then remove variables that are not reachable from the start symbol. The order matters: in , , the variable is not generating; removing it deletes and leaves unreachable. Removing unreachable symbols first finds nothing (all three are reachable) and then keeps as a useless leftover (checked by program).
Other orders of steps 1 to 3 can reintroduce what an earlier step removed; the order above (epsilon, then unit, then useless) is safe.
Chomsky normal form
After steps 1 to 3 every right side has length at least , and length only for a terminal. Then:
- Terminals: in every right side of length at least , replace each terminal by a new variable and add .
- Long right sides: replace () by , , , with new variables .
The table runs the whole conversion on one grammar that has an -production (), a chain of unit productions (), a non-generating variable () and an unreachable variable ():
Conversion to Chomsky normal form, stage by stage: start
Computed by a script, not typed| stage | productions | number of productions | Chomsky normal form? |
|---|---|---|---|
| original | |||
| after removing epsilon productions | |||
| after removing unit productions | |||
| after removing useless symbols | |||
| after replacing terminals in long right sides | |||
| after splitting long right sides (Chomsky normal form) |
Step 0
The six stages of the conversion in the order epsilon, unit, useless, terminals, long right sides. The language is the same at every stage (checked up to length 10).
Each stage was checked to generate the same language (without ) on all strings up to length .
Facts about CNF.
- The parse tree of a string of length is binary: leaves, internal nodes with two children and internal nodes with one leaf child. Every derivation has exactly steps ( of the form and of the form ).
- Example: for the grammar , , , needs steps.
Greibach normal form
Start from a CNF grammar. Number the variables . For : (a) substitute, in with , every production of ; (b) if is now left recursive (), remove the left recursion with a new variable (as in LL(1) preparation). At the end every production of starts with a terminal or with , ; substitute backwards from down to so that every right side starts with a terminal, then do the same for the new variables . The tail of each right side consists of variables because the starting grammar had terminals only in .
Example: (generates ) becomes , . Every derivation of a string of length has exactly steps, because every step produces one terminal at the front (checked on the examples and on random grammars).
The GNF of a big grammar can be much larger than the grammar (for the expression grammar the result has dozens of productions); the exam asks it only for tiny grammars.
The CYK algorithm
CYK decides whether for a grammar in CNF. Let be the set of variables that derive the substring of length that starts at position .
- Length : iff is a production.
- Length : iff there is a production and a split (so ) with and .
- iff .
The table has cells, each needs up to splits and each split looks at all productions: time (textbook statement; the cell and split counts are and , checked for ).
For , , , (the language , ):
CYK algorithm on aabb: start
Computed by a script, not typed- 1S → A C
- 2S → A B
- 3C → S B
- 4A → a
- 5B → b
| start \ length | length 1 | length 2 | length 3 | length 4 |
|---|---|---|---|---|
| start 1 (a) | ||||
| start 2 (a) | ||||
| start 3 (b) | ||||
| start 4 (b) |
Step 0
CYK table for the string aabb and the grammar in Chomsky normal form. The cell (start i, length l) will hold the variables that derive the substring of length l starting at position i. Cells of length 1 come from A → a and B → b; a longer cell is filled by trying every split into two shorter pieces.
The top cell contains : is generated. With one more the top cell does not contain :
CYK algorithm on aabbb: start
Computed by a script, not typed- 1S → A C
- 2S → A B
- 3C → S B
- 4A → a
- 5B → b
| start \ length | length 1 | length 2 | length 3 | length 4 | length 5 |
|---|---|---|---|---|---|
| start 1 (a) | |||||
| start 2 (a) | |||||
| start 3 (b) | |||||
| start 4 (b) | |||||
| start 5 (b) |
Step 0
CYK table for the string aabbb and the grammar in Chomsky normal form. The cell (start i, length l) will hold the variables that derive the substring of length l starting at position i. Cells of length 1 come from A → a and B → b; a longer cell is filled by trying every split into two shorter pieces.
Worked examples
EX 1Nullable variables
Easy, , . and are nullable (they have productions), so is nullable too: is nullable, and .
EX 2Eliminate epsilon productions
EasyFor the grammar above the result is , , (the productions , are gone; gave the copies , and, with both deleted, nothing). The new grammar generates ; to keep add with a new start variable .
EX 3Complete conversion to CNF
MediumContinue: remove units (, ): . No useless symbols. Replace terminals in right sides of length : , (, ): , , , , . All right sides now have length at most , so no splitting is needed: this is in CNF with productions (the original grammar had : removing and units made it larger).
EX 4CYK by hand
MediumGrammar , , , , string . Length : , . Length : : nothing ( is not a right side); : gives ; : nothing. Length : = : and give , no production; : empty; nothing. = : empty; : give : . Length : = : give : . So : accepted. The program's table agrees cell by cell.
EX 5Typical questions
GATE level(a) "A CNF grammar generates a string of length ; how many derivation steps?" . (b) "Number of steps in GNF?" . (c) "Which normal form has every production or ?" Chomsky. (d) "Maximum number of CYK cells for a string of length ?" . (e) "Is every CFL generated by a CNF grammar?" Yes except that needs the special production . (f) "Order of simplification?" epsilon, units, useless (non-generating before unreachable). (g) "Is the simplified grammar always smaller?" No: removing -productions and units can increase the number of productions (above: to ).
Cheat box
Cheat box
- Simplify in this order: remove -productions, then unit productions, then useless symbols (non-generating first, then unreachable).
- CNF: or (and if needed): parse trees binary, derivation of has steps.
- GNF: with variables: derivation of has steps.
- CYK: , cells, means accepted.
- Normal forms generate ; add for .
Traps and GATE patterns
Connections
Across subjects
CNF is the basis of the pumping lemma for CFLs (pumping lemma for CFLs); GNF gives PDAs without -moves (PDA and CFG); the left-recursion removal used for GNF is the same as in LL(1) parsing (grammar transformations in compilers).
PYQs
1 previous year question from your book. I re-solved each one myself (1 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs
Let G = (V, Σ, S, P) be a context-free grammar in
Chomsky Normal Form with Σ = {a, b, c} and V
containing 10 variable symbols including the start
symbol S. The string w = a30b30c 30 is derivable from S.
The number of steps (application of rules) in the
derivation S →* w is _________Self-check
Question 1
A grammar in CNF generates a string of length . How many derivation steps does it take?
Show answerHide
.
Question 2
Which variables are nullable in , , ?
Show answerHide
(directly), (through ), and (since and are nullable).
Question 3
How many cells does CYK fill for a string of length ?
Show answerHide
.
Question 4
Put into CNF.
Show answerHide
, , , .
Revision card
One-screen revision
- Order: , units, useless (non-generating first), then terminals and splitting.
- CNF : steps. GNF : steps.
- CYK: fill by length; in the top cell; .
- handled by .
References
- ·Sipser, Introduction to the Theory of Computation (3rd edition), Section 2.1 (Chomsky normal form, Theorem 2.9)
- ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Section 7.1 (normal forms), Section 7.4.4 (the CYK algorithm)
Verification
scripts/enum-checks/toc_cfg.py: on 300 random grammars with epsilon productions, unit productions and useless symbols every stage of the conversion (epsilon, unit, useless, terminals, binarisation) generates the same language minus the empty string on all strings up to length 8; the result is in Chomsky normal form and every derivation of a string of length n takes exactly 2n - 1 steps (dynamic programming over spans, strings up to length 7); the CYK cell for every substring equals the set of variables that derive it (checked against the languages of the single variables); Greibach conversion (217 of the random grammars were small enough to test) gives productions starting with a terminal followed by variables, the same language, and derivations of exactly n steps; the worked grammars were recomputed; the order of removing useless symbols was shown to matter on S -> AB | a, A -> b, B -> Bb. Running times of the algorithms are textbook statements, quoted. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)