CFG Simplification - Tutorialspoint
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.
Phase 2 − Derivation of an equivalent grammar, G”, from the CFG, G’, such that each symbol appears in a sentential form.
Derivation Procedure −
https://www.tutorialspoint.com/automata_theory/cfg_simplification.htm 1/5
6/12/2021 CFG Simplification - Tutorialspoint
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 }
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 ∅
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 }
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]
https://www.tutorialspoint.com/automata_theory/cfg_simplification.htm 3/5
6/12/2021 CFG Simplification - Tutorialspoint
S → XY, X → a, Y → a | b, Z → a, M → a, N → 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 −
https://www.tutorialspoint.com/automata_theory/cfg_simplification.htm 4/5
6/12/2021 CFG Simplification - Tutorialspoint
https://www.tutorialspoint.com/automata_theory/cfg_simplification.htm 5/5