ContextFree Languages Hinrich Schtze CIS LMU 2013 11


















































































- Slides: 82

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 Context-free grammars for natural language 2

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

Grammars express languages Example: the English language 4

5

A derivation of “the dog walks”: 6

A derivation of “a cat runs”: 7

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

Another Example Grammar: Derivation of sentence : 10

Language? 11

Grammar: Derivation of sentence : 12

Other derivations: 13

Language of the grammar 14

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

Example Grammar : 16

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

We write: Instead of: 18

In general we write: If: 19

By default: 20

Example Grammar Derivations 21

Example Grammar Derivations 22

Another Grammar Example Grammar : 23

Language? 24

Grammar : Derivations: 25

More Derivations 26

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

Example For grammar : Since: 28

A Convenient Notation 29

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

A context-free grammar : Another derivation: 31

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

Example A context-free grammar : 33

Language? 34

A context-free grammar : Another derivation: 35

36

Example A context-free grammar : 37

Language? 38

A context-free grammar : Two derivations: 39

Interpretation? 40

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

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

43

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

Language? 46

Leftmost derivation: Rightmost derivation: 47

Language? 48

Derivation Trees 49

50

51

52

53

Derivation Tree 54

Derivation Tree yield 55

Partial Derivation Trees Partial derivation tree 56

Partial derivation tree 57

sentential form Partial derivation tree yield 58

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

Ambiguity 60

leftmost derivation 61

leftmost derivation 62

Two derivation trees 63

The grammar is ambiguous: string has two derivation trees 64

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 trees 66

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

69

70

Correct result: 71

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

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

74

Unique derivation tree 75

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

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

The string has two derivation trees 80

Ambiguity in natural language? 81

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