Unit – 4
Unit – 1
Pushdown
Introduction to Compiler
Automata
Compiler Design
Unit – 1 : Introduction to Compiler 1 Darshan Institute of Engineering & Technology
Topics to be covered
Translator
Analysis synthesis model of compilation
Phases of compiler
Difference between compiler & interpreter
Types of compiler
Context of compiler (Cousins of compiler)
Pass structure
Unit – 1 : Introduction to Compiler 2 Darshan Institute of Engineering & Technology
Translator હેલ્લો
Unit – 1 : Introduction to Compiler 3 Darshan Institute of Engineering & Technology
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
Source Translator Target
Program Program
Error
Messages (If any)
Unit – 1 : Introduction to Compiler 4 Darshan Institute of Engineering & Technology
Compiler
A compiler is a program that reads a program written in source
language and translates it into an equivalent program in target
language.
void main() 0000 1100 0010
{ 0100
int a=1,b=2,c; 0111 1000 0001
Compiler
c=a+b; 1111 0101 1110
printf(“%d”,c); 1100 0000 1000
} 1011
Source Error Target
Program Messages (If any) Program
Unit – 1 : Introduction to Compiler 5 Darshan Institute of Engineering & Technology
Interpreter
Interpreter is also program that reads a program written in source
language and translates it into an equivalent program in target
language line by line.
Void main() 0000 1100 0010
{ 0000
int a=1,b=2,c; Interpreter 1111 1100 0010
c=a+b;
1010 1100 0010
printf(“%d”,c);
0011 1100 0010
} 1111
Error
Source Target
Messages (If any)
Program Program
Unit – 1 : Introduction to Compiler 6 Darshan Institute of Engineering & Technology
Assembler
• Assembler is a translator which takes the assembly code as an
input and generates the machine code as an output.
MOV id3, R1 0000 1100 0010
MUL #2.0, R1 0100
MOV id2, R2 0111 1000 0001
MUL R2, R1 Assembler 1111 0101 1110
MOV id1, R2 1100 0000 1000
ADD R2, R1 1011
MOV R1, id1 1100 0000 1000
Error
Assembly Code Messages (If any) Machine Code
Unit – 1 : Introduction to Compiler 7 Darshan Institute of Engineering & Technology
Analysis synthesis model of
compilation
Unit – 1 : Introduction to Compiler 8 Darshan Institute of Engineering & Technology
Analysis synthesis model of compilation
There are two part of compilation.
1. Analysis Phase
2. Synthesis Phase
void main() Analysis Synthesis 0000 1100
{ Phase Phase 0111 1000
int a=1,b=2,c; 0001
c=a+b; 1111 0101
printf(“%d”,c); Intermediate 1000
} Representation 1011
Source Code Target Code
Unit – 1 : Introduction to Compiler 9 Darshan Institute of Engineering & Technology
Analysis phase & Synthesis phase
Analysis Phase Synthesis Phase
Analysis part breaks up the The synthesis part constructs
source program into the desired target program
constituent pieces and from the intermediate
creates an intermediate representation.
representation of the source Synthesis phase consist of the
program. following sub phases:
Analysis phase consist of 1. Code optimization
three sub phases: 2. Code generation
1. Lexical analysis
2. Syntax analysis
3. Semantic analysis
Unit – 1 : Introduction to Compiler 10 Darshan Institute of Engineering & Technology
Phases of compiler
Unit – 1 : Introduction to Compiler 11 Darshan Institute of Engineering & Technology
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Intermediate Code
code optimization
Syntax analysis generation
Code
Semantic analysis generation
Unit – 1 : Introduction to Compiler 12 Darshan Institute of Engineering & Technology
Lexical analysis
Lexical Analysis is also called linear analysis Position = initial + rate*60
or scanning.
Lexical analysis
Lexical Analyzer divides the given source
statement into the tokens.
id1 = id2 + id3 * 60
Ex: Position = initial + rate * 60 would be
grouped into the following tokens:
Position (identifier)
= (Assignment symbol)
initial (identifier)
+ (Plus symbol)
rate (identifier)
* (Multiplication symbol)
60 (Number)
Unit – 1 : Introduction to Compiler 13 Darshan Institute of Engineering & Technology
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Intermediate Code
code optimization
Syntax analysis generation
Code
Semantic analysis generation
Unit – 1 : Introduction to Compiler 14 Darshan Institute of Engineering & Technology
Syntax analysis
Syntax Analysis is also called Parsing or Position = initial + rate*60
Hierarchical Analysis. Lexical analysis
The syntax analyzer checks each line of
the code and spots every tiny mistake. id1 = id2 + id3 * 60
If code is error free then syntax
Syntax analysis
analyzer generates the tree.
=
id1 +
id2 *
id3 60
Unit – 1 : Introduction to Compiler 15 Darshan Institute of Engineering & Technology
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Intermediate Code
code optimization
Syntax analysis generation
Code
Semantic analysis generation
Unit – 1 : Introduction to Compiler 16 Darshan Institute of Engineering & Technology
Semantic analysis
=
Semantic analyzer determines the
meaning of a source string. id1 +
It performs following operations: id2 * int to
1. matching of parenthesis in the real
id3 60
expression.
2. Matching of if..else statement. Semantic analysis
3. Performing arithmetic operation =
that are type compatible. id1 +
4. Checking the scope of operation.
id2 *
*Note: Consider id1, id2 and id3 are real id3 inttoreal
60
Unit – 1 : Introduction to Compiler 17 Darshan Institute of Engineering & Technology
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Intermediate Code
code optimization
Syntax analysis generation
Code
Semantic analysis generation
Unit – 1 : Introduction to Compiler 18 Darshan Institute of Engineering & Technology
Intermediate code generator
Two =
important properties of
intermediate code : id1 +
1. It should be easy to produce.
id2 *
2. Easy to translate into target
t3 id3 inttoreal
program. t2 t1
Intermediate form can be represented 60
Intermediate code
using “three address code”.
Three address code consist of a
t1= int to real(60)
sequence of instruction, each of which t2= id3 * t1
has at most three operands. t3= t2 + id2
id1= t3
Unit – 1 : Introduction to Compiler 19 Darshan Institute of Engineering & Technology
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Intermediate Code
code optimization
Syntax analysis generation
Code
Semantic analysis generation
Unit – 1 : Introduction to Compiler 20 Darshan Institute of Engineering & Technology
Code optimization
It improves the intermediate code.
This is necessary to have a faster Intermediate code
execution of code or less consumption
t1= int to real(60)
of memory. t2= id3 * t1
t3= t2 + id2
id1= t3
Code optimization
t1= id3 * 60.0
id1 = id2 + t1
Unit – 1 : Introduction to Compiler 21 Darshan Institute of Engineering & Technology
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Intermediate Code
code optimization
Syntax analysis generation
Code
Semantic analysis generation
Unit – 1 : Introduction to Compiler 22 Darshan Institute of Engineering & Technology
Code generation
The intermediate code instructions are
Code optimization
translated into sequence of machine
instruction. t1= id3 * 60.0
id1 = id2 + t1
Code generation
MOVF id3, R2
MULF #60.0, R2
MOVF id2, R1
ADDF R2,R1
MOVF R1, id1
Unit – 1 : Introduction to Compiler 23 Darshan Institute of Engineering & Technology
Phases of compiler
Source program
Analysis Phase
Lexical analysis
Syntax analysis
Semantic analysis
Symbol table Error detection
and recovery
Intermediate code
Sr. Variable Type Address Code optimization
Name
1 Position Float 0001
Code generation Synthesis Phase
2 Initial Float 0005
3 Rate Float 0009
Target Program
Unit – 1 : Introduction to Compiler 24 Darshan Institute of Engineering & Technology
Exercise
Write output of all the phases of compiler for following
statements:
1. x = b-c*2
2. I=p*n*r/100
Unit – 1 : Introduction to Compiler 25 Darshan Institute of Engineering & Technology
Difference between compiler & interpreter
Compiler Interpreter
Scans the entire program and It translates program’s one
translates it as a whole into machine statement at a time.
code.
It generates intermediate code. It does not generate intermediate
code.
An error is displayed after entire An error is displayed for every
program is checked. instruction interpreted if any.
Memory requirement is more. Memory requirement is less.
Example: C compiler Example: Basic, Python, Ruby
Unit – 1 : Introduction to Compiler 26 Darshan Institute of Engineering & Technology
Context of Compiler
(Cousins of compiler)
Unit – 1 : Introduction to Compiler 27 Darshan Institute of Engineering & Technology
Context of compiler (Cousins of compiler)
Skeletal
Source Preprocessor
Program
Preprocessor
It performs the following functions:
1. Macro processing
2. File inclusion
3. Rational preprocessor
4. Language extensions
Unit – 1 : Introduction to Compiler 28 Darshan Institute of Engineering & Technology
Context of compiler (Cousins of compiler)
Skeletal
Source Preprocessor
Program
Preprocessor
1. Macro processing: Allows user to define macros. Macro is shorthand
for longer constructs.
Ex: #define PI 3.14159265358979323846
2. File inclusion: A preprocessor may include the header file into the
program.
Ex: #include<stdio.h>
3. Rational preprocessor: It provides built in macro for construct like
while statement or if statement.
Unit – 1 : Introduction to Compiler 29 Darshan Institute of Engineering & Technology
Context of compiler (Cousins of compiler)
Skeletal
Source Preprocessor
Program
Preprocessor
4. Language extensions: Add capabilities to the language by using
built-in macros.
• Ex: the language equal is a database query language
embedded in C.
• Statement beginning with ## are taken by preprocessor to be
database access statement unrelated to C and translated into
procedure call on routines that perform the database access.
Unit – 1 : Introduction to Compiler 30 Darshan Institute of Engineering & Technology
Context of compiler (Cousins of compiler)
Skeletal
Source Preprocessor Compiler
Source
Program Program
Compiler
• A compiler is a program that reads a program written in source
language and translates it into an equivalent program in target
language.
Unit – 1 : Introduction to Compiler 31 Darshan Institute of Engineering & Technology
Context of compiler (Cousins of compiler)
Skeletal
Preprocessor Compiler
Source Source Target Assembler
Program Program Assembly
Program
Assembler
• Assembler is a translator which takes the assembly program
(mnemonic) as an input and generates the machine code as an
output.
Unit – 1 : Introduction to Compiler 32 Darshan Institute of Engineering & Technology
Context of compiler (Cousins of compiler)
Skeletal
Preprocessor Compiler
Source Source Target Assembler Relocatable
Program Program Assembly Object Code
Program
Linker / Loader
Libraries &
Linker Object Files
• Linker makes a single program from a several files of relocatable
machine code.
• These files may have been the result of several different compilation,
and one or more library files.
Unit – 1 : Introduction to Compiler 33 Darshan Institute of Engineering & Technology
Context of compiler (Cousins of compiler)
Skeletal
Preprocessor Compiler
Source Source Target Assembler Relocatable
Program Program Assembly Object Code
Program
Linker / Loader
Libraries &
Loader Object Files
• The process of loading consists of: Absolute
1. Taking relocatable machine code. Machine
Code
2. Altering the relocatable address.
3. Placing the altered instructions and
data in memory at the proper
location.
Unit – 1 : Introduction to Compiler 34 Darshan Institute of Engineering & Technology
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 depend on source program.
It includes following phases:
6. Code optimization
7. Code generation phase
8. Error handling and symbol table operation
Unit – 1 : Introduction to Compiler 35 Darshan Institute of Engineering & Technology
Pass structure
Unit – 1 : Introduction to Compiler 36 Darshan Institute of Engineering & Technology
Pass structure
One complete scan of a source program is called pass.
Pass includes reading an input file and writing to the output file.
In a single pass compiler analysis of source statement is
immediately followed by synthesis of equivalent target statement.
While in a two pass compiler intermediate code is generated
between analysis and synthesis phase.
It is difficult to compile the source program into single pass due to:
forward reference
Unit – 1 : Introduction to Compiler 37 Darshan Institute of Engineering & Technology
Pass structure
Forward reference: A forward reference of a program entity is a
reference to the entity which precedes its definition in the program.
This problem can be solved by postponing the generation of target
code until more information concerning the entity becomes
available.
It leads to multi pass model of compilation.
In Pass I: Perform analysis of the source program and note
relevant information.
In Pass II: Generate target code using information noted in pass I.
Unit – 1 : Introduction to Compiler 38 Darshan Institute of Engineering & Technology
Effect of reducing the number of passes
It is desirable to have a few passes, because it takes time to read
and write intermediate file.
If we group several phases into one pass then memory
requirement may be large.
Unit – 1 : Introduction to Compiler 39 Darshan Institute of Engineering & Technology
Types of compiler
Unit – 1 : Introduction to Compiler 40 Darshan Institute of Engineering & Technology
Types of compiler
1. One pass compiler
• It is a type of compiler that compiles whole process in one-pass.
2. Two pass compiler
• It is a type of compiler that compiles whole process in two-pass.
• It generates intermediate code.
3. Incremental compiler
• The compiler which compiles only the changed line from the source
code and update the object code.
4. Native code compiler
• The compiler used to compile a source code for a same type of
platform only.
5. Cross compiler
• The compiler used to compile a source code for a different kinds platform.
Unit – 1 : Introduction to Compiler 41 Darshan Institute of Engineering & Technology
End of Unit-1
Unit – 1 : Introduction to Compiler 42 Darshan Institute of Engineering & Technology