CFG Simplification
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.
Phase 2 − Derivation of an equivalent grammar, G”, from the CFG, G’, such that each
symbol appears in a sentential form.
Derivation Procedure −
Step 2 − Include all symbols, Yi+1, that can be derived from Yi and include all
production rules that have been applied.
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
Phase 1 −
T = { a, c, e }
W2 = { A, C, E } ∪ { S } from rule S → AC
W3 = { A, C, E, S } ∪ ∅
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 }
Y 2 = { S, A, C } from rule S → AC
Y 4 = { S, A, C, a, c }
G” = { { A, C, S }, { a, c }, P, {S}}
where P: S → AC, A → a, C → c
Removal Procedure −
Step 3 Repeat from step 1 until all unit productions are removed.
Problem
S → XY, X → a, Y → Z | b, Z → M, M → N, N → a
Solution −
Y → Z, Z → M, and M → N
S → XY, X → a, Y → Z | b, Z → M, M → a, N → a
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
S → XY, X → a, Y → a | b
Removal Procedure
Step 3 Combine the original productions with the result of step 2 and remove ∈-
productions.
Problem
S → ASA | aB | b, A → B, B → b | ∈
Solution −
S → ASA | aB | b | a, A → B| b | ∈, B → b
S → ASA | aB | b | a | SA | AS | S, A → B| b, B → b