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 Bottom-up parsing Reduces a string w to the start symbol S Reduction is akin to a “reverse derivation” Key decisions during bottom-up parsing: – When to reduce – What production to apply as the parse proceeds Generally speaking, bottom-up 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 Shift-reduce parsing Bottom-up 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 non-terminal symbol Handle always at the top of the stack, not in the middle CSE 450: Compilers K. Stirewalt Question How does a shift-reduce 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 Table-driven form of shift-reduce 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 LR-parsing 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
- Context-free grammar, Bottom-up parsing, LR parser, Top-down parsing, K. Stirewalt