Table of contents
1.
Introduction
2.
Types Of Errors 
2.1.
Types Of Compile Time Errors
2.1.1.
Lexical Error
2.1.2.
Syntactic Error 
2.1.3.
Semantic Error 
3.
Frequently Asked Questions
3.1.
How many types of Lexical errors are possible?
3.2.
How does the syntax analyzer identify the errors? 
3.3.
How does the semantic analyzer identify the errors?
3.4.
What are Logical Errors?
3.5.
Differentiate between Runtime and Compile Time errors. 
4.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Errors in Compiler Design

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Compiler Design

Introduction

The compiler is software that converts a high-level language (Source Language) program to a low-level language (Object/Target/Machine Language). Okay, you know this right? But, what if I tell you that a compiler is not only used for translating but also for error handling and recognition. It is because when the compiler translates the Pure High-Level language( HLL) code into Machine Code, there might be a chance that the source code written by the programmer may contain errors. (See Phases of a Compiler)

Well, I am not. The translation can only be done if and only if the source code is free from any errors. 

So, What is an error? 

An error is defined as a user action that causes an abnormal change or issue in a program. Errors are problems or faults that occur in a program and cause the program's behavior to be abnormal; experienced developers can also make these errors, so don't think that making errors is bad. Bugs and faults are other terms for programming errors.

But before that, we shall discuss what kind of errors are usually handled by the compiler. Henceforth, in this article, we are going to discuss numerous types of errors in Compiler design along with several examples. 

Also read About, Specifications of Tokens in Compiler Design

Types Of Errors 

There are two types of errors: run-time errors and compile-time errors:

Run-time Error - A run-time error is one that occurs during the execution of a program and is usually caused by incorrect system parameters or invalid input data. Examples can be a lack of memory to run an application, a memory conflict with another program, and a logical error. Logic errors occur when code is executed and does not produce the desired result. 

Compile-time errors - Compile-time errors increase during the compilation process before the program is executed. An example of this is a syntax error or a missing file reference that prevents the program from successfully compiling.

We shall be discussing only the Compile-time errors in this article. 

Types Of Compile Time Errors

Compile-time errors are further divided into three categories:

  1. Lexical Phase Errors (See Lexical Analysis in Compiler Design)
  2. Syntactic Phase Errors
  3. Semantic Errors 

Compile TIme Errors

Now, let us discuss each one by one: 

Lexical Error

Lexical errors are errors that your lexer throws when it is unable to continue. This means that there's no way to recognize a lexeme as a valid token for your lexer. If you consider a lexer to be a finite state machine that accepts valid input strings, errors are any input strings that do not result in that finite state machine reaching an accepting state. 

During the Lexical analyzer phase, lexical errors occur. In lexical analyzer conversion of the program into the stream of tokens is done. There are patterns through which the identifiers are identified. 

Phases of compiler

lexical error is a sequence of characters that does not match the pattern of any token.  During the execution of a program, a lexical phase error is found. 

Lexical phase error can be: 

  • Any Spelling errors.
  • Exceeding the length of an identifier or numeric constants. 
  • The appearance of illegal characters.
  • To replace a character with an incorrect character.
  • Transposition of two characters.

Valid patterns can be: 

[0-9]+   ==> NUMBER token
[a-zA-Z] ==> LETTERS token
anything else ==> error!
You can also try this code with Online C Compiler
Run Code

 

As you can see in the above example, all the possible number tokens and letters will only be considered as valid identifiers. Any other value would result in a lexical error for the Number and Letters Pattern. 

Live Example 

#include <stdio.h>
int main()
{
  int total-value; // lexical-error 
  int var1=2, num = 5;
  int sum=num*1;
  if(sum=var1)
    {
      sum=sum+1;
    }
}
You can also try this code with Online C Compiler
Run Code

 

The use of a hyphen(-) in total-value results in an invalid variable name. 

Syntactic Error 

In computer science, a syntactic error is an error in the syntax of a sequence of characters or tokens intended to be written in a specific programming language. This type of error appears during the syntax analysis phase. Syntax or syntactic error is also found during the execution of the program.

Phases of Compiler Design

Some syntax errors can be:

  • Error in structure
  • Unbalanced parenthesis
  • Missing operators

A syntax error can occur when an invalid calculation is entered into a calculator. This can happen if you enter multiple decimal points in one number or if you open brackets without closing them. 

Example1- Missing closing braces

void printHelloNinja( String s )
{  
// function - body 
You can also try this code with Online C Compiler
Run Code

 

As you can see in the above code, the closing braces are missing so it results in syntactic error. 

Example2- Missing Semicolon

x = a + b * c  //missing semicolon 
You can also try this code with Online C Compiler
Run Code

 

Example3- Errors in Expression 

a = (b+c * (c+d); //missing closing parentheses 
i = j * + c ;   // missing argument between “*” and “+”
You can also try this code with Online C Compiler
Run Code

Semantic Error 

This type of error appears during the semantic analysis phase. These types of errors are detected during the compilation process. Now, it is the phase where your defined identifiers are verified.

Phases of Compiler Design

The majority of compile-time errors are scope and declaration errors. For example, undeclared identifiers or multiple declared identifiers. Semantic errors can occur when the invalid variable or operator is used, or the operations are performed in the incorrect order.

There can be different types of compilation errors depending on the program you’ve written.

Some examples of semantic errors are:

  • Operands of incompatible types
  • Variable not declared
  • The failure to match the actual argument with the formal argument

 

Example 1: Use of a non-initialized variable

#include <stdio.h>
int main()
{
    int a = 0, b = 7;
    sum = a + b; // sum is undefined 
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Compilation Error

Example 2: Incompatible Types

#include <stdio.h>
int main()
{
    int a = 0;
    int b = "ABC"; // b is having non-integer value 
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Error

Example 3: Errors in Expressions

#include <stdio.h>
int main()
{
    int a = 5 - s;     // the - operator does not support arguments of type String  
    return 0;
}
You can also try this code with Online C Compiler
Run Code

Error

Now, let’s see some FAQs based on the above discussion: 

Frequently Asked Questions

How many types of Lexical errors are possible?

There are four types of lexical errors: Unterminated comments (for example, /* no closing, etc.), Comments that are nested (/* hello /* hi*/bye*/), Invalid constant (for example, int a=$10;), Invalid identifier (for example, int 1a,2a;)

How does the syntax analyzer identify the errors? 

Syntax analyzer makes use of Parse Tree which follows some set of rules also known as Grammar. The input has to be checked whether it follows the defined rules or not.

How does the semantic analyzer identify the errors?

Semantics Analyzer semantically verifies the Parse Tree whether it is meaningful or not. 

What are Logical Errors?

When a program gives undesired output, one can say that logically the program is wrong. The error results in a logical error. 

Differentiate between Runtime and Compile Time errors. 

Compile-time errors are those that occur during the compilation process and are related to the semantics or syntax of the program. A runtime error is an error that occurs during the execution of code at runtime. During code development, we can easily fix a compile-time error. A runtime error cannot be detected by a compiler.

Conclusion 

To wrap up the article, we’ve discussed all the errors in Compiler Design along with their examples. We’ve also discussed in which phases they occur. At last, we’ve walked through the FAQs. 

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Do upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass