What is error handling in compiler design?


Detection and reporting of errors in the source program is the main function of the compiler. An error can occur at any phase of compilation. A good compiler must determine the line number of the program exactly, where the errors have occurred. Various errors that can occur at a different level of compilation are as follows −

  • The first of these are lexical (scanner) errors − Some of the most common types here consist of illegal or unrecognized characters, mainly, caused by typing errors. A common way for this to happen is for the programmer to type a character that is illegal in any instance in the language, and is never used. There is another type of error that the scanner can detect is an unterminated character or string constant. This happens whenever the programmer types something in quotes and forgets the trailing quote.

  • The second class of error is syntactic − These errors are caught by the parser. These errors are among the most common. The difficult part is to decide from where to continue the syntactical analysis after an error has been found. If the parser is not carefully written, or if the error detection and recovery scheme are not proper, the parser will hit one error and “mess up” after that, and give wrong error messages throughout the rest of the program.

In case of an error, what one would like to see happening, is to have the compiler skip any improper tokens, and continue to detect errors without generating error messages that are not an error but a consequence of the first error. This aspect is so important that some compilers are categorized based on how good their error detection system is.

  • The third type of error is semantic − The semantics that is used in computer languages are far simpler than the semantics that is used in spoken languages. This is the case because in computer languages everything is very much defined. The semantic errors that can occur in a program are related to the fact that some statements can be correct from the syntactical point of view, but they make no sense and no code can be generated to carry out the meaning of the statement.

  • The fourth type of error may encounter during code optimization − During control flow analysis, there may exist some statements which can never be reached.

  • The fifth type of error may occur during code generation − In code generation, the architecture of the computer also plays an important role.

  • The sixth type of error may encounter when the compiler tries to make symbol table entries − In this, there may exist an identifier that has multiple declarations with contradictory attributes.

Updated on: 23-Oct-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements