1 normal forms for context-free grammars. 2 chomsky normal form all productions have form: variable...

70
1 Normal Forms for Context-free Grammars

Post on 20-Dec-2015

228 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

1

Normal Formsfor

Context-free Grammars

Page 2: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

2

Chomsky Normal Form

All productions have form:

BCA

variable variable

aAand

terminal

Page 3: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

3

Examples:

bA

SAA

aS

ASS

Not ChomskyNormal Form

aaA

SAA

AASS

ASS

Chomsky Normal Form

Page 4: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

4

Convertion to Chomsky Normal Form

Example:

AcB

aabA

ABaS

Not ChomskyNormal Form

Page 5: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

5

AcB

aabA

ABaS

Introduce variables for terminals:

cT

bT

aT

ATB

TTTA

ABTS

c

b

a

c

baa

a

cba TTT ,,

Page 6: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

6

Introduce intermediate variable:

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 7: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

7

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 8: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

8

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 9: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

9

From any context-free grammarnot in Chomsky Normal Form

we can obtain: An equivalent grammar in Chomsky Normal Form

In general:

Page 10: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

10

The Procedure

First remove:

Nullable variables

Unit productions

Page 11: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

11

For every symbol : a

In productions: replace with a aT

Add production aTa

New variable: aT

Page 12: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

12

Replace any production nCCCA 21

with

nnn CCV

VCV

VCA

12

221

11

New intermediate variables: 221 ,,, nVVV

Page 13: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

13

Theorem:For any context-free grammar there is an equivalent grammar in Chomsky Normal Form

Page 14: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

14

Observations

• Chomsky normal forms are good for parsing and proving theorems

• It is very easy to find the Chomsky normal form of any context-free grammar

Page 15: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

15

Greinbach Normal Form

All productions have form:

kVVVaA 21

symbol variables

0k

Page 16: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

16

Examples:

bB

bbBaAA

cABS

||

GreinbachNormal Form

aaS

abSbS

Not GreinbachNormal Form

Page 17: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

17

aaS

abSbS

Conversion to Greinbach Normal Form:

bT

aT

aTS

STaTS

b

a

a

bb

GreinbachNormal Form

Page 18: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

18

Theorem:For any context-free grammarthere is an equivalent grammarin Greinbach Normal Form

Page 19: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

19

Observations

• Greinbach normal forms are very good for parsing

• It is hard to find the Greinbach normal form of any context-free grammar

Page 20: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

20

An Applicationof

Chomsky Normal Forms

Page 21: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

21

The CYK Membership Algorithm

Input:

• Grammar in Chomsky Normal Form G

• String

Output:

find if )(GLw

w

Page 22: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

22

The Algorithm

• Grammar :G

bB

ABB

aA

BBA

ABS

• String : w aabbb

Input example:

Page 23: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

23

a a b b b

aa ab bb bb

aab abb bbb

aabb abbb

aabbb

aabbb

Page 24: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

24

aA

aA

bB

bB

bB

aa ab bb bb

aab abb bbb

aabb abbb

aabbb

bB

ABB

aA

BBA

ABS

Page 25: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

25

aA

aA

bB

bB

bB

aa abS,B

bbA

bbA

aab abb bbb

aabb abbb

aabbb

bB

ABB

aA

BBA

ABS

Page 26: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

26

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 27: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

27

Therefore: )(GLaabbb

Time Complexity:3||w

The CYK algorithm can be easily converted to a parser

Observation:

Page 28: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

28

Pushdown AutomataPDAs

Page 29: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

29

Pushdown Automaton -- PDA

Input String

Stack

States

Page 30: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

30

Initial Stack Symbol

Stack

$

Stack

z

bottomspecial symbol

Page 31: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

31

The States

q1 q2a, b c

Inputsymbol

Popsymbol

Pushsymbol

Page 32: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

32

q1 q2a, b c

a

b top

input

stack

a

Replaceeh

$eh

$

c

Page 33: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

33

q1 q2a, c

a a

Pushb

eh

$eh

$

bc

top

input

stack

Page 34: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

34

q1 q2a, b

a a

Popb

eh

$eh

$

top

input

stack

Page 35: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

35

q1 q2a,

a a

No Changeb

eh

$eh

$

btop

input

stack

Page 36: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

36

Non-Determinism

q1

q2a, b c

q3a, b c

q1 q2, b c

transition

These are allowed transitions in a Non-deterministic PDA (NPDA)

Page 37: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

37

NPDA: Non-Deterministic PDA

Example:

,

a, a

b, a q0 q1 q2 q3

b, a

, $ $

Page 38: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

38

a, a

b, a 0q q1 q2 q3

Execution Example:

Input

a a a b b b

currentstate

b, a

Time 0

, , $ $

Stack

$

Page 39: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

39

a, a

b, a q0 q1 q2 q3

Input

a a a b b b

b, a

Time 1

, , $ $

Stack

$

Page 40: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

40

a, a

b, a q0 q1 q2 q3

Input

Stack

a a a b b b

$

a

b, a

Time 2

, , $ $

Page 41: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

41

a, a

b, a q0 q1 q2 q3

Input

Stack

a a a b b b$

aa

b, a

Time 3

, , $ $

Page 42: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

42

a, a

b, a q0 q1 q2 q3

Input

Stack

a a a b b b

$

aaa

b, a

Time 4

, , $ $

Page 43: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

43

a, a

b, a q0 q1 q2 q3

Input

a a a b b b

Stack

$

aaa

b, a

Time 5

, , $ $

Page 44: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

44

a, a

b, a q0 q1 q2 q3

Input

a a a b b b$

a

Stack

b, a

Time 6

, , $ $

a

Page 45: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

45

a, a

b, a q0 q1 q2 q3

Input

a a a b b b$

Stack

b, a

Time 7

, , $ $

a

Page 46: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

46

a, a

b, a q0 q1 q2 q3

Input

a a a b b b

b, a

Time 8

accept, , $ $

$

Stack

Page 47: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

47

A string is accepted if there is a computation such that:

• All the input is consumed

• The last state is a final state

At the end of the computation,we do not care about the stack contents

Page 48: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

48

The input stringis accepted by the NPDA:

aaabbb

a, a

b, a q0 q1 q2 q3

b, a

, , $ $

Page 49: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

49

}0:{ nbaL nn

is the language accepted by the NPDA:

a, a

b, a q0 q1 q2 q3

b, a

In general,

, , $ $

Page 50: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

50

Another NPDA example

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

NPDA M

}{)( RwwML

Page 51: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

51

Execution Example:

Input

Time 0

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

a ab b

Page 52: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

52

Input

a ab

Time 1

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

ab

Page 53: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

53

Input

Time 2

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

aa ab b

b

Page 54: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

54

Input

Time 3

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

aa ab b

b

Guess the middle of string

Page 55: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

55

Input

Time 4

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

aa ab b

b

Page 56: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

56

Input

Time 5

Stack

$

, $ $1q q2

bb

aa

,

,

, q0

bb

aa

,

,

a ab b a

Page 57: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

57

Input

Time 6

Stack

$

, $ $q1

bb

aa

,

,

, q0

bb

aa

,

,

a ab b

accept

q2

Page 58: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

58

Rejection Example:

Input

Time 0

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

a b b b

Page 59: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

59

Input

Time 1

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

aa b b b

Page 60: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

60

Input

Time 2

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

ab

a b b b

Page 61: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

61

Input

Time 3

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

ab

Guess the middle of string

a b b b

Page 62: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

62

Input

Time 4

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

ab

a b b b

Page 63: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

63

Input

Time 5

Stack

$

, $ $1q q2

bb

aa

,

,

, q0

bb

aa

,

,

aa b b b

There is no possible transition.

Input is not consumed

Page 64: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

64

Another computation on same string:

Input Time 0

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

a b b b

Page 65: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

65

Input

Time 1

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

aa b b b

Page 66: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

66

Input

Time 2

Stack

$

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

ab

a b b b

Page 67: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

67

Input

Time 3

Stack

$ab

a b b b

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

b

Page 68: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

68

Input

Time 4

Stack

a b b b

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

$abbb

Page 69: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

69

Input

Time 5

Stack

a b b b

$abbb

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

No final state is reached

Page 70: 1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal

70

, $ $q1 q2

bb

aa

,

,

, q0

bb

aa

,

,

There is no computation that accepts string abbb

)(MLabbb