Error Recovery in Predictive Parsing
We know that the Predictive parser performs Left most derivative while parsing the given sentence. Now the given sentence may be a valid sentence or an invalid sentence with respect to the specified grammar. An error is detected during the predictive parsing when the terminal on top of the stack does not match the next input symbol, or when nonterminal X on top of the stack, then the present input symbol is a, and the parsing table entry M [X, a] is considered empty.
It is also possible that An error may occur while performing predictive parsing (LL(1) parsing)
When the terminal symbol is at top of the stack and it does not match the current input symbol. In LL(1) parsing If the top of the stack is a non-terminal A, then the present input symbol is a, and the parsing table entry M [A, a] is considered empty.
So what should a parser do to handle the error?
The parser design should be able to provide an error message (an error message which depicts as much possible information as it can ). It should be recovering from that error case, and it should be able to continue parsing with the rest of the input.
Error Recovery Techniques:
- Panic-Mode Error Recovery: In Panic-Mode Error Recovery the technique is skipping the input symbols until a synchronizing token is found.
- Phrase-Level Error Recovery: Each empty entry in the parsing table is filled with a pointer to a specific error routing take care of that error case.
Panic-Mode Error Recovery in LL(1) Parsing:
Panic-mode error recovery says that all the input symbols are skipped until a synchronizing token is found from the string.
In this recovery method, we use FOLLOW symbols as synchronizing tokens and the “synch” in the predictive parsing table to indicate synchronizing tokens obtained from the nonterminal’s FOLLOW sets.
What is the synchronizing token?
The terminal symbols which lie in follow set of non-terminal they can be used as a synchronizing token set for that of non-terminal. In simple panic-mode error recovery for the LL(1)parsing.
All the empty entries are marked as synch to indicate that the parser will skip all the input symbols until a symbol in the following set of thee non-terminal A which on the top of the stack. Then Non-terminal A will be popped from the stack by the parser. The parsing continues from that state. For handling the unmatched terminal symbols, the unmatched terminal symbol from the stack by the parser, after which it generates an error message that depicts the unmatched terminal is inserted.
How to select synchronizing set?
There are two ways of selecting synchronizing set:
1. Place all symbols in FOLLOW(A) into the synchronizing set for nonterminal A. We can continue parsing until we skip tokens of an element of FOLLOW(A) is seen and pop it from the stack specifically.
We might add keywords that begin statements to the synchronizing sets for the non-terminals generating expressions.
2. If a nonterminal can generate the empty string, then the production deriving ε can be used as a default. This may produce some delay in error detection, but errors cannot be missed. This approach reduces the number of non-terminals that have to be considered during error recovery.
Whenever we face a case where we find that a terminal that lies on top of a stack is not matching, we can simply pop that specifically and generate a message saying that the terminal was inserted.
Example for Panic Mode Error Recovery:
There are two separate examples explained below by taking two different strings.
Phrase-level recovery in LL(1) Parsing:
Phrase level recovery is implemented by filling in the blank entries in the predictive parsing table with pointers to error routines. At each unfilled entry in the parsing table, it is filled by a pointer to a special error routine that will take care of that error case specifically. These error routines can be of different types like :
- change, insert, or delete input symbols.
- issue appropriate error messages.
- pop items from the stack.