languages and finite automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · it...

49
1 Context-Free Languages

Upload: others

Post on 07-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

1

Context-Free Languages

Page 2: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

2

Regular Languages

}{ nnba }{ Rww

Page 3: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

3

Regular Languages

}{ nnba }{ Rww

Context-Free Languages

Page 4: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

4

Context-Free Languages

Pushdown

Automata

Context-Free

Grammars

stack

automaton

Page 5: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

5

Context-Free Grammars

Page 6: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

6

Example

A context-free grammar :

S

aSbS

aabbaaSbbaSbS

G

A derivation:

Page 7: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

7

A context-free grammar :

S

aSbS

aaabbbaaaSbbbaaSbbaSbS

G

Another derivation:

Page 8: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

8

S

aSbS

)(GL

(((( ))))

}0:{ nba nn

Page 9: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

9

S

bSbS

aSaS

abbaabSbaaSaS

A context-free grammar :G

A derivation:

Example

Page 10: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

10

S

bSbS

aSaS

abaabaabaSabaabSbaaSaS

A context-free grammar :G

Another derivation:

Page 11: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

11

S

bSbS

aSaS

)(GL }*},{:{ bawwwR

Page 12: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

12

S

SSS

aSbS

ababSaSbSSSS

A context-free grammar :G

A derivation:

Example

Page 13: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

13

S

SSS

aSbS

abababaSbabSaSbSSSS

A context-free grammar :G

A derivation:

Page 14: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

14

S

SSS

aSbS

}prefixanyin

)()( and

),()(:{

v

vnvn

wnwnw

ba

ba

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

)(GL

Page 15: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

15

Definition: Context-Free Grammars

Grammar

Productions of the form:

xA

x is string of variables and terminals

),,,( PSTVG

Variables Terminal

symbols

Start

variable

Page 16: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

16

Definition: Context-Free Languages

A language is context-free

if and only if

there is a grammar with

L

G )(GLL

Page 17: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

17

Derivation Order

ABS .1

A

aaAA

.3

.2

B

BbB

.5

.4

aabaaBbaaBaaABABS54321

Leftmost derivation:

aabaaAbAbABbABS32541

Rightmost derivation:

Page 18: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

18

|AB

bBbA

aABS

Leftmost derivation:

abbbbabbbbB

abbBbbBabAbBabBbBaABS

Rightmost derivation:

abbbbabbBbb

abAbabBbaAaABS

Page 19: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

19

Derivation Trees

Page 20: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

20

ABS

ABS |aaAA |BbB

S

BA

Page 21: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

21

ABS |aaAA |BbB

aaABABS

a a A

S

BA

Page 22: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

22

ABS |aaAA |BbB

aaABbaaABABS

S

BA

a a A B b

Page 23: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

23

ABS |aaAA |BbB

aaBbaaABbaaABABS

S

BA

a a A B b

Page 24: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

24

ABS |aaAA |BbB

aabaaBbaaABbaaABABS

S

BA

a a A B b

Derivation Tree

Page 25: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

25

aabaaBbaaABbaaABABS

yield

aab

baa

S

BA

a a A B b

Derivation Tree

ABS |aaAA |BbB

Page 26: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

26

Partial Derivation Trees

ABS

S

BA

Partial derivation tree

ABS |aaAA |BbB

Page 27: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

27

aaABABS

S

BA

a a A

Partial derivation tree

Page 28: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

28

aaABABS

S

BA

a a A

Partial derivation tree

sentential

form

yield

aaAB

Page 29: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

29

aabaaBbaaBaaABABS

aabaaAbAbABbABS

S

BA

a a A B b

Same derivation tree

Sometimes, derivation order doesn’t matter

Leftmost:

Rightmost:

Page 30: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

30

Ambiguity

Page 31: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

31

aEEEEEE |)(||

aaa

E

EE

EE

a

a a

aaaEaa

EEaEaEEE

*

leftmost derivation

Page 32: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

32

aEEEEEE |)(||

aaa

E

EE

a a

EE a

aaaEaa

EEaEEEEEE

leftmost derivation

Page 33: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

33

aEEEEEE |)(||

aaa

E

EE

a a

EE a

E

EE

EE

a

a a

Two derivation trees

Page 34: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

34

The grammar aEEEEEE |)(||

is ambiguous:

E

EE

a a

EE a

E

EE

EE

a

a a

string aaa has two derivation trees

Page 35: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

35

string aaa has two leftmost derivations

aaaEaa

EEaEEEEEE

aaaEaa

EEaEaEEE

*

The grammar aEEEEEE |)(||

is ambiguous:

Page 36: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

36

Definition:

A context-free grammar is ambiguous

if some string has:

two or more derivation trees

G

)(GLw

Page 37: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

37

In other words:

A context-free grammar is ambiguous

if some string has:

two or more leftmost derivations

G

)(GLw

(or rightmost)

Page 38: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

38

Why do we care about ambiguity?

E

EE

a a

EE a

E

EE

EE

a

a a

aaa

take 2a

Page 39: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

39

E

EE

EE

E

EE

EE

222

2

2 2 2 2

2

Page 40: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

40

E

EE

EE

E

EE

EE

6222

2

2 2 2 2

2

8222

4

2 2

2

6

2 2

24

8

Page 41: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

41

E

EE

EE

6222

2

2 2

4

2 2

2

6

Correct result:

Page 42: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

42

• We want to remove ambiguity

• Ambiguity is bad for programming languages

Page 43: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

43

We fix the ambiguous grammar:

aEEEEEE |)(||

New non-ambiguous grammar:

aF

EF

FT

FTT

TE

TEE

)(

Page 44: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

44

aF

EF

FT

FTT

TE

TEE

)(

aaaFaaFFa

FTaTaTFTTTEE

E

E T

T F

F

a

T

F

a

a

aaa

Page 45: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

45

E

E T

T F

F

a

T

F

a

a

aaa

Unique derivation tree

Page 46: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

46

The grammar :

aF

EF

FT

FTT

TE

TEE

)(

is non-ambiguous:

Every string has

a unique derivation tree

G

)(GLw

Page 47: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

47

Inherent Ambiguity

Some context free languages

have only ambiguous grammars

Example: }{}{ mmnmnn cbacbaL

|

|11

aAbA

AcSS

|

|22

bBcB

BaSS

21 | SSS

Page 48: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

48

The string nnn cba

has two derivation trees

S

1S

S

2S

1S c2Sa

Page 49: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might

It does not, of course, follow from this that L is inherently ambiguous as there might exist some other unambiguous grammars for it. But in some way L1 and L2 have conflicting requirements. A rigorous argument, though, is quite technical. One proof can be found in Harrison 1978.

Linz, 6th, page 149

49