9 - CFG Simplification

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Simplifying Context Free Grammars

The definition of context free grammars (CFGs) allows us to develop a


wide variety of grammars. Most of the time, some of the productions of CFGs
are not useful and are redundant. This happens because the definition of CFGs
does not restrict us from making these redundant productions.
By simplifying CFGs we remove all these redundant productions from a
grammar , while keeping the transformed grammar equivalent to the original
grammar. Two grammars are called equivalent if they produce the same
language. Simplifying CFGs is necessary to later convert them into Normal
forms.
Types of redundant productions and the procedure of removing them are
mentioned below.

CFG Simplification methods


1. Eliminating useless symbols
 A symbol X is generating if there exists:
X →* w, for some w → T*
 A symbol X is reachable if there exists:
S →* α X β
 For a symbol X to be “useful”, it has to be both reachable and generating

Algorithm to detect useless symbols :


1.First, eliminate all symbols that are not generating
2.Next, eliminate all symbols that are not reachable

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 * → ε

Algorithm to detect ε-productions:


Go through all productions, and for each , delete every possible subset of
nullable variables. For example,
If P → AxB with both A and B nullable,
Then add productions P → xB | Ax | x .
After this, delet all productions with empty RHS.

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

So the final grammar be :


S → Aa | a | bc | bb
B → a | bc | bb
A → a | bc | bb
Example10 :
Apply the CFG simplification for the following grammar :
S → ACD | AB
A → a | Aa
C→ε
D → Dd | E
E → eAe
F → ff
Solution :
1. Delete B useless because nothing derivable from B .
2. Delete C → ε and also add S → AD .
3. Replace D → Dd | E with D → eAe and delete E → e .
4. Delete F useless because not derivable from S .

So the new grammar be :


????? (H. W.)

You might also like