rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression...

Preview:

Citation preview

1

Topic 3: Parsing and Yaccing

COS 320

Compiling Techniques

Princeton University Spring 2015

Prof. David August

2

Syntactical Analysis

3

Syntactical Analysis

4

Context-Free Grammar

5

Definitions

6

Context-Free Grammar

7

Context-Free Grammars

8

Start Symbol

9

Example

10

Example: Leftmost Derivation

11

Example: Rightmost Derivation

12

Parse Trees

13

Ambiguous Grammars

14

Ambiguous Grammars

15

Ambiguous Grammars

16

End-Of-File Marker

17

Grammars and Lexical Analysis

18

Context-Free Grammars and REs

19

Context Free Grammars and REs

20

Context-Free Grammar with no RE/FA

21

Further Exploration

22

Parsing

23

Outline

•  Recursive Descent Parsing •  Shift-Reduce Parsing •  ML-Yacc •  Recursive Descent Parser Generation

24

Recursive Descent Parsing

25

Example

26

Another Example

27

The Problem

28

Another Option: Shift-Reduce Parsing

29

Shift-Reduce Parsing

30

Example

31

Example

32

Example

33

Example

34

Example

35

The Dangling Else Problem

36

ML-YACC (Yet Another Compiler-Compiler)

37

Context-Free Grammar Specification

38

ML-YACC Declarations

39

Attribute Grammar

40

Rules

41

Example

42

ML-YACC and Ambiguous Grammars

43

ML-YACC and Ambigous Grammars

44

Directives

45

Directives

46

Precedence Examples

47

Default Behavior

48

Direct Rule Precedence Specification

49

Syntax vs. Semantics

50

Recursive Descent/Predictive/LL(1) Parser Generation

51

Problem

52

Formal Techniques

53

Computation of First

54

Computation of Follow

55

Building a Predictive Parser

56

Building a Predictive Parser

57

Predictive Parsing Table

58

Predictive Parsing Table

59

Example

60

Example

61

Predictive Parsing Table

62

Problems

63

Problems

64

Example

65

Example

66

Example

67

Outline

68

Shift-Reduce, Bottom Up, LR(1) Parsing

69

LR(k)

70

Shift Reduce Parsing DFA

71

Parsing Algorithm

72

LR(0) Parsing

73

LR(0) States

74

DFA Table Entry Computation

75

Transition Table

76

Using The Transition Table

77

Another Example

78

Another Example - SLR

79

Another Example – SLR

80

Yet Another Example

81

LR(1) Parsing

82

Yet Another Example – LR(1)

83

Yet Another Example – LR(1)

84

LALR(1)

85

LALR(1)

86

Parsing Power

87

Parsing Error Recovery

88

Error Recovery

89

LL Local Error Recovery

90

LL Local Error Recovery

91

LR Local Error Recovery

92

Global Error Recovery

93

Burke-Fisher

94

Burke-Fisher

95

Burke-Fisher Example

96

Burke-Fisher

97

Burke-Fisher in ML-YACC

Recommended