Error Recovery in LR Parsing
LR parser is a bottom-up parser. It is always used to deal with context-free Grammar (CFGs). It is generally used by computer programming language compilers as well as other associated tools also. LR parser produces leftmost derivation while using the input taking from left to right. By building up from the leaves, it helps to reduce the top-level grammar productions that is why it is called a bottom-up parser. LR parsers are the most powerful parsers among all deterministic parsers which are used widely nowadays. LR parsers are basically of 4 types:
Error Recovery in LR Parsing:
When there is no valid continuation for the input scanned thus far, LR parsers report an error. Before notifying a mistake, a CLR parser never performs a single reduction and an SLR or LALR may do multiple reductions, but they will never move an incorrect input symbol into the stack.
When the parser checks the table and discovers that the relevant action item is empty, an error is recognized in LR parsing. Goto entries can never be used to detect errors.
LR Parser Basically Uses the Mentioned Two Techniques to Detect Errors:
- Syntactic Phase recovery
- Panic mode recovery
Syntactic Phase Recovery:
Syntactic Phase Recovery Follows the Given Steps:
- Programmer mistakes that call error procedures in the parser table are determined based on the language.
- Creating error procedures that can alter the top of the stack and/or certain symbols on input in a way that is acceptable for table error entries.
There are some of the errors that are detected during the syntactic phase recovery:
- Errors in structure
- Missing operator
- Misspelled keywords
- Unbalanced parenthesis
Panic Mode Recovery:
This approach involves removing consecutive characters from the input one by one until a set of synchronized tokens is obtained. Delimiters such as or are synchronizing tokens. The benefit is that it is simple to implement and ensures that you do not end up in an infinite loop. The drawback is that a significant quantity of data is skipped without being checked for additional problems.
Panic mode recovery follows the given steps:
- Scan the stack until you find a state ‘a’ with a goto() on a certain non-terminal ‘B’ (by removing states from the stack).
- Until a symbol ‘b’ that can follow ‘B’ is identified, zero or more input symbols are rejected.
Example of Error recovery using LR Parser:
Consider the following grammar for detecting the errors:
E → E + E E → E * E E → ( E ) E → id
Step 1: Firstly make the parsing table for the given grammar:
Step 2: Let the string given to parse through it is:
Step 3: Now working of parser on the given string is described below:
|0E1 + 4||)$|
|0E1 + 4||$|
|0E1 + 4id3||$|
|0E1 + 4E7||$|
Step 4: Output is given in the form of error is detected:
Action(4, )) = error i.e., “unbalanced right parenthesis” c2 removes right parenthesis “missing operand” e2 pushes id3 on stack
Thus, the error is detected using LR parser in this way.