lecture 10 - university of waterloovganesh/teaching/s2014... · ganesh. lecture 10) review:...

110
Bottom-Up Parsing II (Different types of Shift-Reduce Conflicts) Lecture 10 Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10) 1

Upload: others

Post on 26-Sep-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Bottom-Up Parsing II (Different types of Shift-Reduce Conflicts)

Lecture 10

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

1

Page 2: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

Review: Bottom-Up Parsing

•  Bottom-up parsing is more general than top-down parsing –  And just as efficient –  Doesn’t have issues with left-recursion –  Doesn’t require left-factoring –  Many well-known parser generators (Yacc, Bison,…) –  Can handle many more grammars without

backtracking than otherwise

•  LR(k) parsers are bottom-up 2

Page 3: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

Review: An Introductory Example

•  Bottom-up parsers don’t need left-factored grammars

•  Revert to the “natural” grammar for our example:

E → T + E | T T → int * T | int | (E)

•  Consider the string: int * int + int 3

Page 4: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

Review: The Idea

Bottom-up parsing reduces a string to the start symbol by inverting productions:

int * int + int T → int

int * T + int T → int * T T + int T → int T + T E → T T + E E → T + E E

4

Page 5: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

Review: Bottom-up Parsers and Rightmost derivations in reverse

•  Read the productions in reverse (from bottom to top)

•  This is a rightmost derivation!

int * int + int T → int int * T + int T → int * T T + int T → int T + T E → T T + E E → T + E E

5

Page 6: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

Review: Notation useful for Shift-Reduce Parsers

•  Idea: Split string into two substrings –  Right substring is as yet unexamined by parsing

(a string of terminals) –  Left substring has terminals and non-terminals

•  The dividing point is marked by a | –  The | is not part of the string

•  Initially, all input is unexamined |x1x2 . . . xn

6

Page 7: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

Review: Shift-Reduce Parsing

Bottom-up parsing uses only two kinds of actions:

Shift

Reduce

7

Page 8: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

Review: Shift

•  Shift: Move | one place to the right –  Shifts a terminal to the left string

ABC|xyz ⇒ ABCx|yz

8

Page 9: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

Review: Reduce

•  Apply an inverse production at the right end of the left string –  If D→ Cx is a production, then

ABCx|yz ⇒ ABD|yz

9

Page 10: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Aiken CS 143 Lecture 7 10

The Stack

•  Left string can be implemented by a stack –  Top of the stack is the |

•  Shift pushes a terminal on the stack

•  Reduce pops 0 or more symbols off of the stack (production rhs) and pushes a non-terminal on the stack (production lhs)

Page 11: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

Review: The Example with Reductions Only

int * int | + int reduce T → int int * T | + int reduce T → int * T

T + int | reduce T → int T + T | reduce E → T T + E | reduce E → T + E E | 11

Page 12: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

Review: The Example with Shift-Reduce Parsing

|int * int + int shift int | * int + int shift int * | int + int shift int * int | + int reduce T → int int * T | + int reduce T → int * T T | + int shift T + | int shift T + int | reduce T → int T + T | reduce E → T T + E | reduce E → T + E E | 12

Page 13: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Alex Aiken Lecture 9 (Modified by Professor Vijay Ganesh)

Conflicts

•  In a given state, more than one action (shift or reduce) may lead to a valid parse

•  If it is legal to shift or reduce, there is a shift-reduce conflict

•  If it is legal to reduce by two different productions, there is a reduce-reduce conflict

13

Page 14: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Key Issue: To Shift or Reduce?

•  How do we decide when to shift or reduce?

•  Example grammar: E → T + E | T T → int * T | int | (E)

•  Consider step int | * int + int –  We could reduce by T → int giving T | * int + int –  A fatal mistake!

•  No way to reduce to the start symbol E

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

14

Page 15: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Handles: Symbols replaced by Reduction

•  Intuition: Want to reduce only if the result can still be reduced to the start symbol

•  Handle: Informally, represents the RHS of a

production. Let X → β be a production in G. Then β in the position after α is a handle of αβω

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

15

Page 16: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Handles (Cont.)

•  Handles formalize the intuition –  A handle is a string that can be reduced and also

allows further reductions back to the start symbol (using a particular production at a specific spot)

•  We only want to reduce at handles

•  Note: We have said what a handle is, not how to find handles

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

16

Page 17: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Important Fact #2

Important Fact #2 about bottom-up parsing: In shift-reduce parsing, handles appear only at

the top of the stack, never inside

Using the symbol already shifted into the stack (left context) and the next k lookahead

symbols (right context), decide to shift or reduce

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

17

Page 18: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Alex Aiken Lecture 9 (Modified by Professor Vijay Ganesh)

A Shift-Reduce Parse in Detail (1)

+ int * int int ↑

|int * int + int

18

Page 19: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Alex Aiken Lecture 9 (Modified by Professor Vijay Ganesh)

A Shift-Reduce Parse in Detail (2)

+ int * int int ↑

|int * int + int int | * int + int

19

Page 20: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Alex Aiken Lecture 9 (Modified by Professor Vijay Ganesh)

A Shift-Reduce Parse in Detail (3)

+ int * int int ↑

|int * int + int int | * int + int int * | int + int

20

Page 21: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Alex Aiken Lecture 9 (Modified by Professor Vijay Ganesh)

A Shift-Reduce Parse in Detail (4)

+ int * int int ↑

|int * int + int int | * int + int int * | int + int int * int | + int

21

Page 22: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Alex Aiken Lecture 9 (Modified by Professor Vijay Ganesh)

A Shift-Reduce Parse in Detail (5)

+ int * int int

T

|int * int + int int | * int + int int * | int + int int * int | + int int * T | + int

22

Page 23: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Alex Aiken Lecture 9 (Modified by Professor Vijay Ganesh)

A Shift-Reduce Parse in Detail (6)

T

+ int * int int

T

|int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int

23

Page 24: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Alex Aiken Lecture 9 (Modified by Professor Vijay Ganesh)

A Shift-Reduce Parse in Detail (7)

T

+ int * int int

T

|int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int

24

Page 25: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Alex Aiken Lecture 9 (Modified by Professor Vijay Ganesh)

A Shift-Reduce Parse in Detail (8)

T

+ int * int int

T

|int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int |

25

Page 26: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Alex Aiken Lecture 9 (Modified by Professor Vijay Ganesh)

A Shift-Reduce Parse in Detail (9)

T

+ int * int

T

int

T

|int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int | T + T |

26

Page 27: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Alex Aiken Lecture 9 (Modified by Professor Vijay Ganesh)

A Shift-Reduce Parse in Detail (10)

T E

+ int * int

T

int

T

|int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int | T + T | T + E | ↑

27

Page 28: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Alex Aiken Lecture 9 (Modified by Professor Vijay Ganesh)

A Shift-Reduce Parse in Detail (11)

E

T E

+ int * int

T

int

T

|int * int + int int | * int + int int * | int + int int * int | + int int * T | + int T | + int T + | int T + int | T + T | T + E | E |

28

Page 29: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Theorem: Handle always on top of Stack

•  Proof Method: Informal induction on # of reduce moves

•  Proof: –  True initially, stack is empty

–  Immediately after reducing a handle

•  right-most non-terminal on top of the stack •  next handle must be to right of right-most non-terminal,

because this is a right-most derivation •  Sequence of shift moves reaches next handle

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

29

Page 30: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Summary of Handles

•  In shift-reduce parsing, handles always appear at the top of the stack

•  Handles are never to the left of the rightmost non-terminal –  Therefore, shift-reduce moves are sufficient; the |

need never move left

•  Bottom-up parsing algorithms are based on recognizing handles

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

30

Page 31: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Recognizing Handles

•  There are no known efficient algorithms to recognize handles

•  Solution: use heuristics to guess which stacks are handles

•  On some CFGs, the heuristics always guess correctly –  For the heuristics we use here, these are the SLR

grammars –  Other heuristics work for other grammars

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

31

Page 32: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Grammars

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

32

All CFGs

Unambiguous CFGs

SLR CFGs

will generate conflicts

Page 33: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Viable Prefixes

•  It is not obvious how to detect handles

•  At each step the parser sees only the stack, not the entire input; start with that . . .

α is a viable prefix if there is an ω such that α|ω is a state of a shift-reduce parser

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

33

Page 34: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Huh?

•  What does this mean? A few things:

–  A viable prefix does not extend past the right end of the handle

–  It’s a viable prefix because it is a prefix of the handle

–  As long as a parser has viable prefixes on the stack no parsing error has been detected

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

34

Page 35: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Important Fact #3

Important Fact #3 about bottom-up parsing: For any grammar, the set of viable prefixes is a

regular language

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

35

Page 36: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Important Fact #3 (Cont.)

•  Important Fact #3 is non-obvious

•  We show how to compute automata that accept viable prefixes

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

36

Page 37: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Items

•  An item is a production with a “.” somewhere on the rhs

•  The items for T → (E) are T → .(E) T → (.E) T → (E.) T → (E).

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

37

Page 38: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Items (Cont.)

•  The only item for X → ε is X → . •  Items are often called “LR(0) items”

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

38

Page 39: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Intuition

•  The problem in recognizing viable prefixes is that the stack has only bits and pieces of the rhs of productions –  If it had a complete rhs, we could reduce

•  These bits and pieces are always prefixes of rhs of productions

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

39

Page 40: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Example

Consider the input (int)

–  Then (E|) is a state of a shift-reduce parse

–  (E is a prefix of the rhs of T → (E) •  Will be reduced after the next shift

–  Item T → (E.) says that so far we have seen (E of this production and hope to see )

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

40

Page 41: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Generalization

•  The stack may have many prefixes of rhs’s Prefix1 Prefix2 . . . Prefixn-1Prefixn

•  Let Prefixi be a prefix of rhs of Xi → αi –  Prefixi will eventually reduce to Xi –  The missing part of αi-1 starts with Xi –  i.e. there is a Xi-1 → Prefixi-1 Xi β for some β

•  Recursively, Prefixk+1…Prefixn eventually reduces to the missing part of αk

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

41

Page 42: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

An Example

Consider the string (int * int): (int *|int) is a state of a shift-reduce parse

“(” is a prefix of the rhs of T → (E) “ε” is a prefix of the rhs of E → T “int *” is a prefix of the rhs of T → int * T

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

42

Page 43: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

An Example (Cont.)

The “stack of items” T → (.E) E → .T T → int * .T

Says We’ve seen “(” of T → (E) We’ve seen ε of E → T We’ve seen int * of T → int * T

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

43

Page 44: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Recognizing Viable Prefixes

Idea: To recognize viable prefixes, we must

–  Recognize a sequence of partial rhs’s of productions, where

–  Each sequence can eventually reduce to part of the missing suffix of its predecessor

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

44

Page 45: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

An NFA Recognizing Viable Prefixes

1.  Add a dummy production S’ → S to G 2.  The NFA states are the items of G

–  Including the extra production

3. For item E → α.Xβ add transition E → α.Xβ →X E → αX.β

4. For item E → α.Xβ and production X → γ add E → α.Xβ →ε X → .γ

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

45

Page 46: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

An NFA Recognizing Viable Prefixes (Cont.)

5. Every state is an accepting state 6. Start state is S’ → .S

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

46

Page 47: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

NFA for Viable Prefixes of the Example

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

47

T → . (E) T → (.E) T → (E.) T → (E). ( E )

S’ → E. E → . T+E E → T.+E E → T+.E

E → T+E. S’ → . E

E→ . T

E→ T.

T→ int. T→ .int

T → .int * T T → int * T. T → int *.T

T → int.* T ε

ε

ε

ε

E

ε

T

ε ε

ε

E

+

ε

int

int *

ε ε

ε

ε

Page 48: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

NFA for Viable Prefixes in Detail (1)

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

48

S’ → . E

Page 49: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

NFA for Viable Prefixes in Detail (2)

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

49

S’ → . E

S’ → E.

E

E→ . T

ε

E → . T+E

ε

Page 50: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

NFA for Viable Prefixes in Detail (3)

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

50

S’ → E. E → . T+E

S’ → . E

E→ . T

T → .int * T ε

ε

E

T → . (E)

ε T→ .int

ε

ε

E→ T.

T

Page 51: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

NFA for Viable Prefixes in Detail (4)

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

51

T → . (E)

S’ → E. E → . T+E

S’ → . E

E→ . T

E→ T.

T→ .int

T → .int * T ε

ε

EE → T.+E T

ε

ε ε

ε ε

ε

T

Page 52: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

NFA for Viable Prefixes in Detail (5)

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

52

T → . (E) T → (.E) (

S’ → E. E → . T+E

S’ → . E

E→ . T

E→ T.

T→ .int

T → .int * T ε

ε

E

ε

ε ε

ε ε

ε

T

E → T.+E T

Page 53: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

NFA for Viable Prefixes in Detail (6)

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

53

T → . (E) T → (.E) (

S’ → E. E → . T+E

S’ → . E

E→ . T

E→ T.

T→ .int

T → .int * T ε

ε

ε

ε

E

ε

ε ε

ε ε

ε

T

E → T.+E T

T → (E.) E

Page 54: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

NFA for Viable Prefixes in Detail (7)

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

54

T → . (E) T → (.E) (

S’ → E. E → . T+E

S’ → . E

E→ . T

E→ T.

T→ .int

T → .int * T ε

ε

ε

ε

E

ε

ε ε

ε ε

ε

T

E → T.+E T

T → (E.) E T → (E). )

Page 55: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

NFA for Viable Prefixes in Detail (8)

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

55

T → . (E) T → (.E) (

S’ → E. E → . T+E

S’ → . E

E→ . T

E→ T.

T→ .int

T → .int * T ε

ε

ε

ε

E

ε

ε ε

ε ε

ε

T

E → T.+E T

T → (E.) E T → (E). )

E → T+.E +

Page 56: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

NFA for Viable Prefixes in Detail (9)

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

56

T → . (E) T → (.E) (

S’ → E. E → . T+E

S’ → . E

E→ . T

E→ T.

T→ .int

T → .int * T ε

ε

ε

ε

E

ε

ε ε

ε ε

ε

T

E → T.+E T

T → (E.) E T → (E). )

E → T+.E +

ε

ε E → T+E.

E

Page 57: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

NFA for Viable Prefixes in Detail (10)

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

57

T → . (E) T → (.E) (

S’ → E. E → . T+E

S’ → . E

E→ . T

E→ T.

T→ .int

T → .int * T ε

ε

ε

ε

E

ε

ε ε

ε ε

ε

T

E → T.+E T

T → (E.) E T → (E). )

E → T+.E +

ε

ε E → T+E.

ET→ int. int

Page 58: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

NFA for Viable Prefixes in Detail (11)

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

58

T → . (E) T → (.E) (

S’ → E. E → . T+E

S’ → . E

E→ . T

E→ T.

T→ .int

T → .int * T ε

ε

ε

ε

E

ε

ε ε

ε ε

ε

T

E → T.+E T

T → (E.) E T → (E). )

E → T+.E +

ε

ε E → T+E.

ET→ int. int

T → int.* T int

Page 59: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

NFA for Viable Prefixes in Detail (12)

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

59

T → . (E) T → (.E) (

S’ → E. E → . T+E

S’ → . E

E→ . T

E→ T.

T→ .int

T → .int * T ε

ε

ε

ε

E

ε

ε ε

ε ε

ε

T

E → T.+E T

T → (E.) E T → (E). )

E → T+.E +

ε

ε E → T+E.

ET→ int. int

T → int.* T int

T → int *.T *

Page 60: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

NFA for Viable Prefixes in Detail (13)

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

60

T → . (E) T → (.E) (

S’ → E. E → . T+E

S’ → . E

E→ . T

E→ T.

T→ .int

T → .int * T ε

ε

ε

ε

E

ε

ε ε

ε ε

ε

T

E → T.+E T

T → (E.) E T → (E). )

E → T+.E +

ε

ε E → T+E.

ET→ int. int

T → int.* T int

T → int *.T * T → int * T.

ε

ε

Page 61: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Translation to the DFA

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

61

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

T

Page 62: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Lingo

The states of the DFA are “canonical collections of items”

or “canonical collections of LR(0) items”

The Dragon book gives another way of constructing LR(0) items

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

62

Page 63: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Valid Items

Item X → β.γ is valid for a viable prefix αβ if S’ →* αXω → αβγω

by a right-most derivation After parsing αβ, the valid items are the

possible tops of the stack of items

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

63

Page 64: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Items Valid for a Prefix

An item I is valid for a viable prefix α if the DFA recognizing viable prefixes terminates on

input α in a state s containing I

The items in s describe what the top of the item stack might be after reading input α

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

64

Page 65: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

65

Valid Items Example

•  An item is often valid for many prefixes

•  Example: The item T → (.E) is valid for prefixes

( (( ((( (((( . . .

Page 66: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

66

Valid Items for (((…

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

Page 67: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

LR(0) Parsing

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

67

•  Idea: Assume –  stack contains α –  next input is t –  DFA on input α terminates in state s

•  Reduce by X → β if –  s contains item X → β.

•  Shift if –  s contains item X → β.tω –  equivalent to saying s has a transition labeled t

Page 68: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

LR(0) Conflicts

•  LR(0) has a reduce/reduce conflict if: –  Any state has two reduce items: –  X → β. and Y → ω.

•  LR(0) has a shift/reduce conflict if: –  Any state has a reduce item and a shift item: –  X → β. and Y → ω.tδ

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

68

Page 69: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

LR(0) Conflicts

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

69

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

T

Two shift/reduce conflicts with LR(0) rules

Page 70: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

SLR

•  LR = “Left-to-right scan” •  SLR = “Simple LR”

•  SLR improves on LR(0) shift/reduce heuristics –  Fewer states have conflicts

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

70

Page 71: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

SLR Parsing

•  Idea: Assume –  stack contains α –  next input is t –  DFA on input α terminates in state s

•  Reduce by X → β if –  s contains item X → β.

•  Shift if –  s contains item X → β.tω

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

71

–  t ∈ Follow(X)

Page 72: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

SLR Parsing (Cont.)

•  If there are conflicts under these rules, the grammar is not SLR

•  The rules amount to a heuristic for detecting handles –  The SLR grammars are those where the heuristics

detect exactly the handles

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

72

Page 73: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

SLR Conflicts

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

73

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

T

Follow(E) = { ‘)’, $ } Follow(T) = { ‘+’, ‘)’, $ } No conflicts with SLR rules!

Page 74: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Precedence Declarations Digression

•  Lots of grammars aren’t SLR –  including all ambiguous grammars

•  We can parse more grammars by using precedence declarations –  Instructions for resolving conflicts

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

74

Page 75: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Precedence Declarations (Cont.)

•  Consider our favorite ambiguous grammar: –  E → E + E | E * E | (E) | int

•  The DFA for this grammar contains a state with the following items: –  E → E * E . E → E . + E –  shift/reduce conflict!

•  Declaring “* has higher precedence than +” resolves this conflict in favor of reducing

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

75

Page 76: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Precedence Declarations (Cont.)

•  The term “precedence declaration” is misleading

•  These declarations do not define precedence; they define conflict resolutions –  Not quite the same thing!

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

76

Page 77: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Naïve SLR Parsing Algorithm

1.  Let M be DFA for viable prefixes of G 2.  Let |x1…xn$ be initial configuration 3.  Repeat until configuration is S|$

•  Let α|ω be current configuration •  Run M on current stack α •  If M rejects α, report parsing error

•  Stack α is not a viable prefix •  If M accepts α with items I, let a be next input

•  Shift if X → β. a γ ∈ I •  Reduce if X → β. ∈ I and a ∈ Follow(X) •  Report parsing error if neither applies

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

77

Page 78: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Notes

•  If there is a conflict in the last step, grammar is not SLR(k)

•  k is the amount of lookahead –  In practice k = 1

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

78

Page 79: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

SLR Example

Configuration DFA Halt State Action |int * int$ 1 shift

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

79

Page 80: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration |int * int$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

80

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 81: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

SLR Example

Configuration DFA Halt State Action |int * int$ 1 shift int | * int$ 3 * not in Follow(T) shift

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

81

Page 82: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration int | * int$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

82

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 83: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration int | * int$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

83

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 84: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

SLR Example

Configuration DFA Halt State Action |int * int$ 1 shift int | * int$ 3 * not in Follow(T) shift int * | int$ 11 shift

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

84

Page 85: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration int * | int$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

85

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 86: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration int * | int$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

86

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 87: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration int * | int$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

87

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 88: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

SLR Example

Configuration DFA Halt State Action |int * int$ 1 shift int | * int$ 3 * not in Follow(T) shift int * | int$ 11 shift int * int |$ 3 $ ∈ Follow(T) red. T→int

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

88

Page 89: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration int * int|$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

89

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 90: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration int * int|$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

90

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 91: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration int * int|$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

91

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 92: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration int * int|$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

92

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 93: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

SLR Example

Configuration DFA Halt State Action |int * int$ 1 shift int | * int$ 3 * not in Follow(T) shift int * | int$ 11 shift int * int |$ 3 $ ∈ Follow(T) red. T→int int * T |$ 4 $ ∈ Follow(T) red. T→int*T

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

93

Page 94: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration int * T|$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

94

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 95: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration int * T|$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

95

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 96: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration int * T|$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

96

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 97: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration int * T|$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

97

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 98: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

SLR Example

Configuration DFA Halt State Action |int * int$ 1 shift int | * int$ 3 * not in Follow(T) shift int * | int$ 11 shift int * int |$ 3 $ ∈ Follow(T) red. T→int int * T |$ 4 $ ∈ Follow(T) red. T→int*T T |$ 5 $ ∈ Follow(E) red. E→T

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

98

Page 99: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration T|$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

99

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 100: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Configuration T|$

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

100

S’ → . E E → . T

E → .T + E

T → .(E)

T → .int * T

T → .int

S’ → E . E → T. E → T. + E

T → int. * T T → int.

T → (. E) E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

E → T + E.

E → T + . E E → .T

E → .T + E

T → .(E)

T → .int * T

T → .int

T → int * .T T → .(E)

T → .int * T

T → .int

T → int * T.

T → (E.)

T → (E).

E T

(

int

int *

)

E

E

T

int (

(

int

T+

(

1

2

3 4

5 6

7

8

9 10

11

Page 101: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

SLR Example

Configuration DFA Halt State Action |int * int$ 1 shift int | * int$ 3 * not in Follow(T) shift int * | int$ 11 shift int * int |$ 3 $ ∈ Follow(T) red. T→int int * T |$ 4 $ ∈ Follow(T) red. T→int*T T |$ 5 $ ∈ Follow(T) red. E→T E |$ accept

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

101

Page 102: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Notes

•  Skipped using extra start state S’ in this example to save space on slides

•  Rerunning the automaton at each step is wasteful –  Most of the work is repeated

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

102

Page 103: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

An Improvement

•  Remember the state of the automaton on each prefix of the stack

•  Change stack to contain pairs 〈 Symbol, DFA State 〉

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

103

Page 104: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

An Improvement (Cont.)

•  For a stack 〈 sym1, state1 〉 . . . 〈 symn, staten 〉

staten is the final state of the DFA on sym1 … symn

•  Detail: The bottom of the stack is 〈any,start〉 where –  any is any dummy symbol –  start is the start state of the DFA

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

104

Page 105: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Goto Table

•  Define goto[i,A] = j if statei →A statej

•  goto is just the transition function of the DFA –  One of two parsing tables

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

105

Page 106: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Refined Parser Moves

•  Shift x –  Push 〈a, x〉 on the stack –  a is current input –  x is a DFA state

•  Reduce X → α –  As before

•  Accept •  Error

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

106

Page 107: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Action Table

For each state si and terminal a –  If si has item X → α.aβ and goto[i,a] = j then

action[i,a] = shift j

–  If si has item X → α. and a ∈ Follow(X) and X ≠ S’ then action[i,a] = reduce X → α

–  If si has item S’ → S. then action[i,$] = accept

–  Otherwise, action[i,a] = error

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

107

Page 108: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

SLR Parsing Algorithm

Let I = w$ be initial input Let j = 0 Let DFA state 1 have item S’ → .S Let stack = 〈 dummy, 1 〉

repeat case action[top_state(stack),I[j]] of shift k: push 〈 I[j++], k 〉 reduce X → A: pop |A| pairs, push 〈X, goto[top_state(stack),X]〉 accept: halt normally error: halt and report error

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

108

Page 109: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

Notes on SLR Parsing Algorithm

•  Note that the algorithm uses only the DFA states and the input –  The stack symbols are never used!

•  However, we still need the symbols for semantic actions

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

109

Page 110: Lecture 10 - University of Waterloovganesh/TEACHING/S2014... · Ganesh. Lecture 10) Review: Bottom-Up Parsing • Bottom-up parsing is more general than top-down parsing – And just

More Notes

•  Some common constructs are not SLR(1)

•  LR(1) is more powerful –  Build lookahead into the items –  An LR(1) item is a pair: LR(0) item x lookahead –  [T→ . int * T, $] means

•  After seeing T→ int * T reduce if lookahead is $ –  More accurate than just using follow sets –  Take a look at the LR(1) automaton for your parser!

Prof. Aiken (Modified by Professor Vijay Ganesh. Lecture 10)

110