You are on page 1of 5

CS340: Theory of Computation

Sem I 2015-16

Lecture Notes 9: Chomsky Normal Form


Raghunath Tewari

IIT Kanpur

Normal forms are CFGs whose substitution rules have a special form. Usually normal forms are
general enough in the sense that any CFL will have a CFG in that normal form. Normal forms
have a nice combinatorial structure which are useful in proving properties about CFLs.

Chomsky Normal Form

A CFG is said to be in Chomsky Normal Form (in short, CNF) if the following are true.
1. Every rule is of the form
- A BC, or
- A a,
where A, B, C are variables and a is a terminal.
2. The start variable is not present in the right hand side of any rule.
3. The rule S  may be present (depending on whether the language has  or not).

1.1

Converting a CFG to a grammar in Chomsky Normal Form

Let G = (V, , P, S) be a CFG. Below we give an algorithm to convert G into a CFG in Chomsky
Normal Form.
1. Removing S from RHS of rules
If S appears on the RHS of some rule, add a new start variable S0 and the rule S0 S.
2. Removing -rules
Pick an -rule A  and remove it (where A is not the start variable).
- Now for every occurrence of A on the RHS of some of all rules, add a rule deleting that
occurrence of A. If A occurs multiple times on the RHS of a rule, then multiple rules
might be added.
- If we have the rule R A, then add R , unless the rule R  has been
removed previously.
- Repeat until no more -rules remain, except possibly involving the start variable.
Example: Suppose a grammar had the following rules:
A 
B uAv
C u1 Av1 Aw1

Then the grammar formed by removing the rule A  will have the corresponding set of
rules
B
C
B
C
C
C

uAv
u1 Av1 Aw1
uv
u1 v1 Aw1
u1 Av1 w1
u1 v1 w1

(rule
(rule
(rule
(rule

added)
added with first occurrence of A removed)
added with second occurrence of A removed)
added with both occurrence of A removed)

3. Removing unit rules


Remove a rule A B and for all rules of the form B u add the rule A u, unless
A u is a unit rule that has already been removed. Repeat until no more unit rules
remain.
Example: Suppose a grammar had the following rules:
A B
B u
Then the grammar formed by removing the rule A B will have the corresponding set of
rules
B u
A u
(rule added)
4. Shortening the RHS
For every rule of the form A u1 u2 . . . uk for
with
A
A1
A2

Ak2

k 3, where ui V T , replace the rule


u1 A1
u2 A2
u3 A3

uk1 uk

Here Ai s are the new variables added to the grammar.


5. Replacing certain terminals
If there is a rule of the form A uv where at least one of either u or v is a terminal
symbol (say u), then replace the rule A uv with
A U v
U u
where U is a new variable added to the grammar. Repeat until no such rules remain.

1.2

An Example CFG to CNF

Consider the following CFG where S is the start variable:


S ASB
A aASA | a | 
B SbS | A | bb
We will convert the above grammar into a grammar in CNF. The rules/variables that get added
at each step are shown in bold font.
1. Adding a new start variable S0 , since S appears on RHS of some rules.
S0 S
S ASB
A aASA | a | 
B SbS | A | bb
2. Eliminating A .
S0 S
S ASB | SB
A aASA | a | aSA | aAS | aS
B SbS | A | bb | 
3. Eliminating B .
S0 S
S ASB | SB | AS | S
A aASA | a | aSA | aAS | aS
B SbS | A | bb
4. Eliminating the unit rule S S.
S0 S
S ASB | SB | AS
A aASA | a | aSA | aAS | aS
B SbS | A | bb
5. Eliminating the unit rule B A.
S0 S
S ASB | SB | AS
A aASA | a | aSA | aAS | aS
B SbS | bb | aASA | a | aSA | aAS | aS

6. Eliminating the unit rule S0 S.


S0 ASB | SB | AS
S ASB | SB | AS
A aASA | a | aSA | aAS | aS
B SbS | bb | aASA | a | aSA | aAS | aS
7. Adding variable U1 and rule U1 AS.
S0 U1 B | SB | AS
S U1 B | SB | AS
A aU1 A | a | aSA | aU1 | aS
B SbS | bb | aU1 A | a | aSA | aU1 | aS
U1 AS
8. Adding variable U2 and rule U2 aU1 .
S0 U1 B | SB | AS
S U1 B | SB | AS
A U2 A | a | aSA | aU1 | aS
B SbS | bb | U2 A | a | aSA | aU1 | aS
U1 AS
U2 aU1
9. Adding variable U3 and rule U3 aS.
S0 U1 B | SB | AS
S U1 B | SB | AS
A U2 A | a | U3 A | aU1 | aS
B SbS | bb | U2 A | a | U3 A | aU1 | aS
U1 AS
U2 aU1
U3 aS
10. Adding variable U4 and rule U4 Sb.
S0 U1 B | SB | AS
S U1 B | SB | AS
A U2 A | a | U3 A | aU1 | aS
B U4 S | bb | U2 A | a | U3 A | aU1 | aS
U1 AS
U2 aU1
U3 aS
U4 Sb

11. Adding variables V1 , V2 and rules V1 a, V2 b.


S0 U1 B | SB | AS
S U1 B | SB | AS
A U2 A | a | U3 A | V1 U1 | V1 S
B U4 S | V2 V2 | U2 A | a | U3 A | V1 U1 | V1 S
U1 AS
U2 V1 U1
U3 V1 S
U4 SV2
V1 a
V2 b
Chomsky Normal Form of the given grammar is
S0 U1 B | SB | AS
S U1 B | SB | AS
A U2 A | U3 A | V1 U1 | V1 S | a
B U4 S | V2 V2 | U2 A | U3 A | V1 U1 | V1 S | a
U1 AS
U2 V1 U1
U3 V1 S
U4 SV2
V1 a
V2 b
Exercise 1. Problem 2.14 from textbook.

You might also like