lec15mod - CS415 Compilers LR(1) parsing These slides are...

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

View Full Document Right Arrow Icon
CS415 Compilers LR(1) parsing These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University
Background image of page 1

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

View Full DocumentRight Arrow Icon
Lecture 15 2 cs415, spring 10 LR(k) items The LR(1) table construction algorithm uses LR(1) items to represent valid configurations of an LR(1) parser An LR( k ) item is a pair [ P, δ ], where P is a production A →β with a • at some position in the rhs δ is a lookahead string of length (words or EOF ) The • in an item indicates the position of the top of the stack LR(1): [ A βγ ,a ] means that the input seen so far is consistent with the use of A →βγ immediately after the symbol on top of the stack [A →β γ ,a ] means that the input seen so far is consistent with the use of A →βγ at this point in the parse, and that the parser has already recognized β . [A →βγ •,a ] means that the parser has seen βγ , and that a lookahead symbol of a is consistent with reducing to A.
Background image of page 2
Lecture 15 3 cs415, spring 10 High-level overview 1 Build the canonical collection of sets of LR(1) Items, I a Begin in an appropriate state, s 0 Assume: S’ S, andS’is unique start symbol that does not occur on any RHS of a production (extended CFG - ECFG) [S’ S, EOF ], along with any equivalent items Derive equivalent items as closure( s ) b Repeatedly compute, for each s k , and each X, goto(s ,X) If the set is not already in the collection, add it Record all the transitions created by goto( ) This eventually reaches a fixed point 2 Fill in the table from the collection of sets of LR(1) items The canonical collection completely encodes the transition diagram for the handle-finding DFA LR(1) Table Construction
Background image of page 3

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

View Full DocumentRight Arrow Icon
Lecture 15 4 cs415, spring 10 Computing Closures Closure(s) adds all the items implied by items already in s Any item [A β• B δ ,a ] implies [B •τ ,x] for each production with B on the lhs, and each x F IRST ( δ a ) The algorithm Closure( s ) while ( s is still changing ) items [A →β •B δ ,a ] s productions B τ P b F IRST ( δ a ) // δ might be ε if[B τ ,b ] then add [B τ ,b ] to s ¾ Classic fixed-point method ¾ Halts because s I TEMS ¾ Worklist version is faster Closure “fills out” a state
Background image of page 4
Lecture 15 5 cs415, spring 10 Computing Gotos Goto(s,x)computes the state that the parser would reach if it recognized an x while in state s Goto({ [A β• X δ ,a ] }, X) produces [A β •δ ,a ] (easy part) Should also includes closure( [A β •δ ,a ] ) ( fill out the state ) The algorithm Goto( s, X ) new Ø items [A →β •X δ ,a ] s [A →β X• δ ,a ] return closure(new) ¾ Not a fixed-point method! ¾ Straightforward computation ¾ Uses closure( ) Goto() moves forward
Background image of page 5

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

View Full DocumentRight Arrow Icon
Lecture 15 6 cs415, spring 10 Building the Canonical Collection Start from s 0 = closure( [S’ S, EOF ] ) Repeatedly construct new states, until all are found The algorithm cc closure([S’ S, EOF ]) CC { } while (new sets are still being added to CC) for each unmarked set j CC mark as processed for each x following a in an item in temp goto(
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/29/2010 for the course CS 198:415 taught by Professor Kremer,u. during the Spring '10 term at Rutgers.

Page1 / 28

lec15mod - CS415 Compilers LR(1) parsing These slides are...

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

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