Context Free Grammars Normal
Context Free Grammars Normal
Context Free Grammars Normal
of
Context-Free Grammars
Equivalent
grammar
S aB
S aB | ab
A aaA
Substitute A aaA
A abBc Bb A abBc | abbc
B aA
B aA
Bb
Fall 2005 Costas Buch - RPI 2
S aB | ab
A aaA
A abBc | abbc
B aA
Substitute
B aA
S aB | ab | aaA
A aaA Equivalent
A abBc | abbc | abaAc grammar
Fall 2005 Costas Buch - RPI 3
In general: A xBz
B y1
Substitute
B y1
equivalent
A xBz | xy1z grammar
Fall 2005 Costas Buch - RPI 4
Nullable Variables
production : X
Nullable Variable: Y
Example: S aMb
M aMb
M
Example: S aA
Aa
A B
Unit Productions
BA
B bb
Fall 2005 Costas Buch - RPI 7
Removal of unit productions:
S aA
S aA | aB
Aa
Substitute Aa
A B A B B A| B
BA
B bb
B bb
S aA | aB S aA | aB
Aa Remove Aa
B A| B BB BA
B bb B bb
Final grammar
S aA | aB | aA S aA | aB
Aa Aa
B bb B bb
S aSb
S
SA
A aA Useless Production
S A aA aaA aa aA
Fall 2005 Costas Buch - RPI 12
Another grammar:
SA
A aA
A
B bA Useless Production
Not reachable from S
If there is a derivation
S xAy w L(G )
consists of
terminals
Then variable A is useful
S aSb
S Productions
Variables SA useless
useless A aA useless
useless B C useless
Example Grammar: S aS | A | C
Aa
B aa
C aCb
S aS | A | C Round 1: { A, B}
Aa (the right hand side of a production
has only terminals)
B aa
Round 2: { A, B, S }
C aCb (the right hand side of a production
has terminals and
variables of previous round)
S aS | A | C
Aa S aS | A
B aa Aa
C aCb B aa
S aS | A
Aa S A B
B aa not
reachable
Final Grammar
S aS | A
S aS | A
Aa
Aa
B aa
Contains only
useful variables
A BC or Aa
S AS S AS
S a S AAS
A SA A SA
Ab A aa
Chomsky Not Chomsky
Normal Form Normal Form
Example: S ABa
Not Chomsky
A aab Normal Form
B Ac
we can obtain:
an equivalent grammar
in Chomsky Normal Form
First remove:
Nullable variables
Unit productions
Add production Ta a
New variable: Ta
Fall 2005 Costas Buch - RPI 32
Replace any production A C1C2 Cn
with A C1V1
V1 C2V2
Vn2 Cn1Cn
A a V1V2 Vk k 0
symbol variables
S cAB
S abSb
A aA | bB | b
S aa
Bb
S aTb STb
S abSb S aTa
S aa Ta a
Tb b
Greinbach
Normal Form
Fall 2005 Costas Buch - RPI 37
Observations