0% found this document useful (0 votes)
290 views

Error Detection and Recovery in Compiler

The document discusses error detection and recovery in compilers. It describes four common error recovery strategies used by parsers: panic mode, statement mode, error productions, and global correction. Panic mode ignores input after an error, while statement mode tries corrections to parse the rest of the statement. Error productions incorporate known errors into the grammar. Global correction finds the closest error-free program match. The document also classifies compiler errors into lexical, syntactic, and semantic errors occurring at different stages, and describes example errors and recovery approaches.

Uploaded by

Highlights Mania
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
290 views

Error Detection and Recovery in Compiler

The document discusses error detection and recovery in compilers. It describes four common error recovery strategies used by parsers: panic mode, statement mode, error productions, and global correction. Panic mode ignores input after an error, while statement mode tries corrections to parse the rest of the statement. Error productions incorporate known errors into the grammar. Global correction finds the closest error-free program match. The document also classifies compiler errors into lexical, syntactic, and semantic errors occurring at different stages, and describes example errors and recovery approaches.

Uploaded by

Highlights Mania
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Error detection and Recovery in Compiler

A parser should be able to detect and report any error in the program. It is expected
that when an error is encountered, the parser should be able to handle it and carry
on parsing the rest of the input. Mostly it is expected from the parser to check for
errors but errors may be encountered at various stages of the compilation process.
A program may have the following kinds of errors at various stages:
 Lexical : name of some identifier typed incorrectly
 Syntactical : missing semicolon or unbalanced parenthesis
 Semantical : incompatible value assignment
 Logical : code not reachable, infinite loop
There are four common error-recovery strategies that can be implemented in the
parser to deal with errors in the code.

Panic mode

When a parser encounters an error anywhere in the statement, it ignores the rest of
the statement by not processing input from erroneous input to delimiter, such as
semi-colon. This is the easiest way of error-recovery and also, it prevents the parser
from developing infinite loops.

Statement mode

When a parser encounters an error, it tries to take corrective measures so that the
rest of inputs of statement allow the parser to parse ahead. For example, inserting a
missing semicolon, replacing comma with a semicolon etc. Parser designers have
to be careful here because one wrong correction may lead to an infinite loop.

Error productions

Some common errors are known to the compiler designers that may occur in the
code. In addition, the designers can create augmented grammar to be used, as
productions that generate erroneous constructs when these errors are encountered.

Global correction

The parser considers the program in hand as a whole and tries to figure out what
the program is intended to do and tries to find out a closest match for it, which is
error-free. When an erroneous input (statement) X is fed, it creates a parse tree for
some closest error-free statement Y. This may allow the parser to make minimal
changes in the source code, but due to the complexity (time and space) of this
strategy, it has not been implemented in practice yet.
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 it to user is called Error Handling process.
Functions of Error handler
 Detection
 Reporting
 Recovery

Classification of 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.
 Appearance of illegal characters
 Unmatched string
Example 1 : printf("CC");$
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:
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 }
 Advantage is that it is easy to implement and guarantees not to go to infinite
loop
 Disadvantage is that a considerable amount of input is skipped without
checking it for additional errors
Syntactic phase errors
These errors are detected during syntax analysis phase. Typical syntax errors are
 Errors in structure
 Missing operator
 Misspelled keywords
 Unbalanced parenthesis
Example : swicth(ch)
{
.......
.......
}
The keyword switch is incorrectly written as swicth. Hence, “Unidentified
keyword/identifier” error occurs.
Error recovery:
1. Panic Mode Recovery
 In this method, successive characters from input are removed one at a
time until a designated set of synchronizing tokens is found. Synchronizing
tokens are deli-meters such as ; or }
 Advantage is that its easy to implement and guarantees not to go to
infinite loop
 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 necessary
correction on remaining input so that the rest of input statement allow the
parser to parse ahead.
 The correction can be deletion of extra semicolons, replacing comma by
semicolon or inserting missing semicolon.
 While performing correction, atmost care should be taken for not going
in infinite loop.
 Disadvantage is that it finds difficult to handle situations where actual
error occured before point of detection.
3. Error production
 If 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.
 Disadvantage is that its 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 semantic analysis phase. Typical semantic errors are
 Incompatible type of operands
 Undeclared variables
 Not matching of actual arguments with 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
 If error “Undeclared Identifier” is encountered then, to recover from this a
symbol table entry for corresponding identifier is made.
 If data types of two operands are incompatible then, automatic type conversion
is done by the compiler.

You might also like