The tasks of the Error Handling process are to detect each error, report it to the user, and then make some recover strategy and implement them to handle error. During this whole process processing time of program should not be slow. An Error is the blank entries in the symbol table.
Types or Sources of Error – There are two types of error: run-time and compile-time error:
- A run-time error is an error which takes place during the execution of a program, and usually happens because of adverse system parameters or invalid input data. The lack of sufficient memory to run an application or a memory conflict with another program and logical error are example of this. Logic errors, occur when executed code does not produce the expected result. Logic errors are best handled by meticulous program debugging.
- Compile-time errors rises at compile time, before execution of the program. Syntax error or missing file reference that prevents the program from successfully compiling is the example of this.
Classification of Compile-time error –
- Lexical : This includes misspellings of identifiers, keywords or operators
- Syntactical : missing semicolon or unbalanced parenthesis
- Semantical : incompatible value assignment or type mismatches between operator and operand
- Logical : code not reachable, infinite loop.
Finding error or reporting an error – Viable-prefix is the property of a parser which allows early detection of syntax errors.
- Goal: detection of an error as soon as possible without further consuming unnecessary input
- How: detect an error as soon as the prefix of the input does not match a prefix of any string in the
- Example: for(;), this will report an error as for have two semicolons inside braces.
Error Recovery –
The basic requirement for the compiler is to simply stop and issue a message, and cease compilation. There are some common recovery methods that are follows.
- Panic mode recovery: This is the easiest way of error-recovery and also, it prevents the parser from developing infinite loops while recovering error. The parser discards the input symbol one at a time until one of the designated (like end, semicolon) set of synchronizing tokens (are typically the statement or expression terminators) is found. This is adequate when the presence of multiple errors in same statement is rare. Example: Consider the erroneous expression- (1 + + 2) + 3. Panic-mode recovery: Skip ahead to next integer and then continue. Bison: use the special terminal error to describe how much input to skip.
- Phase level recovery: Perform local correction on the input to repair the error. But error correction is difficult in this strategy.
- Error productions: Some common errors are known to the compiler designers that may occur in the code. Augmented grammars can also be used, as productions that generate erroneous constructs when these errors are encountered. Example: write 5x instead of 5*x
- Global correction: Its aim is to make as few changes as possible while converting an incorrect input string to a valid string. This strategy is costly to implement.
Next related article – Error detection and Recovery in Compiler