Normal Form
Normal Form
-CNF
-GNF
Normal Forms:
Ambiguity is one undesirable property of a CFG that we
might wish to eliminate.
The ways of improving a grammar without changing the
resultant language.
Def:
A CFG is in CNF if every rule is of the form
NT string of exactly 2 NT
NTone terminal
Eg:
ABC
Aa
Where a is any terminal and A,B and C are any variable except that B
and C may not be the start variable.
In addition we permit the rule Sε , where S is the start variable.
CNF:
• We eliminate useless symbols. Those variables or terminals that
do not appears in any derivation of a terminal string from the start
symbol.
• We must eliminate ε-productions, those of the form X ε for
some variable X.
• We must eliminate unit production, those of the form XY for
variables x and Y.
Solu:
First production we have to replace terminal ‘b’ by a NT say
Cb and ‘a’ by Ca hence grammar becomes
S CbA/ CaB
A CbAA/ CaS/a
Ca a
Cbb
B CaBB/ CbS/b
First Production in CNF
S CbA/ CaB
Ca a
Cbb
Production in CNF
Second production is not in CNF S CbA/ CaB
A CbAA/ CaS/a A CbD/ CaS/a
A CbD/ CaS/a B CaE/ CbS/b
DAA Ca a
Cbb
Third production is not in CNF DAA
B CaBB/ CbS/b EBB
B CaE/ CbS/b
EBB
Ex:2
S1A/0B
A1AA/0S/0
B0BB/1
Solu:
SC1A/C0B
AC1Ca/ C0S/0
C11
C00
Ca AA
Cb BB
Ex:3
Sa
Sb
SCSS
Solu:
Sa
Sb
SCD
DSS
Ex:4
SabSb/a/aAb
Step 3
AbS/aAAb SCD/a/XaBb
Solu: AXbS/BaBb
SabSb/a/aBb BaXaA
AbS/BaBb
BbAXb
BaaA
BbAb Cab
DSXb
Step:2 Xaa
SCD/a/aBb Xbb
AbS/ BaBb
BaaA
BbAb
Cab
DSb
Ex:5 Step:2
SbA/aB SCbA/CaB
AbAA/aS/a
ADA/CaS/a
BaBB/bS/b
BEB/CbS/b
Solu: Cbb
SCbA/CaB Caa
ACbAA/CaS/a DCbA
BCaBB/CbS/b ECaB
Cbb
Caa
To Eliminate ε-Production:
To eliminate ε-Productions from a grammar G, we use the following
techniques
Solu:
SaA
Sa
Ab
Ex:2
SABAC
AaA/ ε
BbB/ ε
Cc
SABAC/BAC/ABC/BC/AAC/AC/C
AaA/a
BbB/b
Cc
Exercise:3
SAB Eliminate Bε
AaAA/ε SAB/B
BbBB/ε BbBB
Solu:
It derives
Eliminate Aε
SAB SA
AaAA BbB/b
The production derives
SB Therefore
AaA/a SAB/B/A
AaAA/aA/a
Therefore
SAB/B BbBB/bB/b
AaAA/aA/a
BbBB/ε
Removal of UNIT Production:
A production of the form
NT one NT
i.e, production of the form AB is called unit production
Algorithm:
Removal of unit productions
While (there exist a unit production, AB)
{
Select a unit production AB, such that there exists a production
Bα , where α is a terminal.
Now it can be easily seen that productions Ca, Da, Ea are useless
because if we start deriving from S, these productions will never be
used.
Hence eliminating them gives,
SAB
Aa
Ba/b
Which is completely reduced grammar