fall 2006costas buch - rpi1 simplifications of context-free grammars

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

Upload: nicholas-dunklin

Post on 31-Mar-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 1

Simplifications of

Context-Free Grammars

Page 2: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 2

A Substitution Rule

bB

aAB

abBcA

aaAA

aBS

Substitute

Equivalentgrammar

aAB

abbcabBcA

aaAA

abaBS

|

|

bB

Page 3: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 3

EquivalentgrammarabaAcabbcabBcA

aaAA

aaAabaBS

||

||

aAB

abbcabBcA

aaAA

abaBS

|

|

Substitute aAB

Page 4: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 4

In general:

1yB

xBzA

Substitute

zxyxBzA 1|equivalentgrammar

1yB

Page 5: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 5

Nullable Variables

:production X

Nullable Variable: Y

Example:

M

aMbM

aMbS

Nullable variable production

Page 6: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 6

M

M

aMbM

aMbSSubstitute

abaMbM

abaMbS

|

|

Removing sproduction

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

sproduction

Page 7: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 7

Unit-Productions

YX Unit Production:

(a single variable in both sides)

bbB

AB

BA

aA

aAS

Example:

Unit Productions

Page 8: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 8

bbB

AB

BA

aA

aAS

SubstituteBA

bbB

BAB

aA

aBaAS

|

|

Removal of unit productions:

Page 9: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 9

Remove

bbB

BAB

aA

aBaAS

|

|

bbB

AB

aA

aBaAS

|

BB

XX can be removed immediately

Unit productions of form

Page 10: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 10

SubstituteAB

bbB

aA

aAaBaAS

||

bbB

AB

aA

aBaAS

|

Page 11: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 11

Remove repeated productions

bbB

aA

aBaAS

|

bbB

aA

aAaBaAS

||

Final grammar

Page 12: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 12

Useless Productions

aAA

AS

S

aSbS

aAaaaaAaAAS

Some derivations never terminate...

Useless Production

Page 13: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 13

bAB

A

aAA

AS

Another grammar:

Not reachable from S

Useless Production

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

Fall 2006 Costas Buch - RPI 15

A production is useless if any of its variables is useless

xA

DC

CB

aAA

AS

S

aSbS

Productions

useless

useless

useless

useless

Variables

useless

useless

useless

Page 16: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 16

Example Grammar:

aCbC

aaB

aA

CAaSS

||

Removing Useless Variables and Productions

Page 17: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 17

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

aCbC

aaB

aA

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

Fall 2006 Costas Buch - RPI 18

Then, remove productions that use variablesother than

aCbC

aaB

aA

CAaSS

||

},,{ SBA

aaB

aA

AaSS

|

Page 19: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 19

Second:Find all variablesreachable from

aaB

aA

AaSS

|

S A B

Use a Dependency Graphwhere nodes are variables

unreachable

S

Page 20: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 20

Keep only the variablesreachable from S

aaB

aA

AaSS

|

aA

AaSS

|

Final Grammar

Contains onlyuseful variables

Page 21: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

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 thatunwanted variables and productionsare removed

Page 22: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 22

Normal Formsfor

Context-free Grammars

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

Fall 2006 Costas Buch - RPI 24

Examples:

bA

SAA

aS

ASS

Not ChomskyNormal Form

aaA

SAA

AASS

ASS

Chomsky Normal Form

Page 25: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 25

Conversion to Chomsky Normal Form

Example:

AcB

aabA

ABaS

Not ChomskyNormal Form

We will convert it to Chomsky Normal Form

Page 26: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 26

AcB

aabA

ABaS

Introduce new variables for the terminals:

cT

bT

aT

ATB

TTTA

ABTS

c

b

a

c

baa

a

cba TTT ,,

Page 27: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 27

Introduce new intermediate variableto break first production:

cT

bT

aT

ATB

TTTA

ABTS

c

b

a

c

baa

a

cT

bT

aT

ATB

TTTA

BTV

AVS

c

b

a

c

baa

a

1

1

1V

Page 28: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 28

Introduce intermediate variable:

cT

bT

aT

ATB

TTV

VTA

BTV

AVS

c

b

a

c

ba

a

a

2

2

1

1

2V

cT

bT

aT

ATB

TTTA

BTV

AVS

c

b

a

c

baa

a

1

1

Page 29: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 29

Final grammar in Chomsky Normal Form:

cT

bT

aT

ATB

TTV

VTA

BTV

AVS

c

b

a

c

ba

a

a

2

2

1

1

AcB

aabA

ABaS

Initial grammar

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

Fall 2006 Costas Buch - RPI 31

The Procedure

First remove:

Nullable variables

Unit productions

(Useless variables optional)

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

Fall 2006 Costas Buch - RPI 33

Replace any production nCCCA 21

with

nnn CCV

VCV

VCA

12

221

11

New intermediate variables: 221 ,,, nVVV

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

Fall 2006 Costas Buch - RPI 36

Examples:

bB

bbBaAA

cABS

||

GreinbachNormal Form

aaS

abSbS

Not GreinbachNormal Form

Page 37: Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

Fall 2006 Costas Buch - RPI 37

aaS

abSbS

Conversion to Greinbach Normal Form:

bT

aT

aTS

STaTS

b

a

a

bb

GreinbachNormal Form

Page 38: Fall 2006Costas Buch - RPI1 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