Compiler Design

Error Detection and Recovery in Syntax Analysis

Syntax Error Handling A program can contain errors at many different levels. For example, errors can be of the following types.

1. Lexical, such as misspelling an identifier, keyword, or operator.

2. Syntactic, such as an arithmetic expression with unbalanced parentheses.

3. Semantic, such as an operator applied to an incompatible operand.

4. Logical, such as an infinite recursive call.

In compilers, most of the error detection and recovery is focused on the syntax analysis phase. There are two reasons for this.

(i) There are many syntactic errors. These get detected when a stream of tokens coming from the lexical analyzer disobeys the grammar rules that define the programming language.

(ii) Modern parsing methods are very precise and can detect the presence of syntactic errors quite efficiently.

Goals of an Error Handler in a Compiler

An error handler is a routine in a program that responds to errors. It should have the following features.

It should report the presence of errors clearly and accurately and provide alternatives for dealing with them.

It should recover from each error quickly enough to be able to detect subsequent errors.

It should not significantly slow down the processing of correct programs.

Error Recovery Strategies :

A parser can employ the following strategies to recover from syntactic errors.

1. Panic Mode

2. Phrase Level

3. Error Productions

4. Global Corrections

Panic Mode Recovery:

This is the simplest method to implement and can be used by most parsing methods.

When an error is detected, the parser discards the input symbols, one at a time, until it finds one of the designated sets of synchronizing tokens (synchronizing tokens are delimiters, such as semicolon or end, with a clear role in the source program.

Advantages :

  • It is simple
  • It does not go into an infinite loop.
  • It is adequate in situations where multiple errors are not there in the same statement.

Disadvantage:

Panic amount mode of input correction without often checking it for additional errors.

Phrase Level Recovery:

When an error is detected, the parser performs local corrections on the remaining input. For example, it replaces a prefix of the remaining input by some string which allows the parser to continue.

Some Typical Local Corrections:

  • Replace a comma with a semicolon
  • Delete an inappropriate semicolon
  • Insert a missing semicolon

Disadvantage:

Phrase level correction finds it difficult to cope with situations in which the actual error has occurred before the point of detection.

Error Production:

If we know the common errors that are likely to occur, we can supplement the grammar of the language with error-producing constructs. When this grammar is used by the parser, error diagnostics can be generated to identify the erroneous construct found in the input.

Global Corrections: Ideally a compiler should make as few changes as possible, in processing an incorrect input string.

Suppose we have an incorrect input string x and grammar G. Some algorithms will find a parse tree for a related stringy, such that the number of insertions, deletions, and changes of tokens required to transform x into y, is as small as possible.

Disadvantage: Error production and global corrections are too costly to implement in terms of time and space.

Leave a Reply

Your email address will not be published. Required fields are marked *