Template:Short description Template:Distinguish In formal language theory, a context-free grammar, G, is said to be in Chomsky normal form (first described by Noam Chomsky)<ref>Template:Cite journal Here: Sect.6, p.152ff.</ref> if all of its production rules are of the form:<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref>Template:Cite book</ref>

ABC,   or
Aa,   or
S → ε,

where A, B, and C are nonterminal symbols, the letter a is a terminal symbol (a symbol that represents a constant value), S is the start symbol, and ε denotes the empty string. Also, neither B nor C may be the start symbol, and the third production rule can only appear if ε is in L(G), the language produced by the context-free grammar G.<ref name="Hopcroft.Ullman.1979">Template:Cite book</ref>Template:Rp

Every grammar in Chomsky normal form is context-free, and conversely, every context-free grammar can be transformed into an equivalent one<ref group=note>that is, one that produces the same language</ref> which is in Chomsky normal form and has a size no larger than the square of the original grammar's size.

Converting a grammar to Chomsky normal formEdit

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 on automata theory.<ref name="Hopcroft.Ullman.1979"/>Template:Rp<ref>Template:Cite book Section 7.1.5, p.272</ref><ref>Template:Cite book</ref><ref>Template:Cite book Section 6.2 "Die Chomsky-Normalform für kontextfreie Grammatiken", p. 149–152</ref> The presentation here follows Hopcroft, Ullman (1979), but is adapted to use the transformation names from Lange, Leiß (2009).<ref name="Lange.Leis.2009">Template:Cite journal</ref><ref group=note>For example, Hopcroft, Ullman (1979) merged TERM and BIN into a single transformation.</ref> Each of the following transformations establishes one of the properties required for Chomsky normal form.

START: Eliminate the start symbol from right-hand sidesEdit

Introduce a new start symbol S0, and a new rule

S0S,

where S is the previous start symbol. This does not change the grammar's produced language, and S0 will not occur on any rule's right-hand side.

TERM: Eliminate rules with nonsolitary terminalsEdit

To eliminate each rule

AX1 ... a ... Xn

with a terminal symbol a being not the only symbol on the right-hand side, introduce, for every such terminal, a new nonterminal symbol Na, and a new rule

Naa.

Change every rule

AX1 ... a ... Xn

to

AX1 ... Na ... Xn.

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.<ref name="Hopcroft.Ullman.1979"/>Template:Rp

BIN: Eliminate right-hand sides with more than 2 nonterminalsEdit

Replace each rule

AX1 X2 ... Xn

with more than 2 nonterminals X1,...,Xn by rules

AX1 A1,
A1X2 A2,
... ,
An-2Xn-1 Xn,

where Ai are new nonterminal symbols. Again, this does not change the grammar's produced language.<ref name="Hopcroft.Ullman.1979"/>Template:Rp

DEL: Eliminate ε-rulesEdit

An ε-rule is a rule of the form

A → ε,

where A is not S0, 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 nonterminals nullable, and compute them as follows:

  • If a rule A → ε exists, then A is nullable.
  • If a rule AX1 ... Xn exists, and every single Xi is nullable, then A is nullable, too.

Obtain an intermediate grammar by replacing each rule

AX1 ... Xn

by all versions with some nullable Xi omitted. By deleting in this grammar each ε-rule, unless its left-hand side is the start symbol, the transformed grammar is obtained.<ref name="Hopcroft.Ullman.1979"/>Template:Rp

For example, in the following grammar, with start symbol S0,

S0AbB | C
BAA | AC
Cb | c
Aa | ε

the nonterminal A, and hence also B, is nullable, while neither C nor S0 is. Hence the following intermediate grammar is obtained:<ref group=note>indicating a kept and omitted nonterminal N by Template:Ifsubst style="color:#006000">N and Template:Ifsubst style="color:#ffc0c0">N, respectively</ref>

S0Template:Ifsubst style="color:#006000">AbTemplate:Ifsubst style="color:#006000">B | Template:Ifsubst style="color:#006000">AbTemplate:Ifsubst style="color:#ffc0c0">B | Template:Ifsubst style="color:#ffc0c0">AbTemplate:Ifsubst style="color:#006000">B | Template:Ifsubst style="color:#ffc0c0">AbTemplate:Ifsubst style="color:#ffc0c0">B   |   C
BTemplate:Ifsubst style="color:#006000">AA | Template:Ifsubst style="color:#ffc0c0">ATemplate:Ifsubst style="color:#006000">A | Template:Ifsubst style="color:#006000">ATemplate:Ifsubst style="color:#ffc0c0">A | Template:Ifsubst style="color:#ffc0c0">AεTemplate:Ifsubst style="color:#ffc0c0">A   |   Template:Ifsubst style="color:#006000">AC | Template:Ifsubst style="color:#ffc0c0">AC
Cb | c
Aa | ε

In this grammar, all ε-rules have been "inlined at the call site".<ref group=note>If the grammar had a rule S0 → ε, it could not be "inlined", since it had no "call sites". Therefore it could not be deleted in the next step.</ref> In the next step, they can hence be deleted, yielding the grammar:

S0AbB | Ab | bB | b   |   C
BAA | A   |   AC | C
Cb | c
Aa

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.

UNIT: Eliminate unit rulesEdit

A unit rule is a rule of the form

AB,

where A, B are nonterminal symbols. To remove it, for each rule

BX1 ... Xn,

where X1 ... Xn is a string of nonterminals and terminals, add rule

AX1 ... Xn

unless this is a unit rule which has already been (or is being) removed. The skipping of nonterminal symbol B in the resulting grammar is possible due to B being a member of the unit closure of nonterminal symbol A.<ref>Template:Cite book</ref>

Order of transformationsEdit

Mutual preservation
of transformation results
Transformation X Template:Ifsubst style="color:#004000">always preserves (Template:Aye)
resp. Template:Ifsubst style="color:#400000">may destroy (Template:Nay) the result of Y:
Template:Diagonal split header START TERM BIN DEL UNIT
START Template:Ya Template:Ya Template:Na Template:Na
TERM Template:Ya Template:Na Template:Ya Template:Ya
BIN Template:Ya Template:Ya Template:Ya Template:Ya
DEL Template:Ya Template:Ya Template:Ya Template:Na
UNIT Template:Ya Template:Ya Template:Ya Template:Ya
*UNIT preserves the result of DEL
  if START 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 after UNIT. The table shows which orderings are admitted.

Moreover, the worst-case bloat in grammar size<ref group=note>i.e. written length, measured in symbols</ref> depends on the transformation order. Using |G| to denote the size of the original grammar G, the size blow-up in the worst case may range from |G|2 to 22 |G|, depending on the transformation algorithm used.<ref name="Lange.Leis.2009"/>Template:Rp The blow-up in grammar size depends on the order between DEL and BIN. It may be exponential when DEL is done first, but is linear otherwise. UNIT can incur a quadratic blow-up in the size of the grammar.<ref name="Lange.Leis.2009"/>Template:Rp The orderings START,TERM,BIN,DEL,UNIT and START,BIN,DEL,UNIT,TERM lead to the least (i.e. quadratic) blow-up.

ExampleEdit

File:Syntax tree of arithmetic expression wrt Chomsky normal form grammar.gif
Abstract syntax tree of the arithmetic expression "a^2+4*b" wrt. the example grammar (top) and its Chomsky normal form (bottom)

The following grammar, with start symbol Expr, describes a simplified version of the set of all syntactical valid arithmetic expressions in programming languages 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 "START" of the above conversion algorithm, just a rule S0Expr is added to the grammar. After step "TERM", the grammar looks like this:

S0 Expr
Expr Term | Expr AddOp Term | AddOp Term
Term Factor | Term MulOp Factor
Factor Primary | Factor PowOp Primary
Primary number | variable | Open Expr Close
AddOp → + | −
MulOp → * | /
PowOp → ^
Open → (
Close → )

After step "BIN", the following grammar is obtained:

S0 Expr
Expr Term | Expr AddOp_Term | AddOp Term
Term Factor | Term MulOp_Factor
Factor Primary | Factor PowOp_Primary
Primary number | variable | Open Expr_Close
AddOp → + | −
MulOp → * | /
PowOp → ^
Open → (
Close → )
AddOp_Term AddOp Term
MulOp_Factor MulOp Factor
PowOp_Primary PowOp Primary
Expr_Close Expr Close

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 | 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 → * | /
PowOp → ^
Open → (
Close → )
AddOp_Term AddOp Term
MulOp_Factor MulOp Factor
PowOp_Primary PowOp Primary
Expr_Close Expr Close

The Na introduced in step "TERM" are PowOp, Open, and Close. The Ai introduced in step "BIN" are AddOp_Term, MulOp_Factor, PowOp_Primary, and Expr_Close.

Alternative definitionEdit

Chomsky reduced formEdit

Another way<ref name="Hopcroft.Ullman.1979"/>Template:Rp<ref>Hopcroft et al. (2006)Template:Page needed</ref> to define the Chomsky normal form is:

A formal grammar is in Chomsky reduced form if all of its production rules are of the form:

<math>A \rightarrow\, BC</math> or
<math>A \rightarrow\, a</math>,

where <math>A</math>, <math>B</math> and <math>C</math> are nonterminal symbols, and <math>a</math> is a terminal symbol. When using this definition, <math>B</math> or <math>C</math> 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 formEdit

In a letter where he proposed a term Backus–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'",

<math>\langle A \rangle ::= \, \langle B \rangle \mid \langle C \rangle</math> or
<math>\langle A \rangle ::= \, \langle B \rangle \langle C \rangle</math> or
<math>\langle A \rangle ::=\, a</math>,

where <math>\langle A \rangle</math>, <math>\langle B \rangle</math> and <math>\langle C \rangle</math> are nonterminal symbols, and <math>a</math> is a terminal symbol, because Robert W. Floyd found any BNF syntax can be converted to the above one in 1961.<ref>Template:Cite journal Here: p.354</ref> 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."<ref name="knuth1964">Template:Cite journal</ref> While Floyd's note cites Chomsky's original 1959 article, Knuth's letter does not.

ApplicationEdit

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.<ref>Template:Cite book</ref>

See alsoEdit

NotesEdit

Template:Reflist

ReferencesEdit

<references />

Further readingEdit

  • Cole, Richard. Converting CFGs to CNF (Chomsky Normal Form), October 17, 2007. (pdf) — uses the order TERM, BIN, START, DEL, UNIT.
  • Template:Cite book (Pages 237–240 of section 6.6: simplified forms and normal forms.)
  • Template:Cite book (Pages 98–101 of section 2.1: context-free grammars. Page 156.)
  • Template:Cite book (pages 171-183 of section 7.1: Chomsky Normal Form)
  • Sipser, Michael. Introduction to the Theory of Computation, 2nd edition.
  • Template:Cite book

Template:Noam Chomsky