0% found this document useful (0 votes)
14 views8 pages

Simplification, CNF and GNF NOTES-05.11.2020

The document discusses the simplification of Context-Free Grammars (CFGs), detailing steps for reducing CFGs, removing unit productions, and eliminating null productions. It provides algorithms and examples for each step, including finding equivalent grammars and transforming CFGs into Chomsky Normal Form and Greibach Normal Form. The document serves as a tutorial for understanding CFG simplification techniques in automata theory.

Uploaded by

Shana Apv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

Simplification, CNF and GNF NOTES-05.11.2020

The document discusses the simplification of Context-Free Grammars (CFGs), detailing steps for reducing CFGs, removing unit productions, and eliminating null productions. It provides algorithms and examples for each step, including finding equivalent grammars and transforming CFGs into Chomsky Normal Form and Greibach Normal Form. The document serves as a tutorial for understanding CFG simplification techniques in automata theory.

Uploaded by

Shana Apv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

10/29/2020 CFG Simplification - Tutorialspoint

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

CFGs are reduced in two phases −


Phase 1 − Derivation of an equivalent grammar, G’, from the CFG, G, such that each variable
derives some terminal string.
Derivation Procedure −
Step 1 − Include all symbols, W1, that derive some terminal and initialize i=1.
Step 2 − Include all symbols, Wi+1, that derive Wi.
Step 3 − Increment i and repeat Step 2, until Wi+1 = Wi.
Step 4 − Include all production rules that have Wi in it.
Phase 2 − Derivation of an equivalent grammar, G”, from the CFG, G’, such that each symbol
appears in a sentential form.
Derivation Procedure −
Step 1 − Include the start symbol in Y1 and initialize i = 1.
Step 2 − Include all symbols, Yi+1, that can be derived from Yi and include all production
rules that have been applied.
Step 3 − Increment i and repeat Step 2, until Yi+1 = Yi.

Problem

Find a reduced grammar equivalent to the grammar G, having production rules, P: S → AC | B, A


→ a, C → c | BC, E → aA | e

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 }

Since Y3 = Y4, we can derive G” as −


G” = { { A, C, S }, { a, c }, P, {S}}

where P: S → AC, A → a, C → c

Removal of Unit Productions

Any production rule in the form A → B where A, B ∈ Non-terminal is called unit production..

Removal Procedure −

Step 1 − To remove A → B, add production A → x to the grammar rule whenever B → x occurs in


the grammar. [x ∈ Terminal, x can be Null]

Step 2 − Delete A → B from the grammar.


Step 3 − Repeat from step 1 until all unit productions are removed.

Problem
Remove unit production from the following −

S → XY, X → a, Y → Z | b, Z → M, M → N, N → a
Solution −

There are 3 unit productions in the grammar −


Y → Z, Z → M, and M → N
At first, we will remove M → N.

https://www.tutorialspoint.com/automata_theory/cfg_simplification.htm 2/4
10/29/2020 CFG Simplification - Tutorialspoint

As N → a, we add M → a, and M → N is removed.


The production set becomes
S → XY, X → a, Y → Z | b, Z → M, M → a, N → a
Now we will remove Z → M.
As M → a, we add Z→ a, and Z → M is removed.
The production set becomes

S → XY, X → a, Y → Z | b, Z → a, M → a, N → a
Now we will remove Y → Z.

As Z → a, we add Y→ a, and Y → Z is removed.


The production set becomes

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 of Null Productions

In a CFG, a non-terminal symbol ‘A’ is a nullable variable if there is a production A → ε or there is


a derivation that starts at A and finally ends up with
ε: A → .......… → ε

Removal Procedure

Step 1 − Find out nullable non-terminal variables which derive ε.

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

Remove null production from the following −


S → ASA | aB | b, A → B, B → b | ∈

Solution −
There are two nullable variables − A and B

At first, we will remove B → ε.


After removing B → ε, the production set becomes −

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

Now we will remove A → ε.


After removing A → ε, the production set becomes −

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

Chomsky Normal Form

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.

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. (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

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 −
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

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

https://www.tutorialspoint.com/automata_theory/chomsky_normal_form.htm 2/2
10/29/2020 Greibach Normal Form - Tutorialspoint

Greibach Normal Form

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.

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. (Using the Null production removal algorithm discussed earlier)
Step 3 − Remove unit productions. (Using the Unit production removal algorithm discussed earlier)
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.

Problem

Convert the following CFG into CNF


S → XY | Xn | p
X → mX | m
Y → Xn | o

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

Now after replacing


X in S → XY | Xo | p
with

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

You might also like