To convert a context-free grammar to Chomsky normal form, first add a fresh start symbol. Remove epsilon productions, retaining the exceptional start production only when the empty word is to remain in the language; then remove unit productions and symbols that cannot participate in a terminal derivation. Replace each terminal occurring in a right-hand side of length at least two by a new nonterminal that produces that terminal. Finally split every right-hand side of length greater than two into binary productions using fresh nonterminals.
The formal language is a context-free language. A context-free grammar with start nonterminal symbol is
The production rule for nests one on the left and one on the right on each recursive use, so generates exactly for . Similarly, generates exactly for . The start production rule concatenates these words, yielding
Conversely, every choice of nonnegative integers is achieved by using the recursive production rules exactly times before their epsilon productions. Hence this context-free grammar generates precisely the required formal language, including all cases where either count is zero.
Add a fresh start symbol with .
Find the nullable nonterminals, add the variants obtained by omitting nullable occurrences, and remove all epsilon productions. Omit as well because the requested language excludes .
Remove every unit production by copying the non-unit productions reachable from to .
Remove nonterminals that are unreachable from or cannot derive a terminal word.
In every right-hand side of length at least two, replace a terminal by a fresh nonterminal with .
Replace each right-hand side of length greater than two by a chain of binary productions using fresh nonterminals.
Each remaining production is of the required binary or terminal form, and these transformations preserve every nonempty generated word. Thus .
A context-free grammar is in Chomsky normal form when every production is of one of the forms
where are nonterminal symbols and is a terminal symbol. Under this strict convention there is no epsilon production, so such a grammar cannot generate the empty word. Conversion of an arbitrary grammar therefore gives
If , the two languages are equal. An alternative extended convention permits the exceptional start production ; under that convention conversion preserves the whole language.