Informal language theory, acontext-free grammar,G, is said to be inChomsky normal form (first described byNoam Chomsky)[1] if all of itsproduction rules are of the form:[2][3]
whereA,B, andC arenonterminal symbols, the lettera is aterminal symbol (a symbol that represents a constant value),S is the start symbol, and ε denotes theempty string. Also, neitherB norC may be thestart symbol, and the third production rule can only appear if ε is inL(G), the language produced by the context-free grammarG.[4]: 92–93, 106
Every grammar in Chomsky normal form is context-free, and conversely, every context-free grammar can be transformed into anequivalent one[note 1] which is in Chomsky normal form and has a size no larger than the square of the original grammar's size.
To convert a grammar to Chomsky normal form, a sequence of simple transformations is applied in a certain order; this is described in most textbooks onautomata theory.[4]: 87–94 [5][6][7]The presentation here follows Hopcroft, Ullman (1979), but is adapted to use the transformation names from Lange, Leiß (2009).[8][note 2] Each of the following transformations establishes one of the properties required for Chomsky normal form.
Introduce a new start symbolS0, and a new rule
whereS is the previous start symbol.This does not change the grammar's produced language, andS0 will not occur on any rule's right-hand side.
To eliminate each rule
with a terminal symbola being not the only symbol on the right-hand side, introduce, for every such terminal, a new nonterminal symbolNa, and a new rule
Change every rule
to
If several terminal symbols occur on the right-hand side, simultaneously replace each of them by its associated nonterminal symbol.This does not change the grammar's produced language.[4]: 92
Replace each rule
with more than 2 nonterminalsX1,...,Xn by rules
whereAi are new nonterminal symbols.Again, this does not change the grammar's produced language.[4]: 93
An ε-rule is a rule of the form
whereA is notS0, the grammar's start symbol.
To eliminate all rules of this form, first determine the set of all nonterminals that derive ε.Hopcroft and Ullman (1979) call such nonterminalsnullable, and compute them as follows:
Obtain an intermediate grammar by replacing each rule
by all versions with some nullableXi omitted.By deleting in this grammar each ε-rule, unless its left-hand side is the start symbol, the transformed grammar is obtained.[4]: 90
For example, in the following grammar, with start symbolS0,
the nonterminalA, and hence alsoB, is nullable, while neitherC norS0 is.Hence the following intermediate grammar is obtained:[note 3]
In this grammar, all ε-rules have been "inlined at the call site".[note 4]In the next step, they can hence be deleted, yielding the grammar:
This grammar produces the same language as the original example grammar, viz. {ab,aba,abaa,abab,abac,abb,abc,b,ba,baa,bab,bac,bb,bc,c}, but has no ε-rules.
A unit rule is a rule of the form
whereA,B are nonterminal symbols.To remove it, for each rule
whereX1 ...Xn is a string of nonterminals and terminals, add rule
unless this is a unit rule which has already been (or is being) removed. The skipping of nonterminal symbolB in the resulting grammar is possible due toB being a member of the unit closure of nonterminal symbolA.[9]
| TransformationXalways preserves ( resp.may destroy ( | |||||
Y X | START | TERM | BIN | DEL | UNIT |
|---|---|---|---|---|---|
| START | |||||
| TERM | |||||
| BIN | |||||
| DEL | |||||
| UNIT | ( | ||||
| *UNIT preserves the result ofDEL ifSTART had been called before. | |||||
When choosing the order in which the above transformations are to be applied, it has to be considered that some transformations may destroy the result achieved by other ones. For example,START will re-introduce a unit rule if it is applied afterUNIT. The table shows which orderings are admitted.
Moreover, the worst-case bloat in grammar size[note 5] depends on the transformation order. Using |G| to denote the size of the original grammarG, the size blow-up in the worst case may range from |G|2 to 22 |G|, depending on the transformation algorithm used.[8]: 7 The blow-up in grammar size depends on the order betweenDEL andBIN. It may be exponential whenDEL is done first, but is linear otherwise.UNIT can incur a quadratic blow-up in the size of the grammar.[8]: 5 The orderingsSTART,TERM,BIN,DEL,UNIT andSTART,BIN,DEL,UNIT,TERM lead to the least (i.e. quadratic) blow-up.

The following grammar, with start symbolExpr, describes a simplified version of the set of all syntactical valid arithmetic expressions in programming languages likeC orAlgol60. Bothnumber andvariable are considered terminal symbols here for simplicity, since in acompiler front end their internal structure is usually not considered by theparser. The terminal symbol "^" denotedexponentiation in Algol60.
| Expr | →Term | |ExprAddOpTerm | |AddOpTerm |
| Term | →Factor | |TermMulOpFactor | |
| Factor | →Primary | |Factor ^Primary | |
| Primary | →number | |variable | | (Expr ) |
| AddOp | → + | | − | |
| MulOp | → * | | / |
In step "START" of theabove conversion algorithm, just a ruleS0→Expr is added to the grammar.After step "TERM", the grammar looks like this:
| S0 | →Expr | ||
| Expr | →Term | |ExprAddOpTerm | |AddOpTerm |
| Term | →Factor | |TermMulOpFactor | |
| Factor | →Primary | |FactorPowOpPrimary | |
| Primary | →number | |variable | |OpenExprClose |
| AddOp | → + | | − | |
| MulOp | → * | | / | |
| PowOp | → ^ | ||
| Open | → ( | ||
| Close | → ) |
After step "BIN", the following grammar is obtained:
| S0 | →Expr | ||
| Expr | →Term | |ExprAddOp_Term | |AddOpTerm |
| Term | →Factor | |TermMulOp_Factor | |
| Factor | →Primary | |FactorPowOp_Primary | |
| Primary | →number | |variable | |OpenExpr_Close |
| AddOp | → + | | − | |
| MulOp | → * | | / | |
| PowOp | → ^ | ||
| Open | → ( | ||
| Close | → ) | ||
| AddOp_Term | →AddOpTerm | ||
| MulOp_Factor | →MulOpFactor | ||
| PowOp_Primary | →PowOpPrimary | ||
| Expr_Close | →ExprClose | ||
Since there are no ε-rules, step "DEL" does not change the grammar.After step "UNIT", the following grammar is obtained, which is in Chomsky normal form:
| S0 | →number | |variable | |OpenExpr_Close | |FactorPowOp_Primary | |TermMulOp_Factor | |ExprAddOp_Term | |AddOpTerm |
| Expr | →number | |variable | |OpenExpr_Close | |FactorPowOp_Primary | |TermMulOp_Factor | |ExprAddOp_Term | |AddOpTerm |
| Term | →number | |variable | |OpenExpr_Close | |FactorPowOp_Primary | |TermMulOp_Factor | ||
| Factor | →number | |variable | |OpenExpr_Close | |FactorPowOp_Primary | |||
| Primary | →number | |variable | |OpenExpr_Close | ||||
| AddOp | → + | | − | |||||
| MulOp | → * | | / | |||||
| PowOp | → ^ | ||||||
| Open | → ( | ||||||
| Close | → ) | ||||||
| AddOp_Term | →AddOpTerm | ||||||
| MulOp_Factor | →MulOpFactor | ||||||
| PowOp_Primary | →PowOpPrimary | ||||||
| Expr_Close | →ExprClose | ||||||
TheNa introduced in step "TERM" arePowOp,Open, andClose.TheAi introduced in step "BIN" areAddOp_Term,MulOp_Factor,PowOp_Primary, andExpr_Close.
Another way[4]: 92 [10] to define the Chomsky normal form is:
Aformal grammar is inChomsky reduced form if all of its production rules are of the form:
where, and are nonterminal symbols, and is aterminal symbol. When using this definition, or may be the start symbol. Only those context-free grammars which do not generate theempty string can be transformed into Chomsky reduced form.
In a letter where he proposed a termBackus–Naur form (BNF),Donald E. Knuth implied a BNF "syntax in which all definitions have such a form may be said to be in 'Floyd Normal Form'",
where, and are nonterminal symbols, and is a terminal symbol,becauseRobert W. Floyd found any BNF syntax can be converted to the above one in 1961.[11] 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."[12] While Floyd's note cites Chomsky's original 1959 article, Knuth's letter does not.
Besides its theoretical significance, CNF conversion is used in some algorithms as a preprocessing step, e.g., theCYK algorithm, abottom-up parsing for context-free grammars, and its variant probabilistic CKY.[13]
{{cite book}}: CS1 maint: numeric names: authors list (link)(pages 171-183 of section 7.1: Chomsky Normal Form)