0% found this document useful (0 votes)
89 views22 pages

Error Handler: Presented By: Junaid Khan Department of Computer Science University of Peshawar

The document discusses error handling in compilers. It describes the different types of errors that can occur: lexical errors, syntax errors, semantic errors, logical errors, and spurious errors. It explains that the error handler is responsible for detecting errors during compilation and attempting to recover from errors so that additional errors can be found. The error handler contains routines to handle errors encountered by each phase of compilation.

Uploaded by

Junaid khan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views22 pages

Error Handler: Presented By: Junaid Khan Department of Computer Science University of Peshawar

The document discusses error handling in compilers. It describes the different types of errors that can occur: lexical errors, syntax errors, semantic errors, logical errors, and spurious errors. It explains that the error handler is responsible for detecting errors during compilation and attempting to recover from errors so that additional errors can be found. The error handler contains routines to handle errors encountered by each phase of compilation.

Uploaded by

Junaid khan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Error Handler

Presented by:
Junaid Khan
Department of Computer Science
University of Peshawar
Compiler Errors
Errors
An Error is an abnormal condition in the
source program which either stops the
compilation or results in undesired output.
Programs submitted to a compiler often
have errors of various kinds.
With regard to errors, most compilers are
not very effective at communication since
they deliver their comments to the user
without any knowledge of the user's
"intent" for a sentence.
Compiler Errors(cont..)
Since no user has a perfect knowledge of syntax
and semantics, errors are inevitable.

The compiler detects an error, then attempts to


recover from the error so that it can detect more
errors. That is, even in the presence of errors, the
compiler tries to parse the entire program in
order to detect as many errors as possible.
Error Detection
Each phase can encountered errors.

After detecting an error, a phase must somehow


deal with that error so that compilation can
proceed ,allowing further errors in the source
program to be detected.
A compiler that stops when it find the first error
is not as helpful as it could be.
Error handler
The compiler has, as one of its parts, an error
handler.
Most programming languages specifications do
not describes how a compiler should respond to
errors, the response is left to the compiler
designer.
Planning the error handling right from the start
can both simplify the structure of a compiler and
improve its response to errors.
Error handler(cont..)
The error handler is invoked when the symbols
in a sentence do not match the compiler's current
position in the syntax diagram.

The error handler warns the programmer by


issuing an appropriate message.
Error handler(cont..)
The two basic task of compiler are

Error Detection

Error Recovery.

During the compilation process each phase


may encounter errors so error handler is used
to handle these errors
Error handler contains error handling
routines.
The Many Phases of a Compiler
Source Program

1
Lexical Analyzer

2
Syntax Analyzer

3
Semantic Analyzer

Symbol-table Error Handler


Manager 4 Intermediate
Code Generator

5
Code Optimizer

6
Code Generator

Target Program
Types Errors
There are five types of errors:
1. Lexical Errors.
2. Syntax Errors.
3. Semantic Errors.
4. Logical Errors.
5. Spurious Errors.

Note:
The first three types of errors are
handle by error handler.
9
Lexical Errors
When the remaining characters in the input do
not found a valid token then it is called lexical
errors.

For example

An identifier names beginning with


digit/integer
Misspelling of keywords may or may not be
handled in lexical error handler.
Lexical Errors (cont..)
Few errors are discernible at the lexical level
alone, because lexical analyzer has a very
localized view of a source program.
If the string fi is encountered in a C program for
the first time in the context
Fi(a==f(x))
A lexical analyzer cannot tell whether fi is a
misspelling of the key word ‘if ‘ or an undeclared
function identifier.
Lexical Errors (cont..)
Since fi is a valid identifier, the lexical analyzer
must return the token for an identifier and let
some other phase of the compiler handle any
error.
 [

But, suppose a situation does arise in which the


lexical analyzer is unable to proceed because
none of the patterns for tokens matches a prefix
of the remaining input.
Syntax Errors
When the stream of tokens violates the grammar
rule of a language then it is a Syntax Errors.
Often much of the error detection and recovery
in a compiler is centered around the syntax
analysis phase.
One reasons for this is that many errors are
syntactic in nature or are exposed when the
stream of tokens coming from the lexical
analyzer disobeys the grammatical rules defining
the programming language.
Syntax Errors (cont…)
Another is the precision of modern parsing
methods, they can detect the presence of syntactic
errors in programs very efficiently.

Example:
 Consider the sentence A B
 The sentence requires an operator (+) between the 2 letters.
 The compiler message might be Missing operator
Syntax Errors (cont…)
Example 2:
Consider the sentence A+B+
This sentence is not valid.
 The reason for the rejection is an error in the
input sentence.
The sentence needs another letter at the end.
 The compiler message might appear as
follows:
program.cpp(5) error c2059: Missing operand
^^file name.................^^the compiler's
explanation
............^^line number within the file
Syntax Errors (cont…)
 What the compiler thinks is that you meant to type another
letter.
 In the world of compilers, the "things" that you add are
termed "operands"; thus, the error message "missing
operand".
 However, it is just as likely that you made a mistake typing
the extra "+".
 The conclusion is that the message may, or may not, help
you.
 The best plan is to use the point of error (line 5) as the
starting point to determine which syntax or semantic rule
has been violated.
3. Semantic Errors
When the operation of source program are not
meaningful.
Remember that a semantic error is a rule
violation that has nothing to do with syntax
diagrams.
For example, numbers in a computer are
normally restricted to a maximum number of
digits.
 If you type too many digits 871239817231723178,
you will get a semantics-based error message.
Example

 Consider the sentence 57/0


This sentence is syntactically valid, but fails semantic test.
division by zero is invalid in mathematics so most
compiler check for this condition whenever possible.
 In most situation the dividend is not constant but might
be a number typed by you at run time
in this case division by zero is illegal .
Continue…
 Most computers check for a "division by zero"
error during program execution so you can get
a "division by zero" error message at runtime,
which will stop your program,
 to create a correct program, you must
overcome syntax errors, semantic errors,
runtime errors and writing a good program to
start with.
4. Logical Errors
It is an error in the algorithm of the source
program, due to which an undesired output
results.
A compiler can not detect logical errors.
Accurately detecting the presence of logical
errors at compile time is much more difficult
task.
For example
Infinite loop
Array indexes out of bound.
5. Spurious Errors
These are compiler generated errors.
These are those errors made by compiler
during the error recovery.
For example
we are using a function fi( ) in our source
program but during error recovery
compiler automatically correct it as if ( )
so it is a spurious error and it will
generate a problem in program.
Message
When we pray GOD hears more than we
say.
He answer more than we ask .
He gives more than we imagine.

thanks

You might also like