CFG - Normal Forms

You might also like

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

CFG Normal Forms

CNF Form
Removal of Left Recursion
GNF Form

09-04-2023 1
Normal Forms
• A grammar is said to be in a Normal Form when every
production of a grammar has specific form
• This makes it convenient to design algorithms for working
with CFGs

09-04-2023 2
Normal Forms for CFG
• CNF, Chomsky Normal Form
• GNF, Greibach Normal Form

09-04-2023 3
CNF, Chomsky Normal Form

• A grammar is said to be in Chomsky Normal form (i.e. CNF) if


every production rule of the grammar is of the form:
A->BC or A->a
• where A,B,C are in V and a is in T

09-04-2023 4
Conversion to CNF

Step 1:Simplify the grammar G by eliminating null productions,


useless productions and unit productions

Step 2: Add to the solution, the productions which are already in


CNF

Step 3: For the productions, not in CNF:-


a) Replace the terminals by some variables
b) limit the number of variables on RHS to 2

09-04-2023 5
CNF, Chomsky Normal Form-Example 1
• Consider a CFG with following productions:
S->aB|bA
A->a|aS|bAA
B->b|bS|aBB

09-04-2023 6
CNF, Chomsky Normal Form-Example 1
• Consider a CFG with following productions:
S->aB|bA
A->a|aS|bAA
B->b|bS|aBB

Solution-
Introduce new non-terminals Ca and Cb and production
Ca->a and Cb->b , we get:-
S->CaB| CbA
A->a|CaS| CbAA
B->b| CbS| CaBB

09-04-2023 7
CNF, Chomsky Normal Form-Example 1
S->CaB| CbA
A->a|CaS| CbAA
B->b| CbS| CaBB
Ca->a
Cb->b

Introduce D1 and D2 as two new non-terminals and productions


D1->AA and D2->BB

We get,
S->CaB| CbA
A->a|CaS| CbD1
B->b| CbS| CaD2
Ca->a
Cb->b
D1->AA
D2->BB
The grammar is in CNF
09-04-2023 8
CNF, Chomsky Normal Form-Example 2
Consider a CFG with the following productions:
S->aSa|bSb|ab

09-04-2023 9
CNF, Chomsky Normal Form-Example 2
Consider a CFG with the following productions:
S->aSa|bSb|ab

Introducing new non-terminals Ca and Cb, we get


S->CaSCa| CbSCb| CaCb
Ca->a
Cb->b

Introducing D1 and D2, we get


S->CaD1 | CbD2 | CaCb
Ca->a
Cb->b
D1->SCa
D2->SCb
The grammar is in CNF
09-04-2023 10
CNF, Chomsky Normal Form-Example 3
Consider a CFG with the following productions:
S->aSb|aA|bB|a|b
A->aA|a
B->bB|b

09-04-2023 11
CNF, Chomsky Normal Form-Example 3
Consider a CFG with the following productions:
S->aSb|aA|bB|a|b
A->aA|a
B->bB|b

09-04-2023 12
CNF, Chomsky Normal Form-Example 3
Consider a CFG with the following productions:
S->aSb|aA|bB|a|b
A->aA|a
B->bB|b

Introducing new non-terminals Ca and Cb, we get


S->CaSCb| CaA| CbB|a|b
A->CaA|a
B->CbB|b
Ca->a
Cb->b

Introducing D1, we get


S->CaD1 | CaA| CbB|a|b
A->CaA|a
B->CbB|b
Ca->a
Cb->b
D1->SCb
The grammar is in CNF
09-04-2023 13
Elimination of Left Recursion

• If a grammar contains a pair of productions of the form


A->Aα|β
• then the grammar is left recursive grammar
• Left recursive grammar , if used for specification of the
language then the top down parser may enter into the infinite
loop during the parsing process on some erroneous input.

09-04-2023 14
Elimination of Left Recursion
Left recursion can be eliminated from the grammar by the
following rule-
G=(V,T,P,S)

For Production P:-


A->Aα1|Aα2|………|Aαm|β1|β2|………|βn
β does not start with A

Replace by P1-
A-> β1Z| β2Z|………|βn| β1| β2|………|βn Without Null
Approach
Z-> α1Z| α2Z|………|αmZ| α1| α2|………|αm
OR
A-> β1Z| β2Z|………|βnZ
Z-> α1Z| α2Z|………|αmZ|ε With Null Approach
09-04-2023 15
Elimination of Left Recursion-Example 1
Consider the following grammar:
S->aBDh
B->Bb|c
D->EF
E->g|ε
F->f|ε

