Chomsky normal form
where , and are nonterminal symbols, is a terminal symbol (a symbol that represents a constant value), is the start symbol, and is the empty string. Also, neither nor may be the start symbol, and the third production rule can only appear if is in , namely, the language produced by the context-free grammar .
Every grammar in Chomsky normal form is context-free, and conversely, every context-free grammar can be transformed into an equivalent one which is in Chomsky normal form. Several algorithms for performing such a transformation are known. Transformations are described in most textbooks on automata theory, such as Hopcroft and Ullman, 1979. As pointed out by Lange and Leiß, the drawback of these transformations is that they can lead to an undesirable bloat in grammar size. The size of a grammar is the sum of the sizes of its production rules, where the size of a rule is one plus the length of its right-hand side. Using to denote the size of the original grammar , the size blow-up in the worst case may range from to , depending on the transformation algorithm used.
Chomsky reduced form
Another way to define the Chomsky normal form (e.g., Hopcroft and Ullman 1978, and Hopcroft et al. 2006) is:
A formal grammar is in Chomsky reduced form if all of its production rules are of the form:
where , and are nonterminal symbols, and is a terminal symbol. When using this definition, or may be the start symbol. Only those context-free grammars which do not generate the empty string can be transformed into Chomsky reduced form.
Floyd normal form
where , and are nonterminal symbols, and is a terminal symbol, because Robert W. Floyd found any BNF syntax can be converted to the above one in 1961. But he withdrew this term, "since doubtless many people have independently used this simple fact in their own work, and the point is only incidental to the main considerations of Floyd's note."
Converting a grammar to Chomsky Normal Form
- Introduce a new start variable, and a new rule where is the previous start variable.
- Eliminate all rules
- rules are rules of the form , where and , where is the CFG's variable alphabet.
- Remove every rule with on its right hand side (RHS). For each rule with in its RHS, add a set of new rules consisting of the different possible combinations of replaced or not replaced with . If a rule has as a singleton on its RHS, add a new rule unless has already been removed through this process. For example, examine the following grammar :
- has one rule. When the is removed, we get the following:
- Notice that we have to account for all possibilities of and so we actually end up adding 3 rules.
- Eliminate all unit rules
- After all the rules have been removed, you can begin removing unit rules, or rules whose RHS contains one variable and no terminals (which is inconsistent with CNF).
- To remove
- , where is a string of variables and terminals, add rule unless this is a unit rule which has already been removed.
- Clean up the remaining rules that are not in Chomsky normal form.
- Replace with , where are new variables.
- If , replace in above rules with some new variable and add rule .
The following grammar, with start symbol Expr, describes a simplified version of the set of all syntactical valid arithmetic expressions in imperative programming langages like C or Algol60. Both Number and Variable are considered terminal symbols here for simplicity, since in a compiler front-end their internal structure is usually not considered by the parser. The terminal symbol "^" denoted exponentiation in Algol60.
Expr → Term | Expr AddOp Term | AddOp Term Term → Factor | Term MulOp Factor Factor → Primary | Factor ^ Primary Primary → Number | Variable | ( Expr ) AddOp → + | − MulOp → * | /
In step 1 of the above conversion algorithm, just a rule S0→Expr is added to the grammar. Since there are no ε rules, step 2 can be skipped. After step 3, the following grammar is obtained:
S0 → Number | Variable | ( Expr ) | Factor ^ Primary | Term MulOp Factor | Expr AddOp Term | AddOp Term Expr → Number | Variable | ( Expr ) | Factor ^ Primary | Term MulOp Factor | Expr AddOp Term | AddOp Term Term → Number | Variable | ( Expr ) | Factor ^ Primary | Term MulOp Factor Factor → Number | Variable | ( Expr ) | Factor ^ Primary Primary → Number | Variable | ( Expr ) AddOp → + | − MulOp → * | /
After step 4, the following grammar is obtained, which is in Chomsky normal form:
S0 → Number | Variable | Open Expr_Close | Factor PowOp_Primary | Term MulOp_Factor | Expr AddOp_Term | AddOp Term Expr → Number | Variable | Open Expr_Close | Factor PowOp_Primary | Term MulOp_Factor | Expr AddOp_Term | AddOp Term Term → Number | Variable | Open Expr_Close | Factor PowOp_Primary | Term MulOp_Factor Factor → Number | Variable | Open Expr_Close | Factor PowOp_Primary Primary → Number | Variable | Open Expr_Close AddOp → + | − MulOp → * | / Expr_Close → Expr Close PowOp_Primary → PowOp Primary MulOp_Factor → MulOp Factor AddOp_Term → AddOp Term Open → ( Close → ) PowOp → ^
The Ai introduced in step 4 are Expr_Close, PowOp_Primary, MulOp_Factor, and AddOp_Term. The Vi are Open, Close, and PowOp. The new rules have been appended at the end of the grammar.
Besides its theoretical significance, CNF conversion is used in some algorithms as a preprocessing step, e.g., the CYK algorithm, a bottom-up parsing for context-free grammars, and its variant probabilistic CKY.
- Backus-Naur form
- CYK algorithm
- Greibach normal form
- Kuroda normal form
- Pumping lemma for context-free languages — its proof relies on the Chomsky normal form
- Hopcroft, Ullman (1979); sect.4, p.106
- Noam Chomsky (1959). "On Certain Formal Properties of Grammars". Information and Control 2: 137–167.
- * John E. Hopcroft, Rajeev Motwani, and Jeffrey D. Ullman. Introduction to Automata Theory, Languages, and Computation, 3rd Edition, Addison-Wesley, 2006. ISBN 0-321-45536-3 (see subsection 7.1.5, page 272.)
- Lange, Martin and Leiß, Hans. To CNF or not to CNF? An Efficient Yet Presentable Version of the CYK Algorithm. Informatica Didactica 8, 2009.
- Donald E. Knuth. 1964. Backus Normal Form vs. Backus Naur Form. Communications of the ACM, 7(12):735–736, December.
- Jurafsky and Martin, 2008. p. 465.
- Cole, Richard. Converting CFGs to CNF (Chomsky Normal Form), October 17, 2007. (pdf)
- John E. Hopcroft and Jeffrey D. Ullman, Introduction to Automata Theory, Languages and Computation, Addison-Wesley Publishing, Reading Massachusetts, 1979. ISBN 0-201-02988-X. (See chapter 4.)
- Daniel Jurafsky; James H. Martin (2008). Speech and Language Processing (2nd ed.). Pearson Prentice Hall. ISBN 978-0-13-187321-6.
- John Martin (2003). Introduction to Languages and the Theory of Computation. McGraw Hill. ISBN 0-07-232200-4. (Pages 237–240 of section 6.6: simplified forms and normal forms.)
- Michael Sipser (1997). Introduction to the Theory of Computation. PWS Publishing. ISBN 0-534-94728-X. (Pages 98–101 of section 2.1: context-free grammars. Page 156.)
- Sipser, Michael. Introduction to the Theory of Computation, 2nd edition.