Open In App
Related Articles

Error detection and Recovery in Compiler

Improve Article
Save Article
Like Article

In this phase of compilation, all possible errors made by the user are detected and reported to the user in form of error messages. This process of locating errors and reporting them to users is called the Error Handling process

Functions of an Error handler.

  • Detection
  • Reporting
  • Recovery

Classification of Errors

Compile-time errors

Compile-time errors are of three types:- 

Lexical phase errors

These errors are detected during the lexical analysis phase. Typical lexical errors are:

  • Exceeding length of identifier or numeric constants.
  • The appearance of illegal characters
  • Unmatched string
Example 1 : printf("Geeksforgeeks");$
This is a lexical error since an illegal character $ appears at the end of statement.

Example 2 : This is a comment */
This is an lexical error since end of comment is present but beginning is not present

Error recovery for lexical errors: 

Panic Mode Recovery 

  • In this method, successive characters from the input are removed one at a time until a designated set of synchronizing tokens is found. Synchronizing tokens are delimiters such as; or }
  • The advantage is that it is easy to implement and guarantees not to go into an infinite loop
  • The disadvantage is that a considerable amount of input is skipped without checking it for additional errors

Syntactic phase errors:

These errors are detected during the syntax analysis phase. Typical syntax errors are:

  • Errors in structure
  • Missing operator
  • Misspelled keywords
  • Unbalanced parenthesis
Example : swich(ch)

The keyword switch is incorrectly written as a swich. Hence, an “Unidentified keyword/identifier” error occurs. 

Error recovery for syntactic phase error: 

1. Panic Mode Recovery 

  • In this method, successive characters from the input are removed one at a time until a designated set of synchronizing tokens is found. Synchronizing tokens are deli-meters such as; or }
  • The advantage is that it’s easy to implement and guarantees not to go into an infinite loop
  • The disadvantage is that a considerable amount of input is skipped without checking it for additional errors

2. Statement Mode recovery 

  • In this method, when a parser encounters an error, it performs the necessary correction on the remaining input so that the rest of the input statement allows the parser to parse ahead.
  • The correction can be deletion of extra semicolons, replacing the comma with semicolons, or inserting a missing semicolon.
  • While performing correction, utmost care should be taken for not going in an infinite loop.
  • A disadvantage is that it finds it difficult to handle situations where the actual error occurred before pointing of detection.

3. Error production 

  • If a user has knowledge of common errors that can be encountered then, these errors can be incorporated by augmenting the grammar with error productions that generate erroneous constructs.
  • If this is used then, during parsing appropriate error messages can be generated and parsing can be continued.
  • The disadvantage is that it’s difficult to maintain.

4. Global Correction 

  • The parser examines the whole program and tries to find out the closest match for it which is error-free.
  • The closest match program has less number of insertions, deletions, and changes of tokens to recover from erroneous input.
  • Due to high time and space complexity, this method is not implemented practically.

Semantic errors

These errors are detected during the semantic analysis phase. Typical semantic errors are 

  • Incompatible type of operands
  • Undeclared variables
  • Not matching of actual arguments with a formal one
Example : int a[10], b;
                 a = b;

It generates a semantic error because of an incompatible type of a and b. 

Error recovery for Semantic errors

  • If the error “Undeclared Identifier” is encountered then, to recover from this a symbol table entry for the corresponding identifier is made.
  • If data types of two operands are incompatible then, automatic type conversion is done by the compiler.


Improved code quality: Error detection and recovery in a compiler can improve the overall quality of the code produced. This is because errors can be identified early in the compilation process and addressed before they become bigger issues.

Increased productivity: Error recovery can also increase productivity by allowing the compiler to continue processing the code after an error is detected. This means that developers do not have to stop and fix every error manually, saving time and effort.

Better user experience: Error recovery can also improve the user experience of software applications. When errors are handled gracefully, users are less likely to become frustrated and are more likely to continue using the application.

Better debugging: Error recovery in a compiler can help developers to identify and debug errors more efficiently. By providing detailed error messages, the compiler can assist developers in pinpointing the source of the error, saving time and effort.

Consistent error handling: Error recovery ensures that all errors are handled in a consistent manner, which can help to maintain the quality and reliability of the software being developed.

Reduced maintenance costs: By detecting and addressing errors early in the development process, error recovery can help to reduce maintenance costs associated with fixing errors in later stages of the software development lifecycle.

Improved software performance: Error recovery can help to identify and address code that may cause performance issues, such as memory leaks or inefficient algorithms. By improving the performance of the code, the overall performance of the software can be improved as well.


Slower compilation time: Error detection and recovery can slow down the compilation process, especially if the recovery mechanism is complex. This can be an issue in large software projects where the compilation time can be a bottleneck.

Increased complexity: Error recovery can also increase the complexity of the compiler, making it harder to maintain and debug. This can lead to additional development costs and longer development times.

Risk of silent errors: Error recovery can sometimes mask errors in the code, leading to silent errors that go unnoticed. This can be particularly problematic if the error affects the behavior of the software application in subtle ways.

Potential for incorrect recovery: If the error recovery mechanism is not implemented correctly, it can potentially introduce new errors or cause the code to behave unexpectedly.

Dependency on the recovery mechanism: If developers rely too heavily on the error recovery mechanism, they may become complacent and not thoroughly check their code for errors. This can lead to errors being missed or not addressed properly.

Difficulty in diagnosing errors: Error recovery can make it more difficult to diagnose and debug errors since the error message may not accurately reflect the root cause of the issue. This can make it harder to fix errors and may lead to longer development times.

Compatibility issues: Error recovery mechanisms may not be compatible with certain programming languages or platforms, leading to issues with portability and cross-platform development.

Last Updated : 20 Apr, 2023
Like Article
Save Article
Similar Reads