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

Similar documents
Review: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk

Bottom-Up Parsing II. Lecture 8

Outline. The strategy: shift-reduce parsing. Introduction to Bottom-Up Parsing. A key concept: handles

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

Introduction to Bottom-Up Parsing

Intro to Bottom-up Parsing. Lecture 9

Bottom-Up Parsing. Lecture 11-12

A bottom-up parser traces a rightmost derivation in reverse. Bottom-Up Parsing. Bottom-up parsing is more general than topdown.

Bottom-Up Parsing. Lecture 11-12

Lecture 8: Deterministic Bottom-Up Parsing

Lecture 7: Deterministic Bottom-Up Parsing

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Bottom-Up Parsing LR Parsing

LR Parsing LALR Parser Generators

LR Parsing LALR Parser Generators

S Y N T A X A N A L Y S I S LR

Lexical and Syntax Analysis. Bottom-Up Parsing

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Bottom up parsing. The sentential forms happen to be a right most derivation in the reverse order. S a A B e a A d e. a A d e a A B e S.

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Compilers. Bottom-up Parsing. (original slides by Sam

LR Parsing Techniques

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing

Lecture Bottom-Up Parsing

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

A left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence.

Shift. Reduce. Review: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. ABC xyz ABCx yz. Lecture 8.

SLR parsers. LR(0) items

In One Slide. Outline. LR Parsing. Table Construction

shift-reduce parsing

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

MODULE 14 SLR PARSER LR(0) ITEMS

CS 4120 Introduction to Compilers

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

Monday, September 13, Parsers

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

UNIT-III BOTTOM-UP PARSING

Conflicts in LR Parsing and More LR Parsing Types

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Context-free grammars

Wednesday, August 31, Parsers

Principles of Programming Languages

Parser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Compiler Construction: Parsing

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

LR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing.

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

UNIT III & IV. Bottom up parsing

CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG)

LR Parsing, Part 2. Constructing Parse Tables. An NFA Recognizing Viable Prefixes. Computing the Closure. GOTO Function and DFA States

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Bottom Up Parsing Handout. 1 Introduction. 2 Example illustrating bottom-up parsing

Ambiguity. Grammar E E + E E * E ( E ) int. The string int * int + int has two parse trees. * int

Example CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.

CSE302: Compiler Design

More Bottom-Up Parsing

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

Compiler Construction 2016/2017 Syntax Analysis

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

LR Parsing. Table Construction

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

LR(0) Parsing Summary. LR(0) Parsing Table. LR(0) Limitations. A Non-LR(0) Grammar. LR(0) Parsing Table CS412/CS413

LALR Parsing. What Yacc and most compilers employ.

Ambiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.


Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

LR Parsing Techniques

Chapter 4: LR Parsing

Administrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:30-5:00 TT 10 Evans.

CS2210: Compiler Construction Syntax Analysis Syntax Analysis

LALR stands for look ahead left right. It is a technique for deciding when reductions have to be made in shift/reduce parsing. Often, it can make the

CS606- compiler instruction Solved MCQS From Midterm Papers

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University

CS 164 Programming Languages and Compilers Handout 9. Midterm I Solution

Bottom-Up Parsing. Parser Generation. LR Parsing. Constructing LR Parser

Syntax Analyzer --- Parser

Top down vs. bottom up parsing

Concepts Introduced in Chapter 4

Downloaded from Page 1. LR Parsing

Lecture Notes on Shift-Reduce Parsing

CS415 Compilers. LR Parsing & Error Recovery

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

How do LL(1) Parsers Build Syntax Trees?

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Transcription:

Bottom-Up Parsing II Different types of Shift-Reduce Conflicts) Lecture 10 Ganesh. Lecture 10) 1

Review: Bottom-Up Parsing Bottom-up parsing is more general than topdown 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 LRk) parsers are bottom-up 2

Review: An Introductory xample Bottom-up parsers don t need left-factored grammars Revert to the natural grammar for our example: + * ) Consider the string: * + 3

Review: he Idea Bottom-up parsing reduces a string to the start symbol by inverting productions: * + * + + + + * + 4

Review: Bottom-up Parsers and Rightmost derivations in reverse Read the productions in reverse from bottom to top) his is a rightmost derivation! * + * + + + + * + 5

Review: Notation useful for Shift-Reduce Parsers Idea: Split string o two substrings Right substring is as yet unexamined by parsing a string of terminals) Left substring has terminals and non-terminals he dividing po is marked by a he is not part of the string Initially, all input is unexamined x 1 x 2... x n 6

Review: Shift-Reduce Parsing Bottom-up parsing uses only two kinds of actions: Shift Reduce 7

Review: Shift Shift: Move one place to the right Shifts a terminal to the left string ABC xyz ABCx yz 8

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

