context-free languages hinrich schütze cis, lmu, 2013-11-25 slides based on rpi csci 2400 thanks to...

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

Upload: matilda-ada-bridges

Post on 24-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Context-Free Languages

Hinrich Schütze

CIS, LMU, 2013-11-25

Slides based on RPI CSCI 2400

Thanks to Costas Busch

Take-awayDefinition context-free grammarDefinition context-free languageDerivation, sentential form, sentenceDerivation treesAmbiguityContext-free grammars for natural

language

2

TerminologyFor our purposes in this class:Context free grammar=Constituency grammar=Phrase structure grammar

3

4

GrammarsGrammars express languages

Example: the English language

5

6

A derivation of “the dog walks”:

7

A derivation of “a cat runs”:

8

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

9

Notation

Variable Terminal

Production Rules

10

Another ExampleGrammar:

Derivation of sentence :

11

Language?

12

Grammar:

Derivation of sentence :

13

Other derivations:

14

Language of the grammar

15

More Notation

Grammar

Set of variables

Set of terminal symbols

Start variable

Set of Production rules

16

Example

Grammar :

17

More NotationSentential Form: A sentence that contains variables and terminals

Example:

Sentential Forms sentence

18

We write:

Instead of:

19

In general we write:

If:

20

By default:

21

Example Grammar Derivations

22

Grammar

Example

Derivations

23

Another Grammar ExampleGrammar :

24

Language?

25

Grammar :

Derivations:

26

More Derivations

27

Language of a Grammar

For a grammar with start variable :

String of terminals

28

ExampleFor grammar :

Since:

29

A Convenient Notation

30

Revisit first grammar A context-free grammar :

A derivation:

31

A context-free grammar :

Another derivation:

32

(((( ))))Describes parentheses:

33

A context-free grammar :

Example

34

Language?

35

A context-free grammar :

Another derivation:

36

37

A context-free grammar :

Example

38

Language?

39

A context-free grammar :

Two derivations:

40

Interpretation?

41

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

Describes matched parentheses:

42

Definition: Context-Free Grammars

Grammar

Productions of the form:

String of variables and terminals

Variables Terminalsymbols

Startvariable

Variable

43

44

Definition: Context-Free Languages

A language is context-free

if and only if

there is a context-free grammar with

45

Derivation Order

Leftmost derivation:

Rightmost derivation:

46

Language?

47

Leftmost derivation:

Rightmost derivation:

48

Language?

49

Derivation Trees

50

51

52

53

54

Derivation Tree

55

yield

Derivation Tree

56

Partial Derivation Trees

Partial derivation tree

57

Partial derivation tree

58

Partial derivation tree

sententialform

yield

59

Same derivation tree

Sometimes, derivation order doesn’t matter

Leftmost:

Rightmost:

60

Ambiguity

61

leftmost derivation

62

leftmost derivation

63

Two derivation trees

64

The grammar

is ambiguous:

string has two derivation trees

65

string has two leftmost derivations

The grammar

is ambiguous:

66

Definition:

A context-free grammar is ambiguous

if some string has:

two or more derivation trees

67

In other words:

A context-free grammar is ambiguous

if some string has:

two or more leftmost derivations

(or rightmost)

68

Why do we care about ambiguity?

take

69

70

71

Correct result:

72

• We want to remove ambiguity

• Ambiguity is bad for programming languages

73

We fix the ambiguous grammar:

New non-ambiguous grammar:

74

75

Unique derivation tree

76

The grammar :

is non-ambiguous:

Every string hasa unique derivation tree

77

Another Ambiguous Grammar

IF_STMT if EXPR then STMT

if EXPR then STMT else STMT

Ambiguity?

78

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

79

Inherent Ambiguity

Some context free languageshave only ambiguous grammars

Example:

80

The string

has two derivation trees

81

Ambiguity in natural language?

Take-awayDefinition context-free grammarDefinition context-free languageDerivation, sentential form, sentenceDerivation treesAmbiguityContext-free grammars for natural

language

82