09-04-2023 16
Elimination of Left Recursion-Example 1
Consider the following To eliminate the left recursion
grammar: from the grammar,
S->aBDh Replace this pair of production
B->Bb|c by:-
D->EF B->cZ
E->g|ε Z->bZ|ε
F->f|ε
Thus, Final grammar is :-
The grammar is left recursive S->aBDh
due to production B->cZ
B->Bb|c Z->bZ|ε
D->EF
E->g|ε
With Null Approach
09-04-2023 F->f|ε 17
Elimination of Left Recursion-Example 2
Consider the following
grammar:
S->A
A->Ad|Ae|aB|aC
B->bBC|f
C->g

09-04-2023 18
Elimination of Left Recursion-Example 2
Consider the following To eliminate the left recursion
grammar: from the grammar,
S->A Replace this pair of production
A->Ad|Ae|aB|aC by:-
B->bBC|f A->aBZ|aCZ
C->g Z->dZ|eZ|ε

The grammar is left recursive Thus, Final grammar is :-


due to production S->A
A->Ad|Ae|aB|aC A->aBZ|aCZ
Z->dZ|eZ|ε
B->bBC|f
C->g
With Null Approach
09-04-2023 19
Elimination of Left Recursion-Example 3
Consider the following
grammar:
A->aBD|bDB|c
A->AB|AD
Remove left recursion

09-04-2023 20
Elimination of Left Recursion-Example 3
Consider the following To eliminate the left recursion
grammar: from the grammar,
A->aBD|bDB|c Replace :-
A->AB|AD A->AB|AD
Remove left recursion By this pair of production by:-
A->aBDZ|bDBZ|cZ
Z->BZ|DZ|ε

With Null Approach


09-04-2023 21
GNF

09-04-2023 22
GNF, Greibach Normal Form

• A grammar is said to be in Greibach Normal form (i.e. GNF) if


every production rule of the grammar is of the form:
A->aα
• where A is the non terminal, a is the terminal and α is a string
of non-terminals(possibly empty)
• α belongs to V*

09-04-2023 23
GNF, Greibach Normal Form

• Divide the production of Grammar G into left recursive and


non-left recursive production
• Then eliminate left recursion to get GNF

09-04-2023 24
Conversion to GNF

Step 1:Simplify the grammar G by eliminating null productions,


useless productions and unit productions

Step 2: Add to the solution, the productions which are already in GNF

Step 3: For the productions, not in GNF:-


a) Use Substitution Rule
A->Bα
B->β1| β2|………|βn
then we can write A as
A->β1α | β2α |………|βnα

b) Remove left recursion , Use without epsilon approach

09-04-2023 25
GNF, Greibach Normal Form-Example 1
Consider a CFG with the following productions, Convert to GNF:
S->aSa|bSb|ab For GNF-
A->aα
where A is the non terminal, a is
the terminal and α is a string of
non-terminals(possibly empty)

09-04-2023 26
GNF, Greibach Normal Form-Example 1
Consider a CFG with the following productions, Convert to GNF :
S->aSa|bSb|ab For GNF-
A->aα
where A is the non terminal, a is
the terminal and α is a string of
Soln: non-terminals(possibly empty)

S->aSA|bSB|aB Simple Variable Usage


A->a
B->b
The grammar is in GNF

09-04-2023 27
GNF, Greibach Normal Form-Example 2
• Consider a CFG with the following productions, Convert to GNF :
S->XY|XW
X->YZ|a
For GNF-
Z->c A->aα
Y->WZ|b where A is the non terminal, a is
W->d the terminal and α is a string of
non-terminals(possibly empty)

Use Substitution Rule

09-04-2023 28
GNF, Greibach Normal Form-Example 2
• Consider a CFG with the following • Final productions are-
productions, Convert to GNF :
S->dZZY|bZY|aY|dZZW|bZW|aW
S->XY|XW
X->YZ|a X->dZZ|bZ|a
Z->c Y->dZ|b
Y->WZ|b W->d
W->d Z->c
• Substitute W value in Y production
Y->WZ|b, rewritten as-
Y->dZ|b
• Substitute Y value in X production
X->YZ|a, rewritten as-
X->dZZ|bZ|a
• Substitute X value in S production
S->XY|XW, rewritten as-
S->dZZY|bZY|aY|dZZW|bZW|aW

09-04-2023 29
GNF, Greibach Normal Form-Example 3
Consider a CFG with the following
productions, Convert to GNF :
S->AA|0
A->SS|1

09-04-2023 30
GNF, Greibach Normal Form-Example 3
Consider a CFG with the following
productions, Convert to GNF :
S->AA|0
A->SS|1

