Error Handler

Embed Size (px)

Citation preview

  • 8/8/2019 Error Handler

    1/22

    Error Handler

    Presented by:

    Junaid Khan

    Department of Computer Science

    University of Peshawar

  • 8/8/2019 Error Handler

    2/22

    Compiler Errorsy Errors

    y An Error is an abnormal condition in thesource program which either stops the

    compilation or results in undesired output.

    y Programs submitted to a compiler oftenhave errors of various kinds.

    y

    With regard to errors, most compilers arenot very effective at communication sincethey deliver their comments to the user

    without any knowledge of the user's"intent" for a sentence.

  • 8/8/2019 Error Handler

    3/22

    Compiler Errors(cont..)

    y Since no user has a perfect knowledge of syntaxand semantics, errors are inevitable.

    y The compiler detects an error, then attempts torecover from the error so that it can detect moreerrors. That is, even in the presence of errors, the

    compiler tries to parse the entire program inorder to detect as many errors as possible.

  • 8/8/2019 Error Handler

    4/22

    Error Detectiony Each phase can encountered errors.

    y After detecting an error, a phase must somehowdeal with that error so that compilation canproceed ,allowing further errors in the sourceprogram to be detected.

    y A compiler that stops when it find the first erroris not as helpful as it could be.

  • 8/8/2019 Error Handler

    5/22

    Error handler

    y The compiler has, as one of its parts, an errorhandler.

    y Most programming languages specifications donot describes how a compiler should respond toerrors, the response is left to the compilerdesigner.

    y Planning the error handling right from the startcan both simplify the structure of a compiler andimprove its response to errors.

  • 8/8/2019 Error Handler

    6/22

    Error handler(cont..)y The error handler is invoked when the symbols

    in a sentence do not match the compiler's currentposition in the syntax diagram.

    y The error handler warns the programmer byissuing an appropriate message.

  • 8/8/2019 Error Handler

    7/22

    y The two basic task of compiler are

    y Error Detection

    y Error Recovery.

    y During the compilation process each phasemay encounter errors so error handler is used

    to handle these errors

    y Error handler contains error handlingroutines.

    Error handler(cont..)

  • 8/8/2019 Error Handler

    8/22

    The Many Phases of a CompilerSource Program

    Lexical Analyzer1

    Syntax Analyzer2

    Semantic Analyzer3

    Intermediate

    Code Generator

    4

    Code Optimizer5

    Code Generator6

    Target Program

    Symbol-table

    Manager

    Error Handler

  • 8/8/2019 Error Handler

    9/22

    9

    Types Errorsy 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 arehandle by error handler.

  • 8/8/2019 Error Handler

    10/22

    Lexical Errorsy When the remaining characters in the input do

    not found a valid token then it is called lexicalerrors.

    y For example

    y An identifier names beginning with

    digit/integery Misspelling of keywords may or may not be

    handled in lexical error handler.

  • 8/8/2019 Error Handler

    11/22

    Lexical Errors (cont..)y Few errors are discernible at the lexical level

    alone, because lexical analyzer has a verylocalized view of a source program.

    y If the string fi is encountered in a C program for

    the first time in the context

    Fi(a==f(x))

    y A lexical analyzer cannot tell whether fi is amisspelling of the key word if or an undeclaredfunction identifier.

  • 8/8/2019 Error Handler

    12/22

    Lexical Errors (cont..)y 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 anyerror.

    y [

    y But, suppose a situation does arise in which thelexical analyzer is unable to proceed because

    none of the patterns for tokens matches a prefixof the remaining input.

  • 8/8/2019 Error Handler

    13/22

    Syntax Errorsy When the stream of tokens violates the grammar

    rule of a language then it is a Syntax Errors.

    y Often much of the error detection and recoveryin a compiler is centered around the syntaxanalysis phase.

    y One reasons for this is that many errors are

    syntactic in nature or are exposed when thestream of tokens coming from the lexicalanalyzer disobeys the grammatical rules definingthe programming language.

  • 8/8/2019 Error Handler

    14/22

    Syntax Errors

    (cont)

    y Another is the precision of modern parsingmethods, they can detect the presence of syntacticerrors in programs very efficiently.

    y Example:y Consider the sentence A By The sentence requires an operator (+) between the 2 letters.y The compiler message might be Missing operator

  • 8/8/2019 Error Handler

    15/22

    Syntax Errors (cont)yExample 2:y Consider the sentence A+B+y This sentence is not valid.y The reason for the rejection is an error in

    the input sentence.y The sentence needs another letter at the end.y The compiler message might appear as

    follows:y program.cpp(5) error c2059: Missing

    operand^^file name.................^^the compiler'sexplanation............^^line number within the file

  • 8/8/2019 Error Handler

    16/22

    y What the compiler thinks is that you meant to type anotherletter.

    y In the world of compilers, the "things" that you add are

    termed "operands"; thus, the error message "missingoperand".

    y However, it is just as likely that you made a mistake typingthe extra "+".

    y The conclusion is that the message may, or may not, help

    you.y The best plan is to use the point of error (line 5) as the

    starting point to determine which syntax or semantic rulehas been violated.

    Syntax Errors (cont)

  • 8/8/2019 Error Handler

    17/22

    3. Semantic Errorsy When the operation of source program are not

    meaningful.

    y Remember that a semantic error is a ruleviolation that has nothing to do with syntaxdiagrams.

    y For example, numbers in a computer are

    normally restricted to a maximum number ofdigits.

    y If you type too many digits 871239817231723178,you will get a semantics-based error message.

  • 8/8/2019 Error Handler

    18/22

    Example

    y Consider thesentence 57/0

    y Thissentenceissyntacticallyvalid, but failssemantic test.

    division byzeroisinvalidinmathematicssomost compiler

    check for thiscondition whenever possible.y Inmost situation thedividendisnot constant but might

    be a number typed byyou at run time

    in thiscase division byzeroisillegal .

  • 8/8/2019 Error Handler

    19/22

    Continuey 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,

    y to create a correct program, you mustovercome syntax errors, semantic errors,runtime errors and writing a good program to

    start with.

  • 8/8/2019 Error Handler

    20/22

    4. Logical Errorsy It is an error in the algorithm of the source

    program, due to which an undesired outputresults.

    y A compiler can not detect logical errors.

    y Accurately detecting the presence of logicalerrors at compile time is much more difficulttask.

    yFor exampley Infinite loop

    yArray indexes out of bound.

  • 8/8/2019 Error Handler

    21/22

    5. Spurious Errorsy These are compiler generated errors.

    y These are those errors made by compiler

    during the error recovery.yFor exampleywe 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 willgenerate a problem in program.

  • 8/8/2019 Error Handler

    22/22

    MessageWhen we pray GOD hears more than we

    say.

    He answer more than we ask .

    He gives more than we imagine.

    thanks