he Stack Left string can be implemented by a stack op 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 nonterminal on the stack production lhs) Prof. Aiken CS 143 Lecture 7 10

Review: he xample with Reductions Only * + * + reduce reduce * + + + reduce reduce reduce + 11

Review: he xample with Shift-Reduce Parsing * + * + * + * + * + + + + + + shift shift shift reduce reduce * shift shift reduce reduce reduce + 12

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 shiftreduce conflict If it is legal to reduce by two different productions, there is a reduce-reduce conflict Prof. Alex Aiken Lecture 9 Modified by Professor Vijay Ganesh) 13

Key Issue: o Shift or Reduce? How do we decide when to shift or reduce? xample grammar: + * ) Consider step * + We could reduce by giving * + A fatal mistake! No way to reduce to the start symbol 14

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. hen β in the position after α is a handle of αβω 15

Handles Cont.) Handles formalize the uition 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 16

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 o the stack left context) and the next k lookahead symbols right context), decide to shift or reduce 17

A Shift-Reduce Parse in Detail 1) * + * + Prof. Alex Aiken Lecture 9 Modified by Professor Vijay Ganesh) 18

A Shift-Reduce Parse in Detail 2) * + * + * + Prof. Alex Aiken Lecture 9 Modified by Professor Vijay Ganesh) 19

A Shift-Reduce Parse in Detail 3) * + * + * + * + Prof. Alex Aiken Lecture 9 Modified by Professor Vijay Ganesh) 20

A Shift-Reduce Parse in Detail 4) * + * + * + * + * + Prof. Alex Aiken Lecture 9 Modified by Professor Vijay Ganesh) 21

A Shift-Reduce Parse in Detail 5) * + * + * + * + * + * + Prof. Alex Aiken Lecture 9 Modified by Professor Vijay Ganesh) 22

A Shift-Reduce Parse in Detail 6) * + * + * + * + * + + * + Prof. Alex Aiken Lecture 9 Modified by Professor Vijay Ganesh) 23

A Shift-Reduce Parse in Detail 7) * + * + * + * + * + + + * + Prof. Alex Aiken Lecture 9 Modified by Professor Vijay Ganesh) 24

A Shift-Reduce Parse in Detail 8) * + * + * + * + * + + + + * + Prof. Alex Aiken Lecture 9 Modified by Professor Vijay Ganesh) 25

A Shift-Reduce Parse in Detail 9) * + * + * + * + * + + + + + * + Prof. Alex Aiken Lecture 9 Modified by Professor Vijay Ganesh) 26

A Shift-Reduce Parse in Detail 10) * + * + * + * + * + + + + + + * + Prof. Alex Aiken Lecture 9 Modified by Professor Vijay Ganesh) 27

A Shift-Reduce Parse in Detail 11) * + * + * + * + * + + + + + + * Prof. Alex Aiken Lecture 9 Modified by Professor Vijay Ganesh) + 28

heorem: Handle always on top of Stack Proof Method: Informal induction on # of reduce moves Proof: rue 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 29

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 herefore, shift-reduce moves are sufficient; the need never move left Bottom-up parsing algorithms are based on recognizing handles 30

Recognizing Handles here 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 31

Grammars All CFGs Unambiguous CFGs will generate conflicts SLR CFGs 32

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 33

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 34

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

Important Fact #3 Cont.) Important Fact #3 is non-obvious We show how to compute automata that accept viable prefixes 36

Items An item is a production with a. somewhere on the rhs he items for ) are.).).) ). 37

Items Cont.) he only item for X is X. Items are often called LR0) items 38

Intuition he 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 hese bits and pieces are always prefixes of rhs of productions 39

xample Consider the input ) hen ) is a state of a shift-reduce parse is a prefix of the rhs of ) Will be reduced after the next shift Item.) says that so far we have seen of this production and hope to see ) 40

Generalization he stack may have many prefixes of rhs s Prefix 1 Prefix 2... Prefix n-1 Prefix n Let Prefix i be a prefix of rhs of X i α i Prefix i will eventually reduce to X i he missing part of α i-1 starts with X i i.e. there is a X i-1 Prefix i-1 X i β for some β Recursively, Prefix k+1 Prefix n eventually reduces to the missing part of α k 41

An xample Consider the string * ): * ) is a state of a shift-reduce parse is a prefix of the rhs of ) is a prefix of the rhs of * is a prefix of the rhs of * 42

An xample Cont.) he stack of items.). *. Says We ve seen of ) We ve seen of We ve seen * of * 43

Recognizing Viable Prefixes Idea: o recognize viable prefixes, we must Recognize a sequence of partial rhs s of productions, where ach sequence can eventually reduce to part of the missing suffix of its predecessor 44

