Basic functions of compiler.pptx
Basic functions of compiler.pptx
1. Syntax Errors
2. Runtime Errors
3. Logical Errors
4. Linked Errors
5. Semantic Errors
• These are also referred to as
1. Syntax Errors compile-time errors. These
errors have occurred when the
rule of C writing techniques or
syntaxes has been broken.
These types of errors are
#include<stdio.h> typically flagged by the
int main() compiler prior to compilation.
{
// missing semicolon
• Example 1: In the below
printf(“Apple”)
program we are getting an error
return 0; because of a missing semicolon
} at the end of the output
statement (printf()) called
syntax error.
• This type of error occurs while
the program is running.
2. Runtime Errors
• Because this is not a compilation
error, the compilation will be
completed successfully.
• These errors occur due to
Example: segmentation fault when a
#include <stdio.h> number is divided by division
operator or modulo division
int main() operator.
{ • Example: Let us consider an
array of length 5 i.e. array[5], but
int array[5]; during runtime, if we try to
printf("%d",array[10]); access 10 elements i.e array[10]
return 0; then we get segmentation fault
errors called runtime errors.
} Giving only an array length of 5
• Even if the syntax and other factors
3. Logical Errors are correct, we may not get the
desired results due to logical issues.
Example: • These are referred to as logical
errors.
• We sometimes put a semicolon after
#include <stdio.h> a loop, which is syntactically
int main()
correct but results in one blank
loop.
{ • In that case, it will display the
int i; desired output.
for(i = 0; i <= 5; i++); • Example: In the below example,
{ the for loop iterates 5 times but the
printf(“Green"); output will be displayed only one
} time due to the semicolon at the end
of for loop. This kind of error is
return 0; called a logical error.
}
• When the program is
4. Linker Errors successfully compiled and
attempting to link the different
object files with the main object
Example: file, errors will occur.
• When this error occurs, the
#include<stdio.h> executable is not generated.
Int Main() • This could be due to incorrect
{ function prototyping, an
Printf(“Apple”); incorrect header file, or other
return0; factors.
} • If main() is written as Main(), a
linked error will be generated.
• When a sentence is
5. Semantic Errors syntactically correct but has no
meaning, semantic errors occur.
#include <stdio.h> • This is similar to grammatical
errors.
// Driver code • If an expression is entered on
int main() the left side of the assignment
{ operator, a semantic error may
int x = 10;
b = 20, c;
occur.
x + y = c;
printf("%d", c);
return 0;
}
Phases/Structure of Compiler
• The compilation process takes place in several
phases.
• Moreover, for each step, the output of one step acts as
the input for the next step.
• The phases/structure of the compilation process are is
follows:
1. Lexical Analyzer
2. Syntax Analyzer
3. Semantic Analyzer
4. Intermediate Code Generator (ICG)
5. Code Optimizer
6. Target Code Generator
1. Lexical Analyzer
• It takes the high-level language source code as the
input.
• It scans the characters of source code from left to
right. Hence, the name scanner also.
• It groups the characters into lexemes. Lexemes are a
group of characters which has some meaning.
• Each lexeme corresponds to form a token.
• It removes white spaces and comments.
• It checks and removes the lexical errors(e.g.,
erroneous characters(mistaken and incorrect. Early
explorers had the erroneous notion that the oceans
were full of dragons.), comments, and white space.
2. Syntax Analyzer
• ‘Parser’ is the other name for the syntax analyzer.
• The output of the lexical analyzer is its input.
• It checks for syntax errors in the source code.
• It does this by constructing a parse tree of all the
tokens.
• For the syntax to be correct, the parse tree should
be according to the rules of source code grammar.
• The grammar for such codes is context-free
grammar.
A parser tree is made up of nodes and branches. In
the picture the parse tree is the entire structure, starting
from S and ending in each of the leaf nodes
3. Semantic Analyzer
• It verifies the parse tree of the syntax analyzer.
• It checks the validity of the code in terms of
programming language.
• Like, compatibility of data types, declaration, and
initialization of variables, etc.
• It also produces a verified parse tree.
• Furthermore, we also call this tree an annotated parse
tree.
• It also performs flow checking, type checking, etc.
4. Intermediate Code Generator (ICG)
• It generates an intermediate code.
• This code is neither in high-level language nor in
machine language. It is in an intermediate form.
• It is converted to machine language but, the last
two phases are platform dependent.
• The intermediate code is the same for all the
compilers. Further, we generate the machine code
according to the platform.
• An example of an intermediate code is three
address code.
• Three address code is a type of intermediate
code which is easy to generate and can be easily
converted to machine code.
• General representation
a = b op c
Where a, b or c represents operands like names,
constants or compiler generated temporaries and op
represents the operator
5. Code Optimizer
• It optimizes the intermediate code.
• Its function is to convert the code so that it
executes faster using fewer resources (CPU,
memory).
• It removes any useless lines of code and rearranges
the code.
• The meaning of the source code remains the same.
• Optimization can be categorized into two types:
machine-dependent and machine-independent.
6. Target Code Generator
• The main purpose of the Target Code generator is to
write a code that the machine can understand and also
register allocation, instruction selection, etc.
• The output is dependent on the type of assembler.
• This is the final stage of compilation.
• The optimized code is converted into re-locatable
machine code which then forms the input to the linker
and loader.
Machine dependent
compiler features
•What is Machine Dependent?
In computing, machine dependent refers to features
of a program or system that are specific to a
particular hardware platform or operating system, and
are not portable across different hardware or software
environments.
For example, a machine-dependent program may
use instructions or features that are specific to a
particular type of processor or operating system, and
will not work on other types of hardware or software.
Machine-dependent programs are often written
in assembly language, which is a low-level
programming language that is closely tied to the
hardware of the computer.
Intermediate form of the program
Intermediate code forms: An intermediate code
form of source program is an internal form of a
program created by the compiler while translating the
program to form a high –level language to assembly
code(or)object code(machine code).
Intermediate code:
During the translation of a source program into the
object code for a target machine, a compiler may generate a
middle- level language code, which is known as intermediate
code or intermediate text. The complexity of this code lies
between the source language code and the object code.
Postfix Notation.
Example.
Syntax Tree.
Example − Draw Syntax Tree for the string a + b ∗ c − d.
Three-Address Code.
Quadruples representation − Records with fields for the
operators and operands can be define three address statements.
The operand is the object that is being worked on by an
operation.
Operations can be mathematical ones such as
multiplication or addition, or they can be more
sophisticated functions.