8/9/2019 Context Free Grammars Normal
1/38
Fall 2005 Costas Buch - RPI 1
Simplifications
ofContext-Free Grammars
8/9/2019 Context Free Grammars Normal
2/38
Fall 2005 Costas Buch - RPI 2
A Substitution Rule
bB
aAB
abBcA
aaAA
aBS
p
p
p
p
p
Substitute
Equivalent
grammar
aAB
abbcabBcA
aaAA
abaBS
p
p
p
p
|
|
bB p
8/9/2019 Context Free Grammars Normal
3/38
Fall 2005 Costas Buch - RPI 3
Equivalent
grammarabaAcabbcabBcA
aaAA
aaAabaBS
||
||
p
p
p
aAB
abbcabBcA
aaAA
abaBS
p
p
p
p
|
|
SubstituteaABp
8/9/2019 Context Free Grammars Normal
4/38
Fall 2005 Costas Buch - RPI 4
In general:
1yB
xBzA
p
p
Substitute
zxyxBzA 1|pequivalent
grammar
1yBp
8/9/2019 Context Free Grammars Normal
5/38
Fall 2005 Costas Buch - RPI 5
Nullable Variables
:productionP PpX
Nullable Variable: P-Y
Example:
Pp
p
p
M
aMbM
aMbS
Nullable variable productionP
8/9/2019 Context Free Grammars Normal
6/38
Fall 2005 Costas Buch - RPI 6
PpM
Pp
p
p
M
aMbM
aMbSSubstitute
abaMbM
abaMbS
|
|
p
p
Removing sproductionP
After we remove all theall the nullable variables disappear
(except for the start variable)
sproductionP
8/9/2019 Context Free Grammars Normal
7/38
Fall 2005 Costas Buch - RPI 7
Unit-Productions
YpU
nit Production:(a single variable in both sides)
bbB
ABBA
aA
aAS
p
pp
p
pExample:
Unit Productions
8/9/2019 Context Free Grammars Normal
8/38
Fall 2005 Costas Buch - RPI 8
bbB
AB
BA
aA
aAS
p
p
p
p
p
Substitute
BAp
bbB
BAB
aA
aBaAS
p
p
p
p
|
|
Removal of unit productions:
8/9/2019 Context Free Grammars Normal
9/38
Fall 2005 Costas Buch - RPI 9
Remove
bbB
BAB
aA
aBaAS
p
p
p
p
|
|
bbB
AB
aA
aBaAS
p
p
p
p |
BB p
XX p
can be removed immediately
Unit productions of form
8/9/2019 Context Free Grammars Normal
10/38
Fall 2005 Costas Buch - RPI 10
SubstituteABp
bbB
aA
aAaBaAS
p
p
p ||
bbB
AB
aA
aBaAS
p
p
p
p |
8/9/2019 Context Free Grammars Normal
11/38
Fall 2005 Costas Buch - RPI 11
Remove repeated productions
bbB
aA
aBaAS
p
p
p |
bbB
aA
aAaBaAS
p
p
p ||
Final grammar
8/9/2019 Context Free Grammars Normal
12/38
Fall 2005 Costas Buch - RPI 12
Useless Productions
aAA
AS
S
aSbS
pp
p
p
P
--- aAaaaaAaAAS
Some derivations never terminate...
Useless Production
8/9/2019 Context Free Grammars Normal
13/38
Fall 2005 Costas Buch - RPI 13
bABA
aAA
AS
pp
p
p
P
Another grammar:
Not reachable from S
Useless Production
8/9/2019 Context Free Grammars Normal
14/38
Fall 2005 Costas Buch - RPI 14
In general:
If there is a derivation)(GLwxAyS --
Then variable is usefulA
Otherwise, variable is uselessA
consists ofterminals
8/9/2019 Context Free Grammars Normal
15/38
Fall 2005 Costas Buch - RPI 15
A production is useless
if any of its variables is useless
xAp
DC
CB
aAA
AS
S
aSbS
p
p
p
p
p
p
PProductionsuseless
useless
useless
useless
Variables
useless
useless
useless
8/9/2019 Context Free Grammars Normal
16/38
Fall 2005 Costas Buch - RPI 16
Example Grammar:
aCbC
aaB
aACAaSS
p
p
pp ||
Removing Useless Variables and Productions
8/9/2019 Context Free Grammars Normal
17/38
Fall 2005 Costas Buch - RPI 17
First: find all variables that can produce
strings with only terminals or
aCbC
aaB
aA
CAaSS
p
p
p
p || },{ BA
},,{ SBA
Round 1:
Round 2:
(these are possible useful variables)
P
(the right hand side of a production
has only terminals)
(the right hand side of a productionhas terminals and
variables of previous round)
T
his process can be generalized
8/9/2019 Context Free Grammars Normal
18/38
Fall 2005 Costas Buch - RPI 18
Then, remove productions that use variables
other than
aCbC
aaB
aA
CAaSS
p
p
p
p ||
},,{ SBA
aaB
aA
AaSS
p
p
p |
8/9/2019 Context Free Grammars Normal
19/38
Fall 2005 Costas Buch - RPI 19
Second: Find all variablesreachable from
aaB
aA
AaSS
p
p
p |
S A B
Use a Dependency Graph
not
reachable
S
8/9/2019 Context Free Grammars Normal
20/38
Fall 2005 Costas Buch - RPI 20
Keep only the variables
reachable from S
aaB
aA
AaSS
p
p
p |
aA
AaSS
p
p |
Final Grammar
Contains only
useful variables
8/9/2019 Context Free Grammars Normal
21/38
Fall 2005 Costas Buch - RPI 21
Removing All
Step 1: Remove Nullable Variables
Step 2: Remove Unit-Productions
Step 3: Remove Useless Variables
This sequence guarantees that
unwanted variables and production
are removed
8/9/2019 Context Free Grammars Normal
22/38
Fall 2005 Costas Buch - RPI 22
Normal Forms
forContext-free Grammars
8/9/2019 Context Free Grammars Normal
23/38
Fall 2005 Costas Buch - RPI 23
Chomsky Normal Form
Each productions has form:
BCAp
variable variable
aApor
terminal
8/9/2019 Context Free Grammars Normal
24/38
Fall 2005 Costas Buch - RPI 24
Examples:
bA
SAA
aS
ASS
p
p
p
p
Not ChomskyNormal Form
aaA
SAA
AASS
ASS
p
p
p
p
ChomskyNormal Form
8/9/2019 Context Free Grammars Normal
25/38
Fall 2005 Costas Buch - RPI 25
Convertion to Chomsky Normal Form
Example:
AcB
aabA
ABaS
p
p
pNot Chomsky
Normal Form
We will convert it to Chomsy Normal Form
8/9/2019 Context Free Grammars Normal
26/38
Fall 2005 Costas Buch - RPI 26
AcB
aabAABaS
p
p
p
Introduce new variables for the terminals:
cT
bT
aT
ATB
TTTA
ABTS
c
b
a
c
baa
a
p
p
p
p
p
pcba
TTT,,
8/9/2019 Context Free Grammars Normal
27/38
Fall 2005 Costas Buch - RPI 27
Introduce new intermediate variable
to break first production:
cT
bT
aT
ATB
TTTA
ABTS
c
b
a
c
baa
a
p
p
p
p
p
p
cT
bT
aT
ATB
TTTA
BTV
AVS
c
b
a
c
baa
a
p
p
p
p
p
p
p
1
1
1V
8/9/2019 Context Free Grammars Normal
28/38
Fall 2005 Costas Buch - RPI 28
Introduce intermediate variable:
cT
bT
aT
ATB
TTV
VTA
BTV
AVS
c
b
a
c
ba
a
a
p
p
p
p
p
p
p
p
2
2
11
2V
c
b
aT
ATB
TTTA
BTV
AVS
c
b
a
c
baa
a
p
p
p
p
p
p
p
1
1
8/9/2019 Context Free Grammars Normal
29/38
Fall 2005 Costas Buch - RPI 29
Final grammar in Chomsky Normal Form:
cT
bT
aT
ATB
TTV
VTA
BTV
AVS
c
b
a
c
ba
a
a
p
p
p
p
p
p
p
p
2
2
1
1
AcB
aabA
ABaS
p
p
p
Initial grammar
8/9/2019 Context Free Grammars Normal
30/38
Fall 2005 Costas Buch - RPI 30
From any context-free grammar
(which doesnt produce )
not in Chomsky Normal Form
we can obtain:
an equivalent grammar
in Chomsky Normal Form
In general:
P
8/9/2019 Context Free Grammars Normal
31/38
Fall 2005 Costas Buch - RPI 31
The Procedure
First remove:
Nullable variables
Unit productions
8/9/2019 Context Free Grammars Normal
32/38
Fall 2005 Costas Buch - RPI 32
Then, for every symbol :a
In productions: replace witha aT
Add production aTa p
New variable: aT
8/9/2019 Context Free Grammars Normal
33/38
Fall 2005 Costas Buch - RPI 33
Replace any productionnCCCA .21p
with
nnn CCV
VCV
VCA
12
221
11
p
p
p
-
New intermediate variables: 221 ,,, nVVV-
8/9/2019 Context Free Grammars Normal
34/38
Fall 2005 Costas Buch - RPI 34
Observations
Chomsky normal forms are good
for parsing and proving theorems
It is easy to find the Chomsky normal
form for any context-free grammar
8/9/2019 Context Free Grammars Normal
35/38
Fall 2005 Costas Buch - RPI 35
Greinbach Normal Form
All productions have form:
kVVVaA .21p
symbol variables
0uk
8/9/2019 Context Free Grammars Normal
36/38
Fall 2005 Costas Buch - RPI 36
Examples:
bB
bbBaAA
cABS
p
p
p
||
GreinbachNormal Form
aaS
abSbS
p
p
Not Greinbach
Normal Form
8/9/2019 Context Free Grammars Normal
37/38
Fall 2005 Costas Buch - RPI 37
aaS
abSbS
p
p
Conversion to Greinbach Normal Form:
bT
aT
aTS
STaTS
b
a
a
bb
p
p
p
p
Greinbach
Normal Form
8/9/2019 Context Free Grammars Normal
38/38
Fall 2005 Costas Buch - RPI 38
Observations
Greinbach normal forms are very good
for parsing strings (better than Chomsky Normal Forms)
However, it is hard to find the
Greinbach normal of a grammar