Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI...

82
Context-Free Languages Hinrich Schütze CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch

Transcript of Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI...

Page 1: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

Context-Free Languages Hinrich Schütze

CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400

Thanks to Costas Busch

Page 2: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

Take-away Definition context-free grammar Definition context-free language Derivation, sentential form, sentence Derivation trees Ambiguity Context-free grammars for natural language

2

Page 3: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

Terminology For our purposes in this class: Context free grammar = Constituency grammar = Phrase structure grammar

3

Page 4: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

4

Grammars Grammars express languages Example: the English language

verbpredicate

nounarticlephrasenoun

predicatephrasenounsentence

_

_

Page 5: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

5

walksverb

runsverb

dognoun

catnoun

thearticle

aarticle

Page 6: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

6

A derivation of “the dog walks”:

walksdogtheverbdogthe

verbnounthe

verbnounarticle

verbphrasenoun

predicatephrasenounsentence

__

Page 7: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

7

A derivation of “a cat runs”:

runscataverbcata

verbnouna

verbnounarticle

verbphrasenoun

predicatephrasenounsentence

__

Page 8: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

8

Language of the grammar:

L = { “a cat runs”, “a cat walks”, “the cat runs”, “the cat walks”, “a dog runs”, “a dog walks”, “the dog runs”, “the dog walks” }

Page 9: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

9

Notation

dognoun

catnoun

Variable Terminal

Production Rules

Page 10: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

10

Another Example Grammar: Derivation of sentence :

λ→

SaSbS

abaSbS ⇒⇒

ab

aSbS→ λ→S

Page 11: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

11

Language?

Page 12: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

12

aabbaaSbbaSbS ⇒⇒⇒

aSbS→ λ→S

aabb

λ→

SaSbSGrammar:

Derivation of sentence :

Page 13: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

13

Other derivations:

aaabbbaaaSbbbaaSbbaSbS ⇒⇒⇒⇒

aaaabbbbaaaaSbbbbaaaSbbbaaSbbaSbS

⇒⇒

⇒⇒⇒

Page 14: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

14

Language of the grammar

λ→

