ContextFree Languages Hinrich Schtze CIS LMU 2013 11

  • Slides: 82
Download presentation
Context-Free Languages Hinrich Schütze CIS, LMU, 2013 -11 -25 Slides based on RPI CSCI

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

Take-away Definition context-free grammar Definition context-free language Derivation, sentential form, sentence Derivation trees Ambiguity

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

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

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

Grammars express languages Example: the English language 4

Grammars express languages Example: the English language 4

5

5

A derivation of “the dog walks”: 6

A derivation of “the dog walks”: 6

A derivation of “a cat runs”: 7

A derivation of “a cat runs”: 7

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

Notation Production Rules Variable Terminal 9

Notation Production Rules Variable Terminal 9

Another Example Grammar: Derivation of sentence : 10

Another Example Grammar: Derivation of sentence : 10

Language? 11

Language? 11

Grammar: Derivation of sentence : 12

Grammar: Derivation of sentence : 12

Other derivations: 13

Other derivations: 13

Language of the grammar 14

Language of the grammar 14

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 15

Example Grammar : 16

Example Grammar : 16

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 17

We write: Instead of: 18

We write: Instead of: 18

In general we write: If: 19

In general we write: If: 19

By default: 20

By default: 20

Example Grammar Derivations 21

Example Grammar Derivations 21

Example Grammar Derivations 22

Example Grammar Derivations 22

Another Grammar Example Grammar : 23

Another Grammar Example Grammar : 23

Language? 24

Language? 24

Grammar : Derivations: 25

Grammar : Derivations: 25

More Derivations 26

More Derivations 26

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 27

Example For grammar : Since: 28

Example For grammar : Since: 28

A Convenient Notation 29

A Convenient Notation 29

Revisit first grammar A context-free grammar : A derivation: 30

Revisit first grammar A context-free grammar : A derivation: 30

A context-free grammar : Another derivation: 31

A context-free grammar : Another derivation: 31

Describes parentheses: (((( )))) 32

Describes parentheses: (((( )))) 32

Example A context-free grammar : 33

Example A context-free grammar : 33

Language? 34

Language? 34

A context-free grammar : Another derivation: 35

A context-free grammar : Another derivation: 35

36

36

Example A context-free grammar : 37

Example A context-free grammar : 37

Language? 38

Language? 38

A context-free grammar : Two derivations: 39

A context-free grammar : Two derivations: 39

Interpretation? 40

Interpretation? 40

Describes matched parentheses: () ((( ))) (( )) 41

Describes matched parentheses: () ((( ))) (( )) 41

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 42

43

43

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 44

Derivation Order Leftmost derivation: Rightmost derivation: 45

Derivation Order Leftmost derivation: Rightmost derivation: 45

Language? 46

Language? 46

Leftmost derivation: Rightmost derivation: 47

Leftmost derivation: Rightmost derivation: 47

Language? 48

Language? 48

Derivation Trees 49

Derivation Trees 49

50

50

51

51

52

52

53

53

Derivation Tree 54

Derivation Tree 54

Derivation Tree yield 55

Derivation Tree yield 55

Partial Derivation Trees Partial derivation tree 56

Partial Derivation Trees Partial derivation tree 56

Partial derivation tree 57

Partial derivation tree 57

sentential form Partial derivation tree yield 58

sentential form Partial derivation tree yield 58

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

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

Ambiguity 60

Ambiguity 60

leftmost derivation 61

leftmost derivation 61

leftmost derivation 62

leftmost derivation 62

Two derivation trees 63

Two derivation trees 63

The grammar is ambiguous: string has two derivation trees 64

The grammar is ambiguous: string has two derivation trees 64

The grammar is ambiguous: string has two leftmost derivations 65

The grammar is ambiguous: string has two leftmost derivations 65

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 66

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) 67

Why do we care about ambiguity? take 68

Why do we care about ambiguity? take 68

69

69

70

70

Correct result: 71

Correct result: 71

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

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

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

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

74

74

Unique derivation tree 75

Unique derivation tree 75

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

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

Another Ambiguous Grammar IF_STMT if EXPR then STMT else STMT Ambiguity? 77

Another Ambiguous Grammar IF_STMT if EXPR then STMT else STMT Ambiguity? 77

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 78

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

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

The string has two derivation trees 80

The string has two derivation trees 80

Ambiguity in natural language? 81

Ambiguity in natural language? 81

Take-away Definition context-free grammar Definition context-free language Derivation, sentential form, sentence Derivation trees Ambiguity

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