CNF Conversion Flowchart
CNF Conversion Flowchart
Removal of all
null-productions
Conversion to
CNF
Removal of all
unit-productions
CFG → CNF
Removal of non- ↓
generating symbols
Conversion to
Removal of non- GNF
reachable symbols
3
Example
• Remove the useless symbols from the
given CFG:
S → AC C is found guilty.
S → BA
C → CB
C → AC
A→a
B → aC
B→b
4
Example
• Remove the useless symbols from the
given CFG:
S → BA No symbol is non-
A→a reachable
B→b
This is the required
simplified CFG.
5
Task 1
• Remove the useless symbols from the
given CFG:
S → aAa A generates babab.
A → bBB B generates ab.
B → ab C generates aab.
C → aB
6
Task 1
• Remove the useless symbols from the
given CFG:
S → aAa None is found
A → bBB guilty.
B → ab
C → aB
7
Task 1
• Remove the useless symbols from the
given CFG:
S → aAa A is reachable.
A → bBB B is reachable.
B → ab C is not reachable.
C → aB
8
Task 1
• Remove the useless symbols from the
given CFG:
S → aAa C is found guilty.
A → bBB
B → ab
C → aB
9
Task 1
• Remove the useless symbols from the
given CFG:
S → aAa
A → bBB
B → ab
10
Task 2
• Remove the useless symbols from the
given CFG:
S → aC A generates ad.
S → SB B always calls B.
A → bSCa C generates ad.
A → ad
B → aSB
B → bBC
C → aBC
C → ad
11
Task 2
• Remove the useless symbols from the
given CFG:
S → aC B is found guilty.
S → SB
A → bSCa
A → ad
B → aSB
B → bBC
C → aBC
C → ad
12
Task 2
• Remove the useless symbols from the
given CFG:
S → aC A is not reachable.
A → bSCa C is reachable.
A → ad
C → ad
13
Task 2
• Remove the useless symbols from the
given CFG:
S → aC A is found guilty.
A → bSCa
A → ad
C → ad
14
Task 2
• Remove the useless symbols from the
given CFG:
S → aC
C → ad
15
Removal of Unit Productions
• A production is called a unit-production if it
is in the following format:
16
Removal of Unit Productions
• Let the CFG be:
S → AB S ⇒ AB
A→E ⇒ EB
B→C ⇒ aB
C→D ⇒ aC
D→b ⇒ aD
E→a ⇒ ab
6 steps
17
Removal of Unit Productions
• Let the CFG be:
S → AB S ⇒ AB
A→E ⇒ EB
Unit
productions B→C ⇒ aB
C→D ⇒ aC
D→b ⇒ aD
E→a ⇒ ab
6 steps
18
Removal of Unit Productions
• After removing the unit-productions, CFG
is:
S → AB S ⇒ AB
A→a ⇒ aB
B→b ⇒ ab
3 steps
19
Removal of Unit Productions
While (unit-production exists)
do
{
select a unit production, say A→B
for(every non-unit production B→xi)
{
add production A→xi to CFG
}
remove A→B from CFG
}
20
Example
• Let the CFG:
S → AB A→E E→a
A→E
B→C
C→D A→a
D→b
E→a
21
Example
• CFG:
S → AB C→D D→b
B→C
C→D
D→b C→b
E→a
A→a
22
Example
• CFG:
S → AB B→C C→b
B→C
D→b
E→a B→b
A→a
C→b
23
Example
• CFG:
24
Example
• CFG:
S → AB
A→a
B→b
25
Example
• Let the CFG:
S → AB C→D D→b
A→a
B→C
C→D C→b
D→b
26
Example
• CFG:
S → AB B→C C→b
A→a
B→C
D→b B→b
C→b
27
Example
• CFG:
28
Example
• CFG:
S → AB
A→a
B→b
29
Chomsky Normal Form (CNF)
• A CFG is said to be in CNF if all the
productions of the grammar are in the
following format:
30
Chomsky Normal Form (CNF)
STEP I: Simplification of CFG
1. Eliminate -Productions.
2. Eliminate Unit-Productions.
3. Eliminate Useless Productions.
STEP II: Substitution on RHS
4. Substitute the terminals on the RHS of
length two or more and add new rules.
STEP III: Restriction of RHS
5. Restrict the number of variables on the
RHS to two.
31
Example
Let the CFG:
S → bA
S → aB
A → bAA
There are no Null productions
A → aS
There are no Unit productions
A→a
Every symbol is generating
B → aBB Every symbol is reachable
B → bS
B→a
32
Example
CFG:
S → bA
S → aB
A → bAA
A → aS
A→a C→b
B → aBB D→a
B → bS
B→a
33
Example
CFG:
S → bA
S → aB
A → bAA
A → aS
A→a C→b
B → aBB D→a
B → bS
B→a
34
Example
CFG:
S → CA
S → DB
A → CAA
A → DS
A→a C→b
B → DBB D→a
B → CS
B→a
35
Example
CFG:
S → CA
S → DB
A → CAA
C→b
A → DS
D→a
A→a
B → DBB
B → CS
B→a
36
Example
CFG:
S → CA
S → DB
A → CAA
C→b
A → DS
D→a
A→a
E → AA
B → DBB
F → BB
B → CS
B→a
37
Example
CFG:
S → CA
S → DB
A → CAA
C→b
A → DS
D→a
A→a
E → AA
B → DBB
F → BB
B → CS
B→a
38
Example
CFG:
S → CA
S → DB
A → CE
C→b
A → DS
D→a
A→a
E → AA
B → DF
F → BB
B → CS
B→a This is the required CNF
39
Example
CFG:
S → CA|DB
A → CE|DS
A→a
B → DF|CS
B→a
C→b
D→a
E → AA This is the required CNF
F → BB 40
Example
Let the CFG:
S → ABa
A → aab
B → Ac
There are no Null productions
There are no Unit productions
Every symbol is generating
Every symbol is reachable
41
Example
CFG:
S → ABa
A → aab
B → Ac
C→a
D→b
E →c
42
Example
CFG:
S → ABa
A → aab
B → Ac
C→a
D→b
E →c
43
Example
CFG:
S → ABC
A → CCD
B → AE
C→a
D→b
E →c
44
Example
CFG:
S → ABC
A → CCD
B → AE
C→a
D→b
E →c
45
Example
CFG:
S → ABC
A → CCD
B → AE
C→a
D→b
E →c
F → BC
G → CC
46
Example
CFG:
S → ABC
A → CCD
B → AE
C→a
D→b
E →c
F → BC
G → CC
47
Example
CFG:
S → AF
A → GD
B → AE
C→a
D→b
E →c
F → BC
G → CC
This is the required CNF
48
Example
CFG:
S → AF
A → GD
B → AE
C→a
D→b
E →c
F → BC
G → CC
This is the required CNF
49
Example
Let the CFG:
E→E+E
E→E*E
E → id
There are no Null productions
There are no Unit productions
Every symbol is generating
Every symbol is reachable
50
Example
CFG:
E→E+E
E→E*E
E → id
P →+
M→ *
51
Example
CFG:
E→E+E
E→E*E
E → id
P →+
M→*
52
Example
CFG:
E → EPE
E → EME
E → id
P →+
M→*
53
Example
CFG:
E → EPE
E → EME
E → id
P →+
M→*
54
Example
CFG:
E → EPE
E → EME
E → id
P →+
M→*
C1 → EP
C2 → EM
55
Example
CFG:
E → EPE
E → EME
E → id
P →+
M→*
C1 → EP
C2 → EM
56
Example
CFG:
E → C1E
E → C2E
E → id P →+
M→*
C1 → EP
C2 → EM
E → C1E
E → C2E
E → id
P →+
M→*
C1 → EP
C2 → EM
This is the required CNF
58
Practice Question
• Convert the following CFG into CNF.
S → abAB
A → bAB|
B → BAa|
59
Greibach Normal Form (GNF)
• A CFG is said to be in GNF if all the
productions of the grammar are in the
following format:
60
Greibach Normal Form (GNF)
STEP I: Prerequisite Conversion
1. Convert CFG to CNF.
STEP II: Greibach Conversion
2. Rename all the Variables in ascending order
as Ai.
3. Make sure that LHS variable subscript is
always less than the leftmost variable
subscript on RHS.
1. If needed, make proper substitutions.
2. Remove Left Recursion, if any.
4. Make proper substitutions until we get GNF.
61
Assignment
• Convert the following two CFGs into GNF.
S → XY S → AB | BC
X → YS | b A → aB | bA | a
Y → SX | a B → bB | cC | b
C→c
62