09-04-2023 31
GNF, Greibach Normal Form-Example 3
Consider a CFG with the following Now substitute the Value of A in S
productions, Convert to GNF :
production
S->AA|0
A->SS|1 S->0SZA|1ZA|0SA|1A|0 GNF Form
A->0SZ|1Z|0S|1 GNF Form
Substitute first S in production of A Z->ASZ|AS
A->SS|1 Substitution Rule Substitution Rule
A->AAS|0S|1
So left recursion is there Now substitute the Value of A in Z
production
To eliminate Left recursion- S->0SZA|1ZA|0SA|1A|0 GNF Form
A->0SZ|1Z|0S|1 GNF Form A->0SZ|1Z|0S|1 GNF Form
Z->ASZ|AS
Z-> 0SZSZ|1ZSZ|0SSZ|1SZ
New Productions are: 0SZS|1ZS|0SS|1S GNF Form
S->AA|0 Removal of Recursion Substitution Rule
A->0SZ|1Z|0S|1 GNF Form
Z->ASZ|AS

09-04-2023 32
GNF, Greibach Normal Form-Example 4
Convert the following grammar into GNF
E->E+T|T
T->T*F|F
F->(E)|a

09-04-2023 33
GNF, Greibach Normal Form-Example 4
Convert the following grammar into GNF
E->E+T|T
T->T*F|F
F->(E)|a

Substitution Rule-
E->E+T|T*F|F or E->E+T|T*F|(E)|a
T->T*F|(E)|a
F->(E)|a

Step 1: Remove unit productions


E->T
T->F

After removal of unit productions, we get


E->E+T|T*F|(E)|a
T->T*F|(E)|a
F->(E)|a

09-04-2023 34
GNF, Greibach Normal Form-Example 4
Step 2: Replace terminals of the productions which are not in GNF form by
variables
E->E+T|T*F|(E)|a
T->T*F|(E)|a
F->(E)|a

Introduce
A1->+
A2->*
A3->)

The resulting Productions are-


E->EA1T|TA2F|(EA3|a
T->TA2F|(EA3|a
F->(EA3|a
Now all F productions are in GNF form
09-04-2023 35
GNF, Greibach Normal Form-Example 4
Step 3:Remove Left Recursion for T
E->EA1T|TA2F|(EA3|a
T->TA2F|(EA3|a
F->(EA3|a

Left Recursion Removal-for T


Replace T->TA2F|(EA3|a by
T->(EA3Z1|aZ1|(EA3|a
Z1->A2FZ1|A2F

Substituting, we get
A2->*

T->(EA3Z1|aZ1|(EA3|a
Z1->*FZ1|*F

So Productions are
E->EA1T|TA2F|(EA3|a
T->(EA3Z1|aZ1|(EA3|a
Z1->*FZ1|*F
F->(EA3|a

09-04-2023 36
GNF, Greibach Normal Form-Example 4
Productions are
E->EA1T|TA2F|(EA3|a
T->(EA3Z1|aZ1|(EA3|a
Z1->*FZ1|*F
F->(EA3|a

For Production E->TA2F , Substitute the value of T


Replace E->TA2F by
E->(EA3Z1A2F|aZ1A2F|(EA3A2F|aA2F
so Replace E->EA1T|TA2F|(EA3|a by
E->EA1T|(EA3Z1A2F|aZ1A2F|(EA3A2F|aA2F|(EA3|a

Productions are
E->EA1T|(EA3Z1A2F|aZ1A2F|(EA3A2F|aA2F|(EA3|a
T->(EA3Z1|aZ1|(EA3|a
Z1->*FZ1|*F
F->(EA3|a
All productions of E are in GNF except E->EA1T

09-04-2023 37
Productions are
GNF, Greibach Normal Form-Example 4
E->EA1T|(EA3Z1A2F|aZ1A2F|(EA3A2F|aA2F|(EA3|a
T->(EA3Z1|aZ1|(EA3|a
Z1->*FZ1|*F
F->(EA3|a
All productions of E are in GNF except E->EA1T
Removing Left recursion for E->EA1T
Replace E->EA1T|(EA3Z1A2F|aZ1A2F|(EA3A2F|aA2F|(EA3|a by

E->(EA3Z1A2FZ2|aZ1A2FZ2|(EA3A2FZ2|aA2FZ2|(EA3Z2|aZ2|
(EA3Z1A2F|aZ1A2F|(EA3A2F|aA2F|(EA3|a|
Z2->A1TZ2| A1T
Substituting + for A1
Z2->+TZ2| +T

Resulting grammar is-


E->(EA3Z1A2FZ2|aZ1A2FZ2|(EA3A2FZ2|aA2FZ2|(EA3Z2|aZ2|
(EA3Z1A2F|aZ1A2F|(EA3A2F|aA2F|(EA3|a|
Z2->+TZ2| +T
T->(EA3Z1|aZ1|(EA3|a
Z1->*FZ1|*F
F->(EA3|a

09-04-2023 38

You might also like