Error Recovery Strategies in Syntax Analysis
When a parser detects a syntax error, it must attempt to recover and continue parsing the remaining input to find further errors. There are four main strategies used for error recovery:
1. Panic-Mode Recovery
- The simplest and most widely used method.
- On detecting an error, the parser discards input symbols one by one until it finds a synchronizing token (like
;
or}
). - Synchronizing tokens are chosen so that parsing can resume safely.
- Advantages: Simple, fast, and guaranteed to terminate (no infinite loop).
- Drawback: May skip large portions of code and miss additional nearby errors.
2. Phrase-Level Recovery
- The parser makes local corrections to the remaining input.
- Example corrections:
- Replace
,
with;
- Insert a missing
)
- Delete an extra
;
- Replace
- Common in error-repairing compilers.
- Advantage: Can fix many syntactic errors.
- Drawback: May fix the error after the actual location, leading to confusion.
3. Error Productions
- Extend the grammar with special rules (error productions) that match common programming mistakes.
- The parser uses these rules to detect and report specific errors.
- Advantage: Gives meaningful diagnostics.
- Drawback: Requires anticipating and coding all possible common errors.
4. Global Correction
- A theoretical approach where the compiler finds the least number of changes (insert, delete, replace tokens) to make the program syntactically correct.
- Advantage: Finds the most optimal correction.
- Drawback: Too slow and costly in practice (not used in real-world compilers).