BENGAL COLLEGE OF
ENGINEERING AND
TECHNOLOGY
UNDER THE GUIDANCE OF
MR . AKHIL PRATAP SINGH
TERM PAPER ON
“CHOMSKY NORMAL FORM(CNF) , GREIBACH
NORMAL FORM(GNF) AND THEIR APPLICATIONS ”
SUBMITTED BY:-
NAYAN KUMAR
(53)
Chomsky normal form(CNF)
A CFG is in Chomsky Normal Form if the Productions are in the
following form−
A→a
A → BC
S→ε
where A, B, and C are non-terminals and a is terminal.
Algorithm to Convert into Chomsky Normal Form
Step 1 − If the start symbol S occurs on some right side, create a new
start symbol S’ and a new production S’→ S.
Step 2 − Remove Null productions.
Step 3 − Remove unit productions.
Step 4 − Replace each production A → B1…Bn where n > 2 with A
→ B1C where C → B2 …Bn. Repeat this step for all productions
having two or more symbols in the right side.
Step 5 − If the right side of any production is in the form A → aB
where a is a terminal and A, B are non-terminal, then the production
is replaced by A → XB and X → a. Repeat this step for every
production which is in the form A → aB.
Problem
Convert the following CFG into CNF
S → ASA | aB, A → B | S, B → b | ε
Solution
(1) Since S appears in R.H.S, we add a new state S0 and S0→S is
added to the production set and it becomes −
S0→S, S→ ASA | aB, A → B | S, B → b | ∈
(2) Now we will remove the null productions −
B → ∈ and A → ∈
After removing B → ε, the production set becomes −
S0→S, S→ ASA | aB | a, A → B | S | ∈, B → b
After removing A → ∈, the production set becomes −
S0→S, S→ ASA | aB | a | AS | SA | S, A → B | S, B → b
(3) Now we will remove the unit productions.
After removing S → S, the production set becomes −
S0→S, S→ ASA | aB | a | AS | SA, A → B | S, B → b
After removing S0→ S, the production set becomes −
S0→ ASA | aB | a | AS | SA, S→ ASA | aB | a | AS | SA
A → B | S, B → b
After removing A→ B, the production set becomes −
S0 → ASA | aB | a | AS | SA, S→ ASA | aB | a | AS | SA
A→S|b
B→b
After removing A→ S, the production set becomes −
S0 → ASA | aB | a | AS | SA, S→ ASA | aB | a | AS | SA
A → b |ASA | aB | a | AS | SA, B → b
(4) Now we will find out more than two variables in the R.H.S
Here, S0→ ASA, S → ASA, A→ ASA violates two Non-terminals in
R.H.S.
Hence we will apply step 4 and step 5 to get the following final
production set which is in CNF −
S0→ AX | aB | a | AS | SA
S→ AX | aB | a | AS | SA
A → b |AX | aB | a | AS | SA
B→b
X → SA
(5) We have to change the productions S0→ aB, S→ aB, A→ aB
And the final production set becomes −
S0→ AX | YB | a | AS | SA
S→ AX | YB | a | AS | SA
A → b A → b |AX | YB | a | AS | SA
B→b
X → SA
Y →a
Applications:-
Simplicity of proofs.
Enables parsing.
GREIBACH NORMAL
FORM(GNF)
A CFG is in Greibach Normal Form if the Productions are in the
following forms −
A→b
A → bD1…Dn
where A, D1,....,Dn are non-terminals and b is a terminal.
Algorithm to Convert a CFG into Greibach
Normal Form
Step 1 − If the start symbol S occurs on some right side, create a new
start symbol S’ and a new production S’ → S.
Step 2 − Remove Null productions.
Step 3 − Remove unit productions.
Step 4 − Remove all direct and indirect left-recursion.
Step 5 − Do proper substitutions of productions to convert it into the
proper form of GNF.
Example:
S → XB | AA
A → a | SA
B→b
X→a
Solution:
The given grammar G is already in CNF and there is no left recursion,
The production rule A → SA is not in GNF, so we substitute S → XB
| AA in the production rule A → SA as:
S → XB | AA
A → a | XBA | AAA
B→b
X→a
The production rule S → XB and B → XBA is not in GNF, so we
substitute X → a in the production rule S → XB and B → XBA as:
S → aB | AA
A → a | aBA | AAA
B→b
X→a
Now we will remove left recursion (A → AAA), we get:
S → aB | AA
A → aC | aBAC
C → AAC | ε
B→b
X→a
Now we will remove null production C → ε, we get:
S → aB | AA
A → aC | aBAC | a | aBA
C → AAC | AA
B→b
X→a
The production rule S → AA is not in GNF, so we substitute A → aC
| aBAC | a | aBA in production rule S → AA as:
S → aB | aCA | aBACA | aA | aBAA
A → aC | aBAC | a | aBA
C → AAC
C → aCA | aBACA | aA | aBAA
B→b
X→a
The production rule C → AAC is not in GNF, so we substitute A →
aC | aBAC | a | aBA in production rule C → AAC as:
S → aB | aCA | aBACA | aA | aBAA
A → aC | aBAC | a | aBA
C → aCAC | aBACAC | aAC | aBAAC
C → aCA | aBACA | aA | aBAA
B→b
X→a
Hence, this is the GNF form for the grammar G.
Applications:-
GNF is used in “Shamir’s Theorem” which tells us that every
CFL is essentially a language whose member are strings of
balanced parenthesis.
GNF is used to prove that every CFG can be recognized by a
PDA(Push Down Automata) that works in real time (without
null transition).