CS453 : JavaCUP and error recovery CS453 Shift-reduce Parsing 1
Shift-reduce parsing in an LR parser LR(k) parser – Left-to-right parse – Right-most derivation – K-token look ahead LR parsing algorithm using an LR parse table – Performs a shift-reduce parse – Look at state at top of stack and input symbol to find action in table – shift(n): advance input, push state n on stack – reduce(k): pop rhs of grammar rule k, look up state on top of stack and lhs for goto n, push lhs(k) and n onto stack – accept: stop and success – error: stop and fail CS453 Shift-reduce Parsing 2
Interpreting the javacup.dump file Shows the whole state transition diagram, or finite state machine for push down automata Items – A production from a grammar with a dot indicating position in parse. – item is (A à alpha . X beta, z) – A is nonterminal – alpha and beta are possibly empty strings of terminals and/or nonterminals where alpha is on top of the stack – X is a terminal or nonterminal – z is a terminal/token CS453 Shift-reduce Parsing 3
Interpreting the javacup.dump file cont … Transitions in the LALR_state transition table in javacup.dump – (A à alpha . X beta, z) – When X is a terminal, then shift X and transition to another state determined by X. – When X is a non-terminal, then goto another state determined by the state under X in the stack and by X, once X has been reduced. – An item with the parsing dot at the end of the production – ( A à gamma ., w) – Causes a reduction to the production A à gamma when the next token in the input is w. CS453 Shift-reduce Parsing 4
Summary on Precedence and Associativity in JavaCUP Tokens and productions are given precedence. – Tokens are given precedence level in precedence declarations. – Productions are given precedence level of last operator on right-hand side, ie. top of the Stack When in shift-reduce conflict, – shift if look ahead token has higher precedence than reduce production – reduce if reduce production has higher precedence than the look ahead token Within the same precedence level associativity determines whether to shift or reduce. – left associativity results in a reduce – right associativity results in a shift CS453 Shift-reduce Parsing 5
Error detection and recovery Given a SLR, LR(1) or LALR parse table, what constitutes an error? An empty slot in the parse table When the parser encounters an empty slot in its table, it has detected an error. What are the possible reactions to this? 1. Stop easy but not user friendly 2. Try to get back (recover) and parse more of the program more complicated, why? How to resume? What to do with unfinished stack symbols and states CS453 Shift-reduce Parsing 6
How to resume and clean up the parsing stack The simplest approach to resume parsing is called panic mode error recovery : discard input tokens until a so called “anchor” token appears. An anchor token is much like a look ahead token, such as “;” for statement. - Imagine the parser is parsing a statement and an error occurs, then discarding input tokens until “;” and declaring an erroneous statement gets the parser back and allows it to parse a next statement or an “}” - At the same time, certain grammar symbols and states corresponding to part of the statement (shifted tokens, reduced non-terminals) are on the stack. So, while the parser skips input tokens to the end of statement, it should also remove these grammar symbols and states off the stack. CS453 Shift-reduce Parsing 7
Error recovery in JCup Approach to error recovery same as in (original) YACC. The compiler writer decides which “major” non terminals will have error recovery associated with them – Typical choices: expression, statement, block by adding to the grammar error productions of the form A à à error α where error is a reserved word. On encountering an error, JCup pops grammar symbols and states off the stack until it reaches a symbol with an item: A à à . error α The parser shifts error onto the stack. If α is empty a reduction A à à error α occurs immediately. If α is a string of terminals the parser skips input symbols until α occurs on input. It shifts α onto the stack and performs reduction A à à error α CS453 Shift-reduce Parsing 8
Simple example In PA0, we can add en error production to stmt: � stmts ::= stmts stmt � | � stmt � ; � stmt ::= PRINT exp:e EOL � {: System.out.println(“ “ + e); :} � | � error EOL � {: System.out.println(“Erroneous statement skipped”); :} � ; � Now the parser skips erroneous statements and resumes with the next. Let’s do it to it … CS453 Shift-reduce Parsing 9
Recommend
More recommend