Compilers: 3 Year Spring Term
Compilers: 3 Year Spring Term
3rd year
Spring term
11 Error Detection
11.1 Introduction
1
Error Detection
Introduction
Detection of Errors
• To be useful, a compiler should detect all errors in
the source code and report them to the user.
• These errors culd be:
• Lexical errors: e.g., badly formed identifiers or constants, symbols
which are not part of the language, badly formed comments, etc.
• Syntactic errors: chains of syntactic units that do not conform to the
syntax of the source language.
• Semantic errors: e.g., operations conducted on incompatible types,
undeclared variables, double declaration of variable, reference before
assignment, etc.
• Run-time errors: errors detectable solely at run time, pointers with null
value or whose value is outside allowed limits, or indexing of vectors
with unsuitable indices, etc.
Error Detection
Introduction
Detection of Errors
• To be useful, a compiler should detect all errors in
the source code and report them to the user.
• These errors culd be:
• Lexical errors: e.g., badly formed identifiers or constants, symbols which
are not part of the language, badly formed comments, etc.
Compilation• Syntactic errors: chains of syntactic units that do not conform to the syntax
of the source language.
Errors
• Semantic errors: e.g., operations conducted on incompatible types,
undeclared variables, double declaration of variable, reference before
assignment, etc.
• Run-time errors: errors detectable solely at run time, pointers with null
Execution value or whose value is outside allowed limits, or indexing of vectors with
Errors unsuitable indices, etc.
2
Error Detection
Introduction
A good compiler…
• Reports ALL errors
• Does not falsely report errors.
• Does not repeatly report the same error.
Error Detection
Introduction
Detection of Errors
• How many errors will the c compiler report?
void main () {
int i,j,k;
i=0; /* Asigno 0 a i //
j=;
=k;
}
3
Error Detection
Introduction
Detection of Errors
• How many errors will the c compiler report?
void main () {
int i,j,k;
i=0; /* Asigno 0 a i //
j=;
=k;
}
• Answer: 2
1) Comment on line 3 not terminated
2) Function main has no closing bracket
7
Error Detection
Introduction
4
Error Detection
Introduction
• If we delete the { on line 4, ‘int j’ is taken as the body of the while loop.
• This declaration of j is reported as an error (already declared in line 2)
• The close bracket on line 10 is seen as closing the main() function.
• All lines 11-65, meant to be part of ‘main()’, are reported to be “instruction located
outside a function”
• Any real errors in lines 11-65 would NOT be reported!
9
Error Detection
Introduction
10
5
Error Detection
Introduction
Over-reporting errors
• Assume we forget to declare a variable
• If the variable is referenced 20 times, should we
receive 20 error messages? Or just 1?
• Ideally, when a reference to an undeclared variable
is found:
• The symbol table is checked to see if any
previous such error was detected for this variable.
• If yes, no message is given,
• Else:
• An error message is printed,
• The symbol table is modified to indicate this. 11
Error Detection
Introduction
Over-reporting errors
• Alternatively, the symbol table can be used to store
each occurence undeclared reference to the
variable.
• At the end of the scope, a single message is
printed:
• Undeclared reference to variable j on lines 21, 22 and 23
12
6
Error Detection
Automatic Error Correction
Error Detection
Automatic Error Correction
14
7
Error Detection
Automatic Error Correction
Error Detection
Automatic Error Correction
16
8
Error Detection
Automatic Error Correction
17
Error Detection
Automatic Error Correction
18
9
Error Detection
Automatic Error Correction
19
10