TOC Lecture-11
Simplification of
Context Free Grammar
Introduction
• 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 /Reduction of CFG
Removing Removing Removing
Useless productions NULL productions UNIT productions
Removing Useless Productions
Removing Useless Productions
• Definition:
Symbol X is said to be useful for a grammar G = (V, T, P, S) if
∗
1. X is generating variable. i.e., if X ֜ w, where w ∈ T*.
∗
2. X is reachable, i.e., S ֜ αXβ
• If X is not useful, then it is useless.
• Omitting useless symbols from a grammar does not change
the language generated.
Example-1
• Eliminate useless productions:
S aSb | | A
A aA
• S A is redundant as A cannot be transformed into a
terminal string.
• Therefore, reduced grammar is:
S aSb |
Example-2
• Eliminate useless productions:
S A
A aA |
B bA
• B is a useless symbol as it is not reachable from S.
• Therefore, reduced grammar is:
S A
A aA |
Theorem
• Let G = (V, T, S, P) be a CFG and assume that L(G) ≠ φ, then
G’ =(V’, T’, P’, S) be a grammar without useless symbols by
1. Eliminating non-generating symbols
2. Eliminating symbols that are non-reachable
• Elimination has to be performed only in the order of 1
followed by 2. Otherwise the grammar produced will not be
completely useless symbols eliminated.
Question-1
• Given G = ({S, A, B, C}, {a, b}, S, P). Find the useless symbols and
remove them.
S aS | A | C
A a
B aa
C aCb
Step1: Non-generating symbols = {C} Step2: Non-reachable symbols = {B}
S aS | A S aS | A
A a A a
B aa
Question-2
• Given G = ({S, A, B, C}, {a, b}, S, P). Find the useless symbols and
remove them.
S AB | CA
A a
B BC | AB
C AB | b
Step1: Non-generating symbols = {B} Step2: Non-reachable symbols = None
S CA S CA
A a A a
Cb Cb
Removing Null Productions
Removing Null Productions
• If is there in L(G), then we have to find out an equivalent G
without -productions.
• Example for a grammar G with -productions is:
S ABA , A aA | , B bB |
• The procedure to find out an equivalent G without -productions:
1. Find nullable variables.
2. Add productions with nullable variables removed.
3. Remove -productions and duplicates.
Removing Null Productions
• Step 1: Find nullable variables
– Variables that can be replaced by null ().
∗
– If A ֜ then A is a nullable variable.
• Example: Consider the grammar with productions
S ABA , A aA | , B bB |
− A is nullable because of the production A .
− B is nullable because of the production B .
− S is nullable because both A and B are nullable.
Removing Null Productions
• Step 2: Add productions with nullable variables removed
– For each production of the form A w,
– Create all possible productions of the form A w’,
– Where w’ is obtained from w by removing one or more
occurrences of nullable variables.
• Example: S ABA , A aA | , B bB |
After finding A, B and S as nullable variables:
S ABA | BA | AA | AB | A | B |
A aA | | a
B bB | | b
Removing Null Productions
• Step 3: Remove -productions and duplicates
– The desired grammar consists of the original productions
together with the productions constructed in step 2, minus any
productions of the form A .
• Example: For the above example we get the final grammar
with productions
S ABA | BA | AA | AB | A | B
A aA | a
B bB | b
Question-1
Eliminate Null Productions:
G = ({S, A, B, D}, {a, b}, {S → aS | AB, A → , B→ , D →b}, S)
Step1: Find Nullable variables = {S, A, B}
Step2: Add productions with Nullable variables removed
S → aS | AB | a | A | B
A→
B→
D→b
Step3: Remove -productions and duplicates
S → aS | AB | a | A | B
D→b
Question-2
Eliminate Null Productions:
G = ({S, X, Y}, {a, b}, {S → a | Xb | aYa , X → Y | , Y → b | X}, S)
Step1: Find Nullable variables = {X, Y}
Step2: Add productions with Nullable variables removed
S → a | Xb | aYa | b | aa
X→Y|
Y→b|X|
Step3: Remove -productions and duplicates
S → a | Xb | aYa | b | aa
X→Y
Y→b|X
Note
• To get a grammar without -productions and useless symbols:
1. Eliminate -productions and obtain G1
2. Eliminate useless symbols from G1 and obtain G2
Question
Eliminate -productions and useless symbols from the grammar:
S → a | aA | B | C , A →aB | , B → aA , C → aCD , D → ddd
Step1: Eliminate -productions. Nullable variables = {A}
New set of productions are:
P1 = {S → a | aA | B | C , A → aB , B → aA | a , C → aCD , D → ddd}
Step2: Eliminate useless symbols:
Step2(a): Eliminate non-generating symbols = {C}
P2 = {S → a | aA | B , A → aB , B → aA | a , D → ddd}
Step2(b): Eliminate non-reachable symbols = {D}
P3 = {S → a | aA | B , A → aB , B → aA | a}
Removing Unit Productions
Removing Unit Productions
• Unit production is of form A B, where A and B are
variables.
• Algorithm for eliminating unit productions from productions
set P :
1. Add all non unit productions of P to P1
2. For each unit production A B,
a) add to P1 all productions A α,
b) where B α is a non-unit production in P.
3. Delete all the unit productions
Question-1
• Eliminate unit productions from:
S ABA | BA | AA | AB | A | B,
A aA | a,
B bB | b
Solution: The unit productions are S → A, S →B.
A and B are derivable.
Add productions from derivable.
S→ ABA | BA | AA | AB | A | B | aA | a | bB | b
A → aA | a ; B → bB | b
Remove unit productions from above productions to get
S→ ABA | BA | AA | AB | aA | a | bB | b ;
A → aA | a ; B → bB | b
Question-2
• Eliminate unit productions from
S Aa | B , A a | bc | B , B A | bb
Solution:
Unit productions are S → B, A → B and B → A. So A and B are
derivable.
Add productions from derivable and eliminate unit productions to
get,
S → Aa | bb | a | bc
A → a | bc | bb
B → bb | a | bc