Professional Documents
Culture Documents
CFG - Normal Forms
CFG - Normal Forms
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
09-04-2023 4
Conversion to CNF
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
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
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
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)
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|ε
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|ε
09-04-2023 22
GNF, Greibach Normal Form
09-04-2023 23
GNF, Greibach Normal Form
09-04-2023 24
Conversion to GNF
Step 2: Add to the solution, the productions which are already in GNF
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)
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)
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
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->)
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
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
09-04-2023 38