11
SYNTAX ANALYSIS & SYNTAX ANALYSIS & ERROR RECOVERY ERROR RECOVERY By: Sarthak Swaroop By: Sarthak Swaroop

SYNTAX ANALYSIS & ERROR RECOVERY By: Sarthak Swaroop

Embed Size (px)

Citation preview

Page 1: SYNTAX ANALYSIS & ERROR RECOVERY By: Sarthak Swaroop

SYNTAX ANALYSIS SYNTAX ANALYSIS & ERROR & ERROR

RECOVERYRECOVERY

By: Sarthak SwaroopBy: Sarthak Swaroop

Page 2: SYNTAX ANALYSIS & ERROR RECOVERY By: Sarthak Swaroop

SYNTAX ANALYSISSYNTAX ANALYSIS

The main features of syntax analysis, The main features of syntax analysis, which is done by parser, are as which is done by parser, are as follows:follows:

Checks the GrammarChecks the Grammar Parse Tree ProductionParse Tree Production Output ErrorsOutput Errors

Page 3: SYNTAX ANALYSIS & ERROR RECOVERY By: Sarthak Swaroop

Types of ErrorTypes of Error

There are mainly four types of error. There are mainly four types of error. They are as follows: They are as follows:

Lexical Error : Such as misspelling an Lexical Error : Such as misspelling an identifier, keyword or operator.identifier, keyword or operator.

Syntactic Error : Such as an arithmetic Syntactic Error : Such as an arithmetic expression with unbalanced expression with unbalanced parentheses.parentheses.

Semantic Error : Such as operator Semantic Error : Such as operator applied to an incompatible operand.applied to an incompatible operand.

Logical Error : Such as infinitely Logical Error : Such as infinitely recursive call.recursive call.

Page 4: SYNTAX ANALYSIS & ERROR RECOVERY By: Sarthak Swaroop

ERROR HANDLING ERROR HANDLING TECHNIQUESTECHNIQUES

Page 5: SYNTAX ANALYSIS & ERROR RECOVERY By: Sarthak Swaroop

1. Panic Mode1. Panic Mode In case of an error like:In case of an error like: a=b + c // no semi-colona=b + c // no semi-colon

d=e + f ;d=e + f ;

The compiler will discard all The compiler will discard all subsequent tokens till a semi-colon is subsequent tokens till a semi-colon is encountered .encountered .

This is a crude method but often turns This is a crude method but often turns out to be the best method. out to be the best method.

Page 6: SYNTAX ANALYSIS & ERROR RECOVERY By: Sarthak Swaroop

This method often skips a This method often skips a considerable amount of input considerable amount of input without checking it for additional without checking it for additional errors, it has an advantage of errors, it has an advantage of simplicitysimplicity

In situations where multiple errors In situations where multiple errors in the same statements are rare, this in the same statements are rare, this method may be quite adequate.method may be quite adequate.

Page 7: SYNTAX ANALYSIS & ERROR RECOVERY By: Sarthak Swaroop

2. Phrase Level Recovery2. Phrase Level Recovery

On discovering an error, a parser may On discovering an error, a parser may perform local correction on the perform local correction on the remaining input; that is, it may replace a remaining input; that is, it may replace a prefix of the remaining input by some prefix of the remaining input by some string that allows the parser to continue. string that allows the parser to continue.

For example, in case of an error like the For example, in case of an error like the one above, it will report the error, one above, it will report the error, generate the “;” and continue.generate the “;” and continue.

Page 8: SYNTAX ANALYSIS & ERROR RECOVERY By: Sarthak Swaroop

3. Error Production3. Error Production

If we have an idea of common errors If we have an idea of common errors that might occur, we can include the that might occur, we can include the errors in the grammar at hand. For errors in the grammar at hand. For example if we have a production rule example if we have a production rule like:like:

EE +E | -E | *E | /E +E | -E | *E | /E Then, a=+b;Then, a=+b; a=-b;a=-b; a=*b;a=*b; a=/b;a=/b;

Page 9: SYNTAX ANALYSIS & ERROR RECOVERY By: Sarthak Swaroop

Here, the last two are error situations.Here, the last two are error situations.

Now, we change the grammar as:Now, we change the grammar as:

EE +E | -E | *A | /A +E | -E | *A | /A

AA E E

Hence, once it encounters *A, it sends Hence, once it encounters *A, it sends an error an error

message asking the user if he is sure message asking the user if he is sure he wantshe wants

to use a unary “*”.to use a unary “*”.

Page 10: SYNTAX ANALYSIS & ERROR RECOVERY By: Sarthak Swaroop

4. Global Correction4. Global Correction

We would like compiler to make as We would like compiler to make as few changes as possible in few changes as possible in processing an incorrect input string.processing an incorrect input string.

There are algorithms for choosing a There are algorithms for choosing a minimal amount of changes to minimal amount of changes to obtain a globally least-cost obtain a globally least-cost correction. Suppose we have a line correction. Suppose we have a line like this:like this:

THIS IS A OCMPERIL SCALS.THIS IS A OCMPERIL SCALS.

Page 11: SYNTAX ANALYSIS & ERROR RECOVERY By: Sarthak Swaroop

To correct this, there is an To correct this, there is an attractorattractor, , which checks which checks how different tokens how different tokens are from the initial inputsare from the initial inputs, checks , checks the closest attractor to the incorrect the closest attractor to the incorrect token is.token is.

This is more of a This is more of a probabilistic type of probabilistic type of error correctionerror correction. Unfortunately, these . Unfortunately, these methods are in general too costly to methods are in general too costly to implement in terms of space and time. implement in terms of space and time.