Download - Simplifications Context-Free Grammars

Transcript
Page 1: Simplifications Context-Free Grammars

Fall 2004 COMP 335 1

Simplifications of

Context-Free Grammars

Page 2: Simplifications Context-Free Grammars

Fall 2004 COMP 335 2

A Substitution Rule

bBaABabBcAaaAAaBS

→→→→→

Substitute

Equivalentgrammar

aABabbcabBcA

aaAAabaBS

→→→→

|

|

bB →

Page 3: Simplifications Context-Free Grammars

Fall 2004 COMP 335 3

A Substitution Rule

EquivalentgrammarabaAcabbcabBcA

aaAAaaAabaBS

||

||

→→→

aABabbcabBcA

aaAAabaBS

→→→→

|

|

Substitute aAB →

Page 4: Simplifications Context-Free Grammars

Fall 2004 COMP 335 4

In general:

1yB

xBzA

Substitute

zxyxBzA 1|→equivalentgrammar

1yB →

Page 5: Simplifications Context-Free Grammars

Fall 2004 COMP 335 5

Nullable Variables

:production−λ λ→A

Nullable Variable: λ⇒⇒KA

Page 6: Simplifications Context-Free Grammars

Fall 2004 COMP 335 6

Removing Nullable Variables

Example Grammar:

λ→→→

MaMbM

aMbS

Nullable variable

Page 7: Simplifications Context-Free Grammars

Fall 2004 COMP 335 7

λ→Mλ→

→→

MaMbM

aMbSSubstitute

abMaMbM

abSaMbS

→→→→

Final Grammar

Page 8: Simplifications Context-Free Grammars

Fall 2004 COMP 335 8

Unit-Productions

BA→Unit Production:

(a single variable in both sides)

Page 9: Simplifications Context-Free Grammars

Fall 2004 COMP 335 9

Removing Unit Productions

Observation:

AA→

Is removed immediately

Page 10: Simplifications Context-Free Grammars

Fall 2004 COMP 335 10

Example Grammar:

bbBABBAaAaAS

→→→→→

Page 11: Simplifications Context-Free Grammars

Fall 2004 COMP 335 11

bbBABBAaAaAS

→→→→→

SubstituteBA→

bbBBAB

aAaBaAS

→→→→

|

|

Page 12: Simplifications Context-Free Grammars

Fall 2004 COMP 335 12

Remove

bbBBAB

aAaBaAS

→→→→

|

|

bbBABaA

aBaAS

→→→→ |

BB →

Page 13: Simplifications Context-Free Grammars

Fall 2004 COMP 335 13

SubstituteAB →

bbBaA

aAaBaAS

→→→ ||

bbBABaA

aBaAS

→→→→ |

Page 14: Simplifications Context-Free Grammars

Fall 2004 COMP 335 14

Remove repeated productions

bbBaA

aBaAS

→→→ |

bbBaA

aAaBaAS

→→→ ||

Final grammar

Page 15: Simplifications Context-Free Grammars

Fall 2004 COMP 335 15

Useless Productions

aAAAS

SaSbS

→→→→

λ

KKK ⇒⇒⇒⇒⇒⇒ aAaaaaAaAAS

Some derivations never terminate...

Useless Production

Page 16: Simplifications Context-Free Grammars

Fall 2004 COMP 335 16

bABA

aAAAS

→→→→

λ

Another grammar:

Not reachable from S

Useless Production

Page 17: Simplifications Context-Free Grammars

Fall 2004 COMP 335 17

In general:

if wxAyS ⇒⇒⇒⇒ KK

then variable is usefulA

