Introduction To Compiler
Introduction To Compiler
CD : COMPILER DESIGN
Unit no : 1
Introduction to Introduction to
Compilers
Compilers (01CE0714)
Source Target
Compiler
Program Program
Error
Interpreter :
Interpreter is a translator which is used to convert
programs in high-level language to low-level language.
Interpreter translates line by line and reports the error
once it encountered during the translation process.
Compiler There are 6 Phases of compiler It makes Two passes over given
input.
Vs Ex : C , C ++ Ex : 8085 , 8086
Assembler
HLL
Pre Processor
Language
Processing Pure HLL
System Compiler
/ Translation Assembly Language
Processor
/ Context of Assembler
Compiler Relocatable Machine code
/Cousins of Linker &
Compiler Loader
Library ,
Relocatable object files
System
/ Translation
Processor
/ Context of
Compiler
/Cousins of
Compiler
3) Linker and Loader
A program called loader performs the two functions of
Language loading and link editing
Processing The process of loading consist of taking relocatable
System machine code , altering the relocatable address and
placing the altered instructions and data in memory at
/ Translation the proper location.
Processor Linker allows us to make a single program from several
/ Context of files of relocatable machine code.
Types of Linking : Static and Dynamic
Compiler
Types of Loader : Compile and Go loader, Absolute
/Cousins of Loader, Relocating Loader.
Compiler
Lexical Analyzer
Phases of
Compiler
1) Lexical Analysis
It is also called Linear analysis or scanning.
In this phase, input character from source code is read
from left to right and then break into stream of units.
Phases of These units are called tokens.
Compiler Sequence of characters having a collective meaning is
/ Analysis – called Token.
Synthesis Tokens can be categorized into identifiers, constants,
literals, keywords, operators, delimeters etc.
Model of
So, Token is the smallest meaningful entities of program
Compilation are produced as output.
1) Lexical Analysis
Example : position = initial + rate * 60
id1 = id2 + id3 * 60
Phases of
Compiler
/ Analysis – Position Identifier 1
Synthesis =
Initial
Equal to operator
Identifier 2
Model of + Addition Operator
Compilation Rate
*
Identifier 3
Multiplication Operator
60 Constant
Lexical Analyzer
Syntax Analyzer
Phases of
Compiler
2) Syntax Analysis
It is also called Hierarchical analysis or Parsing.
It determines if the sentence formed from the words
are syntactically (grammatically) correct.
Phases of
It creates syntax tree from generated tokens if the code
Compiler is error free.
/ Analysis – Syntax tree consist operators as internal node and
Synthesis operands as leaf node.
Model of This phase check each and every line and try to detect
errors if it is grammatically (syntax wise) not correct.
Compilation
2) Syntax Analysis
position = initial + rate * 60
Three Operators : = + *
Phases of position = initial + rate * 60
Compiler position = initial + rate * 60
/ Analysis – position = initial + rate * 60
Synthesis
Model of
Compilation
Lexical Analyzer
Syntax Analyzer
Phases of Semantic
Analyzer
Compiler
3) Semantic Analysis
It determines meaning of string
In semantic analysis various operations are performed
like
Phases of
Performing check ; whether operator have compatible
Compiler arguments or not (type checking) , matching of
/ Analysis – parenthesis, scope of operation etc.
Synthesis Ensuring that components of a program fits together
meaningfully.
Model of
Compilation
3) Semantic Analysis
position = initial + rate * 60
Phases of
Compiler
/ Analysis –
Synthesis
Model of
Compilation
Lexical Analyzer
Syntax Analyzer
Phases of Semantic
Analyzer
Compiler
Intermediate
Code Generator
4) Intermediate Code Generator
Intermediate code generation should have two
properties :
it should be easy to produce,
Phases of
easy to translate.
Compiler
Intermediate code is called “Three Address Code”.
/ Analysis –
It is called three address code as it maximum consist
Synthesis three operands.
Model of
Compilation
4) Intermediate Code Generator
Example : position = initial + rate * 60
t1 = inttoreal(60)
Phases of t2 = rate * t1
Compiler t3 = initial + t2
/ Analysis – position = t3
Synthesis
Model of
Compilation
Lexical Analyzer
Syntax Analyzer
Phases of Semantic
Analyzer
Compiler
Intermediate
Code Generator
Code Optimizer
5) Code Optimization
This phase improves the intermediate code, in such a
way that a machine code can be produced, which
occupies less memory space and less execution time
Phases of without changing the functionality or correctness of
program.
Compiler
Example :
/ Analysis –
t1 = rate * 60.00
Synthesis
position = initial + t1
Model of
Compilation
Lexical Analyzer
Syntax Analyzer
Semantic
Phases of Analyzer
Compiler Intermediate
Code Generator
Code Optimizer
Code Generator
6) Code Generation
In Code generation phase the target code gets
generated.
In this phase intermediate (optimized) code is
Phases of translated into a sequence of machine instructions that
Compiler perform the same operation.
/ Analysis – Example :
Synthesis MOVF id3,R1 position = initial + rate * 60
id 1 id2 id3
Syntax Analyzer
Semantic
Compiler Manager
Intermediate
handler
Code Generator
Code Optimizer
Code Generator
Lexical Analyzer
Analysis Phase
Syntax Analyzer
Semantic
Compiler Manager
Intermediate
handler
Code Generator
Code Optimizer
Synthesis Phase
Code Generator
There are two parts of Compilation Process :
1) Analysis Phase
It breaks up source program into small pieces and
create an intermediate representation of source
Phases of program.
Compiler 2) Synthesis Phase
/ Analysis – It constructs the desired target program from
Synthesis intermediate representation.
Model of
Compilation
1) Analysis Phase
Consist of Three Sub Phases :
Lexical analysis
Phases of Syntax analysis
Compiler Semantic analysis
/ Analysis –
Synthesis 2) Synthesis Phase :
Model of Consist of Three Sub Phases :
Compilation Intermediate Code generation
Code Optimization
Code Generation
Symbol Table :
A symbol table is data structure used by language
translators.
In order to keep track of identifiers used by program,
Phases of compiler maintains the record of the same with their
Compiler various attributes.
/ Analysis – Symbol table stores information for different entities :
Identifier, Function, Procedure, Constant , Label Name,
Synthesis Compiler generated temporaries etc.
Model of
Compilation
Error Detection and Recovery :
Each phase can encounter errors, However after
detecting an error a phase must some how deal with
that error. So that Compilation can proceed, allowing
Phases of further errors in the source program to be detected.
Compiler Lexical Analyzer can detect errors where the character
remaining in the input do not form any token of the
/ Analysis – language.
Synthesis Errors where the token stream violates the structure
Model of rule (syntax) of the language are determined by syntax
analysis phase.
Compilation
During Semantic analysis, the compiler tries to detect
constructs that have the right syntactic structure but no
meaning to the operation involved.
a=b+c*60
Intermediate code generator
Lexical analyzer
t1:= inttofloat (60)
Id1=id2+id3*60 t2:= id3 * t1
t3:= id2 + t2
Phases of
id1:= t3
Syntax analyzer
Compiler =
+
Code optimizer
/ Analysis – id1
id2 *
Synthesis id3 60
t1:= id3 * 60.0
id1:= id2 + t1
Model of Semantic analyzer
Compilation =
Code generator
+
id1
MOVEF id3, R2
id2 *
MULF #60.0,R2
id3 Int to float MOVF id2, R1
ADDF R2,R1
MOVF R1,id1
60
Example :
What will be the output of each phase for the following
statement?
Statement : x=a+b*c
Practice Time
Front End Back End
A DS 1 (DL,02) (C,1)
.......
Pass Phase
Various phases are logically grouped The process of compilation is carried
together to form a pass. out in various step is called Phase.
Require Less Memory Require More Memory
Execution is faster Execution is slower
Less memory operation(read/write) More memory operations to be
to be performed performed
Types of
Compiler
Incremental Compiler :
The compiler which compiles only the changed lines
from the source code and update the object code.
Parallelizing Compiler :
A Compiler which is specially designed to run in
Types of parallel computer architecture is known as
parallelizing compiler.
Compiler
int a=0;
int b=1;
printf("Enter Input");
Scanf();
if(a==10)
printf();
Compiler Construction Tools :
1) Scanner Generators
2) Parser Generators
3) Syntax – directed translation engines
Compiler 4) Automatic Code Generators
Construction 5) Data Flow Engine
Tools
Scanner generators: This tool takes regular expressions as
input. For example LEX for Unix Operating System.
Parser generators: A parser generator takes a grammar as
input and automatically generates source code which can
parse streams of characters with the help of a grammar
Syntax-directed translation engines: These software
Compiler tools offer an intermediate code by using the parse tree. It
has a goal of associating one or more translations with each
Construction node of the parse tree.
Tools Automatic code generators: Takes intermediate code and
converts them into Machine Language
Data-flow engines: This tool is helpful for code
optimization. Here, information is supplied by user and
intermediate code is compared to analyze any relation. It is
also known as data-flow analysis. It helps you to find out
how values are transmitted from one part of the program to
another part.
Compiler itself must be bug free.
It must generate correct machine code.
Generated machine code must run fast (Execution
speed).
Compilation time must be less.
Qualities of Compilation must be portable.
Compiler It must be print good diagnostics and Error message.
Generated code must work well with existing debugger.
It must have consistent and Predictable optimization.
1. Aho, Lam, Sethi, and Ullman, Compilers: Principles,
Techniques and Tools,SecondEdition, Pearson,
2014
2. D. M. Dhamdhere: Systerm Programming, Mc Graw
Hill Publication
3. Dick Grune, Henri E. Bal, Jacob, Langendoen:
References Modern Compiler Design, Wiley India Publication
1) Develop Analysis – Synthesis model of Compiler for
a = b + c * 20. Write down explanation and output
for each phase of compiler.
2) Difference between Compiler and Interpreter.
3) Explain Cousins of compiler.
4) Give the name of Front End and Back End with
explanation, and take one example to show the
MU Questions output of the every phase.
5) List out different types of compiler and explain any
two of them in detail.
6) Explain the Language processor in detail.
7) Define the term: Compiler, Interpreter, Assembler,
Translator
Department of CE
Thanks
Unit no : 1
Introduction to
Compilers
(01CE0714)