lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · lecture 8 1...

89
Bottom-Up Parsing II Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Upload: others

Post on 09-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Bottom-Up Parsing II

Lecture 8

1

Instructor: Fredrik KjolstadSlides based on slides designed by Prof. Alex Aiken

Page 2: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Review: Shift-Reduce Parsing

Bottom-up parsing uses two actions:

ShiftABC|xyz Þ ABCx|yz

ReduceCbxy|ijk Þ CbA|ijk

2

Page 3: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Recall: 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– pushes a non-terminal on the stack

• production lhs

3

Page 4: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Key Issue

• How do we decide when to shift or reduce?

• Example grammar:E ® T + E | TT ® 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

4

Page 5: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Definition: Handles

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

• Assume a rightmost derivation S ®* aXw ® abw

• Then X ® b in the position after a is a handle of abw

5

Page 6: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

6

Page 7: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

7

Page 8: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Why?

• Informal induction on # of reduce moves:

• 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

8

Page 9: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

9

Page 10: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

10

Page 11: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Grammars

11

All CFGs

Unambiguous CFGs

SLR CFGs

will generateconflicts

Page 12: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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 . . .

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

12

Page 13: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

13

Page 14: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Important Fact #3

Important Fact #3 about bottom-up parsing:

For any grammar, the set of viable prefixes is a regular language

14

Page 15: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Important Fact #3 (Cont.)

• Important Fact #3 is non-obvious

• We show how to compute automata that accept viable prefixes

15

Page 16: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Items

• An item is a production with a “.” somewhere on the rhs, denoting a focus point

• The items for T ® (E) areT ® .(E)T ® (.E)T ® (E.)T ® (E).

16

Page 17: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Items (Cont.)

• The only item for X ® e is X ® .

• Items are often called “LR(0) items”

17

Page 18: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

18

Page 19: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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 )

19

Page 20: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Generalization

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

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

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

20

Page 21: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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)“e” is a prefix of the rhs of E ® T“int *” is a prefix of the rhs of T ® int * T

21

E ® T + E | TT ® int * T | int | (E)

Page 22: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

An Example (Cont.)

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