otherwise, variable is uselessA

)(GLw∈

contains only terminals

Page 18: Simplifications Context-Free Grammars

Fall 2004 COMP 335 18

A production is useless if any of its variables is useless

xA→

DCCBaAAAS

SaSbS

→→→→→→

λ Productionsuseless

useless

useless

useless

Variablesuseless

useless

useless

Page 19: Simplifications Context-Free Grammars

Fall 2004 COMP 335 19

Removing Useless Productions

Example Grammar:

aCbCaaBaA

CAaSS

→→→→ ||

Page 20: Simplifications Context-Free Grammars

Fall 2004 COMP 335 20

First: find all variables that can producestrings with only terminals

aCbCaaBaA

CAaSS

→→→→ || },{ BA

AS →

},,{ SBA

Round 1:

Round 2:

Page 21: Simplifications Context-Free Grammars

Fall 2004 COMP 335 21

Keep only the variablesthat produce terminal symbols:

aCbCaaBaA

CAaSS

→→→→ ||

},,{ SBA

aaBaA

AaSS

→→→ |

(the rest variables are useless)

Remove useless productions

Page 22: Simplifications Context-Free Grammars

Fall 2004 COMP 335 22

Second: Find all variablesreachable from

aaBaA

AaSS

→→→ |

S A B

Use a Dependency Graph

notreachable

S

Page 23: Simplifications Context-Free Grammars

Fall 2004 COMP 335 23

Keep only the variablesreachable from S

aaBaA

AaSS

→→→ |

aAAaSS

→→ |

Final Grammar

(the rest variables are useless)

Remove useless productions

Page 24: Simplifications Context-Free Grammars

Fall 2004 COMP 335 24

Removing All

Step 1: Remove λ-productions

Step 2: Remove Unit-productions

Step 3: Remove Useless productions

Page 25: Simplifications Context-Free Grammars

Fall 2004 COMP 335 25

Normal Formsfor

Context-free Grammars

Page 26: Simplifications Context-Free Grammars

Fall 2004 COMP 335 26

Chomsky Normal Form

Each productions has form:

BCA→

variable variable

aA→or

terminal

Page 27: Simplifications Context-Free Grammars

Fall 2004 COMP 335 27

Examples:

bASAAaSASS

→→→→

Not ChomskyNormal Form

aaASAAAASSASS

→→→→

Chomsky Normal Form

Page 28: Simplifications Context-Free Grammars

Fall 2004 COMP 335 28

Conversion to Chomsky Normal Form

Example:

AcBaabAABaS

→→→

Not ChomskyNormal Form

Page 29: Simplifications Context-Free Grammars

Fall 2004 COMP 335 29

AcBaabAABaS

→→→

Introduce variables for terminals:

cTbTaTATB

TTTAABTS

c

b

a

c

baa

a

→→→→→→

cba TTT ,,

Page 30: Simplifications Context-Free Grammars

Fall 2004 COMP 335 30

Introduce intermediate variable:

cTbTaTATB

TTTAABTS

c

b

a

c

baa

a

→→→→→→

cTbTaTATB

TTTABTVAVS

c

b

a

c

baa

a

→→→→→→→

1

1

1V

Page 31: Simplifications Context-Free Grammars

Fall 2004 COMP 335 31

Introduce intermediate variable:

cTbTaTATB

TTVVTA

BTVAVS

c

b

a

c

ba

a

a

→→→→→→→→

2

2

1

1

2V

cTbTaTATB

TTTABTVAVS

c

b

a

c

baa

a

→→→→→→→

1

1

Page 32: Simplifications Context-Free Grammars

Fall 2004 COMP 335 32

Final grammar in Chomsky Normal Form:

cTbTaTATB

TTVVTA

BTVAVS

c

b

a

c

ba

a

a

→→→→→→→→

2

2

1

1

AcBaabAABaS

→→→

Initial grammar

Page 33: Simplifications Context-Free Grammars

Fall 2004 COMP 335 33

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 34: Simplifications Context-Free Grammars

Fall 2004 COMP 335 34

The Procedure

First remove:

Nullable variables

Unit productions

Page 35: Simplifications Context-Free Grammars

Fall 2004 COMP 335 35

Then, for every symbol :a

In productions: replace with a aT

Add production aTa →

New variable: aT

Page 36: Simplifications Context-Free Grammars

Fall 2004 COMP 335 36

Replace any production nCCCA L21→

with

nnn CCV

VCVVCA

12

221

11

−− →

→→

K

New intermediate variables: 221 ,,, −nVVV K

Page 37: Simplifications Context-Free Grammars

Fall 2004 COMP 335 37

Theorem: For any context-free grammar(which doesn’t produce )there is an equivalent grammar in Chomsky Normal Form

λ

Page 38: Simplifications Context-Free Grammars

Fall 2004 COMP 335 38

Observations

• Chomsky normal forms are goodfor parsing and proving theorems

• It is very easy to find the Chomsky normalform for any context-free grammar

Page 39: Simplifications Context-Free Grammars

Fall 2004 COMP 335 39

Greibach Normal Form

All productions have form:

kVVVaA L21→

symbol variables

0≥k

Page 40: Simplifications Context-Free Grammars

Fall 2004 COMP 335 40

Examples:

bBbbBaAA

cABS

→→→

||

GreibachNormal Form

aaSabSbS

→→

Not GreibachNormal Form

Page 41: Simplifications Context-Free Grammars

Fall 2004 COMP 335 41

aaSabSbS

→→

Conversion to Greibach Normal Form:

bTaT

aTSSTaTS

b

a

a

bb

→→→→

GreibachNormal Form

Page 42: Simplifications Context-Free Grammars

Fall 2004 COMP 335 42

Theorem: For any context-free grammar(which doesn’t produce ) there is an equivalent grammarin Greibach Normal Form

λ

Page 43: Simplifications Context-Free Grammars

Fall 2004 COMP 335 43

Observations

• Greibach normal forms are very goodfor parsing

• It is hard to find the Greibach normalform of any context-free grammar

Page 44: Simplifications Context-Free Grammars

Fall 2004 COMP 335 44

The CYK Parser

Page 45: Simplifications Context-Free Grammars

Fall 2004 COMP 335 45

The CYK Membership Algorithm

Input:

• Grammar in Chomsky Normal Form G

• String

Output:

find if )(GLw∈

w

Page 46: Simplifications Context-Free Grammars

Fall 2004 COMP 335 46

The Algorithm

• Grammar :G

bBABBaABBAABS

→→→→→

• String : w aabbb

Input example:

Page 47: Simplifications Context-Free Grammars

Fall 2004 COMP 335 47

a a b b b

aa ab bb bb

aab abb bbb

aabb abbb

aabbb

aabbb

Page 48: Simplifications Context-Free Grammars

Fall 2004 COMP 335 48

aA

aA

bB

bB

bB

aa ab bb bb

aab abb bbb

aabb abbb

aabbb

bBABBaABBAABS

→→→→→

Page 49: Simplifications Context-Free Grammars

Fall 2004 COMP 335 49

a A

a A

b B

b B

b B

aa

ab S,B

bb A

bb A

aab abb bbb

aabb abbb

aabbb

bBABBaABBAABS

→→→→→

Page 50: Simplifications Context-Free Grammars

Fall 2004 COMP 335 50

aA

aA

bB

bB

bB

aa abS,B

bbA

bbA

aabS,B

abbA

bbbS,B

aabbA

abbbS,B

aabbbS,B

bBABBaABBAABS

→→→→→

Page 51: Simplifications Context-Free Grammars

Fall 2004 COMP 335 51

Therefore: )(GLaabbb ∈

Time Complexity: 3|| w

The CYK algorithm can be easily converted to a parser(bottom up parser)

Observation: