25
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

rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

Embed Size (px)

Citation preview

Page 1: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

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

Page 2: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

5

Definitions

6

Context-Free Grammar

7

Context-Free Grammars

8

Start Symbol

Page 3: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

9

Example

10

Example: Leftmost Derivation

11

Example: Rightmost Derivation

12

Parse Trees

Page 4: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

13

Ambiguous Grammars

14

Ambiguous Grammars

15

Ambiguous Grammars

16

End-Of-File Marker

Page 5: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

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

Page 6: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

21

Further Exploration

22

Parsing

23

Outline

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

24

Recursive Descent Parsing

Page 7: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

25

Example

26

Another Example

27

The Problem

28

Another Option: Shift-Reduce Parsing

Page 8: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

29

Shift-Reduce Parsing

30

Example

31

Example

32

Example

Page 9: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

33

Example

34

Example

35

The Dangling Else Problem

36

ML-YACC (Yet Another Compiler-Compiler)

Page 10: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

37

Context-Free Grammar Specification

38

ML-YACC Declarations

39

Attribute Grammar

40

Rules

Page 11: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

41

Example

42

ML-YACC and Ambiguous Grammars

43

ML-YACC and Ambigous Grammars

44

Directives

Page 12: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

45

Directives

46

Precedence Examples

47

Default Behavior

48

Direct Rule Precedence Specification

Page 13: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

49

Syntax vs. Semantics

50

Recursive Descent/Predictive/LL(1) Parser Generation

51

Problem

52

Formal Techniques

Page 14: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

53

Computation of First

54

Computation of Follow

55

Building a Predictive Parser

56

Building a Predictive Parser

Page 15: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

57

Predictive Parsing Table

58

Predictive Parsing Table

59

Example

60

Example

Page 16: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

61

Predictive Parsing Table

62

Problems

63

Problems

64

Example

Page 17: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

65

Example

66

Example

67

Outline

68

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

Page 18: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

69

LR(k)

70

Shift Reduce Parsing DFA

71

Parsing Algorithm

72

LR(0) Parsing

Page 19: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

73

LR(0) States

74

DFA Table Entry Computation

75

Transition Table

76

Using The Transition Table

Page 20: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

77

Another Example

78

Another Example - SLR

79

Another Example – SLR

80

Yet Another Example

Page 21: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

81

LR(1) Parsing

82

Yet Another Example – LR(1)

83

Yet Another Example – LR(1)

84

LALR(1)

Page 22: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

85

LALR(1)

86

Parsing Power

87

Parsing Error Recovery

88

Error Recovery

Page 23: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

89

LL Local Error Recovery

90

LL Local Error Recovery

91

LR Local Error Recovery

92

Global Error Recovery

Page 24: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

93

Burke-Fisher

94

Burke-Fisher

95

Burke-Fisher Example

96

Burke-Fisher

Page 25: rs - Computer Science Department at Princeton University ·  · 2015-04-28Statement Expression Expression List stmt stmt ... expr + expr ( stmt, expr ) list expr list list, expr

97

Burke-Fisher in ML-YACC