context-free languages

74
1 Context-Free Languages

Upload: jennifer-snyder

Post on 31-Dec-2015

61 views

Category:

Documents


0 download

DESCRIPTION

Context-Free Languages. Regular Languages. Context-Free Languages. Regular Languages. stack. automaton. Context-Free Languages. Context-Free Grammars. Pushdown Automata. Context-Free Grammars. Grammars. Grammars express languages Example: the English language. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Context-Free Languages

1

Context-Free Languages

Page 2: Context-Free Languages

2

Regular Languages

}0:{ nba nn }{ Rww

**ba *)( ba

Page 3: Context-Free Languages

3

Regular Languages

}{ nnba }{ Rww

Context-Free Languages

Page 4: Context-Free Languages

4

Context-Free Languages

PushdownAutomata

Context-FreeGrammars

stack

automaton

Page 5: Context-Free Languages

5

Context-Free Grammars

Page 6: Context-Free Languages

6

GrammarsGrammars express languages

Example: the English language

verbpredicate

nounarticlephrasenoun

predicatephrasenounsentence

_

_

Page 7: Context-Free Languages

7

walksverb

runsverb

dognoun

catnoun

thearticle

aarticle

Page 8: Context-Free Languages

8

A derivation of “the dog walks”:

walksdogthe

verbdogthe

verbnounthe

verbnounarticle

verbphrasenoun

predicatephrasenounsentence

_

_

Page 9: Context-Free Languages

9

A derivation of “a cat runs”:

runscata

verbcata

verbnouna

verbnounarticle

verbphrasenoun

predicatephrasenounsentence

_

_

Page 10: Context-Free Languages

10

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 11: Context-Free Languages

11

Notation

dognoun

catnoun

Variable Terminal

Production Rules

Page 12: Context-Free Languages

12

Another ExampleGrammar:

Derivation of sentence :

S

aSbS

abaSbS

ab

aSbS S

Page 13: Context-Free Languages

13

aabbaaSbbaSbS

aSbS S

aabb

S

aSbSGrammar:

Derivation of sentence :

Page 14: Context-Free Languages

14

Other derivations:

aaabbbaaaSbbbaaSbbaSbS

aaaabbbbaaaaSbbbb

aaaSbbbaaSbbaSbS

Page 15: Context-Free Languages

15

Language of the grammar

S

aSbS

}0:{ nbaL nn

Page 16: Context-Free Languages

16

More Notation

Grammar PSTVG ,,,

:V

:T

:S

:P

Set of variables

Set of terminal symbols

Start variable

Set of Production rules

Page 17: Context-Free Languages

17

Example

Grammar :

S

aSbSG

PSTVG ,,,

}{SV },{ baT

},{ SaSbSP

Page 18: Context-Free Languages

18

More NotationSentential Form: A sentence that contains variables and terminals

Example:

aaabbbaaaSbbbaaSbbaSbS

Sentential Forms sentence

Page 19: Context-Free Languages

19

We write:

Instead of:

aaabbbS*

aaabbbaaaSbbbaaSbbaSbS

Page 20: Context-Free Languages

20

In general we write:

If:

nww*

1

nwwww 321

Page 21: Context-Free Languages

21

By default: ww*

Page 22: Context-Free Languages

22

Example

S

aSbS

aaabbbS

aabbS

abS

S

*

*

*

*

Grammar Derivations

Page 23: Context-Free Languages

23

baaaaaSbbbbaaSbb

aaSbbS

S

aSbS

Grammar

Example

Derivations

Page 24: Context-Free Languages

24

Another Grammar ExampleGrammar :

A

aAbA

AbS

Derivations:

aabbbaaAbbbaAbbAbS

abbaAbbAbS

bAbS

⇒⇒⇒⇒

⇒⇒⇒

⇒⇒

G

Page 25: Context-Free Languages

25

More Derivations

aaaabbbbbaaaaAbbbbb

aaaAbbbbaaAbbbaAbbAbS

bbaS

bbbaaaaaabbbbS

aaaabbbbbS

nn

Page 26: Context-Free Languages

26

Language of a Grammar

For a grammar with start variable :

GS

}:{)( wSwGL

String of terminals

Page 27: Context-Free Languages

27

ExampleFor grammar :

A

aAbA

AbS

}0:{)( nbbaGL nn

Since: bbaS nn

G

Page 28: Context-Free Languages

28

A Convenient Notation

A

aAbA|aAbA

thearticle

aarticle

theaarticle |

Page 29: Context-Free Languages

29

Example A context-free grammar :

S

aSbS

aabbaaSbbaSbS

G

A derivation:

Page 30: Context-Free Languages

30

A context-free grammar :

S

aSbS

aaabbbaaaSbbbaaSbbaSbS

G

Another derivation:

Page 31: Context-Free Languages

31

S

aSbS

)(GL

(((( ))))

}0:{ nba nn

Describes parentheses:

Page 32: Context-Free Languages

32

S

bSbS

aSaS

abbaabSbaaSaS

A context-free grammar :G

A derivation:

Example

Page 33: Context-Free Languages

33

S

bSbS

aSaS

abaabaabaSabaabSbaaSaS

A context-free grammar :G

Another derivation:

Page 34: Context-Free Languages

34

S

bSbS

aSaS

)(GL }*},{:{ bawwwR

Page 35: Context-Free Languages

35

S

SSS

aSbS

ababSaSbSSSS

A context-free grammar :G

A derivation:

Example

Page 36: Context-Free Languages

36

S

SSS

aSbS

abababaSbabSaSbSSSS

A context-free grammar :G

A derivation:

Page 37: Context-Free Languages

37

S

SSS

aSbS

}prefixanyin

)()( and

),()(:{

v

vnvn

wnwnw

ba

ba

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

)(GL

Describes matched parentheses:

Page 38: Context-Free Languages

38

Definition: Context-Free Grammars

Grammar

Productions of the form:

xAString of variables and terminals

),,,( PSTVG

Variables Terminalsymbols

Startvariable

Variable

Page 39: Context-Free Languages

39

*},:{)(*

TwwSwGL

),,,( PSTVG

Page 40: Context-Free Languages

40

Definition: Context-Free Languages

A language is context-free

if and only if

there is a context-free grammar with

L

G)(GLL

Page 41: Context-Free Languages

41

Derivation Order ABS .1

A

aaAA

.3

.2

B

BbB

.5

.4

aabaaBbaaBaaABABS54321

Leftmost derivation:

aabaaAbAbABbABS32541

Rightmost derivation:

Page 42: Context-Free Languages

42

|AB

bBbA

aABS

Leftmost derivation:

abbbbabbbbB

abbBbbBabAbBabBbBaABS

Rightmost derivation:

abbbbabbBbb

abAbabBbaAaABS

Page 43: Context-Free Languages

43

Derivation Trees

Page 44: Context-Free Languages

44

ABS

ABS |aaAA |BbB

S

BA

Page 45: Context-Free Languages

45

ABS |aaAA |BbB

aaABABS

a a A

S

BA

Page 46: Context-Free Languages

46

ABS |aaAA |BbB

aaABbaaABABS S

BA

a a A B b

Page 47: Context-Free Languages

47

ABS |aaAA |BbB

aaBbaaABbaaABABS S

BA

a a A B b

Page 48: Context-Free Languages

48

ABS |aaAA |BbB

aabaaBbaaABbaaABABS S

BA

a a A B b

Derivation Tree

Page 49: Context-Free Languages

49

aabaaBbaaABbaaABABS

yield

aab

baa

S

BA

a a A B b

Derivation Tree

ABS |aaAA |BbB

Page 50: Context-Free Languages

50

Partial Derivation Trees

ABS

S

BA

Partial derivation tree

ABS |aaAA |BbB

Page 51: Context-Free Languages

51

aaABABS

S

BA

a a A

Partial derivation tree

Page 52: Context-Free Languages

52

aaABABS

S

BA

a a A

Partial derivation tree

sententialform

yield

aaAB

Page 53: Context-Free Languages

53

aabaaBbaaBaaABABS

aabaaAbAbABbABS S

BA

a a A B b

Same derivation tree

Sometimes, derivation order doesn’t matter

Leftmost:

Rightmost:

Page 54: Context-Free Languages

54

Ambiguity

Page 55: Context-Free Languages

55

aEEEEEE |)(|| aaa

E

EE

EE

a

a a

aaaEaa

EEaEaEEE

*

leftmost derivation

Page 56: Context-Free Languages

56

aEEEEEE |)(|| aaa

E

EE

a a

EE a

aaaEaa

EEaEEEEEE

leftmost derivation

Page 57: Context-Free Languages

57

aEEEEEE |)(|| aaa

E

EE

a a

EE a

E

EE

EE

a

a a

Two derivation trees

Page 58: Context-Free Languages

58

The grammar aEEEEEE |)(|| is ambiguous:

E

EE

a a

EE a

E

EE

EE

a

a a

string aaa has two derivation trees

Page 59: Context-Free Languages

59

string aaa has two leftmost derivations

aaaEaa

EEaEEEEEE

aaaEaa

EEaEaEEE

*

The grammar aEEEEEE |)(|| is ambiguous:

Page 60: Context-Free Languages

60

Definition:

A context-free grammar is ambiguous

if some string has:

two or more derivation trees

G

)(GLw

Page 61: Context-Free Languages

61

In other words:

A context-free grammar is ambiguous

if some string has:

two or more leftmost derivations

G

)(GLw

(or rightmost)

Page 62: Context-Free Languages

62

Why do we care about ambiguity?

E

EE

a a

EE a

E

EE

EE

a

a a

aaa

take 2a

Page 63: Context-Free Languages

63

E

EE

EE

E

EE

EE

222

2

2 2 2 2

2

Page 64: Context-Free Languages

64

E

EE

EE

E

EE

EE

6222

2

2 2 2 2

2

8222

4

2 2

2

6

2 2

24

8

Page 65: Context-Free Languages

65

E

EE

EE

6222

2

2 2

4

2 2

2

6

Correct result:

Page 66: Context-Free Languages

66

• We want to remove ambiguity

• Ambiguity is bad for programming languages

Page 67: Context-Free Languages

67

We fix the ambiguous grammar:

aEEEEEE |)(||

New non-ambiguous grammar:

aF

EF

FT

FTT

TE

TEE

)(

Page 68: Context-Free Languages

68

aF

EF

FT

FTT

TE

TEE

)(

aaaFaaFFa

FTaTaTFTTTEE

E

E T

T F

F

a

T

F

a

a

aaa

Page 69: Context-Free Languages

69

E

E T

T F

F

a

T

F

a

a

aaa

Unique derivation tree

Page 70: Context-Free Languages

70

The grammar :

aF

EF

FT

FTT

TE

TEE

)(

is non-ambiguous:

Every string hasa unique derivation tree

G

)(GLw

Page 71: Context-Free Languages

71

Another Ambiguous Grammar

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

Page 72: Context-Free Languages

72

If expr1 then if expr2 then stmt1 else stmt2

IF_STMT

expr1 then

elseif expr2 then

STMT

stmt1

if

IF_STMT

expr1 then else

if expr2 then

STMT stmt2if

stmt1

stmt2

Page 73: Context-Free Languages

73

Inherent Ambiguity

Some context free languageshave only ambiguous grammars

Example: }{}{ mmnmnn cbacbaL

||11

aAbA

AcSS

||22

bBcB

BaSS

21 | SSS

Page 74: Context-Free Languages

74

The string nnn cba

has two derivation trees

S

1S

S

2S

1S c 2Sa