Simplification, CNF and GNF NOTES-05.11.2020
Simplification, CNF and GNF NOTES-05.11.2020
CFG Simplification
In a CFG, it may happen that all the production rules and symbols are not needed for the derivation
of strings. Besides, there may be some null productions and unit productions. Elimination of these
productions and symbols is called simplification of CFGs. Simplification essentially comprises of
the following steps −
Reduction of CFG
Removal of Unit Productions
Removal of Null Productions
Reduction of CFG
Problem
Solution
https://www.tutorialspoint.com/automata_theory/cfg_simplification.htm 1/4
10/29/2020 CFG Simplification - Tutorialspoint
Phase 1 −
T = { a, c, e }
W1 = { A, C, E } from rules A → a, C → c and E → aA
W2 = { A, C, E } U { S } from rule S → AC
W3 = { A, C, E, S } U ∅
Since W2 = W3, we can derive G’ as −
G’ = { { A, C, E, S }, { a, c, e }, P, {S}}
where P: S → AC, A → a, C → c , E → aA | e
Phase 2 −
Y1 = { S }
Y2 = { S, A, C } from rule S → AC
Y3 = { S, A, C, a, c } from rules A → a and C → c
Y4 = { S, A, C, a, c }
where P: S → AC, A → a, C → c
Any production rule in the form A → B where A, B ∈ Non-terminal is called unit production..
Removal Procedure −
Problem
Remove unit production from the following −
S → XY, X → a, Y → Z | b, Z → M, M → N, N → a
Solution −
https://www.tutorialspoint.com/automata_theory/cfg_simplification.htm 2/4
10/29/2020 CFG Simplification - Tutorialspoint
S → XY, X → a, Y → Z | b, Z → a, M → a, N → a
Now we will remove Y → Z.
S → XY, X → a, Y → a | b, Z → a, M → a, N → a
Now Z, M, and N are unreachable, hence we can remove those.
The final CFG is unit production free −
S → XY, X → a, Y → a | b
Removal Procedure
Step 2 − For each production A → a, construct all productions A → x where x is obtained from ‘a’
by removing one or multiple non-terminals from Step 1.
Step 3 − Combine the original productions with the result of step 2 and remove ε - productions.
Problem
Solution −
There are two nullable variables − A and B
https://www.tutorialspoint.com/automata_theory/cfg_simplification.htm 3/4
10/29/2020 CFG Simplification - Tutorialspoint
S→ASA | aB | b | a, A ε B| b | &epsilon, B → b
S→ASA | aB | b | a | SA | AS | S, A → B| b, B → b
This is the final production set without null transition.
https://www.tutorialspoint.com/automata_theory/cfg_simplification.htm 4/4
10/29/2020 Chomsky Normal Form - Tutorialspoint
A CFG is in Chomsky Normal Form if the Productions are in the following forms −
A→a
A → BC
S→ε
where A, B, and C are non-terminals and a is terminal.
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. (Using the Null production removal algorithm discussed earlier)
Step 3 − Remove unit productions. (Using the Unit production removal algorithm discussed earlier)
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
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 −
https://www.tutorialspoint.com/automata_theory/chomsky_normal_form.htm 1/2
10/29/2020 Chomsky Normal Form - Tutorialspoint
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
S→ AX | YB | a | AS | SA
A → b A → b |AX | YB | a | AS | SA
B→b
X → SA
Y→a
https://www.tutorialspoint.com/automata_theory/chomsky_normal_form.htm 2/2
10/29/2020 Greibach Normal Form - Tutorialspoint
A CFG is in Greibach Normal Form if the Productions are in the following forms −
A→b
A → bD1…Dn
S→ε
where A, D1,....,Dn are non-terminals and b is a terminal.
Problem
Solution
Here, S does not appear on the right side of any production and there are no unit or null
productions in the production rule set. So, we can skip Step 1 to Step 3.
Step 4
mX | m
https://www.tutorialspoint.com/automata_theory/greibach_normal_form.htm 1/2
10/29/2020 Greibach Normal Form - Tutorialspoint
we obtain
S → mXY | mY | mXo | mo | p.
And after replacing
X in Y → Xn | o
with the right side of
X → mX | m
we obtain
Y → mXn | mn | o.
Two new productions O → o and P → p are added to the production set and then we came to the
final GNF as the following −
S → mXY | mY | mXC | mC | p
X → mX | m
Y → mXD | mD | o
O→o
P→p
https://www.tutorialspoint.com/automata_theory/greibach_normal_form.htm 2/2