You are on page 1of 38

Fall 2006 Costas Buch - RPI 1

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)

(the right hand side of production


that has only terminals)
(the right hand side of a production
has terminals and
variables of previous round)
This process can be generalized
Fall 2006 Costas Buch - RPI 18
Then, remove productions that use variables
other than
aCb C
aa B
a A
C A aS S

| |
} , , { 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

Introduce new variables for the terminals:


c T
b T
a T
AT B
T T T A
ABT S
c
b
a
c
b a a
a

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:

Fall 2006 Costas Buch - RPI 31


The Procedure
First remove:

Nullable variables

Unit productions
(Useless variables optional)
Fall 2006 Costas Buch - RPI 32
Then, for every symbol : a
In productions with length at least 2
replace with a
a
T
Add production a T
a

New variable:
a
T
Productions of form
do not need to change!
a A
Fall 2006 Costas Buch - RPI 33
Replace any production
n
C C C A
2 1

with
n n n
C C V
V C V
V C A
1 2
2 2 1
1 1

New intermediate variables:


2 2 1
, , ,
n
V V V
Fall 2006 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
Fall 2006 Costas Buch - RPI 35
Greinbach Normal Form
All productions have form:
k
V V V a A
2 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

Conversion to Greinbach Normal Form:


b T
a T
aT S
ST aT S
b
a
a
b b

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

You might also like