Error Handling in Compiler Design | Compiler Design - Computer Science Engineering (CSE) PDF Download

Introduction

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.

Functions of Error Handler


  • Error Detection
  • Error Report
  • Error Recovery

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:

  • Logic errors occur when programs operate incorrectly but do not terminate abnormally (or crash). Unexpected or undesired outputs or other behaviour may result from a logic error, even if it is not immediately recognized as such.
  • A run-time error is an error that 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 is an 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 rise at compile-time, before the execution of the program. Syntax error or missing file reference that prevents the program from successfully compiling is an example of this.

Error Handling in Compiler Design | Compiler Design - Computer Science Engineering (CSE)

Classification of Compile-time error


  • Lexical : This includes misspellings of identifiers, keywords or operators
  • Syntactical : a 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 that 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 language. 

Example: for(;), this will report an error as for having 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 as follows.
We already discuss the errors. Now, let’s try to understand the recovery of errors in every phase of the compiler.

Error Handling in Compiler Design | Compiler Design - Computer Science Engineering (CSE)

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.

The document Error Handling in Compiler Design | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Error Handling in Compiler Design - Compiler Design - Computer Science Engineering (CSE)

1. What is error handling in compiler design?
Ans. Error handling in compiler design refers to the process of detecting and managing errors or exceptional conditions that may occur during the compilation of a program. It involves identifying syntax or semantic errors in the source code and providing appropriate error messages or recovery strategies.
2. Why is error handling important in compiler design?
Ans. Error handling is crucial in compiler design as it helps in improving the overall reliability and usability of a compiler. It ensures that programmers receive meaningful error messages, which aids in debugging and fixing code issues. Moreover, error handling allows the compiler to gracefully handle errors and recover from them, preventing the termination of the compilation process.
3. What are the different types of errors encountered in compiler design?
Ans. In compiler design, various types of errors can occur, including: - Lexical errors: These errors occur when the compiler encounters invalid or unrecognized tokens in the source code. - Syntax errors: Syntax errors occur when the compiler identifies violations of the programming language's grammar rules. - Semantic errors: Semantic errors occur when the compiler detects logical or contextual errors in the code, such as type mismatches or undeclared variables. - Linker errors: Linker errors occur during the linking phase when the compiler fails to resolve references to external functions or variables. - Runtime errors: Runtime errors occur when the compiled program is executed and encounters issues like division by zero or accessing invalid memory locations.
4. How does a compiler handle errors during the compilation process?
Ans. When a compiler encounters an error during the compilation process, it follows a series of steps to handle the error. These steps may include: 1. Error detection: The compiler identifies the error by analyzing the source code or intermediate representations. 2. Error reporting: The compiler generates an error message that describes the type and location of the error. 3. Error recovery: The compiler attempts to recover from the error by applying various strategies like skipping erroneous code, inserting missing symbols, or suggesting possible corrections. 4. Error propagation: The compiler continues the compilation process after error recovery, but the presence of an error may affect the quality or correctness of the generated code.
5. How can programmers benefit from effective error handling in compilers?
Ans. Effective error handling in compilers provides several benefits to programmers, including: - Improved debugging: Meaningful error messages help programmers identify and fix errors more efficiently. - Enhanced code quality: The compiler's ability to detect and report errors encourages programmers to write cleaner and more error-free code. - Faster development: Quick error recovery strategies allow programmers to fix errors promptly and continue with the development process. - Better user experience: Clear error messages assist users in understanding and resolving issues, leading to a better overall user experience with the compiled program. - Increased productivity: Efficient error handling reduces the time and effort required for debugging and troubleshooting, thus improving overall productivity.
26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Error Handling in Compiler Design | Compiler Design - Computer Science Engineering (CSE)

,

MCQs

,

Summary

,

Free

,

study material

,

Sample Paper

,

Extra Questions

,

mock tests for examination

,

Objective type Questions

,

shortcuts and tricks

,

Viva Questions

,

ppt

,

Error Handling in Compiler Design | Compiler Design - Computer Science Engineering (CSE)

,

past year papers

,

Error Handling in Compiler Design | Compiler Design - Computer Science Engineering (CSE)

,

Previous Year Questions with Solutions

,

pdf

,

Semester Notes

,

Important questions

,

video lectures

,

practice quizzes

,

Exam

;