The tasks of the Error Handling process are to detect each error, report it to the user, and then make some recovery strategy and implement them to handle the error. During this whole process processing time of the program should not be slow.
Error handler=Error Detection+Error Report+Error Recovery.
An Error is the blank entries in the symbol table.
Errors in the program should be detected and reported by the parser. Whenever an error occurs, the parser can handle it and continue to parse the rest of the input. Although the parser is mostly responsible for checking for errors, errors may occur at various stages of the compilation process.
So, there are many types of errors and some of these are:
Types or Sources of Error
There are three types of error: logic, run-time and compile-time error:
Viable-prefix is the property of a parser that allows early detection of syntax errors.
Example: for(;), this will report an error as for having two semicolons inside braces.
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 as follows.
We already discuss the errors. Now, let’s try to understand the recovery of errors in every phase of the compiler.
1. 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 the same statement is rare. Example: Consider the erroneous expression- (1 + + 2) + 3. Panic-mode recovery: Skip ahead to the next integer and then continue. Bison: use the special terminal error to describe how much input to skip.
E->int|E+E|(E)|error int|(error)
2. Phase level recovery: When an error is discovered, the parser performs local correction on the remaining input. If a parser encounters an error, it makes the necessary corrections on the remaining input so that the parser can continue to parse the rest of the statement. You can correct the error by deleting extra semicolons, replacing commas with semicolons, or reintroducing missing semicolons. To prevent going in an infinite loop during the correction, utmost care should be taken. Whenever any prefix is found in the remaining input, it is replaced with some string. In this way, the parser can continue to operate on its execution.
3. Error productions: The use of the error production method can be incorporated if the user is aware of common mistakes that are encountered in grammar in conjunction with errors that produce erroneous constructs. When this is used, error messages can be generated during the parsing process, and the parsing can continue. Example: write 5x instead of 5*x
4. Global correction: In order to recover from erroneous input, the parser analyzes the whole program and tries to find the closest match for it, which is error-free. The closest match is one that does not do many insertions, deletions, and changes of tokens. This method is not practical due to its high time and space complexity.
26 videos|66 docs|30 tests
|
1. What is error handling in compiler design? |
2. Why is error handling important in compiler design? |
3. What are the different types of errors encountered in compiler design? |
4. How does a compiler handle errors during the compilation process? |
5. How can programmers benefit from effective error handling in compilers? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|