Error handling in Compiler Design

The process of error handling is to detect each error, tell it to the user, and then form a recovery strategy to handle the errors. The whole process happens in such a way that processing time does not get affected by it. Therefore, in simple words,

error handler= error detection+error report+error recovery

Some functions of error handler:
● Error detection
● Error report
● Error recovery

Types Of Errors

On a major level, there are three types of errors. Let’s know about them one by one.

1. Logic errors

Logic errors usually happen when there is something wrong with the logic of the program. It happens when the program operates incorrectly or it crashes. It may not be recognized correctly but the unexpected output may come.

2. Run time error

Run time error happens during the execution of the program. Mostly, the reasons for run time error are adverse parameters or invalid input. Lack of memory may also cause programs to show run time errors.

Runtime errors are further sub-divided into the following categories-

Ways to avoid runtime errors

● Don’t use uninitialized variables.
● Check every occurrence of variable and make sure it is b=not getting
● Don’t declare too much memory for variables
● Often use return statement at the end of programs
● Avoid specifying null pointer or void

3. Compile-time errors

As its name says, compile-time errors arise at compile-time only. The reason for compile-
time error can be missing syntax or missing file reference. Compile-time errors are also
further divided into the following subcategories:
1. Lexical- It comprises spelling mistakes in identifiers, keywords, and operators.
2. Syntactical- It happens due to the missing of a semicolon.
3. Semantical- It happens when a type mismatches between operand and operator.
4. Logical- It happens when there is an infinite loop or code is not reachable.

Finding error or reporting an error

Now, let’s have a look at how you can easily find an arrow in the program and how to report
it. The viable-prefix property of a parser allows the detection of syntax errors in a program.
There are two things you need to take care of:
● Goal- Here, the goal is to detect errors as soon as possible without wasting
unnecessary input.
● How- Detect the errors as soon as the input’s prefix does not match with the prefix of
any string in the language that you are using.

Error recovery

Whenever there is an arrow in the program, the compiler stops and simply gives out a message regarding the error. As we have discussed errors and their several types, now let’s have a look at the recovery of these errors in the compiler.
Following are the types of recovery used to get errors in the compiler:

1. Panic mode recovery

It is known as the easiest way to recover errors in a program. It simply prevents the
parser from going into infinite loops. When the parser gets an error in a statement, it
ignores the rest of the statement by not processing input from erroneous input
to delimiter, such as semi-colon.
The advantage of panic mode recovery is that it is very easy to implement and
guarantees that it won’t go into an infinite loop. And the advantage of using
this method is that the amount of input is skipped before checking for extra

2. Phase level recovery/ Statement mode recovery

When an error is encountered, the parser does local correction on the
remaining input. Then it parses the rest of the statement. The errors can be
handled by removing extra semi-colons, or adding some missing semi-colons.
Therefore, care needs to be taken to prevent it from going into an infinite loop.
The disadvantage of using the statement mode recovery method is that it
does not work properly when the real error occurs before pointing of

3. Error productions

The use of this recovery method is only helpful when the user is aware of
common mistakes such as grammatical mistakes, and other such kinds of
errors. When the compiler finds these kinds of errors in the program, the error
messages are generated during the process of parsing. Then, the parting
process can continue to work. For example, write 3x instead of 3*x.
The only disadvantage of using error production is that it is difficult to handle.

4. Global correction

When it comes to recovering from erroneous input, the parser has to analyze
the whole program. While analyzing, it tries to find the closest error-free match
for it. In this, the closest error-free match refers to the one that does not do
many insertions, deletions, and changes of the token. This recovery method
requires a high time complexity and large space complexity. Due to this
reason, it is not practical to apply this method.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aditi Jha

Aditi Jha


Hello everyone, iam Aditi Jha from Delhi and Iam pursuing in electronics and communication from Banasthali Vidyapith.