Sub topic 7
Chomsky and Greibach Normal Forms
Prerequisites
Assume that you know context-free grammars (CFGs), derivations, parse trees, and the meaning of a language generated by a grammar. You should also know basic set operations and the difference between terminals and nonterminals.
A grammar can describe the correct language but still be difficult to analyze. Normal forms restrict the shape of productions without changing the language, apart from carefully handled empty-string cases. They are useful for parsing algorithms and proofs.
Simplifying a grammar first
Before converting a grammar, remove productions that add unnecessary structure.
- An epsilon production has the form \(A \to \epsilon\), where \(\epsilon\) is the empty string.
- A unit production has the form \(A \to B\), where both symbols are nonterminals.
- A useless symbol either cannot derive a string of terminals or cannot be reached from the start symbol.
A typical simplification order is:
- Remove useless symbols.
- Remove epsilon productions.
- Remove unit productions.
- Remove any useless symbols created during the process.
If the original language contains \(\epsilon\), introduce a new start symbol \(S0\) with \(S0 \to S\) and allow \(S_0 \to \epsilon\) when required.
Chomsky normal form
A CFG is in Chomsky normal form (CNF) when every production has one of these forms:
\[ A \to BC \]
or
\[ A \to a \]
Here, \(A,B,C\) are nonterminals and \(a\) is a terminal. The start symbol may also have \(S \to \epsilon\), if the language contains the empty string.
Long right-hand sides are broken into binary productions. Terminals inside longer right-hand sides are replaced by new variables.
Original: A -> a B C D
CNF: A -> X B
X -> Y C
Y -> Z D
Z -> aThe exact names of new variables do not matter. What matters is that every new production follows the CNF pattern.
CNF is especially useful for the CYK algorithm. CYK checks whether a string belongs to a CFG by combining results for shorter substrings.
Greibach normal form
A CFG is in Greibach normal form (GNF) when every production begins with a terminal:
\[ A \to a\alpha \]
where \(a\) is a terminal and \(\alpha\) is a possibly empty sequence of nonterminals. Thus, examples include \(A \to aBC\) and \(B \to b\), but not \(A \to BC\).
GNF has an important derivation property: every derivation step produces one terminal at the left edge. A string of length \(n\) therefore requires exactly \(n\) useful production steps, excluding a possible start production for \(\epsilon\).
| Normal form | Production shape | Main use |
|---|---|---|
| CNF | \(A \to BC\) or \(A \to a\) | CYK and structural proofs |
| GNF | \(A \to a\alpha\) | Leftmost derivations and PDA constructions |
Conversion choices and common errors
CNF conversion is usually mechanical after simplification. GNF conversion may require eliminating left recursion and ordering variables carefully. Both conversions can increase grammar size.
Do not remove \(A \to \epsilon\) blindly. First determine whether the language contains \(\epsilon\). Also remember that normal form describes productions, not a unique grammar. Two different normal-form grammars can generate the same language.
Exercise
Which production violates CNF, and how can it be repaired: A -> aBC?
Show answer
It violates CNF because it mixes a terminal with two variables. Introduce X -> a, then use A -> XB.