Error Detection and Recovery in Compiler
Error Detection and Recovery in Compiler
Error detection and recovery are essential functions of a compiler to ensure that a program is correctly
processed. Error detection refers to identifying mistakes in the source code, such as syntax, semantic, or
logical errors. When an error is found, the compiler generates an error message to help the programmer fix
it.
Error recovery allows the compiler to handle errors gracefully without abruptly stopping the compilation
process. It ensures that minor errors do not prevent the compiler from analyzing the rest of the program.
Common recovery techniques include skipping incorrect parts, suggesting corrections, and continuing
compilation.
Effective error handling improves the debugging process, enhances code reliability, and helps developers
write error-free programs efficiently.
Compile-time errors
Compile-time errors are of three types:-
1. 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(“Some_String”);$
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 phase 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.
2. 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:
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.
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.
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.
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.
3. 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.