context free grammars_simplification

21
Costas Busch - LSU 1 Simplifications of Context-Free Grammars

Transcript of context free grammars_simplification

Page 1: context free grammars_simplification

Costas Busch - LSU 1

Simplifications of

Context-Free Grammars

Page 2: context free grammars_simplification

Costas Busch - LSU 2

A Substitution Rule

bBaABabBcAaaAAaBS

Substitute

Equivalentgrammar

aABabbcabBcA

aaAAabaBS

|

|

bB

Page 3: context free grammars_simplification

Costas Busch - LSU 3

EquivalentgrammarabaAcabbcabBcA

aaAAaaAabaBS

||

||

aABabbcabBcA

aaAAabaBS

|

|

Substitute aAB

Page 4: context free grammars_simplification

Costas Busch - LSU 4

In general:

1yB

xBzA

Substitute

zxyxBzA 1|equivalentgrammar

1yB

Page 5: context free grammars_simplification

Costas Busch - LSU 5

Nullable Variables:production X

Nullable Variable: Y

Example:

MaMbMaMbS

Nullable variable production

Page 6: context free grammars_simplification

Costas Busch - LSU 6

M

MaMbMaMbS

SubstituteabaMbMabaMbS|

|

Removing sproduction

After we remove all the all the nullable variables disappear(except for the start variable)

sproduction

Page 7: context free grammars_simplification

Costas Busch - LSU 7

Unit-ProductionsYX Unit Production:

(a single variable in both sides)

bbBABBAaAaAS

Example:

Unit Productions

Page 8: context free grammars_simplification

Costas Busch - LSU 8

bbBABBAaAaAS

SubstituteBA

bbBBAB

aAaBaAS

|

|

Removal of unit productions:

Page 9: context free grammars_simplification

Costas Busch - LSU 9

Remove

bbBBAB

aAaBaAS

|

|

bbBABaA

aBaAS

|

BB

XX can be removed immediatelyUnit productions of form

Page 10: context free grammars_simplification

Costas Busch - LSU 10

SubstituteAB

bbBaA

aAaBaAS

||

bbBABaA

aBaAS

|

Page 11: context free grammars_simplification

Costas Busch - LSU 11

Remove repeated productions

bbBaA

aBaAS

|

bbBaA

aAaBaAS

||

Final grammar

Page 12: context free grammars_simplification

Costas Busch - LSU 12

Useless Productions

aAAAS

SaSbS

aAaaaaAaAAS

Some derivations never terminate...

Useless Production

Page 13: context free grammars_simplification

Costas Busch - LSU 13

bABA

aAAAS

Another grammar:

Not reachable from S

Useless Production

Page 14: context free grammars_simplification

Costas Busch - LSU 14

In general:

If there is a derivation)(GLwxAyS

Then variable is usefulA

Otherwise, variable is uselessA

consists of terminals

Page 15: context free grammars_simplification

Costas Busch - LSU 15

A production is useless if any of its variables is useless

xA

DCCBaAAAS

SaSbS

Productionsuselessuselessuselessuseless

Variablesuselessuselessuseless

Page 16: context free grammars_simplification

Costas Busch - LSU 16

Example Grammar:

aCbCaaBaA

CAaSS

||

Removing Useless Variables and Productions

Page 17: context free grammars_simplification

Costas Busch - LSU 17

First:find all variables that can producestrings with only terminals or

aCbCaaBaA

CAaSS

|| },{ BA

},,{ SBA

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

Page 18: context free grammars_simplification

Costas Busch - LSU 18

Then, remove productions that use variablesother than

aCbCaaBaA

CAaSS

||

},,{ SBA

aaBaA

AaSS

|

Page 19: context free grammars_simplification

Costas Busch - LSU 19

Second:Find all variablesreachable from

aaBaA

AaSS

|

S A B

Use a Dependency Graphwhere nodes are variables

unreachable

S

Page 20: context free grammars_simplification

Costas Busch - LSU 20

Keep only the variablesreachable from S

aaBaA

AaSS

|

aAAaSS

|

Final Grammar

Contains onlyuseful variables

Page 21: context free grammars_simplification

Costas Busch - LSU 21

Removing AllStep 1: Remove Nullable Variables

Step 2: Remove Unit-Productions

Step 3: Remove Useless Variables

This sequence guarantees thatunwanted variables and productionsare removed