Professional Documents
Culture Documents
GTU # 2170701
Unit – 1
Introduction to
Compiler Design
Translator
Translator
A translator is a program that takes one form of program as input and converts it into another
form.
Types of translators are:
1. Compiler
2. Interpreter
3. Assembler
Error
Messages (If any)
Prof. Dixita B Kagathara #2170701 (CD) Unit 1 – Introduction to Compiler Design 4
Compiler
A compiler is a program that reads a program written in source language and translates it into
an equivalent program in target language.
1. Analysis Phase
2. Synthesis Phase
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #2170701 (CD) (PS)
#3130006 Unit 1–Unit
Introduction
1 – Basicto
Probability
Compiler Design 9
Analysis phase & Synthesis phase
Analysis Phase Synthesis Phase
Analysis part breaks up the source The synthesis part constructs the desired
program into constituent pieces and target program from the intermediate
creates an intermediate representation of representation.
the source program. Synthesis phase consist of the following sub
Analysis phase consists of three sub phases:
phases:
1. Code optimization
1. Lexical analysis
2. Code generation
2. Syntax analysis
3. Semantic analysis
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #2170701 (CD) (PS)
#3130006 Unit1 –Unit
Introduction
1 – Basic to
Probability
Compiler Design 10
Phases of compiler
Phases of compiler
Compiler
Lexical analysis
Intermediate Code
code optimization
Syntax analysis generation
Code generation
Semantic analysis
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #2170701 (CD) (PS)
#3130006 Unit 1–Unit
Introduction
1 – Basicto
Probability
Compiler Design 12
Lexical analysis
Lexical Analysis is also called linear analysis or scanning.
Lexical Analyzer divides the given source statement into the Position = initial + rate*60
tokens.
Ex: Position = initial + rate * 60 would be grouped into the
Lexical analysis
following tokens:
Position (identifier) id1 = id2 + id3 * 60
= (Assignment symbol)
initial (identifier)
+ (Plus symbol)
rate (identifier)
* (Multiplication symbol)
60 (Number)
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #2170701 (CD) (PS)
#3130006 Unit1 –Unit
Introduction
1 – Basic to
Probability
Compiler Design 13
Phases of compiler
Compiler
Lexical analysis
Intermediate Code
code optimization
Syntax analysis generation
Code generation
Semantic analysis
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #2170701 (CD) (PS)
#3130006 Unit 1–Unit
Introduction
1 – Basicto
Probability
Compiler Design 14
Syntax analysis
Syntax Analysis is also called Parsing or Hierarchical Position = initial + rate*60
Analysis.
The syntax analyzer checks each line of the code and Lexical analysis
spots every tiny mistake.
If code is error free then syntax analyzer generates the id1 = id2 + id3 * 60
tree.
Syntax analysis
id1 +
id2 *
id3 60
Compiler
Lexical analysis
Intermediate Code
code optimization
Syntax analysis generation
Code generation
Semantic analysis
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #2170701 (CD) (PS)
#3130006 Unit 1–Unit
Introduction
1 – Basicto
Probability
Compiler Design 16
Semantic analysis
Semantic analyzer determines the meaning of a source =
string.
id1 +
It performs following operations:
1. matching of parenthesis in the expression. id2 * int to
real
2. Matching of if..else statement. id3 60
3. Performing arithmetic operation that are type
compatible. Semantic analysis
4. Checking the scope of operation. =
*Note: Consider id1, id2 and id3 are real
id1 +
id2 *
id3 inttoreal
60
Prof. Dixita B Kagathara #2170701 (CD) Unit 1 – Introduction to Compiler Design 17
Phases of compiler
Compiler
Lexical analysis
Intermediate Code
code optimization
Syntax analysis generation
Code generation
Semantic analysis
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #2170701 (CD) (PS)
#3130006 Unit 1–Unit
Introduction
1 – Basicto
Probability
Compiler Design 18
Intermediate code generator
Two important properties of intermediate code : =
1. It should be easy to produce.
id1 +
2. Easy to translate into target program.
id2 *
Intermediate form can be represented using “three
address code”. t3 id3 inttoreal
t2 t1
Three address code consist of a sequence of instruction, 60
each of which has at most three operands. Intermediate code
Compiler
Lexical analysis
Intermediate Code
code optimization
Syntax analysis generation
Code generation
Semantic analysis
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #2170701 (CD) (PS)
#3130006 Unit 1–Unit
Introduction
1 – Basicto
Probability
Compiler Design 20
Code optimization
It improves the intermediate code.
This is necessary to have a faster execution of code Intermediate code
or less consumption of memory.
t1= int to real(60)
t2= id3 * t1
t3= t2 + id2
id1= t3
Code optimization
Compiler
Lexical analysis
Intermediate Code
code optimization
Syntax analysis generation
Code generation
Semantic analysis
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #2170701 (CD) (PS)
#3130006 Unit 1–Unit
Introduction
1 – Basicto
Probability
Compiler Design 22
Code generation
The intermediate code instructions are translated into
sequence of machine instruction. Code optimization
Code generation
MOV id3, R2
MUL #60.0, R2
MOV id2, R1
ADD R2,R1
MOV R1, id1
Id3R2
Id2R1
Prof. Dixita B Kagathara #2170701 (CD) Unit 1 – Introduction to Compiler Design 23
Phases of compiler
Source program
Analysis Phase
Lexical analysis
Syntax analysis
Semantic analysis
Symbol table Error detection
and recovery
Intermediate code
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #2170701 (CD) (PS)
#3130006 Unit1 –Unit
Introduction
1 – Basic to
Probability
Compiler Design 25
Grouping of Phases
Front end & back end (Grouping of phases)
Front end
Depends primarily on source language and largely independent of the target machine.
It includes following phases:
1. Lexical analysis
2. Syntax analysis
3. Semantic analysis
4. Intermediate code generation
5. Creation of symbol table
Back end
Depends on target machine and do not depends on source program.
It includes following phases:
1. Code optimization
2. Code generation phase
3. Error handling and symbol table operation
Prof. Dixita B Kagathara #2170701 (CD) Unit 1 – Introduction to Compiler Design 27
Difference between compiler & interpreter
Compiler Interpreter
Scans the entire program and translates it It translates program’s one statement at a
as a whole into machine code. time.
It generates intermediate code. It does not generate intermediate code.
An error is displayed after entire program is An error is displayed for every instruction
checked. interpreted if any.
Memory requirement is more. Memory requirement is less.
Example: C compiler Example: Basic, Python, Ruby
Compiler
Preprocessor
Assembler Target Assembly
Program
Linker
Assembler
Loader
Relocatable Object
Code
Libraries & Linker / Loader
Object Files
Absolute Machine
Code
Prof. Dixita B Kagathara #2170701 (CD) Unit 1 – Introduction to Compiler Design 30
Context of compiler (Cousins of compiler)
Skeletal Source Program
Preprocessor
Some of the task performed by preprocessor: Preprocessor
Target Assembly
Program
Assembler
Relocatable Object
Code
Libraries & Linker / Loader
Object Files
Absolute Machine
Code
Prof. Dixita B Kagathara #2170701 (CD) Unit 1 – Introduction to Compiler Design 32
Context of compiler (Cousins of compiler)
Skeletal Source Program
Assembler
Assembler is a translator which takes the assembly Preprocessor
Target Assembly
Program
Assembler
Relocatable Object
Code
Libraries & Linker / Loader
Object Files
Absolute Machine
Code
Prof. Dixita B Kagathara #2170701 (CD) Unit 1 – Introduction to Compiler Design 33
Context of compiler (Cousins of compiler)
Skeletal Source Program
Linker
Linker makes a single program from a several files Preprocessor
Pass I:
Pass II:
Prof. Dixita
Jay R Dhamsaniya
B Kagathara #2170701 (CD) (PS)
#3130006 Unit 1–Unit
Introduction
1 – Basicto
Probability
Compiler Design 40
Thank You