During program translation, a compiler may encounter different types of errors. Error handling helps detect, report, and recover from these errors so programmers can identify and correct issues in their code.
It consists of three main components:
- Error Detection: Identifies errors in the source program.
- Error Reporting: Displays meaningful error messages and locations.
- Error Recovery: Allows compilation to continue after detecting an error when possible.
Sources of Error
Errors can occur during different phases of compilation, including lexical, syntax, and semantic analysis.
- Lexical Errors: Invalid characters or malformed tokens.
- Syntax Errors: Incorrect grammar or statement structure.
- Semantic Errors: Undeclared variables, incompatible types, or invalid operations.
- Code Generation Errors: Problems encountered while generating target code, such as unsupported operations or target-specific constraints.
A compiler reports these errors and may apply recovery techniques to continue processing the remaining source code.
Types of Errors
In programming and compiler design, errors are broadly classified into two main types:
1. Run-Time Errors
Run-time errors occur while a program is executing. They may result from invalid operations, resource limitations, or unexpected input.
Examples:
- Division by zero, where the language or execution environment treats it as an error.
- Memory allocation failure.
- Invalid array access in languages with bounds checking.
2. Compile-Time Errors
Compile-time errors are detected during compilation and can prevent successful code generation.
Examples:
- Missing semicolons or unmatched brackets.
- Undeclared variables.
- Type mismatches.
- Missing required header files or dependencies.
Error Detection and Reporting
In bottom-up parsing, a viable prefix is a prefix of a right-sentential form that does not extend beyond the end of a handle. LR parsers use viable-prefix recognition to identify invalid syntax during parsing.
- A parser detects a syntax error when no valid parsing action exists for the current state and lookahead token.
- Error reporting identifies the problem and provides its location when possible.
Example:
Valid C Syntax
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}
Invalid C Syntax
for (int i = 0;; i < 10) {
printf("%d\n", i);
}
Error Recovery
There are several methods that a compiler uses to recover from errors. These methods help the compiler continue processing the code instead of stopping immediately.
Common recovery methods include:
Recovery Method | Description |
|---|---|
Panic Mode Recovery | Skips input tokens until a synchronization token, such as ; or }, is found. |
Phrase-Level Recovery | Makes small local corrections, such as inserting or deleting a token. |
Error Productions | Adds grammar productions for common mistakes to generate specific error messages. |
Global Correction | Attempts to find a minimum-cost sequence of changes that makes the input syntactically valid. |

