• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
May 26, 2022 |8.8K Views

Error Detection and Recovery in Lexical Analyser

  Share  1 Like
Description
Discussion

Error Handler is connected to all the phases of Compiler (Lexical Analyzer, Syntax Analyzer, Semantic Analyzer, Intermediate Code Generator, Code Optimizer, and Target Code Generator). The tasks of the Error Handler are to detect each error, report it to the user, and then make some recovery strategies and implement the strategies to handle the error.

Three primary functions of Error Handler are:
1) Error Detection
2) Error Reporting
3) Error Recovery

Error Detection and reporting:

1) Lexical: It detects error when sequence of any input doesn’t match any token pattern.
2) Syntactical: These errors are detected during syntax phase analysis if something is missing like parenthesis, semicolon, operator, etc
3) Semantical: These errors are detected during semantic analysis phase. Such errors can occur due to wrong matching of operators with operands, assigning incompatible value to a variable, undeclared variable, etc.
4) Logical: Any error that is not specific enough to be included in above three categories and that includes logical issues in problem are logical errors. Example: Code not reachable, code iterating infinitely, etc.

Error Recovery:

Lexical error: Lexical errors are recovered using Panic Mode recovery

1) Unknown characters are deleted.
2) Missing characters are inserted.
3) Transpose the characters.
4) Replace the characters.

Syntactic Phase Error: Errors are recovered using:

1) Panic Mode Recovery: Missing parenthesis, semicolon, etc. are inserted.
2) Statement Mode Recovery/ Phrase Level Recovery: In this method, right after discovering any error, recovery is done by correcting remaining input such that the remaining input statement allows parser to move further. It includes removing extra semicolons, etc.
3) Error productions: If any compiler designer has prior knowledge of errors which are common then such known errors can be recovered by increasing the existing grammar with error production that produce incorrect constructs.
4) Global correction: In Global Correction recovery method, parser considers full program at once. If there’s any error, it tries to find the closest matching of that program which is error-free and creates a syntax tree/ parse tree accordingly.

Semantic Errors:
In semantic phase, errors are being recovered by using symbol table. If datatypes of two operands are incompatible, compiler performs automatic type conversion.

Error recovery & detection in Compiler Design: https://www.geeksforgeeks.org/error-detection-recovery-compiler/