{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs160-lec9 - CMPSC 160 Translation of Programming Languages...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CMPSC 160 Translation of Programming Languages Lecture 9: Introduction to LR Parsing LR Parsers Shift-reduce parsers are very fast and simple to implement They keep moving more input onto the stack All the while they look for handles to reduce The tricky part is recognizing the handles The big picture is that LR parsers use a state machine (to recognize handles) in coordination with a stack (to handle the recursive nature of grammars) to parse an input.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 LR Parsers Then the hard part is figuring out the language of handles that we need to recognize There are several different ways of doing this: SLR, LALR, LR each of varying power and complexity At the end of the day, they all generate a state machine in the form of a table LR Parsers A table-driven LR parser looks like Scanner Table-driven Parser A CTION & G OTO Tables Parser Generator source code grammar IR Stack
Background image of page 2
3 LR Parser Tables SLR, LALR, and LR are all different ways of automatically generating a state machine to capture handles encoded into the form of a table. However, unlike a normal state machine, this one is searching the top of a stack There are in fact two different tables The ACTION table tells you if you should shift, reduce, accept, or throw and error. It additionally tells you how to update the state on a shift The GOTO table tells you how to update the state on a reduce The other major difference is that now we not only push symbols onto the stack, we also push an associated state onto the stack with each symbol (always in pairs: 1 symbol and 1 state (just a number)) Refinement of the Bottom-up Parsing Algorithm A simple algorithm to construct a rightmost derivation: S γ 0 γ 1 γ 2 γ n-1 γ n w for i n to 1 by -1 Find the handle < α i →β i , k i > in γ i Replace β i with α i to generate γ i-1 Basic shift-reduce parsing algorithm push $ lookahead = get_ next_token( ) repeat until (top of stack == start symbol and lookahead == $) if the top of the stack is a handle α→β then /* reduce β to α */ pop | β | symbols off the stack push α onto the stack else if (token $) then /* shift */ push lookahead lookahead = get_next_token( )
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 push($); // $ is the end-of-file symbol push( s 0 ); // s 0 is the start state of the DFA that recognizes handles lookahead = get_next_token(); repeat forever s = top_of_stack(); if ( ACTION[s,lookahead] == reduce α→β ) then pop 2*| β | symbols; s = top_of_stack(); push( α ); push(GOTO[ s , α ]); else if ( ACTION[ s ,lookahead] == shift s i ) then push(lookahead); push( s i ); lookahead = get_next_token(); else if ( ACTION[s,lookahead] == accept and lookahead == $ ) then return success; else error(); The skeleton parser uses A CTION & G OTO does | words | shifts does |derivation| reductions does 1 accept LR Shift-Reduce Parsers First part: Initialization Push $ and the specified init state (usually called s 0 ) Case: Shift We consult the ACTION table, and see if it tells us to do a shift If it does, then we move the look-ahead onto the stack and
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}