Unit - 4 Error Recovery
Unit - 4 Error Recovery
Unit - 4 Error Recovery
GTU # 2170701
Unit – 4
Error Recovery
Errors
Prof. Jay
Dixita
RBDhamsaniya
Kagathara #3130006
#2170701(PS)
(CD) Unit
Unit1 4––Basic
ErrorProbability
Recovery 4
Lexical error
Lexical errors can be detected during lexical analysis phase.
Typical lexical phase errors are:
1. Spelling errors
2. Exceeding length of identifier or numeric constants
3. Appearance of illegal characters
Example:
fi ( )
{
}
In above code 'fi' cannot be recognized as a misspelling of keyword if rather lexical analyzer
will understand that it is an identifier and will return it as valid identifier.
Thus misspelling causes errors in token formation.
Prof. Jay
Dixita
RBDhamsaniya
Kagathara #3130006
#2170701(PS)
(CD) Unit
Unit1 4––Basic
ErrorProbability
Recovery 5
Syntax error
Syntax error appear during syntax analysis phase of compiler.
Typical syntax phase errors are:
1. Errors in structure
2. Missing operators
3. Unbalanced parenthesis
The parser demands for tokens from lexical analyzer and if the tokens do not satisfy the
grammatical rules of programming language then the syntactical errors get raised.
Example:
Prof. Jay
Dixita
RBDhamsaniya
Kagathara #3130006
#2170701(PS)
(CD) Unit
Unit1 4––Basic
ErrorProbability
Recovery 6
Semantic error
Semantic error detected during semantic analysis phase.
Typical semantic phase errors are:
1. Incompatible types of operands
2. Undeclared variable
3. Not matching of actual argument with formal argument
Example:
id1=id2+id3*60 (Note: id1, id2, id3 are real)
(Directly we can not perform multiplication due to incompatible types of variables)
Prof. Jay
Dixita
RBDhamsaniya
Kagathara #3130006
#2170701(PS)
(CD) Unit
Unit1 4––Basic
ErrorProbability
Recovery 7
Error recovery strategies (Ad-Hoc & systematic methods)
There are mainly four error recovery strategies:
1. Panic mode
2. Phrase level recovery
3. Error production
4. Global generation
Prof. Jay
Dixita
RBDhamsaniya
Kagathara #3130006
#2170701(PS)
(CD) Unit
Unit1 4––Basic
ErrorProbability
Recovery 9
Panic mode
In this method on discovering error, the parser discards input symbol one at a time. This
process is continued until one of a designated set of synchronizing tokens is found.
Synchronizing tokens are delimiters such as semicolon or end.
These tokens indicate an end of the statement.
If there is less number of errors in the same statement then this strategy is best choice.
Prof. Jay
Dixita
RBDhamsaniya
Kagathara #3130006
#2170701(PS)
(CD) Unit
Unit1 4––Basic
ErrorProbability
Recovery 10
Phrase level recovery
In this method, on discovering an error parser performs local correction on remaining input.
The local correction can be replacing comma by semicolon, deletion of semicolons or inserting
missing semicolon.
This type of local correction is decided by compiler designer.
This method is used in many error-repairing compilers.
Prof. Jay
Dixita
RBDhamsaniya
Kagathara #3130006
#2170701(PS)
(CD) Unit
Unit1 4––Basic
ErrorProbability
Recovery 11
Error production
If we have 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.
Then we use the grammar augmented by these error production to construct a parser.
If error production is used then, during parsing we can generate appropriate error message and
parsing can be continued.
Prof. Jay
Dixita
RBDhamsaniya
Kagathara #3130006
#2170701(PS)
(CD) Unit
Unit1 4––Basic
ErrorProbability
Recovery 12
Global correction
Given an incorrect input string x and grammar G, the algorithm will find a parse tree for a
related string y, such that number of insertions, deletions and changes of token require to
transform x into y is as small as possible.
Such methods increase time and space requirements at parsing time.
Global correction is thus simply a theoretical concept.
Prof. Jay
Dixita
RBDhamsaniya
Kagathara #3130006
#2170701(PS)
(CD) Unit
Unit1 4––Basic
ErrorProbability
Recovery 13