Theory of Computation & Computer Graphics โ Introduction to Context Free Language, NEC licence examination syllabus (Nepal Engineering Council).
Chomsky Normal Form (CNF)
Every context-free grammar can be rewritten so that every production has one of just two shapes.
๐ Where this lives: CNF exists to make algorithms possible. The CYK parsing algorithm โ the one that parses any context-free grammar, including ambiguous ones that no LL or LR parser can handle โ requires CNF as its input, because its dynamic programming table depends on every production splitting a substring into exactly two pieces. Natural language parsers, which must cope with grammars far messier than any programming language's, rely on this. Search "CYK algorithm Chomsky normal form natural language parsing".
The form and the conversion
A CONTEXT-FREE GRAMMAR IS IN CHOMSKY NORMAL FORM IF EVERY
PRODUCTION HAS ONE OF EXACTLY TWO SHAPES:
A โ B C two VARIABLES, and neither may be the start
symbol if ฮต โ L(G)
A โ a a single TERMINAL
plus, if the empty string is in the language, the single
permitted exception
S โ ฮต where S is the start symbol, which must then
appear on no right-hand side
THE THEOREM: EVERY CONTEXT-FREE LANGUAGE WITHOUT ฮต (or with the
one exception above) HAS A GRAMMAR IN CHOMSKY NORMAL FORM. The
proof is the conversion algorithm below, so the result is
constructive.
โโ WHY THIS FORM IS USEFUL โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
1. EVERY DERIVATION TREE IS BINARY. Each internal node has
either two variable children or one terminal child, so the
tree is a binary tree โ which is what makes dynamic
programming over substrings work.
2. THE DERIVATION LENGTH IS FIXED AND KNOWN. Deriving a string
of length n takes EXACTLY 2n โ 1 STEPS: nโ1 applications of
A โ BC to build the binary tree, and n applications of
A โ a to produce the terminals.
THIS IS THE KEY PROPERTY FOR PROOFS โ it converts "does
this grammar derive w?" from an unbounded search into a
search of fixed depth, which is what makes membership
decidable by exhaustive search even before CYK.
3. NO ฮต-PRODUCTIONS AND NO UNIT PRODUCTIONS, so a derivation
never stalls or merely renames a variable.
4. IT IS THE PRECONDITION FOR CYK and for the proof of the
CONTEXT-FREE PUMPING LEMMA, which relies on a long string
forcing a deep binary tree and hence a repeated variable on
some root-to-leaf path.
โโ THE CONVERSION, IN FIVE STEPS AND IN THIS ORDER โโโโโโโโโ
THE ORDER MATTERS. Performing the steps out of sequence
reintroduces the forms just removed, and this is the single
most common exam error.
STEP 1 โ ADD A NEW START SYMBOL.
Introduce Sโ โ S, where S was the old start symbol.
WHY: this guarantees the start symbol appears on no
right-hand side, which is required if Sโ โ ฮต must later be
retained.
STEP 2 โ ELIMINATE ฮต-PRODUCTIONS.
First find all NULLABLE variables: A is nullable if
A โ ฮต, or if A โ XโโฆXโ where every Xแตข is nullable.
Compute this to a fixed point.
Then for every production containing nullable symbols, add
all versions with those symbols omitted โ but NEVER add
A โ ฮต itself.
A โ BC with both nullable yields A โ BC | B | C.
STEP 3 โ ELIMINATE UNIT PRODUCTIONS (A โ B).
For each variable A, find every variable reachable from A
by unit productions alone. For each such B, copy all of
B's NON-UNIT productions to A. Then delete all unit
productions.
STEP 4 โ REPLACE TERMINALS IN LONG RIGHT-HAND SIDES.
In any production whose right-hand side has length โฅ 2,
replace each terminal a by a new variable Xโ, and add
Xโ โ a.
So A โ aB becomes A โ Xโ B together with Xโ โ a.
STEP 5 โ BREAK UP LONG RIGHT-HAND SIDES.
A โ BโBโBโโฆBโ with k โฅ 3 becomes a chain:
A โ Bโ Yโ
Yโ โ Bโ Yโ
โฎ
Y_{kโ2} โ B_{kโ1} Bโ
introducing kโ2 new variables.
โโ A FULLY WORKED CONVERSION โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
GIVEN:
S โ A S A | a B
A โ B | S
B โ b | ฮต
STEP 1 โ new start: Sโ โ S
STEP 2 โ ฮต-elimination.
NULLABLE: B (directly, B โ ฮต); then A (since A โ B).
S IS NOT NULLABLE โ S โ ASA needs S itself, and S โ aB
needs the terminal a.
Rewriting each production with nullable symbols omitted in
all combinations:
S โ A S A gives S โ ASA | SA | AS | S
S โ a B gives S โ aB | a
A โ B gives A โ B (and A โ ฮต is dropped)
A โ S unchanged
B โ b unchanged; B โ ฮต deleted
AFTER STEP 2:
Sโ โ S
S โ ASA | SA | AS | S | aB | a
A โ B | S
B โ b
STEP 3 โ unit elimination.
Unit pairs: Sโ โ S; S โ S (trivial); A โ B, A โ S.
Copy the non-unit productions:
Sโ โ ASA | SA | AS | aB | a
S โ ASA | SA | AS | aB | a
A โ ASA | SA | AS | aB | a | b
B โ b
STEP 4 โ terminals in long right-hand sides.
Only aB is affected: introduce Xโ โ a.
Sโ โ ASA | SA | AS | XโB | a
S โ ASA | SA | AS | XโB | a
A โ ASA | SA | AS | XโB | a | b
B โ b
Xโ โ a
STEP 5 โ break up ASA, which has length 3.
For S: S โ A Yโ, Yโ โ S A
For A: A โ A Yโ, Yโ โ S A
(and likewise for Sโ)
FINAL GRAMMAR IN CHOMSKY NORMAL FORM:
Sโ โ A Yโ | S A | A S | Xโ B | a
S โ A Yโ | S A | A S | Xโ B | a
A โ A Yโ | S A | A S | Xโ B | a | b
B โ b
Xโ โ a
Yโ โ S A
Yโ โ S A
EVERY PRODUCTION IS NOW EITHER TWO VARIABLES OR ONE
TERMINAL. โ
(Yโ and Yโ are identical and could be merged โ a legitimate
tidying step, but not required for correctness.)
Consequences and use
โโ THE SIZE OF THE RESULT โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The conversion does not preserve grammar size:
ยท STEP 2 can produce EXPONENTIALLY many productions in the
worst case, since a right-hand side with k nullable
symbols expands into up to 2แต versions.
ยท STEP 3 can produce O(nยฒ) productions.
ยท STEPS 4 and 5 are linear.
IN PRACTICE the blow-up is modest, but the theoretical cost is
worth stating: CNF BUYS ALGORITHMIC CONVENIENCE AT THE PRICE
OF GRAMMAR SIZE, and the CYK algorithm's O(nยณยท|G|) running
time depends on that |G|.
โโ WHAT IS PRESERVED AND WHAT IS NOT โโโโโโโโโโโโโโโโโโโโโโโ
PRESERVED: the LANGUAGE. L(Gโฒ) = L(G), except for the ฮต
handling, which is exactly why the Sโ โ ฮต exception is
stated so carefully.
NOT PRESERVED: the SHAPE OF THE PARSE TREES. The CNF grammar
derives the same strings, but the trees are binary and full
of introduced variables such as Yโ and Xโ. THE ORIGINAL
STRUCTURE โ which operator grouped with which operand โ IS
RECOVERABLE, but only by mapping back through the
conversion.
THIS IS WHY COMPILERS DO NOT PARSE IN CNF: the tree shape is
the meaning, and CNF distorts it. CNF is for ALGORITHMS AND
PROOFS, not for language implementation.
ALSO NOT PRESERVED: AMBIGUITY IS PRESERVED, which is worth
noting explicitly. Converting an ambiguous grammar to CNF
yields an ambiguous CNF grammar โ the conversion cannot fix
a defect that lies in the language's structure.
โโ THE CYK ALGORITHM, WHICH CNF EXISTS TO SERVE โโโโโโโโโโโโ
Given a CNF grammar and a string w = aโaโโฆaโ, build a table
where V[i,j] is the set of variables deriving the substring of
length j starting at position i:
BASE: V[i,1] = { A : A โ aแตข is a production }
STEP: V[i,j] = { A : A โ BC is a production and, for some
split point k with 1 โค k < j,
B โ V[i,k] and C โ V[i+k, jโk] }
ACCEPT if S โ V[1,n].
THE COST: nยฒ/2 table entries, each requiring up to n splits to
be tried, each checked against every production โ hence
O(nยณยท|G|).
THE BINARY FORM IS ESSENTIAL: because every production splits
a substring into exactly TWO pieces, there is a single split
point k to iterate over. A production of length three would
require two split points and a fourth power.
โโ COMPARISON WITH GREIBACH NORMAL FORM โโโโโโโโโโโโโโโโโโโโ
The next topic covers GNF, and the contrast is the point:
CHOMSKY NF GREIBACH NF
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
production
shape A โ BC or A โ a A โ aฮฑ, ฮฑ a string
of variables
every RHS two variables starts with EXACTLY
or one terminal ONE terminal
derivation
length exactly 2n โ 1 exactly n steps for a
for |w| = n string of length n
tree shape BINARY arbitrary branching
used for CYK parsing, proving CFL = PDA,
the CF pumping eliminating left
lemma recursion
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
THE UNDERLYING DIFFERENCE: CNF CONTROLS THE **SHAPE** OF THE
TREE, MAKING IT BINARY SO THAT SUBSTRINGS CAN BE COMBINED
PAIRWISE. GNF CONTROLS THE **PROGRESS** OF THE DERIVATION,
GUARANTEEING THAT EVERY STEP CONSUMES EXACTLY ONE INPUT
SYMBOL. Each normal form is designed for the algorithm it
serves.
The property that makes CNF worth the trouble: deriving a string of length n takes exactly 2n โ 1 steps. That fixed, known derivation length converts "does this grammar derive w?" from an unbounded search into a bounded one โ which is precisely what CYK exploits.
๐ Go further: CNF is why compilers and natural-language parsers diverge. A compiler wants the parse tree's shape, because that shape determines evaluation order โ so it uses LL or LR parsing on the original grammar and never touches CNF. A natural-language parser faces grammars that are wildly ambiguous (a sentence may have hundreds of valid parses) and cannot be made LR at all, so it converts to CNF and runs CYK, accepting the cubic cost and reconstructing structure afterwards. Same theory, opposite engineering choices, driven entirely by whether the grammar can be made deterministic. Search "CYK parsing probabilistic context free grammar natural language".
๐ก Exam angle: state the two permitted production shapes and the ฮต exception. The guaranteed question is a full conversion: perform the five steps in order โ new start symbol, ฮต-elimination via nullable variables, unit elimination, terminal replacement, binarisation โ showing the grammar after each step. Compute the nullable set explicitly and remember that removing A โ ฮต means adding all versions with A omitted, never adding A โ ฮต back. Know that a length-n string takes 2n โ 1 derivation steps and that CNF is required by CYK. Be ready to contrast CNF with GNF.
Syllabus points
CNF rules
Converting a CFG to CNF (numerical)
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Related topics in Introduction to Context Free Language