This preview shows pages 1–5. Sign up to view the full content.
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 Document
Unformatted text preview: 1 CMPSC 160 Translation of Programming Languages Lecture 9: 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. 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 tabledriven LR parser looks like Scanner Tabledriven Parser ACTION & GOTO Tables Parser Generator source code grammar IR Stack 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 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( ) 4 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 ACTION & GOTO 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
This note was uploaded on 11/23/2010 for the course MATH 104b taught by Professor Ceniceros,h during the Spring '08 term at UCSB.
 Spring '08
 Ceniceros,H
 Numerical Analysis

Click to edit the document details