49
1 Simplifications of Context-Free Grammars

1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

  • View
    236

  • Download
    2

Embed Size (px)

Citation preview

Page 1: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

1

Simplifications of

Context-Free Grammars

Page 2: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

2

A Substitution Rule

substitute B

bB

abbAB

abBcA

aaAA

aA

abbcA

ababbAcA

aaAA

aA

equivalentgrammar

Page 3: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

3

In general:

321 ||| yyyB

xBzA

zxyzxyzxyA n||| 21

substitute B

equivalentgrammar

Page 4: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

4

Useless Productions

aAA

AS

S

aSbS

Page 5: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

5

aAA

AS

S

aSbS

aAaaaaAaAAS

never terminates...

Page 6: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

6

aAA

AS

S

aSbS

Useless Production

Page 7: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

7

bAB

A

aAA

AS

Another grammar:

Page 8: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

8

bAB

A

aAA

AS

Not reachable from S

Useless Production

Page 9: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

9

In general:

If wxAyS

Then A

is uselessOtherwise A

is useful

)(GLw

Page 10: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

10

Removing Useless Productions

Example Grammar

aCbC

aaB

aA

CAaSS

||

Page 11: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

11

First: find all variables that producestrings with only terminals

aCbC

aaB

aA

CAaSS

|| },{ BA

},,{ SBA

Round 1:

Round 2:

Page 12: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

12

Keep only the variablesthat produce terminal symbols

aCbC

aaB

aA

CAaSS

||

},,{ SBA

aaB

aA

AaSS

|

Page 13: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

13

Second: Find all variablesreachable from S

aaB

aA

AaSS

|

S A B

Dependency Graph

notreachable

Page 14: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

14

Keep only the variablesreachable from S

aaB

aA

AaSS

|

aA

AaSS

|

Final Grammar

Page 15: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

15

Nullable Variables

:production A

Nullable Variable: A

Page 16: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

16

Removing Nullable Variables

Example Grammar:

M

aMbM

aMbS

Nullable variable

Page 17: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

17

Substitute

M

M

aMbM

aMbS

abM

aMbM

abS

aMbS

Final Grammar

Page 18: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

18

Unit-Productions

BAUnit Production:

Page 19: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

19

Removing Unit Productions

Observation:

AA

Is removed immediately

Page 20: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

20

Example Grammar:

bbB

AB

BA

aA

aAS

Page 21: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

21

bbB

AB

BA

aA

aAS

substitute

BA

bbB

BAB

aA

aBaAS

|

|

Page 22: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

22

bbB

BAB

aA

aBaAS

|

|

bbB

AB

aA

aBaAS

|

Page 23: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

23

substitute

AB

bbB

aA

aAaBaAS

||

bbB

AB

aA

aBaAS

|

Page 24: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

24

Remove repeated productions

bbB

aA

aBaAS

|

bbB

aA

aAaBaAS

||

Final grammar

Page 25: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

25

Removing All

1. Remove Nullable Variables

2. Remove Unit-Productions

3. Remove Useless Variables

Page 26: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

26

Normal Formsfor

Context-free Grammars

Page 27: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

27

Chomsky Normal Form

All productions have form:

BCA

variable variable

aAOR

terminal

Page 28: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

28

Examples:

bA

SAA

aS

ASS

ChomskyNormal Form

aaA

SAA

AASS

ASS

NOTChomskyNormal Form

Page 29: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

29

Convertion to Chomsky Normal Form

Example:

AcB

aabA

ABaS

ChomskyNormal Form

NOT

Page 30: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

30

AcB

aabA

ABaS

Introduce variables for terminals:

cT

bT

aT

ATB

TTTA

ABTS

c

b

a

c

baa

a

cba TTT ,,

Page 31: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

31

Introduce intermediate variables:

cT

bT

aT

ATB

TTTA

ABTS

c

b

a

c

baa

a

cT

bT

aT

ATB

TTV

VTA

BTV

AVS

c

b

a

c

ba

a

a

2

2

1

1

21, VV

Page 32: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

32

Final grammar inChomsky 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 33: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

33

From any context-free grammar Not in Chomsky Normal Form

We can obtain: An equivalent grammar in Chomsky Normal Form

In general:

Page 34: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

34

The Procedure:

Removenullable variablesunit productions

Page 35: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

35

For every symbol :

aT

a

Add new variable

In productions, replace with a aT

Add production aTa

Page 36: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

36

Replace any production: nCCCA 21

with:

nnn CCV

VCV

VCA

12

221

11

New variables: 221 ,,, nVVV

Page 37: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

37

Theorem:

For any context-free grammarthere is an equivalent grammarin Chomsky Normal Form

Page 38: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

38

Greinbach Normal Form

All rules have form:

kVVVaA 21

symbol variables

0k

Page 39: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

39

Examples:

bB

bbBaAA

cABS

||

GreinbachNormal Form

aaS

abSbS

GreinbachNormal Form

NOT

Page 40: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

40

aaS

abSbS

Conversion to Greinbach Normal Form

bT

aT

aTS

STaTS

b

a

a

bb

GreinbachNormal Form

Page 41: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

41

Theorem:

For any context-free grammar

there is an equivalent grammarin Greinbach Normal Form

Page 42: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

42

An Applicationof

Chomsky Normal Forms

Page 43: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

43

The CYK Membership Algorithm

Inputs: • Grammar in Chomsky Normal Form

G

• String w

Output:

find if )(GLw

Page 44: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

44

Algorithm: Input Examples

Grammar :G

bB

ABB

aA

BBA

ABS

String : w aabbb

Page 45: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

45

a a b b b

aa ab bb bb

aab abb bbb

aabb abbb

aabbb

aabbb

Page 46: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

46

aA

aA

bB

bB

bB

aa ab bb bb

aab abb bbb

aabb abbb

aabbb

bB

ABB

aA

BBA

ABS

Page 47: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

47

aA

aA

bB

bB

bB

aa abS,B

bbA

bbA

aab abb bbb

aabb abbb

aabbb

bB

ABB

aA

BBA

ABS

Page 48: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

48

aA

aA

bB

bB

bB

aa abS,B

bbA

bbA

aabS,B

abbA

bbbS,B

aabbA

abbbS,B

aabbbS,B

bB

ABB

aA

BBA

ABS

Page 49: 1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar

49

Therefore: )(GLaabbb

Time Complexity:3||w

It can be easily converted to a parser