SaSbS

}0:{ ≥= nbaL nn

Page 15: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

15

More Notation Grammar ( )PSTVG ,,,=

:V

:T

:S

:P

Set of variables

Set of terminal symbols

Start variable

Set of Production rules

Page 16: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

16

Example Grammar :

λ→

SaSbSG

( )PSTVG ,,,=

}{SV = },{ baT =

},{ λ→→= SaSbSP

Page 17: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

17

More Notation Sentential Form: A sentence that contains variables and terminals Example: aaabbbaaaSbbbaaSbbaSbS ⇒⇒⇒⇒

Sentential Forms sentence

Page 18: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

18

We write: Instead of:

aaabbbS*⇒

aaabbbaaaSbbbaaSbbaSbS ⇒⇒⇒⇒

Page 19: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

19

In general we write: If:

nww*

1 ⇒

nwwww ⇒⇒⇒⇒ 321

Page 20: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

20

By default:

ww*⇒

Page 21: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

21

Example

λ→

SaSbS

aaabbbS

aabbS

abS

S

*

*

*

*

⇒λ

Grammar Derivations

Page 22: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

22

baaaaaSbbbbaaSbb

aaSbbS

⇒λ→

SaSbS

Grammar

Example

Derivations

Page 23: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

23

Another Grammar Example Grammar :

λ→

AaAbAAbSG

Page 24: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

24

Language?

Page 25: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

25

Grammar :

λ→

AaAbAAbS

Derivations:

S→ Ab→ bS→ Ab→ aAbb→ abbS→ Ab→ aAbb→ aaAbbb→ aabbb

G

Page 26: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

26

More Derivations

aaaabbbbbaaaaAbbbbbaaaAbbbbaaAbbbaAbbAbS

⇒⇒

⇒⇒⇒⇒

bbaS

bbbaaaaaabbbbS

aaaabbbbbS

nn∗

Page 27: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

27

Language of a Grammar For a grammar with start variable :

GS

}:{)( wSwGL∗⇒=

String of terminals

Page 28: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

28

Example For grammar :

λ→

AaAbAAbS

}0:{)( ≥= nbbaGL nn

Since: bbaS nn∗⇒

G

Page 29: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

29

A Convenient Notation

λ→

AaAbA

λ|aAbA→

thearticleaarticle

→ theaarticle |→

Page 30: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

30

Revisit first grammar A context-free grammar :

λ→

SaSbS

aabbaaSbbaSbS ⇒⇒⇒

G

A derivation:

Page 31: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

31

A context-free grammar :

λ→

SaSbS

aaabbbaaaSbbbaaSbbaSbS ⇒⇒⇒⇒

G

Another derivation:

Page 32: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

32

λ→

SaSbS

=)(GL

(((( ))))

}0:{ ≥nba nn

Describes parentheses:

Page 33: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

33

λ→

SbSbSaSaSA context-free grammar : G

Example

Page 34: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

34

Language?

Page 35: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

35

λ→

SbSbSaSaS

abaabaabaSabaabSbaaSaS ⇒⇒⇒⇒

A context-free grammar : G

Another derivation:

Page 36: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

36

λ→

SbSbSaSaS

=)(GL }*},{:{ bawwwR ∈

Page 37: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

37

λ→

SSSSaSbSA context-free grammar : G

Example

Page 38: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

38

Language?

Page 39: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

39

λ→

SSSSaSbS

abababaSbabSaSbSSSS ⇒⇒⇒⇒⇒

A context-free grammar : G

Two derivations:

ababSaSbSSSS ⇒⇒⇒⇒

Page 40: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

40

λ→

SSSSaSbS

}prefixanyin )()( and

),()(:{

vvnvn

wnwnw

ba

ba≥

==)(GL

Interpretation?

Page 41: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

41

λ→

SSSSaSbS

}prefixanyin )()( and

),()(:{

vvnvn

wnwnw

ba

ba≥

=

() ((( ))) (( ))

=)(GL

Describes matched parentheses:

Page 42: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

42

Definition: Context-Free Grammars

Grammar

Productions of the form: xA→String of variables and terminals

),,,( PSTVG =

Variables Terminal symbols

Start variable

Variable

Page 43: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

43

*},:{)(*

TwwSwGL ∈⇒=

),,,( PSTVG =

Page 44: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

44

Definition: Context-Free Languages A language is context-free if and only if there is a context-free grammar with

L

G)(GLL =

Page 45: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

45

Derivation Order ABS→.1

λ→

AaaAA

.3

.2λ→

BBbB

.5

.4

aabaaBbaaBaaABABS54321⇒⇒⇒⇒⇒

Leftmost derivation:

aabaaAbAbABbABS32541⇒⇒⇒⇒⇒

Rightmost derivation:

Page 46: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

46

Language?

Page 47: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

47

λ|ABbBbAaABS

Leftmost derivation:

abbbbabbbbBabbBbbBabAbBabBbBaABS

⇒⇒

⇒⇒⇒⇒

Rightmost derivation:

abbbbabbBbbabAbabBbaAaABS

⇒⇒

⇒⇒⇒⇒

Page 48: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

48

Language?

Page 49: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

49

Derivation Trees

Page 50: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

50

ABS⇒

ABS→ λ|aaAA→ λ|BbB→

S

BA

Page 51: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

51

ABS→ λ|aaAA→ λ|BbB→

aaABABS ⇒⇒

a a A

S

BA

Page 52: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

52

ABS→ λ|aaAA→ λ|BbB→

aaABbaaABABS ⇒⇒⇒S

BA

a a A B b

Page 53: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

53

ABS→ λ|aaAA→ λ|BbB→

aaBbaaABbaaABABS ⇒⇒⇒⇒S

BA

a a A B b

λ

Page 54: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

54

ABS→ λ|aaAA→ λ|BbB→

aabaaBbaaABbaaABABS ⇒⇒⇒⇒⇒S

BA

a a A B b

λ λ

Derivation Tree

Page 55: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

55

aabaaBbaaABbaaABABS ⇒⇒⇒⇒⇒

yield

aabbaa

=

λλ

S

BA

a a A B b

λ λ

Derivation Tree

ABS→ λ|aaAA→ λ|BbB→

Page 56: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

56

Partial Derivation Trees

ABS⇒

S

BA

Partial derivation tree

ABS→ λ|aaAA→ λ|BbB→

Page 57: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

57

aaABABS ⇒⇒

S

BA

a a A

Partial derivation tree

Page 58: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

58

aaABABS ⇒⇒

S

BA

a a A

Partial derivation tree

sentential form

yield aaAB

Page 59: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

59

aabaaBbaaBaaABABS ⇒⇒⇒⇒⇒

aabaaAbAbABbABS ⇒⇒⇒⇒⇒S

BA

a a A B b

λ λ

Same derivation tree

Sometimes, derivation order doesn’t matter Leftmost:

Rightmost:

Page 60: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

60

Ambiguity

Page 61: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

61

aEEEEEE |)(|| ∗+→

aaa ∗+

E

EE

EE

+

a

a a

aaaEaaEEaEaEEE*+⇒∗+⇒

∗+⇒+⇒+⇒

leftmost derivation

Page 62: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

62

aEEEEEE |)(|| ∗+→

aaa ∗+

E

EE

+

a a

EE a

aaaEaaEEaEEEEEE

∗+⇒∗+⇒

∗+⇒∗+⇒∗⇒

leftmost derivation

Page 63: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

63

aEEEEEE |)(|| ∗+→

aaa ∗+

E

EE

+

a a

EE a

E

EE

EE

+

a

a a

Two derivation trees

Page 64: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

64

The grammar aEEEEEE |)(|| ∗+→

is ambiguous:

E

EE

+

a a

EE a

E

EE

EE

+

a

a a

string aaa ∗+ has two derivation trees

Page 65: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

65

string aaa ∗+ has two leftmost derivations

aaaEaaEEaEEEEEE

∗+⇒∗+⇒

∗+⇒∗+⇒∗⇒

aaaEaaEEaEaEEE*+⇒∗+⇒

∗+⇒+⇒+⇒

The grammar aEEEEEE |)(|| ∗+→

is ambiguous:

Page 66: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

66

Definition:

A context-free grammar is ambiguous if some string has: two or more derivation trees

G

)(GLw∈

Page 67: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

67

In other words:

A context-free grammar is ambiguous if some string has: two or more leftmost derivations

G

)(GLw∈

(or rightmost)

Page 68: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

68

Why do we care about ambiguity?

E

EE

+

a a

EE a

E

EE

EE

+

a

a a

aaa ∗+

take 2=a

Page 69: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

69

E

EE

+

EE

E

EE

EE

+

222 ∗+

2

2 2 2 2

2

Page 70: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

70

E

EE

+

EE

E

EE

EE

+

6222 =∗+

2

2 2 2 2

2

8222 =∗+

4

2 2

2

6

2 2

24

8

Page 71: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

71

E

EE

EE

+

6222 =∗+

2

2 2

4

2 2

2

6

Correct result:

Page 72: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

72

•  We want to remove ambiguity

•  Ambiguity is bad for programming languages

Page 73: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

73

We fix the ambiguous grammar:

aEEEEEE |)(|| ∗+→

New non-ambiguous grammar:

aFEFFTFTT

TETEE

∗→

+→

)(

Page 74: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

74

aFEFFTFTT

TETEE

∗→

+→

)(

aaaFaaFFaFTaTaTFTTTEE

∗+⇒∗+⇒∗+⇒

∗+⇒+⇒+⇒+⇒+⇒

E

E T+

T ∗ F

F

a

T

F

a

a

aaa ∗+

Page 75: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

75

E

E T+

T ∗ F

F

a

T

F

a

a

aaa ∗+

Unique derivation tree

Page 76: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

76

The grammar :

aFEFFTFTT

TETEE

∗→

+→

)(

is non-ambiguous:

Every string has a unique derivation tree

G

)(GLw∈

Page 77: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

77

Another Ambiguous Grammar

IF_STMT if EXPR then STMT →| if EXPR then STMT else STMT

Ambiguity?

Page 78: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

78

If expr1 then if expr2 then stmt1 else stmt2

IF_STMT

expr1 then

else if expr2 then

STMT

stmt1

if

IF_STMT

expr1 then else

if expr2 then

STMT stmt2 if

stmt1

stmt2

Page 79: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

79

Inherent Ambiguity Some context free languages have only ambiguous grammars

Example: }{}{ mmnmnn cbacbaL ∪=

λ||11

aAbAAcSS

λ||22

bBcBBaSS

→21 | SSS→

Page 80: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

80

The string nnn cba

has two derivation trees

S

1S

S

2S

1S c 2Sa

Page 81: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

81

Ambiguity in natural language?

Page 82: Context-Free Languageshs/teach/13w/intro/pdf/08syn.pdf · CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch . Take-away Definition context-free grammar Definition

Take-away Definition context-free grammar Definition context-free language Derivation, sentential form, sentence Derivation trees Ambiguity Context-free grammars for natural language

82