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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPSC 160 Translation of Programming Languages Lecture 8: 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. 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 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 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( ) push($); // $ is the end-of-file 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 Shift-Reduce Parsers First part: Initialization Push $ and the specified init state (usually called s 0 ) Case: Shift...
View Full Document

Page1 / 31

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

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online