Top Banner
Fall 2006 Costas Buch - RPI 1 Simplifications of Context-Free Grammars
38

Simplifications of Context-Free Grammars

Mar 19, 2016

Download

Documents

emmy

Simplifications of Context-Free Grammars. A Substitution Rule. Equivalent grammar. Substitute. Substitute. Equivalent grammar. In general:. Substitute. equivalent grammar. Nullable Variables. Nullable Variable:. Example:. Nullable variable. Removing. Substitute. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 1

Simplifications of

Context-Free Grammars

Page 2: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 2

A Substitution Rule

bBaABabBcAaaAAaBS

Substitute

Equivalentgrammar

aABabbcabBcA

aaAAabaBS

|

|

bB

Page 3: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 3

EquivalentgrammarabaAcabbcabBcA

aaAAaaAabaBS

||

||

aABabbcabBcA

aaAAabaBS

|

|

Substitute aAB

Page 4: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 4

In general:

1yB

xBzA

Substitute

zxyxBzA 1|equivalentgrammar

1yB

Page 5: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 5

Nullable Variables:production X

Nullable Variable: Y

Example:

MaMbMaMbS

Nullable variable production

Page 6: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 6

M

MaMbMaMbS

SubstituteabaMbMabaMbS|

|

Removing sproduction

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

sproduction

Page 7: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 7

Unit-ProductionsYX Unit Production:

(a single variable in both sides)

bbBABBAaAaAS

Example:

Unit Productions

Page 8: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 8

bbBABBAaAaAS

SubstituteBA

bbBBAB

aAaBaAS

|

|

Removal of unit productions:

Page 9: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 9

Remove

bbBBAB

aAaBaAS

|

|

bbBABaA

aBaAS

|

BB

XX can be removed immediatelyUnit productions of form

Page 10: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 10

SubstituteAB

bbBaA

aAaBaAS

||

bbBABaA

aBaAS

|

Page 11: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 11

Remove repeated productions

bbBaA

aBaAS

|

bbBaA

aAaBaAS

||

Final grammar

Page 12: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 12

Useless Productions

aAAAS

SaSbS

aAaaaaAaAAS

Some derivations never terminate...

Useless Production

Page 13: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 13

bABA

aAAAS

Another grammar:

Not reachable from S

Useless Production

Page 14: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 14

In general:

If there is a derivation)(GLwxAyS

Then variable is usefulA

Otherwise, variable is uselessA

consists of terminals

Page 15: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 15

A production is useless if any of its variables is useless

xA

DCCBaAAAS

SaSbS

Productionsuselessuselessuselessuseless

Variablesuselessuselessuseless

Page 16: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 16

Example Grammar:

aCbCaaBaA

CAaSS

||

Removing Useless Variables and Productions

Page 17: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 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: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 18

Then, remove productions that use variablesother than

aCbCaaBaA

CAaSS

||

},,{ SBA

aaBaA

AaSS

|

Page 19: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 19

Second:Find all variablesreachable from

aaBaA

AaSS

|

S A B

Use a Dependency Graphwhere nodes are variables

unreachable

S

Page 20: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 20

Keep only the variablesreachable from S

aaBaA

AaSS

|

aAAaSS

|

Final Grammar

Contains onlyuseful variables

Page 21: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 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

Page 22: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 22

Normal Formsfor

Context-free Grammars

Page 23: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 23

Chomsky Normal Form

Each productions has form:

BCA

variable variable

aAor

terminal

Page 24: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 24

Examples:

bASAAaSASS

Not ChomskyNormal Form

aaASAAAASSASS

Chomsky Normal Form

Page 25: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 25

Conversion to Chomsky Normal Form

Example:

AcBaabAABaS

Not ChomskyNormal Form

We will convert it to Chomsky Normal Form

Page 26: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 26

AcBaabAABaS

Introduce new variables for the terminals:

cTbTaTATB

TTTAABTS

c

b

a

c

baa

a

cba TTT ,,

Page 27: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 27

Introduce new intermediate variableto break first production:

cTbTaTATB

TTTAABTS

c

b

a

c

baa

a

cTbTaTATB

TTTABTVAVS

c

b

a

c

baa

a

1

1

1V

Page 28: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 28

Introduce intermediate variable:

cTbTaTATBTTVVTABTVAVS

c

b

a

c

ba

a

a

2

2

1

1

2V

cTbTaTATB

TTTABTVAVS

c

b

a

c

baa

a

1

1

Page 29: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 29

Final grammar in Chomsky Normal Form:

cTbTaTATBTTVVTABTVAVS

c

b

a

c

ba

a

a

2

2

1

1

AcBaabAABaS

Initial grammar

Page 30: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 30

From any context-free grammar(which doesn’t produce )not in Chomsky Normal Form

we can obtain: an equivalent grammar in Chomsky Normal Form

In general:

Page 31: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 31

The Procedure

First remove:

Nullable variables

Unit productions

(Useless variables optional)

Page 32: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 32

Then, for every symbol : a

In productions with length at least 2 replace with a aT

Add production aTa

New variable: aT

Productions of formdo not need to change!

aA

Page 33: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 33

Replace any production nCCCA 21

with

nnn CCV

VCVVCA

12

221

11

New intermediate variables: 221 ,,, nVVV

Page 34: Simplifications  of  Context-Free Grammars

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

Page 35: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 35

Greinbach Normal Form

All productions have form:

kVVVaA 21

symbol variables

0k

Page 36: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 36

Examples:

bBbbBaAA

cABS

||

GreinbachNormal Form

aaSabSbS

Not GreinbachNormal Form

Page 37: Simplifications  of  Context-Free Grammars

Fall 2006 Costas Buch - RPI 37

aaSabSbS

Conversion to Greinbach Normal Form:

bTaTaTSSTaTS

b

a

a

bb

GreinbachNormal Form

Page 38: Simplifications  of  Context-Free Grammars

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