
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:
- Lexical Phase Errors (See Lexical Analysis in Compiler Design)
- Syntactic Phase Errors
- Semantic 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.
A 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!
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;
}
}
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.
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
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
Example3- Errors in Expression
a = (b+c * (c+d); //missing closing parentheses
i = j * + c ; // missing argument between “*” and “+”
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.
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;
}
Example 2: Incompatible Types
#include <stdio.h>
int main()
{
int a = 0;
int b = "ABC"; // b is having non-integer value
return 0;
}
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;
}
Now, let’s see some FAQs based on the above discussion: