ContextFree Languages 1 Regular Languages 2 ContextFree Languages

  • Slides: 74
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

Grammars express languages Example: the English language 6

Grammars express languages Example: the English language 6

7

7

A derivation of “the dog walks”: 8

A derivation of “the dog walks”: 8

A derivation of “a cat runs”: 9

A derivation of “a cat runs”: 9

Language of the grammar: L = { “a cat runs”, “a cat walks”, “the

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” } 10

Notation Production Rules Variable Terminal 11

Notation Production Rules Variable Terminal 11

Another Example Grammar: Derivation of sentence : 12

Another Example Grammar: Derivation of sentence : 12

Grammar: Derivation of sentence : 13

Grammar: Derivation of sentence : 13

Other derivations: 14

Other derivations: 14

Language of the grammar 15

Language of the grammar 15

More Notation Grammar Set of variables Set of terminal symbols Start variable Set of

More Notation Grammar Set of variables Set of terminal symbols Start variable Set of Production rules 16

Example Grammar : 17

Example Grammar : 17

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

More Notation Sentential Form: A sentence that contains variables and terminals Example: Sentential Forms sentence 18

We write: Instead of: 19

We write: Instead of: 19

In general we write: If: 20

In general we write: If: 20

By default: 21

By default: 21

Example Grammar Derivations 22

Example Grammar Derivations 22

Example Grammar Derivations 23

Example Grammar Derivations 23

Another Grammar Example Grammar : Derivations: 24

Another Grammar Example Grammar : Derivations: 24

More Derivations 25

More Derivations 25

Language of a Grammar For a grammar with start variable : String of terminals

Language of a Grammar For a grammar with start variable : String of terminals 26

Example For grammar : Since: 27

Example For grammar : Since: 27

A Convenient Notation 28

A Convenient Notation 28

Example A context-free grammar : A derivation: 29

Example A context-free grammar : A derivation: 29

A context-free grammar : Another derivation: 30

A context-free grammar : Another derivation: 30

Describes parentheses: (((( )))) 31

Describes parentheses: (((( )))) 31

Example A context-free grammar : A derivation: 32

Example A context-free grammar : A derivation: 32

A context-free grammar : Another derivation: 33

A context-free grammar : Another derivation: 33

34

34

Example A context-free grammar : A derivation: 35

Example A context-free grammar : A derivation: 35

A context-free grammar : A derivation: 36

A context-free grammar : A derivation: 36

Describes matched parentheses: () ((( ))) (( )) 37

Describes matched parentheses: () ((( ))) (( )) 37

Definition: Context-Free Grammars Grammar Variables Terminal symbols Start variable Productions of the form: Variable

Definition: Context-Free Grammars Grammar Variables Terminal symbols Start variable Productions of the form: Variable String of variables and terminals 38

39

39

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

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

Derivation Order Leftmost derivation: Rightmost derivation: 41

Derivation Order Leftmost derivation: Rightmost derivation: 41

Leftmost derivation: Rightmost derivation: 42

Leftmost derivation: Rightmost derivation: 42

Derivation Trees 43

Derivation Trees 43

44

44

45

45

46

46

47

47

Derivation Tree 48

Derivation Tree 48

Derivation Tree yield 49

Derivation Tree yield 49

Partial Derivation Trees Partial derivation tree 50

Partial Derivation Trees Partial derivation tree 50

Partial derivation tree 51

Partial derivation tree 51

sentential form Partial derivation tree yield 52

sentential form Partial derivation tree yield 52

Sometimes, derivation order doesn’t matter Leftmost: Rightmost: Same derivation tree 53

Sometimes, derivation order doesn’t matter Leftmost: Rightmost: Same derivation tree 53

Ambiguity 54

Ambiguity 54

leftmost derivation 55

leftmost derivation 55

leftmost derivation 56

leftmost derivation 56

Two derivation trees 57

Two derivation trees 57

The grammar is ambiguous: string has two derivation trees 58

The grammar is ambiguous: string has two derivation trees 58

The grammar is ambiguous: string has two leftmost derivations 59

The grammar is ambiguous: string has two leftmost derivations 59

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

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

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 (or rightmost) 61

Why do we care about ambiguity? take 62

Why do we care about ambiguity? take 62

63

63

64

64

Correct result: 65

Correct result: 65

 • Ambiguity is bad for programming languages • We want to remove ambiguity

• Ambiguity is bad for programming languages • We want to remove ambiguity 66

We fix the ambiguous grammar: New non-ambiguous grammar: 67

We fix the ambiguous grammar: New non-ambiguous grammar: 67

68

68

Unique derivation tree 69

Unique derivation tree 69

The grammar : is non-ambiguous: Every string has a unique derivation tree 70

The grammar : is non-ambiguous: Every string has a unique derivation tree 70

Another Ambiguous Grammar IF_STMT if EXPR then STMT else STMT 71

Another Ambiguous Grammar IF_STMT if EXPR then STMT else STMT 71

If expr 1 then if expr 2 then stmt 1 else stmt 2 IF_STMT

If expr 1 then if expr 2 then stmt 1 else stmt 2 IF_STMT if expr 1 if then expr 2 STMT then stmt 1 else STMT else stmt 2 IF_STMT if expr 1 if then expr 2 then stmt 1 72

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

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

The string has two derivation trees 74

The string has two derivation trees 74