Error Handling &Recovery in Compilers
Muhammad Hamza Sadiq (141)
Compiler Construction
Presentation
Presented by :
Hafiz Muneeb-Ul-Hassan (143)
Presented to :
Sir Faiz Rasool
Mian Ahad Bilal (062)
2.
Introduction to ErrorHandling
What is it?
It's detecting, reporting, and recovering from errors. These errors are
found in source code. This happens during compilation.
Our Goal
Continue compilation after errors. Developers can then fix bugs
efficiently. We aim for maximum recovery.
3.
Types of CompilationErrors
1 Lexical Errors
Invalid tokens appear in the code. For example, "int @x = 5;" contains an invalid
character.
2 Syntax Errors
Grammar rules are violated. A common example is a missing semicolon. Code
structure is incorrect.
3 Semantic Errors
Meaning issues arise in the code. This includes problems like type mismatches.
The logic is flawed.
4 Runtime Errors
These occur during program execution. A classic case is dividing by zero. They
crash the program.
4.
Phases of ErrorHandling
Lexical Analyzer
Detects invalid tokens. This is the first stage. It ensures valid input.
Syntax Analyzer
Also known as the Parser. It detects grammar violations. It checks
code structure.
Semantic Analyzer
Ensures correct meaning and type usage. It validates logical
coherence. This prevents subtle bugs.
5.
Detection vs. Recovery
ErrorDetection
The process of recognizing that an error has occurred. This is the
first step in handling issues.
Error Recovery
The attempt to proceed with compilation. This happens despite
the identified error. It keeps the process moving.
6.
Key Error RecoveryStrategies
Panic Mode Skips tokens until a synchronizing
token. Simple and fast, but may skip too
much code.
Phrase Level Inserts or replaces symbols for minor
corrections. Suitable for small syntax
issues.
Error Productions Grammar includes common errors.
Compiler knows how to handle them.
Offers specific fixes.
Global Correction Algorithms find minimum changes
needed. Very accurate but can be slow
and complex.
7.
Tools for ErrorHandling
Lex (flex)
A tool for lexical analysis. It handles lexical errors. It generates scanners for token recognition.
Yacc (bison)
A parser generator. It handles syntax errors. It uses grammar rules for parsing.
Both Tools
They can include error rules. They also support error tokens. This aids structured error handling.
8.
Practical Example
int main(){ int x = 10 printf("%d", x);
return 0;}
In this example, a semicolon is missing after "int x = 10". The compiler might
employ panic mode recovery, skipping to the next line. It then reports a syntax error.
This allows compilation to proceed.
9.
Real-World Applications
• IDEslike GCC, Clang, and Visual Studio use error recovery extensively.
• They highlight multiple errors in a single compilation run.
• These tools often suggest potential fixes. This helps developers correct issues
quickly.
• Error handling prevents program crashes during compilation. It ensures a
smoother development experience.
10.
Conclusion & NextSteps
1
User Experience
Error handling improves
the user experience
significantly. It makes
programming less
frustrating.
2
Compiler
Capability
Compilers must detect
and recover from many
errors. This ensures
robust and reliable
compilation.
3
Tool Utilization
Tools like Lex and Yacc
are crucial. They
implement structured
error handling strategies
effectively.