1. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
1
Compiler Design
Unit – 4
Pushdown
Automata
Chapter One
Introduction to Compiler
2. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
2
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
3. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
3
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
Translator
Source
Program
Target
Program
(If any)
4. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
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.
Error
Messages
Compiler
void main()
{
int a=1,b=2,c;
c=a+b;
printf(“%d”,c);
}
Source
Program
0000 1100 0010
0100
0111 1000 0001
1111 0101 1110
1100 0000 1000
1011
Target
Program
(If any)
5. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
5
Interpreter
Interpreter is also program that reads a program written in source
language and translates it into an equivalent program in target
language
Interpreter
Source
Program
Target
Program
line by line.
Void main()
{
int a=1,b=2,c;
c=a+b;
printf(“%d”,c);
}
0000 1100 0010
0000
1111
1010 1100 0010
0011 1100 0010
1111 1100 0010
Error
Messages (If any)
6. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
6
Assembler
• Assembler is a translator which takes the assembly code as an
input and generates the machine code as an output.
Assembler
MOV id3, R1
MUL #2.0, R1
MOV id2, R2
MUL R2, R1
MOV id1, R2
ADD R2, R1
MOV R1, id1
Assembly Code
0000 1100 0010
0100
0111 1000 0001
1111 0101 1110
1100 0000 1000
1011
1100 0000 1000
Machine Code
Error
Messages (If any)
7. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
7
Analysis synthesis model of
compilation
8. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
8
Analysis synthesis model of compilation
There are two part of compilation.
1. Analysis Phase
2. Synthesis Phase
Analysis
Phase
Synthesis
Phase
Intermediate
Representation
void main()
{
int a=1,b=2,c;
c=a+b;
printf(“%d”,c);
}
Source Code
0000 1100
0111 1000
0001
1111 0101
1000
1011
Target Code
9. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
9
Analysis phase & Synthesis phase
Analysis Phase
Analysis part breaks up the
source program into
constituent pieces and
creates an intermediate
representation of the source
program.
Analysis phase consist of
three sub phases:
1. Lexical analysis
2. Syntax analysis
3. Semantic analysis
Synthesis Phase
The synthesis part constructs
the desired target program
from the intermediate
representation.
Synthesis phase consist of the
following sub phases:
1. Code optimization
2. Code generation
10. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
10
Phases of compiler
11. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
11
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
12. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
12
Lexical analysis
Position = initial + rate*60
Lexical analysis
id1
Lexical Analysis is also called linear analysis
or scanning.
Lexical Analyzer divides the given source
statement into the tokens.
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)
= id2 + id3 * 60
13. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
13
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
14. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
14
Syntax analysis
Position = initial + rate*60
Syntax analysis
id1 = id2 + id3 * 60
Syntax Analysis is also called Parsing or
Hierarchical Analysis.
The syntax analyzer checks each line of
the code and spots every tiny mistake.
If code is error free then syntax
analyzer generates the tree.
Lexical analysis
=
id1 +
id2 *
id3 60
15. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
15
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
16. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
16
Semantic analysis
Semantic analyzer determines the
meaning of a source string.
It performs following operations:
1. matching of parenthesis in the
expression.
2. Matching of if..else statement.
3. Performing arithmetic operation
that are type compatible.
4. Checking the scope of operation.
=
id1 +
id2 *
id3 60
Semantic analysis
=
id1 +
id2 *
id3 inttoreal
60
int to
real
*Note: Consider id1, id2 and id3 are real
17. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
17
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
18. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
18
Intermediate code generator
Two important properties of
intermediate code :
1. It should be easy to produce.
2. Easy to translate into target
program.
Intermediate form can be represented
using “three address code”.
Three address code consist of a
sequence of instruction, each of which
has at most three operands.
=
id1 +
id2 *
id3 inttoreal
60
Intermediate code
t1= int to real(60)
t2= id3 * t1
t3= t2 + id2
id1= t3
t1
t2
t3
19. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
19
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
20. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
20
Code optimization
It improves the intermediate code.
This is necessary to have a faster
execution of code or less consumption
of memory.
Intermediate code
t1= int to real(60)
t2= id3 * t1
t3= t2 + id2
id1= t3
Code optimization
t1= id3 * 60.0
id1 = id2 + t1
21. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
21
Phases of compiler
Compiler
Analysis phase Synthesis phase
Lexical analysis
Syntax analysis
Semantic analysis
Intermediate
code
generation
Code
optimization
Code
generation
22. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
22
Code generation
The intermediate code instructions are
translated into sequence of machine
instruction.
Code generation
MOVF id3, R2
MULF #60.0, R2
MOVF id2, R1
ADDF R2,R1
MOVF R1, id1
Code optimization
t1= id3 * 60.0
id1 = id2 + t1
23. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
23
Phases of compiler
Symbol table Error detection
and recovery
Lexical analysis
Code optimization
Syntax analysis
Semantic analysis
Intermediate code
Code generation
Target Program
Source program
Analysis Phase
Synthesis Phase
Sr. Variable
Name
Type Address
1 Position Float 0001
2 Initial Float 0005
3 Rate Float 0009
24. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
24
Exercise
Write output of all the phases of compiler for following
statements:
1. x = b-c*2
2. I=p*n*r/100
25. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
25
Difference between compiler & interpreter
Compiler Interpreter
Scans the entire program and
translates it as a whole into machine
code.
It translates program’s one
statement at a time.
It generates intermediate code. It does not generate intermediate
code.
Memory requirement is more. Memory requirement is less.
An error is displayed after entire
program is checked.
An error is displayed for every
instruction interpreted if any.
Example: C compiler Example: Basic, Python, Ruby
26. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
26
Context of Compiler
(Cousins of compiler)/Language Processing
27. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
27
Context of compiler (Cousins of compiler)
Preprocessor
Skeletal
Source
Program
It performs the following functions:
1. Macro processing
2. File inclusion
3. Rational preprocessor
4. Language extensions
Preprocessor
28. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
28
Context of compiler (Cousins of compiler)
Preprocessor
Skeletal
Source
Program
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.
Preprocessor
29. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
29
Context of compiler (Cousins of compiler)
Preprocessor
Skeletal
Source
Program
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.
Preprocessor
30. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
30
Context of compiler (Cousins of compiler)
Preprocessor Compiler
Skeletal
Source
Program
Source
Program
• A compiler is a program that reads a program written in source
language and translates it into an equivalent program in target
language.
Compiler
31. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
31
Context of compiler (Cousins of compiler)
Preprocessor Compiler Assembler
Skeletal
Source
Program
Source
Program
Target
Assembly
Program
• Assembler is a translator which takes the assembly program
(mnemonic) as an input and generates the machine code as an
output.
Assembler
32. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
32
Context of compiler (Cousins of compiler)
Preprocessor Compiler Assembler
Skeletal
Source
Program
Source
Program
Target
Assembly
Program
Relocatable
Object Code
Libraries &
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.
Linker
Linker / Loader
33. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
33
Context of compiler (Cousins of compiler)
Preprocessor Compiler Assembler
Skeletal
Source
Program
Source
Program
Target
Assembly
Program
Relocatable
Object Code
Absolute
Machine
Code
Libraries &
Object Files
• The process of loading consists of:
1. Taking relocatable machine code.
2. Altering the relocatable address.
3. Placing the altered instructions and
data in memory at the proper
location.
Loader
Linker / Loader
34. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
34
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
35. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
35
Pass structure
36. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
36
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
37. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
37
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.
38. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
38
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.
39. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
39
Types of compiler
40. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
40
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.
41. Unit – 1 : Introduction to Compiler Darshan Institute of Engineering & Technology
41
End of Unit-1