Sub topic 7
Chomsky and Greibach Normal Forms
This chapter assumes you can write a context-free grammar (CFG), derive strings from it, and remove useless symbols, ε-productions, and unit productions. The previous chapter covered that cleanup, and the conversions below depend on it.
Why normal forms exist
A general CFG lets a production body be any mix of terminals and variables, of any length. That freedom makes proofs and algorithms awkward. A normal form restricts production shapes without changing the language. Two forms matter for GATE:
- Chomsky normal form (CNF) makes every parse tree binary. The CYK algorithm and the pumping lemma proof both rely on this.
- Greibach normal form (GNF) makes every production start with a terminal. This is useful for building PDAs with no ε-moves and for bounding derivation length.
Every CFL that does not contain ε has a grammar in each form. If ε is in the language, a single production S -> ε is allowed, and only when S never appears on a right-hand side.
Chomsky normal form
A grammar is in CNF if every production has one of these shapes:
A -> B C, where B and C are variables.A -> a, where a is a terminal.S -> ε, only for the start symbol, and only if S is not on any right-hand side.
Conversion procedure
The steps in detail:
- If S appears on a right-hand side, add a new start symbol
S0 -> S. - Remove ε-productions. For each nullable variable, add copies of every production with that variable deleted in each possible combination.
- Remove unit productions
A -> B. Replace each one with the non-unit productions of B, following chains of units. - Remove useless symbols: first those that derive no terminal string, then those unreachable from the start symbol.
- TERM: in any body of length 2 or more, replace each terminal
awith a new variableXa, and addXa -> a. - BIN: replace
A -> B1 B2 ... Bkfor k > 2 withA -> B1 Y1,Y1 -> B2 Y2, and so on, until every body has two variables.
Worked example 1: ε and unit productions
Start with S -> A B, A -> a | ε, B -> b.
- A is nullable. Deleting A from
S -> A BgivesS -> B. The grammar is nowS -> A B | B,A -> a,B -> b. S -> Bis a unit production. Replace it withS -> b. The grammar isS -> A B | b,A -> a,B -> b.- Every body is already a single terminal or two variables, so this is CNF. The language is {ab, b}.
The ε-removal step produced a unit production, which is why the order matters.
Worked example 2: a^n b^n
Start with S -> a S b | a b.
- TERM: introduce
A -> aandB -> b. This givesS -> A S B | A B. - BIN: split
A S Busing a new variable X. This givesS -> A X | A B,X -> S B,A -> a,B -> b.
This is the grammar used in the CYK chapter. The derivation of aabb is:
S => AX => aX => aSB => aABB => aaBB => aabB => aabb
Counting facts
In CNF, a string of length n ≥ 1 is derived in exactly 2n − 1 steps. There are n − 1 steps using A -> B C and n steps using A -> a. The parse tree is binary, so its internal nodes above the leaves all have exactly two children. The derivation above has 7 steps for n = 4.
Conversion can make the grammar larger. Removing ε-productions can grow a production body of length k into up to 2^k variants. The BIN step adds only a linear number of productions.
Greibach normal form
A grammar is in GNF if every production has the form A -> a α, where a is a terminal and α is a (possibly empty) string of variables. The special case S -> ε is allowed under the same condition as in CNF.
A GNF derivation consumes exactly one terminal per step. The leftmost derivation of a string of length n therefore takes exactly n steps. A PDA built from a GNF grammar needs no ε-moves, because each step reads one input symbol.
Conversion idea
The hard part is left recursion, that is, productions A -> A α. The standard fix converts A -> A α | β into:
A -> β Z | βZ -> α Z | α
Here β does not start with A. For example, A -> A a | b becomes A -> b Z | b and Z -> a Z | a.
The full procedure is:
- Get the grammar into CNF, or at least remove ε and unit productions.
- Order the variables A1, ..., Am. For each Ai, substitute for any leading Aj with j < i, so that every body starts with a terminal or with Ak where k ≥ i.
- Remove the left recursion that remains on Ai, which introduces new variables Z.
- Work backwards from Am to A1. Substitute the productions that start with terminals into those that start with a variable, until every body begins with a terminal.
For S -> aSb | ab, the process is short. Replace the trailing b with a variable B and get S -> a S B | a B, B -> b. This is already GNF.
CNF versus GNF
| Property | CNF | GNF |
|---|---|---|
| Production shapes | `A -> BC` or `A -> a` | `A -> a α`, α a string of variables |
| Steps to derive length n | 2n − 1 | n |
| Parse tree | Binary | Each step consumes one terminal |
| Main use | CYK, pumping lemma | PDAs without ε-moves, left-recursion removal |
| Main conversion difficulty | Size growth from ε removal | Eliminating left recursion |
Checking the shapes in code
This script checks whether a grammar is in CNF or GNF. A grammar is a dictionary from a variable to a list of bodies, and each body is a tuple of symbols. Keys are variables, and any other symbol is a terminal.
def uses_start(rules, start):
return any(start in body for bodies in rules.values() for body in bodies)
def is_cnf(rules, start="S"):
variables = set(rules)
for lhs, bodies in rules.items():
for body in bodies:
if body == ():
if lhs != start or uses_start(rules, start):
return False
elif len(body) == 1:
if body[0] in variables:
return False
elif len(body) == 2:
if not all(s in variables for s in body):
return False
else:
return False
return True
def is_gnf(rules, start="S"):
variables = set(rules)
for lhs, bodies in rules.items():
for body in bodies:
if body == ():
if lhs != start or uses_start(rules, start):
return False
elif body[0] in variables or not all(s in variables for s in body[1:]):
return False
return True
cnf = {"S": [("A", "X"), ("A", "B")], "X": [("S", "B")],
"A": [("a",)], "B": [("b",)]}
gnf = {"S": [("a", "S", "B"), ("a", "B")], "B": [("b",)]}
raw = {"S": [("a", "S", "b"), ("a", "b")]}
print(is_cnf(cnf), is_gnf(cnf)) # True False
print(is_cnf(gnf), is_gnf(gnf)) # False True
print(is_cnf(raw), is_gnf(raw)) # False FalseCommon errors
- Forgetting to apply TERM to bodies like
ab. A body of length 2 that contains a terminal is not CNF. - Leaving a unit production behind after ε removal.
- Using the 2n − 1 count for a grammar that is not in CNF. It holds only for CNF.
- Assuming the ε-production rule applies to non-start variables. Only
S -> εis allowed, and only if S is not on any right-hand side. - Treating
A -> A αas GNF. It starts with a variable, so it is left-recursive and not in GNF.
Exercise
A grammar in CNF generates a string of length 10. How many derivation steps does it take, and how many of them use a production of the form A -> a?
Show answer
A CNF derivation of a string of length n takes 2n − 1 = 19 steps. Exactly n = 10 of them use A -> a. The other 9 use A -> B C.
Exercise
Convert S -> A B | a, A -> a | ε, B -> b into CNF. State the language.
Show answer
A is nullable, so S -> A B gives the extra production S -> B. The grammar becomes S -> A B | B | a, A -> a, B -> b. Replace the unit production S -> B with S -> b. The result is S -> A B | a | b, A -> a, B -> b, which is in CNF. The language is {a, ab, b}.
Exercise
Convert A -> A a | b into Greibach normal form.
Show answer
A has the left-recursive production A -> A a and the non-recursive production A -> b. Use a new variable Z. The result is A -> b Z | b and Z -> a Z | a. Every body starts with a terminal, so it is in GNF. It generates b a*, the same language as the original.