Professional Documents
Culture Documents
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.
S aSb |
Example-2
• Eliminate useless productions:
S A
A aA |
B bA
S A
A aA |
Theorem
S aS | A | C
A a
B aa
C aCb
S AB | CA
A a
B BC | AB
C AB | b
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: 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 .
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)
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)
S → a | Xb | aYa | b | aa
X→Y
Y→b|X
Note
• To get a grammar without -productions and useless symbols:
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