This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 CSE 450: Compilers K. Stirewalt Parsing and syntax analysis Part 5: LR parsing Readings: Read Section 4.6 in Dragon Book CSE 450: Compilers K. Stirewalt Bottomup parsing Reduces a string w to the start symbol S Reduction is akin to a “reverse derivation” Key decisions during bottomup parsing: – When to reduce – What production to apply as the parse proceeds Generally speaking, bottomup parsers can delay commitment to a production longer than can predictive parses – I.e., they can “see more context” – Often enables use of “nicer looking” grammars, including some ambiguous grammars CSE 450: Compilers K. Stirewalt Shiftreduce parsing Bottomup parsing method where right sententials are represented using a combination of a stack of symbols and a stream of unconsumed tokens Right sentential always of form β k w – w is a sequence of tokens – β k represented using the contents of the stack – shifting tokens from w to β involves pushing those tokens onto the stack – reduction involves popping one or more symbols from stack and replacing with a nonterminal symbol Handle always at the top of the stack, not in the middle CSE 450: Compilers K. Stirewalt Question How does a shiftreduce parser know when to shift and when to reduce? I.e., how does the parser know when it’s matched a handle? CSE 450: Compilers K. Stirewalt LR parsing Tabledriven form of shiftreduce parsing Key idea: – LR parsers maintain a stack of states , rather than a stack of symbols, to represent the β component of a right sentential being matched – States denote symbols, but there might be more than one state associated with the same symbol – States track where the parser is in a parse Easiest to understand is the “simple LR” (SLR) All other LRparsing methods are similar CSE 450: Compilers K. Stirewalt States and items Parse states represent sets of items which represent how much of a production the parser has seen at a given point Item is a production with a dot (‘.’) at some position in the production body E.g., production A → X Y Z gives rise to items: – A → . X Y Z – A → X . Y Z – A → X Y . Z – A → X Y Z . 2 CSE 450: Compilers K. Stirewalt Interpretation of items Item of form A → . X Y Z means we hope to see a string derivable from X Y Z next on the input Item of form A → X Y . Z means if symbols X and Y are on the stack ( Y on top) we hope to see a string derivable from Z next on the input Multiple items will group into the same parse state; thus when in a given state, the parser may take any number of actions based on what’s on top of the stack and on the next input CSE 450: Compilers K. Stirewalt States are item closures If I is a set of items of grammar G , then...
View
Full Document
 Spring '08
 STIREWALT
 Contextfree grammar, Bottomup parsing, LR parser, Topdown parsing, K. Stirewalt

Click to edit the document details