Vous êtes sur la page 1sur 21

Simplifications

of
Context-Free Grammars

Costas Busch - LSU 1


A Substitution Rule

Equivalent
grammar
S  aB
S  aB | ab
A  aaA
Substitute A  aaA
A  abBc B b A  abBc | abbc
B  aA
B  aA
Bb
Costas Busch - LSU 2
S  aB | ab
A  aaA
A  abBc | abbc
B  aA
Substitute
B  aA

S  aB | ab | aaA
Equivalent
A  aaA
A  abBc | abbc | abaAc
grammar
Costas Busch - LSU 3
In general: A  xBz

B  y1

Substitute
B  y1

equivalent
A  xBz | xy1z grammar
Costas Busch - LSU 4
Nullable Variables

  production : X 
Nullable Variable: Y  
Example: S  aMb
M  aMb
M 

Nullable variable   production


Costas Busch - LSU 5
Removing   production s
S  aMb S  aMb | ab
Substitute
M  aMb M  M  aMb | ab
M 

After we remove all the   production s


all the nullable variables disappear
(except for the start variable)

Costas Busch - LSU 6


Unit-Productions
Unit Production: X Y
(a single variable in both sides)

Example: S  aA
Aa
A B
Unit Productions
BA
B  bb
Costas Busch - LSU 7
Removal of unit productions:

S  aA
S  aA | aB
Aa
Substitute Aa
A B A B B  A| B
BA
B  bb
B  bb

Costas Busch - LSU 8


Unit productions of form X X
can be removed immediately

S  aA | aB S  aA | aB
Aa Remove Aa
B  A| B BB BA
B  bb B  bb

Costas Busch - LSU 9


S  aA | aB
S  aA | aB | aA
Aa Substitute
BA Aa
BA
B  bb
B  bb

Costas Busch - LSU 10


Remove repeated productions

Final grammar
S  aA | aB | aA S  aA | aB
Aa Aa
B  bb B  bb

Costas Busch - LSU 11


Useless Productions

S  aSb
S 
SA
A  aA Useless Production

Some derivations never terminate...

S  A  aA  aaA    aaaA  
Costas Busch - LSU 12
Another grammar:

SA
A  aA
A
B  bA Useless Production
Not reachable from S

Costas Busch - LSU 13


In general:

If there is a derivation
S    xAy    w  L(G)
consists of
terminals
Then variable A is useful

Otherwise, variable A is useless

Costas Busch - LSU 14


A production A  x is useless
if any of its variables is useless

S  aSb
S  Productions
Variables S  A useless
useless A  aA useless
useless B  C useless

useless C  D useless
Costas Busch - LSU 15
Removing Useless Variables and Productions

Example Grammar: S  aS | A | C
Aa
B  aa
C  aCb

Costas Busch - LSU 16


First: find all variables that can produce
strings with only terminals or 
(possible useful variables)

S  aS | A | C Round 1: { A, B}
Aa (the right hand side of production
that 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)

This process can be generalized


Costas Busch - LSU 17
Then, remove productions that use variables
other than { A, B, S }

S  aS | A | C
Aa S  aS | A
B  aa Aa
C  aCb B  aa

Costas Busch - LSU 18


Second: Find all variables
reachable from S
Use a Dependency Graph
where nodes are variables

S  aS | A
Aa S A B
B  aa unreachable

Costas Busch - LSU 19


Keep only the variables
reachable from S

Final Grammar
S  aS | A
S  aS | A
Aa
Aa
B  aa
Contains only
useful variables

Costas Busch - LSU 20


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
Costas Busch - LSU 21

Vous aimerez peut-être aussi