Error Detection and Recovery in Compiler Design
Previous Year Questions from RGPV:
Year Question
Dec 2020 Explain different error recovery strategies used in compiler design.
June 2019 Write short notes on error handling and error recovery techniques.
Dec 2018 Discuss error detection and recovery techniques with suitable examples.
What is Error Detection and Recovery?
During the compilation process, errors in the source code must be detected and handled so that the
compiler can either recover from them or report them meaningfully to the programmer.
• Error Detection: Identifying the location and type of error.
• Error Recovery: Compiler’s response to an error, i.e., how it continues compilation after an error
is found.
📚 Types of Errors in Compiler Design
Type of Error Description Example
Lexical Error Invalid tokens or characters int 9var = 5;
Syntax Error Violates grammar rules if (a > b
Semantic Error Meaningless statements a = true + 5;
Logical Error Correct syntax but incorrect logic average = sum - n;
Runtime Error Errors during execution (rarely compiler-level) Divide by zero
🛠 Error Handling Goals
1. Report errors clearly
2. Recover and continue compilation
3. Avoid repeated error messages from a single fault
4. Generate as much of the correct object code as possible
1
Phases Involved in Error Detection
Phase Handles
Lexical Analyzer Misspelled keywords, identifiers
Syntax Analyzer Missing semicolon, unbalanced braces
Semantic Analyzer Type mismatch, undeclared vars
Common Error Recovery Strategies
1. Panic Mode Recovery
• Skip tokens until a synchronizing token (like ; , } ) is found.
• Simple and fast.
Example:
int a = 5 b = 6; // Missing semicolon, skip to next semicolon
Good for catching multiple errors. May skip important code parts.
2. Phrase Level Recovery
• Make local corrections to the input.
• Insert, delete, or replace a token to fix the error.
Example:
if (a > b) // missing body
⇒ if (a > b) { }
Works well for small mistakes. Might not reflect intended logic.
3. Error Productions
• Extend grammar with known error patterns.
• When matched, specific recovery rules are applied.
Example:
<if-stmt> → 'if' '(' expr ')' stmt | error ')' stmt
Helpful in controlled environments. Difficult to design comprehensive rules.
2
4. Global Correction
• Tries to find minimum changes to make the input valid.
• Uses algorithms to calculate edits.
Example: If input is: int a 5; → Suggest: int a = 5;
Most accurate. Computationally expensive.
Flowchart: Error Handling in Syntax Analysis
+----------------+
| Parser reads |
| next token |
+--------+-------+
|
v
+------v-------+
| Is token |
| valid? |-- No --> Error Handler
+------+-------+
| Yes
v
Continue Parsing
Comparison Table: Error Recovery Strategies
Strategy Speed Complexity Accuracy Use-case
Panic Mode Fast Low Low Suitable for quick recovery
Phrase Level Medium Medium Medium Simple typo recovery
Error Productions Medium High Medium Predictable error patterns
Global Correction Slow Very High High High accuracy needed
📦 Sticky Notes for Quick Revision
Lexical Errors – Misspelled identifiers/keywords\ Syntax Errors – Grammar rule violations (missing
semicolon)\ Semantic Errors – Type mismatches, undeclared variables\ Panic Mode – Skip tokens
until synchronizing token\ Phrase Level – Try local correction like insert/delete\ Error Productions –
Add extra grammar rules to catch errors\ Global Correction – Use algorithm to fix input (slow but
accurate)
3
🗣 Hinglish Explanation:
Compiler jab code check karta hai to agar koi galti milti hai (error), to usko detect aur
recover karna padta hai. Panic mode mein wo galat token skip kar deta hai. Phrase level
mein chhoti chhoti correction karta hai. Global correction sabse accurate hoti hai par time
lagta hai.
This completes your RGPV-style notes for Error Detection and Recovery in Compiler Design. Want
me to continue with Intermediate Code Generation next?