Error Recovery Strategies in Compiler Design
The error may occur at various levels of compilation, so error handling is important for the correct execution of code. There are mainly five error recovery strategies, which are as follows:
- Panic mode
- Phase level recovery
- Error production
- Global correction
- Symbol table
This strategy is used by most parsing methods. In this method of discovering the error, the parser discards input symbols one at a time. This process is continued until one of the designated sets of synchronizing tokens is found. Synchronizing tokens are delimiters such as semicolons or ends. These tokens indicate an end of the input statement.
Thus, in panic mode recovery a considerable amount of input checking it for additional errors. If there is less number of errors in the same statement then this strategy is the best choice.
// After int a,
5abcd , sum, $2 ; // parser discards input symbol one at a time.
- It’s easy to use.
- The program never falls into the loop.
Phase Level Recovery:
In this strategy, on discovering an error parser performs original correction on the remaining input. It can replace a prefix of the remaining input with some string. This actually helps the parser to continue its job. The original correction can be replacing the comma with semicolons, omission of semicolons, or, fitting missing semicolons. This type of original correction is decided by the compiler developer.
Advantages: This method is used in many errors repairing compilers.
Disadvantages: While doing the replacement the program should be prevented from falling into an infinite loop.
It requires good knowledge of common errors that might be encountered, then we can augment the grammar for the corresponding language with error productions that generate the erroneous constructs. If error production is used during parsing we can generate an appropriate error message to indicate the error that has been recognized in the input. This method is extremely different to maintain, because if we change grammar then it becomes necessary to change the corresponding productions.
Example: Suppose the input string is abcd.
Grammar: S-> A
A-> aA | bA | a | b
The input string is not obtainable by the above grammar, so we need to add Augmented Grammar.
Grammar: E->SB // AUGMENT THE GRAMMAR
A-> aA| bA | a | b
Now, string abcd is possible to obtain.
- Syntactic phase errors are generally recovered by error productions.
- The method is very difficult to maintain because if we change the grammar then it becomes necessary to change the corresponding production.
- It is difficult to maintain by the developers.
We often want such a compiler that makes very few changes in processing an incorrect input string. Given an incorrect input string x and grammar G, the algorithm itself can find a parse tree for a related string y; such that a number of insertions, deletions, and changes of token require to transform x into y is as low as possible. Global correction methods increase time & space requirements at parsing time. This is simply a theoretical concept.
Advantages: It makes very few changes in processing an incorrect input string.
Disadvantages: It is simply a theoretical concept, which is unimplementable.
In semantic errors, errors are recovered by using a symbol table for the corresponding identifier and if data types of two operands are not compatible, automatically type conversion is done by the compiler.
Advantages: It allows basic type conversion, which we generally do in real-life calculations.
Disadvantages: Only Implicit type conversion is possible.