Professional Documents
Culture Documents
Simplifications
of
Context-Free Grammars
Fall 2006 Costas Buch - RPI 2
A Substitution Rule
b B
aA B
abBc A
aaA A
aB S
Substitute
Equivalent
grammar
aA B
abbc abBc A
aaA A
ab aB S
|
|
b B
Fall 2006 Costas Buch - RPI 3
Equivalent
grammar
abaAc abbc abBc A
aaA A
aaA ab aB S
| |
| |
aA B
abbc abBc A
aaA A
ab aB S
|
|
Substitute
aA B
Fall 2006 Costas Buch - RPI 4
In general:
1
y B
xBz A
Substitute
z xy xBz A
1
|
equivalent
grammar
1
y B
Fall 2006 Costas Buch - RPI 5
Nullable Variables
: production
X
Nullable Variable:
Y
Example:
M
aMb M
aMb S
Nullable variable
production
Fall 2006 Costas Buch - RPI 6
M
M
aMb M
aMb S
Substitute
ab aMb M
ab aMb S
|
|
Removing
s production
After we remove all the
all the nullable variables disappear
(except for the start variable)
s production
Fall 2006 Costas Buch - RPI 7
Unit-Productions
Y X Unit Production:
(a single variable in both sides)
bb B
A B
B A
a A
aA S
Example:
Unit Productions
Fall 2006 Costas Buch - RPI 8
bb B
A B
B A
a A
aA S
Substitute
B A
bb B
B A B
a A
aB aA S
|
|
Removal of unit productions:
Fall 2006 Costas Buch - RPI 9
Remove
bb B
B A B
a A
aB aA S
|
|
bb B
A B
a A
aB aA S
|
B B
X X
can be removed immediately
Unit productions of form
Fall 2006 Costas Buch - RPI 10
Substitute
A B
bb B
a A
aA aB aA S
| |
bb B
A B
a A
aB aA S
|
Fall 2006 Costas Buch - RPI 11
Remove repeated productions
bb B
a A
aB aA S
|
bb B
a A
aA aB aA S
| |
Final grammar
Fall 2006 Costas Buch - RPI 12
Useless Productions
aA A
A S
S
aSb S
aA aa aaA aA A S
Some derivations never terminate...
Useless Production
Fall 2006 Costas Buch - RPI 13
bA B
A
aA A
A S
Another grammar:
Not reachable from S
Useless Production
Fall 2006 Costas Buch - RPI 14
In general:
If there is a derivation
) (G L w xAy S
Then variable is useful
A
Otherwise, variable is useless
A
consists of
terminals
Fall 2006 Costas Buch - RPI 15
A production is useless
if any of its variables is useless
x A
D C
C B
aA A
A S
S
aSb S
Productions
useless
useless
useless
useless
Variables
useless
useless
useless
Fall 2006 Costas Buch - RPI 16
Example Grammar:
aCb C
aa B
a A
C A aS S
| |
Removing Useless Variables and Productions
Fall 2006 Costas Buch - RPI 17
First:
find all variables that can produce
strings with only terminals or
aCb C
aa B
a A
C A aS S
| |
} , { B A
} , , { S B A
Round 1:
Round 2:
(possible useful variables)
| |
} , , { S B A
aa B
a A
A aS S
|
Fall 2006 Costas Buch - RPI 19
Second: Find all variables
reachable from
aa B
a A
A aS S
|
S A B
Use a Dependency Graph
where nodes are variables
unreachable
S
Fall 2006 Costas Buch - RPI 20
Keep only the variables
reachable from S
aa B
a A
A aS S
|
a A
A aS S
|
Final Grammar
Contains only
useful variables
Fall 2006 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 productions
are removed
Fall 2006 Costas Buch - RPI 22
Normal Forms
for
Context-free Grammars
Fall 2006 Costas Buch - RPI 23
Chomsky Normal Form
Each productions has form:
BC A
variable variable
a A or
terminal
Fall 2006 Costas Buch - RPI 24
Examples:
b A
SA A
a S
AS S
Not Chomsky
Normal Form
aa A
SA A
AAS S
AS S
Chomsky
Normal Form
Fall 2006 Costas Buch - RPI 25
Conversion to Chomsky Normal Form
Example:
Ac B
aab A
ABa S
Not Chomsky
Normal Form
We will convert it to Chomsky Normal Form
Fall 2006 Costas Buch - RPI 26
Ac B
aab A
ABa S
c b a
T T T , ,
Fall 2006 Costas Buch - RPI 27
Introduce new intermediate variable
to break first production:
c T
b T
a T
AT B
T T T A
ABT S
c
b
a
c
b a a
a
c T
b T
a T
AT B
T T T A
BT V
AV S
c
b
a
c
b a a
a
1
1
1
V
Fall 2006 Costas Buch - RPI 28
Introduce intermediate variable:
c T
b T
a T
AT B
T T V
V T A
BT V
AV S
c
b
a
c
b a
a
a
2
2
1
1
2
V
c T
b T
a T
AT B
T T T A
BT V
AV S
c
b
a
c
b a a
a
1
1
Fall 2006 Costas Buch - RPI 29
Final grammar in Chomsky Normal Form:
c T
b T
a T
AT B
T T V
V T A
BT V
AV S
c
b
a
c
b a
a
a
2
2
1
1
Ac B
aab A
ABa S
Initial grammar
Fall 2006 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:
with
n n n
C C V
V C V
V C A
1 2
2 2 1
1 1
symbol variables
0 k
Fall 2006 Costas Buch - RPI 36
Examples:
b B
b bB aA A
cAB S
| |
Greinbach
Normal Form
aa S
abSb S
Not Greinbach
Normal Form
Fall 2006 Costas Buch - RPI 37
aa S
abSb S
Greinbach
Normal Form
Fall 2006 Costas Buch - RPI 38
Observations
Greinbach normal forms are very good
for parsing strings (better than Chomsky Normal Forms)
However, it is difficult to find the
Greinbach normal of a grammar