Open In App

Error Recovery Strategies in Compiler Design

Last Updated : 18 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Panic mode
  2. Phrase level recovery
  3. Error production
  4. Global correction
  5. Symbol table

Panic Mode:

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 is for additional errors. If there is less number of errors in the same statement, then this strategy is best choice.

Example:   

C




int a, 5abcd, sum, $2;


// After   int a, 5abcd , sum, $2 ;    // parser discards input symbol one at a time.

Advantage:

  1. It’s easy to use.
  2. The program never falls into the loop.

Disadvantage:  

  1. This technique may lead to semantic error or runtime error in further stages.

Phrase Level Recovery:

In this strategy, on discovering an error, parser performs local 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 local correction can be replacing the comma with semicolons, omission of semicolons, or, fitting missing semicolons. This type of local correction is decided by the compiler developer.  

Examples:

C




int a,b           
// AFTER RECOVERY:
int a,b;  //Semicolon is added by the compiler


 

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.

Error Production:

It requires good knowledge of common errors that might get 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 difficult 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  

                                                   B-> cd

The input string is not obtainable by the above grammar, so we need to add Augmented Grammar.

                                Grammar:   E->SB          // AUGMENT THE GRAMMAR

                                                   S-> A

                                                   A-> aA| bA | a | b  

                                                   B-> cd

Now, string abcd is possible to obtain.

Advantages: 

  1. Syntactic phase errors are generally recovered by error productions.

Disadvantages:

  1. The method is very difficult to maintain because if we change the grammar then it becomes necessary to change the corresponding production.
  2. It is difficult to maintain by the developers.

Global Correction:

We often want such a compiler that makes very few changes in processing an incorrect input string to the correct input string. Given an incorrect input string x and grammar G, the algorithm itself can find a parse tree for a related string y (Expected output string); 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.

Symbol Table:

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads