Assignme
nt
Submitted To: Mam
Saba
Submitted by:
Aleza
Roll Number: 33
Course Name: Compiler
Construction
Course Code: CSI-
Topic : Error Detection and
Recovery
Government Post
Error Detection And
Recovery
To understand error detection and recovery, we
first need to understand the
scenario of a compiler. A compiler has six
phases:
The Six Phases of a Compiler:
1. Lexical Analysis
2. Syntax Analysis
3. Semantic Analysis
4. Intermediate Code Generation
5. Code Optimization
6. Code Generation
These six phases are connected with the
symbol table and error handler.
Symbol Table:
A symbol table is a data structure that stores
information about variables, keywords, and symbols
in a program.
Error Handler
Error handler is connected by each
phase of the compiler . If an error
occurs in any phase, it is reported to
the error handler. The error handler
performs three primary operations:
•Error detection
•Error Report
•Error recovery
Errors can occur at compile-time or runtime. At
compile-time, major errors typically occur in three
phases:
•Lexical Analysis
•Syntax Analysis
• Semantic Analysis
Lexical phase errors
Lexical phase errors occur when the
compiler encounters invalid or
unrecognized characters, keywords, or
identifiers in the source code. Examples
include:
Exceeding length of identifier or numeric
constant.
The appearance of illegal characters.
Unmatched strings.
Example:
printf(“GEEK FORGEEKS”);$
Lexical error occur because illegal
character $appear in the statement.
In simple words, lexical phase errors
happen when the compiler doesn't
Syntax Errors
Syntax Analysis checks if the
code follows the programming
language's rules, like:
• Correct order of words
(keywords, symbols)
• Matching brackets and
parentheses.
• Proper sentence structure.
•Missing operaters.
It's like checking if a sentence
is grammatically correct in
English!
Semantic errors
A mistake in the logic or meaning of code .
Code is grammatically correct but logically
wrong .Causes program to produce incorrect
results.
Example:
Code: x = 5 / 0
Grammar is correct, but dividing by zero is
meaningless!
Error Recovery
Compilers can easily detect errors and
report them, but the error recovery
process involves specific methods.
We have five error recovery methods.
• Panic Mode
• Phase-Level Recovery
• Sentence-Level Recovery (Error
Production)
• Global Correction
• Using symbol table
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 }
It’s easy to implement.
The disadvantage is that a
considerable amount of input is
skipped without checking it for
additional errors.
Phase Level
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.
A disadvantage is that it finds it
difficult to handle situations where
the actual error occurred before
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.