Error detection and Recovery in Compiler - GeeksforGeeks

13 Jan, 2022
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
Compile-time errors are of three types:- 
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
These errors are detected during the syntax analysis phase. Typical syntax errors are:
Errors in structure
Missing operator
Misspelled keywords
Unbalanced parenthesis
Example : switch(ch)
              {
                 .......
                 .......
              }
The keyword switch is incorrectly written as a switch. Hence, an “Unidentified keyword/identifier” error occurs. 
Error recovery for syntactic phase recovery: 
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.
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.
saurabh1990aror
kashishsoda
vaibhavsinghtanwar
jmpatel7102000
adnanirshad158
sagartomar9927
Compiler Design
GATE CS
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Construction of LL(1) Parsing Table
Directed Acyclic graph in Compiler Design (with examples)
Types of Parsers in Compiler Design
Difference between Compiler and Interpreter
Peephole Optimization in Compiler Design
Layers of OSI Model
ACID Properties in DBMS
TCP/IP Model
Types of Operating Systems
Normal Forms in DBMS