This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CMPSC 160 Translation of Programming Languages Lecture 8: Introduction to LR Parsing LR Parsers • Shiftreduce 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. 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 tabledriven LR parser looks like Scanner Tabledriven Parser A CTION & G OTO Tables Parser Generator source code grammar IR Stack 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 Bottomup Parsing Algorithm A simple algorithm to construct a rightmost derivation: S ⇒ γ ⇒ γ 1 ⇒ γ 2 ⇒ … ⇒ γ n1 ⇒ γ n ⇒ w for i ← n to 1 by 1 Find the handle < α i →β i , k i > in γ i Replace β i with α i to generate γ i1 Basic shiftreduce 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( ) push($); // $ is the endoffile symbol push( s ); // s 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 ShiftReduce Parsers • First part: Initialization – Push $ and the specified init state (usually called s 0 ) • Case: Shift...
View
Full Document
 Spring '12
 Drexel
 Contextfree grammar, Bottomup parsing, LR parser

Click to edit the document details