Professional Documents
Culture Documents
9 - CFG Simplification
9 - CFG Simplification
9 - CFG Simplification
production is useless, if it involves a useless variable. Note that the concept of 'useless
variable' includes the case that the variable does not lead to a terminal string, and the
case that the variable cannot be reached from the start symbol. The elimination of
useless variables and productions from a grammar (or the selection of those variables
and productions, which are useful) proceeds in two phases:
1. Determine and select those variables, which can lead to a terminal string, and
subsequently determine and select the related productions.
2. Determine and select those variables, which can be reached from the start symbol,
and select related productions.
Example 1:
S → AB | a | A
A→b
Solution :
1. S , A are generating
2. B is not generating (and therefore B is useless)
3. Eliminating B… (i.e., remove all productions that involve B)
S→ a | A
A→b
Example 2:
S → aSb | 𝜆 | A
A → aA
Solution :
nonterminal ( non-generating ) A is useless , so :
S → aSb | 𝜆
Example 3:
S→A
A → aA | ε
B → bA
Solution :
B cannot be reached from S , so nonterminal B is useless
S→A
A → aA | ε
Example 4:
Eliminate useless symbols from the grammar with productions
S → AB | CA
B → BC |AB
A→a
C → AB | b
Solution :
Step 1: Eliminate non-generating symbols B
N = {A, C, S}
P1 = {S → CA , A → a , C → b}
Step 2: Eliminate symbols that are non reachable
Sol. All nonterminal are reachable.
Example 5:
G = ( { S,A,B,C } , {a , b } , S , P )
S → aS | A | C
A→a
B → aa
C → aCb
Solution :
Step 1: S → aS | A
A→a
B → aa
Step 2: S → aS | A
A→a
2. Eliminating ε-productions
Any variable A such that there is a production in P:
A→ε …….. is a nullable variable.
If P contains A → B1B2B3…BN and B1, B2, …BN are nullable variables, then
A is nullable. That mean A * → ε
Example 6:
Eliminating ε-productions for the language represented by the following CFG
grammar :
S → AB
A → aAA | ε
B → bBB | ε
Solution :
Goal: To construct G1, which is the grammar for L- { ε }
All variables are nullable, S, A and B are nullable since:
A → ε and B → ε, S is nullable
Since S → AB and A and B are nullable
G1 can be constructed from G as follows:
B → bBB | ε ==> B → b | bB | bBB
Similarly, A → a | aA | aAA
Similarly, S → A | B | AB
So the simplified grammar :
G1:
S → A | B | AB
A → a | aA | aAA
B → b | bB | bBB
Example 7:
Find out the grammar without ε-productions for the following grammar :
S → aS | AB
A→ε
B→ε
D→b
Solution :
Nullable variables = { S, A , B }
New set of productions :
S → aS | AB | A | B | a
D→b
3. Eliminating unit productions:
Unit production is one which is of the form A→ B, where both A & B are
nonterminal .
Example 8:
E→T|E+T
T→F|T*F
F → I | (E)
I → a | b | Ia | Ib | I0 | I1
Solution :
How to eliminate unit productions ?
o Replace E → T with E → F | T * F
o Then , upon recursive application wherever there is a unit production :
Step 1: E → F | T * F | E + T
Step 2: E → I | (E) | T * F | E + T
Step 3: E → a | b | Ia | Ib | I0 | I1 | (E) | T * F | E + T
o Now ,E has no unit productions
Similarly , eliminate for the remainder of the unit productions
Example 9:
S → Aa | B
B → A | bb
A → a | bc | B
Solution :
o Replace A → a | bc | B with A → a | bc | bb
So the grammar be :
S → Aa | B
B → A | bb
A → a | bc | bb
o Replace B → A | bb with B → a | bc | bb
So the grammar be :
S → Aa | B
B → a | bc | bb
A → a | bc | bb
o Replace S → Aa | B with S → Aa | a | bc | bb