ContextFree Languages 1 Regular Languages 2 ContextFree Languages

  • Slides: 43
Download presentation
Context-Free Languages 1

Context-Free Languages 1

Regular Languages 2

Regular Languages 2

Context-Free Languages Regular Languages 3

Context-Free Languages Regular Languages 3

Context-Free Languages Context-Free Grammars Pushdown Automata stack automaton 4

Context-Free Languages Context-Free Grammars Pushdown Automata stack automaton 4

Context-Free Grammars 5

Context-Free Grammars 5

A context-free grammar G: 6

A context-free grammar G: 6

A context-free grammar G: 8

A context-free grammar G: 8

9

9

A context-free grammar G: 10

A context-free grammar G: 10

Definition of Context-Free Grammars Any grammar G=(V, T, S, P) V: variables T: terminal

Definition of Context-Free Grammars Any grammar G=(V, T, S, P) V: variables T: terminal symbols S: start variable P: production rules of the form : string of variables and terminals 12

Definition of Context-Free Languages A language L is context-free if and only if there

Definition of Context-Free Languages A language L is context-free if and only if there is a grammar G with L = L(G) 13

Derivation Order Leftmost derivation: Rightmost derivation: 14

Derivation Order Leftmost derivation: Rightmost derivation: 14

Derivation Trees 15

Derivation Trees 15

16

16

17

17

18

18

19

19

Derivation Tree 20

Derivation Tree 20

sentence Derivation Tree yield 21

sentence Derivation Tree yield 21

Partial Derivation Trees Partial derivation tree 22

Partial Derivation Trees Partial derivation tree 22

sentential form Partial derivation tree yield 23

sentential form Partial derivation tree yield 23

Often, derivation order doesn’t matter Same Derivation Tree 24

Often, derivation order doesn’t matter Same Derivation Tree 24

Ambiguity 25

Ambiguity 25

26

26

27

27

28

28

The grammar is ambiguous: string has two derivation trees 29

The grammar is ambiguous: string has two derivation trees 29

The grammar is ambiguous: string has two leftmost derivations 30

The grammar is ambiguous: string has two leftmost derivations 30

In general: A context-free grammar if some string is ambiguous has: two or more

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

In other words: A context-free grammar if some string is ambiguous has: two or

In other words: A context-free grammar if some string is ambiguous has: two or more leftmost derivations rightmost 32

Why do we care about ambiguity? take 33

Why do we care about ambiguity? take 33

34

34

35

35

The right solution: 36

The right solution: 36

Therefore: Ambiguity is bad for programming languages We want to remove the ambiguity 37

Therefore: Ambiguity is bad for programming languages We want to remove the ambiguity 37

We fix the ambiguous grammar: New grammar: 38

We fix the ambiguous grammar: New grammar: 38

39

39

Unique derivation tree 40

Unique derivation tree 40

The grammar Is not ambiguous 41

The grammar Is not ambiguous 41

Inherent Ambiguity Some context free languages have only ambiguous grammars Example: 42

Inherent Ambiguity Some context free languages have only ambiguous grammars Example: 42

The string has two derivation trees 43

The string has two derivation trees 43