An NFA Recognizing Viable Prefixes 1. Add a dummy production S S to G 2. he NFA states are the items of G Including the extra production 3. For item α.xβ add transition α.xβ X αx.β 4. For item α.xβ and production X γ add α.xβ X.γ 45

An NFA Recognizing Viable Prefixes Cont.) 5. very state is an accepting state 6. Start state is S.S 46

NFA for Viable Prefixes of the xample ). ).).) ). S.. + S...+ +. +. +... *..* * *. 47

NFA for Viable Prefixes in Detail 1) S. 48

NFA for Viable Prefixes in Detail 2) S. S.. +. 49

NFA for Viable Prefixes in Detail 3). ) S.. + S.... 50

NFA for Viable Prefixes in Detail 4). ) S.. + S...+.. 51

NFA for Viable Prefixes in Detail 5). ).) S.. + S...+.. 52

NFA for Viable Prefixes in Detail 6). ).) S.. + S...).+.. 53

NFA for Viable Prefixes in Detail 7). ).).) ) ). S.. + S...+.. 54

NFA for Viable Prefixes in Detail 8). ).).) ) ). S.. + S...+ + +... 55

NFA for Viable Prefixes in Detail 9). ).) S.. + S...).+ + ) ). +. +... 56

NFA for Viable Prefixes in Detail 10). ).) S.. + S...).+. ) + ). +. +... 57

NFA for Viable Prefixes in Detail 11). ).) S.. + S...).+. ) + ). +. +...*. 58

NFA for Viable Prefixes in Detail 12). ).) S.. + S...).+. ) + ). +. +... *..* * 59

NFA for Viable Prefixes in Detail 13). ).) S.. + S...).+. ) + ). +. +... *..* * *. 60

+. ranslation to the DFA S.. +. +. * S.... + *.) *..).. +.). *..) +.. ).. +.)... ). ) 61

Lingo he states of the DFA are canonical collections of items or canonical collections of LR0) items he Dragon book gives another way of constructing LR0) items 62

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 63

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 he items in s describe what the top of the item stack might be after reading input α 64

Valid Items xample An item is often valid for many prefixes xample: he item.) is valid for prefixes... 65

+. Valid Items for S... +. * S.... + *.) *..) +.. +.). *..) +.. ).. +.)... ). ) 66

LR0) 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ω equivalent to saying s has a transition labeled t 67

LR0) Conflicts LR0) has a reduce/reduce conflict if: Any state has two reduce items: X β. and Y ω. LR0) has a shift/reduce conflict if: Any state has a reduce item and a shift item: X β. and Y ω.tδ 68

+. LR0) Conflicts S... +. * S.... + *.) *..) +. +.. +.).. ).. + *..).) wo shift/reduce conflicts with LR0) rules... ). ) 69

SLR LR = Left-to-right scan SLR = Simple LR SLR improves on LR0) shift/reduce heuristics Fewer states have conflicts 70

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 β. t FollowX) Shift if s contains item X β.tω 71

SLR Parsing Cont.) If there are conflicts under these rules, the grammar is not SLR he rules amount to a heuristic for detecting handles he SLR grammars are those where the heuristics detect exactly the handles 72

+. SLR Conflicts S... + S... *.. + *.). *..). +.. +.). *..) ) ). +.. ).. + Follow) = { ), $ }.) Follow) = { +, ), $ }. No conflicts with SLR rules! 73

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 74

Precedence Declarations Cont.) Consider our favorite ambiguous grammar: + * ) he DFA for this grammar contains a state with the following items: *.. + shift/reduce conflict! Declaring * has higher precedence than + resolves this conflict in favor of reducing 75

Precedence Declarations Cont.) he term precedence declaration is misleading hese declarations do not define precedence; they define conflict resolutions Not quite the same thing! 76

Naïve SLR Parsing Algorithm 1. Let M be DFA for viable prefixes of G 2. Let x 1 x n $ 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 FollowX) Report parsing error if neither applies 77

Notes If there is a conflict in the last step, grammar is not SLRk) k is the amount of lookahead In practice k = 1 78

SLR xample Configuration DFA Halt State Action * $ 1 shift 79

2 Configuration * $ S.. S... +.) 1.. +. *. * *..) 5 3 11 ).. + 4 +... +.). *..) ) 10 6 9 +.. ).. +.) 8. 7 80

SLR xample Configuration DFA Halt State Action * $ 1 shift * $ 3 * not in Follow) shift 81

2 Configuration * $ S.. S... +.) 1.. +. *. * *..) 5 3 11 +.. ).. + 4. +.). *..) ) 10 6 9 +.. ).. +.) 8. 7 82

2 Configuration * $ S.. S... +.) 1.. +. *. * *..) 5 3 11 +.. ).. + 4. +.). *..) ) 10 6 9 +.. ).. +.) 8. 7 83

SLR xample Configuration DFA Halt State Action * $ 1 shift * $ 3 * not in Follow) shift * $ 11 shift 84

2 Configuration * $ S.. S... +.) 1.. +. *. * *..) 5 3 11 +.. ).. + 4. +.). *..) ) 10 6 9 +.. ).. +.) 8. 7 85

2 Configuration * $ S.. S... +.) 1.. +. *. * *..) 5 3 11 +.. ).. + 4. +.). *..) ) 10 6 9 +.. ).. +.) 8. 7 86

2 Configuration * $ S.. S... +.) 1.. +. *. * *..) 5 3 11 +.. ).. + 4. +.). *..) ) 10 6 9 +.. ).. +.) 8. 7 87

SLR xample Configuration DFA Halt State Action * $ 1 shift * $ 3 * not in Follow) shift * $ 11 shift * $ 3 $ Follow) red. 88

2 Configuration * $ S.. S... +.) 1.. +. *. * *..) 5 3 11 ).. + 4 +... +.). *..) ) 10 6 9 +.. ).. +.) 8. 7 89

2 Configuration * $ S.. S... +.) 1.. +. *. * *..) 5 3 11 ).. + 4 +... +.). *..) ) 10 6 9 +.. ).. +.) 8. 7 90

2 Configuration * $ S.. S... +.) 1.. +. *. * *..) 5 3 11 ).. + 4 +... +.). *..) ) 10 6 9 +.. ).. +.) 8. 7 91

2 Configuration * $ S.. S... +.) 1.. +. *. * *..) 5 3 11 ).. + 4 +... +.). *..) ) 10 6 9 +.. ).. +.) 8. 7 92

SLR xample Configuration DFA Halt State Action * $ 1 shift * $ 3 * not in Follow) shift * $ 11 shift * $ 3 $ Follow) red. * $ 4 $ Follow) red. * 93

2 Configuration * $ S.. S... +.) 1.. +. *. * *..) 5 3 11 ).. + 4 +... +.). *..) ) 10 6 9 +.. ).. +.) 8. 7 94

2 Configuration * $ S.. S... +.) 1.. +. *. * *..) 5 3 11 ).. + 4 +... +.). *..) ) 10 6 9 +.. ).. +.) 8. 7 95

2 Configuration * $ S.. S... +.) 1.. +. *. * *..) 5 3 11 ).. + 4 +... +.). *..) ) 10 6 9 +.. ).. +.) 8. 7 96

2 Configuration * $ S.. S... +.) 1.. +. *. * *..) 5 3 11 ).. + 4 +... +.). *..) ) 10 6 9 +.. ).. +.) 8. 7 97

SLR xample Configuration DFA Halt State Action * $ 1 shift * $ 3 * not in Follow) shift * $ 11 shift * $ 3 $ Follow) red. * $ 4 $ Follow) red. * $ 5 $ Follow) red. 98

+. 7 2 Configuration $ S.. S... +.) 1.. +. *. * *..) 5 3 11. + 4.. +.). *..) ) ). 10 6 9 +.. ).. +.) 8. 99

+. 7 2 Configuration $ S.. S... +.) 1.. +. *. * *..) 5 3 11. + 4.. +.). *..) ) ). 10 6 9 +.. ).. +.) 8. 100

SLR xample Configuration DFA Halt State Action * $ 1 shift * $ 3 * not in Follow) shift * $ 11 shift * $ 3 $ Follow) red. * $ 4 $ Follow) red. * $ 5 $ Follow) red. $ accept 101

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 102

An Improvement Remember the state of the automaton on each prefix of the stack Change stack to contain pairs Symbol, DFA State 103

An Improvement Cont.) For a stack sym 1, state 1... sym n, state n state n is the final state of the DFA on sym 1 sym n Detail: he bottom of the stack is any,start where any is any dummy symbol start is the start state of the DFA 104

Goto able Define goto[i,a] = j if state i A state j goto is just the transition function of the DFA One of two parsing tables 105

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 rror 106

Action able For each state s i and terminal a If s i has item X α.aβ and goto[i,a] = j then action[i,a] = shift j If s i has item X α. and a FollowX) and X S then action[i,a] = reduce X α If s i has item S S. then action[i,$] = accept Otherwise, action[i,a] = error 107

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_statestack),i[j]] of shift k: push I[j++], k reduce X A: pop A pairs, push X, goto[top_statestack),x] accept: halt normally error: halt and report error 108

Notes on SLR Parsing Algorithm Note that the algorithm uses only the DFA states and the input he stack symbols are never used! However, we still need the symbols for semantic actions 109

More Notes Some common constructs are not SLR1) LR1) is more powerful Build lookahead o the items An LR1) item is a pair: LR0) item x lookahead [. *, $] means After seeing * reduce if lookahead is $ More accurate than just using follow sets ake a look at the LR1) automaton for your parser! 110