FALLSEM2023-24 CSI1003 TH VL2023240104080 2023-10-05 Reference-Material-I

You might also like

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

Normal Form in CFG

Problem1
Simplify the following grammar
S-> aA|aBB
A-> aAA|Ɛ
B->bB|bbC
C-> B
Solution for Problem1
S->aA |a
A->aAA|aA|a
Problem2
Convert the following CFG to CNF
S-> AB|aB
A->aab|Ɛ
B->bbA
Solution 2
Eliminate Ɛ productions
S-> AB|B|aB
A-> aab
B->bbA|bb
Elimate unit productions
S->AB|bbA|bb|aB
A-> aab
B->bbA|bb
No useless symbols or productions
Solution to Problem2
Replace terminals by a variable

S-> AB|CbD|CaB|CbCb
A->ECb
B->DA|CbCb
Ca->a,Cb->b
D->CbA
E->CaCa
Converting CFG to GNF
1. Eliminate the null productions and the unit
productions and construct a CNF
2. Rename the variables as A1,A2,….with S =A1
3. For each production of the form Ai -> Ajα
apply the following:
(a) if j>i – leave the productions as it is
(b) if j=i – apply lemma 4
(c) if j<i – apply lemma 3
Converting CFG to GNF
4. For each production of the form Ai -> Ajα,
where j>i apply the substitution lemma if Ai is
in GNF
Lemma 3
Lemma 3 Substitution Rule
Let G=[V, T, S, P] be a CFG. Let A -> B α be
production in P and B is B-> β1| β2| β3….
The equivalent grammar can be obtained by
substituting B in A
Then the resulting grammar is,
A -> β1 α| β2 α ….
Lemma 4
• Lemma 4 Elimination of Left Recursion
Grammar of the form A->A α | β is called left
recursive grammar. To eliminate left recursion,
rewrite the grammar as
A-> β A’
A’-> αA’ |Ɛ
If we eliminate Ɛ production, then we get
A-> β| β A’
A’-> α |αA’
Lemma4
• If there is a CFG as
A -> A α1 | A α2 |A α3….| β1 | β2| β3…
The equivalent grammar without left recursion
is
A -> β1A’| β2A’| β3A’ ….| β1| β2| β3….
A’ -> α1A’| α2A’| α3A’…..| α1| α2| α3…
Example
Convert the CFG to GNF
S ->AA|a
A ->SS|b
Problem 2
S-> XA|BB
B-> b|SB
X->b
A->a

You might also like