0% found this document useful (0 votes)
18 views

Basic functions of compiler.pptx

The document discusses various aspects of compiler design, including machine-dependent and machine-independent features, the compilation process, types of programming errors, and the phases of a compiler. It outlines the basic functions of a compiler, the significance of intermediate code, and the optimization techniques used for machine-dependent code. Additionally, it details the structure of a compiler, including the roles of lexical, syntax, and semantic analyzers, as well as the code generation and optimization processes.

Uploaded by

msword879
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Basic functions of compiler.pptx

The document discusses various aspects of compiler design, including machine-dependent and machine-independent features, the compilation process, types of programming errors, and the phases of a compiler. It outlines the basic functions of a compiler, the significance of intermediate code, and the optimization techniques used for machine-dependent code. Additionally, it details the structure of a compiler, including the roles of lexical, syntax, and semantic analyzers, as well as the code generation and optimization processes.

Uploaded by

msword879
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

UNIT II

Machine dependent compiler features -


Intermediate form of the program - Machine
dependent code optimization - Machine
independent compiler features - Compiler
design options - Division into passes –
Interpreters – p-code compilers -
Compiler-compilers.
Basic functions of compiler
• Compiler
A compiler is a software that converts the source code to the
object code. It is also called as language translator.
In other words, we can say that it converts the high-level
language to machine/binary language.
Moreover, it is necessary to perform this step to make the
program executable.
This is because the computer understands only binary
language.
Some compilers convert the high-level language to an
assembly language as an intermediate step.
Whereas some others convert it directly to machine code.
This process of converting the source code into machine
code is called compilation.
Errors in programming

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.

A basic example of an operand would be a variable


declared in a program that would change value because of
operations.
 For example, a programmer can create a variable x. He
can set the value of x at anything, for example, one.
Then, that value can be changed using an operator, for
example, by entering something like x=x +3. The value of
x then becomes 4.
Machine dependent code optimization
 Machine-dependent optimization is done after the
target code has been generated and when the code is
transformed according to the target machine
architecture.
 It involves CPU registers and may have
absolute memory references rather than relative
references.
 Machine-dependent optimizers put efforts to
take maximum advantage of memory hierarchy.
 (In computer science, program optimization, code
optimization, or software optimization, is the process
of modifying a software system to make some aspect
•Basic Blocks
 Source codes generally have a number of instructions,
which are always executed in sequence and are
considered as the basic blocks of the code.
 These basic blocks do not have any jump statements
among them, i.e., when the first instruction is executed,
all the instructions in the same basic block will be
executed in their sequence of appearance without losing
the flow control of the program.
 A program can have various constructs as basic
blocks, like IF- THEN-ELSE, SWITCH-CASE
conditional statements and loops such as DO-WHILE,
FOR, and REPEAT-UNTIL, etc.
Basic block identification
 We may use the following algorithm to find the basic
blocks in a program:
 Search header statements of all the basic blocks from
where a basic block starts:
 First statement of a program.
 Statements that are target of any branch
(conditional/unconditional).
 Statements that follow any branch statement.
 Header statements and the statements following them
form a basic block.
 A basic block does not include any header statement of
any other basic block.
 Basic blocks are important concepts from both
code generation and optimization point of view.
Machine independent compiler
features

You might also like