SaysWe’ve seen “(” of T ® (E)We’ve seen e of E ® TWe’ve seen int * of T ® int * T

22

E ® T + E | TT ® int * T | int | (E)

Page 23: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

23

Page 24: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

An NFA Recognizing Viable Prefixes

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

– Including the extra production

3. For item E ® a.Xb add transitionE ® a.Xb ®X E ® aX.b

4. For item E ® a.Xb and production X ® g add E ® a.Xb ®e X ® .g

24

Page 25: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

An NFA Recognizing Viable Prefixes (Cont.)

5. Every state is an accepting state

6. Start state is S’® .S

25

Page 26: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

NFA for Viable Prefixes

26

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.* Te

e

e

e

E

e

T

ee

e

E

+

e

int

int*

Te

e e

e

e

Te

E ® T + E | TT ® int * T | int | (E)

Page 27: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

NFA for Viable Prefixes

27

S’® . E

E ® T + E | TT ® int * T | int | (E)

Page 28: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

NFA for Viable Prefixes

28

S’® . E

S’® E.E

E® . T

e

E ® . T+E

e

E ® T + E | TT ® int * T | int | (E)

Page 29: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

NFA for Viable Prefixes

29

S’® E.E ® . T+E

S’® . E

E® . T

T ® .int * Te

e

E

T ® . (E)

eT® .int

e

e

E® T.

T

E ® T + E | TT ® int * T | int | (E)

Page 30: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

NFA for Viable Prefixes

30

T ® . (E)

S’® E.E ® . T+E

S’® . E

E® . T

E® T.

T® .int

T ® .int * Te

e

EE ® T.+ET

e

ee

e e

e

T

E ® T + E | TT ® int * T | int | (E)

Page 31: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

NFA for Viable Prefixes

31

T ® . (E) T ® (.E)(

S’® E.E ® . T+E

S’® . E

E® . T

E® T.

T® .int

T ® .int * Te

e

E

e

ee

e e

e

T

E ® T.+ET

E ® T + E | TT ® int * T | int | (E)

Page 32: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

NFA for Viable Prefixes

32

T ® . (E) T ® (.E)(

S’® E.E ® . T+E

S’® . E

E® . T

E® T.

T® .int

T ® .int * Te

e

e

e

E

e

ee

e e

e

T

E ® T.+ET

T ® (E.)E

E ® T + E | TT ® int * T | int | (E)

Page 33: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

NFA for Viable Prefixes

33

T ® . (E) T ® (.E)(

S’® E.E ® . T+E

S’® . E

E® . T

E® T.

T® .int

T ® .int * Te

e

e

e

E

e

ee

e e

e

T

E ® T.+ET

T ® (E.)E T ® (E).)

E ® T + E | TT ® int * T | int | (E)

Page 34: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

NFA for Viable Prefixes

34

T ® . (E) T ® (.E)(

S’® E.E ® . T+E

S’® . E

E® . T

E® T.

T® .int

T ® .int * Te

e

e

e

E

e

ee

e e

e

T

E ® T.+ET

T ® (E.)E T ® (E).)

E ® T+.E+

E ® T + E | TT ® int * T | int | (E)

Page 35: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

NFA for Viable Prefixes

35

T ® . (E) T ® (.E)(

S’® E.E ® . T+E

S’® . E

E® . T

E® T.

T® .int

T ® .int * Te

e

e

e

E

e

ee

e e

e

T

E ® T.+ET

T ® (E.)E T ® (E).)

E ® T+.E+

e

e E ® T+E.

E

E ® T + E | TT ® int * T | int | (E)

Page 36: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

NFA for Viable Prefixes

36

T ® . (E) T ® (.E)(

S’® E.E ® . T+E

S’® . E

E® . T

E® T.

T® .int

T ® .int * Te

e

e

e

E

e

ee

e e

e

T

E ® T.+ET

T ® (E.)E T ® (E).)

E ® T+.E+

e

e E ® T+E.

ET® int.int

E ® T + E | TT ® int * T | int | (E)

Page 37: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

NFA for Viable Prefixes

37

T ® . (E) T ® (.E)(

S’® E.E ® . T+E

S’® . E

E® . T

E® T.

T® .int

T ® .int * Te

e

e

e

E

e

ee

e e

e

T

E ® T.+ET

T ® (E.)E T ® (E).)

E ® T+.E+

e

e E ® T+E.

ET® int.int

T ® int.* Tint

E ® T + E | TT ® int * T | int | (E)

Page 38: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

NFA for Viable Prefixes

38

T ® . (E) T ® (.E)(

S’® E.E ® . T+E

S’® . E

E® . T

E® T.

T® .int

T ® .int * Te

e

e

e

E

e

ee

e e

e

T

E ® T.+ET

T ® (E.)E T ® (E).)

E ® T+.E+

e

e E ® T+E.

ET® int.int

T ® int.* Tint

T ® int *.T*

E ® T + E | TT ® int * T | int | (E)

Page 39: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

NFA for Viable Prefixes

39

T ® . (E) T ® (.E)(

S’® E.E ® . T+E

S’® . E

E® . T

E® T.

T® .int

T ® .int * Te

e

e

e

E

e

ee

e e

e

T

E ® T.+ET

T ® (E.)E T ® (E).)

E ® T+.E+

e

e E ® T+E.

ET® int.int

T ® int.* Tint

T ® int *.T* T ® int * T.

Te

e

e

E ® T + E | TT ® int * T | int | (E)

Page 40: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Translation to the DFA

40

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 41: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

41

Page 42: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Valid Items

Item X ® b.g is valid for a viable prefix ab ifS’®* aXw ® abgw

by a right-most derivation

After parsing ab, the valid items are the possible tops of the stack of items

42

Page 43: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Items Valid for a Prefix

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

input a in a state s containing I

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

43

Page 44: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

44

Valid Items Example

• An item is often valid for many prefixes

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

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

Page 45: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

45

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 46: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

LR(0) Parsing

46

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

• Reduce by X ® b if– s contains item X ® b.

• Shift if– s contains item X ® b.tw– equivalent to saying s has a transition labeled t

Page 47: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

LR(0) Conflicts

• LR(0) has a reduce/reduce conflict if:– Any state has two reduce items:– X ® b. and Y ® w.

• LR(0) has a shift/reduce conflict if:– Any state has a reduce item and a shift item:– X ® b. and Y ® w.td

47

Page 48: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

LR(0) Conflicts

48

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/reduceconflicts withLR(0) rules

Page 49: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

SLR

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

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

49

Page 50: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

SLR Parsing

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

• Reduce by X ® b if– s contains item X ® b.

• Shift if– s contains item X ® b.tw

50

– t Î Follow(X)

Page 51: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

51

Page 52: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

SLR Conflicts

52

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 withSLR rules!

Page 53: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

53

Page 54: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

54

Page 55: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Precedence Declarations (Cont.)

• The term “precedence declaration” is misleading

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

55

Page 56: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Naïve SLR Parsing Algorithm

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

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

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

• Reduce if X ® b. Î I and a Î Follow(X)• Shift if X ® b. a g Î I• Report parsing error if neither applies

56

Page 57: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

57

Page 58: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

SLR Example

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

58

Page 59: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration |int * int$

59

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

34

56

7

8

910

11

Page 60: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

SLR Example

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

60

Page 61: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration int | * int$

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+

(

1

2

34

56

7

8

910

11

Page 62: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration int | * int$

62

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

34

56

7

8

910

11

Page 63: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

SLR Example

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

63

Page 64: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration int * | int$

64

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

34

56

7

8

910

11

Page 65: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration int * | int$

65

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

34

56

7

8

910

11

Page 66: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration int * | int$

66

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

34

56

7

8

910

11

Page 67: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

SLR Example

Configuration DFA Halt State Action|int * int$ 1 shiftint | * int$ 3 * not in Follow(T) shiftint * | int$ 11 shiftint * int |$ 3 $ Î Follow(T) red. T®int

67

Page 68: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration int * int|$

68

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

34

56

7

8

910

11

Page 69: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration int * int|$

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+

(

1

2

34

56

7

8

910

11

Page 70: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration int * int|$

70

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

34

56

7

8

910

11

Page 71: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration int * int|$

71

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

34

56

7

8

910

11

Page 72: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

SLR Example

Configuration DFA Halt State Action|int * int$ 1 shiftint | * int$ 3 * not in Follow(T) shiftint * | int$ 11 shiftint * int |$ 3 $ Î Follow(T) red. T®intint * T |$ 4 $ Î Follow(T) red. T®int*T

72

Page 73: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration int * T|$

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+

(

1

2

34

56

7

8

910

11

Page 74: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration int * T|$

74

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

34

56

7

8

910

11

Page 75: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration int * T|$

75

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

34

56

7

8

910

11

Page 76: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration int * T|$

76

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

34

56

7

8

910

11

Page 77: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

SLR Example

Configuration DFA Halt State Action|int * int$ 1 shiftint | * int$ 3 * not in Follow(T) shiftint * | int$ 11 shiftint * int |$ 3 $ Î Follow(T) red. T®intint * T |$ 4 $ Î Follow(T) red. T®int*TT |$ 5 $ Î Follow(E) red. E®T

77

Page 78: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration T|$

78

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

34

56

7

8

910

11

Page 79: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Configuration T|$

79

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

34

56

7

8

910

11

Page 80: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

SLR Example

Configuration DFA Halt State Action|int * int$ 1 shiftint | * int$ 3 * not in Follow(T) shiftint * | int$ 11 shiftint * int |$ 3 $ Î Follow(T) red. T®intint * T |$ 4 $ Î Follow(T) red. T®int*TT |$ 5 $ Î Follow(T) red. E®TE |$ accept

80

Page 81: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

81

Page 82: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

An Improvement

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

• Change stack to contain pairsá Symbol, DFA State ñ

82

Page 83: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

83

Page 84: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

84

Page 85: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Refined Parser Moves

• Shift x– Push áa, xñ on the stack – a is current input– x is a DFA state

• Reduce X ® a– As before

• Accept• Error

85

Page 86: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

Action Table

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

action[i,a] = shift j

– If si has item X ® a. and a Î Follow(X) and X ¹ S’then action[i,a] = reduce X ® a

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

– Otherwise, action[i,a] = error

86

Page 87: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

SLR Parsing Algorithm

Let I = w$ be initial inputLet j = 0Let DFA state 1 have item S’® .SLet stack = á dummy, 1 ñ

repeatcase 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 normallyerror: halt and report error

87

Page 88: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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

88

Page 89: Lecture 8 - web.stanford.eduweb.stanford.edu/class/cs143/lectures/lecture08.pdf · Lecture 8 1 Instructor: Fredrik Kjolstad Slides based on slides designed by Prof. Alex Aiken

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!

89