COMPILER CONSTRUCTION
Instructor:
Mr. Sheraz Babar
1
Lecture 01
2
Compiler
The compiler is software that converts a program written in a high-
level language (Source Language) to low-level language
(Object/Target/Machine Language).
A compiler is a program that can read a program in one language
(the source language) and translate it into an equivalent program in
another language (the target language) .
An important role of the compiler is to report any errors in the
source program that it detects during the translation process.
3
Compiler
4
Compiler
Examples
• Typical Compilers:
– VC, VC++, GCC, JavaC
– FORTRAN, Pascal, VB(?)
• Translators
– Word to PDF
– PDF to Postscript
5
Source Code
A computer program in its original programming language (such as
FORTRAN or C) before translation into object code usually by a co
mpiler
Optimized for human readability
Matches human notions/approach of grammar
Uses named constructs such as variables and procedures
6
Source Code
Example
int expr( int n )
{
int d;
d = 4*n*n*(n+1)*(n+1);
return d;
}
7
Assembly Code
Optimized for hardware
Consists of machine instructions
Uses registers (such as Ax, Bx, Cx, Dx) and unnamed memory loc
ations
Much harder to understand by humans
8
Assembly Code
Example:
START:
MOV AX,10
MOV BX,10
ADD AX, BX
MOV AH,4CH
INT 21H
ENDS
9
Compiler Types
Types of Compiler
Cross Compiler that runs on a machine ‘A’ and produces a code
for another machine ‘B’. It is capable of creating code for a platform
other than the one on which the compiler is running.
– For example, a compiler that runs on a PC but generates
code that runs on an Android smartphone is a cross
compiler.
10
Compiler Types
Source-to-source Compiler or transcompiler or transpiler is a
compiler that translates source code written in one programming
language into the source code of another programming language.
– For example, while the traditional compiler may
convert C to assembly
Decompiler
Basically, it is not a compiler. It is just the reverse of the
compiler. It converts the machine code into high-level language.
11
How to Translate
Is there a unique translation? No!
Is there an algorithm for an “ideal translation”? No!
12
How to Translate
Translation is a complex process
source language and generated code are very different
Need to structure the translation
13
Features of a Compiler
The features are as follows:
• It gives good compilation speed.
• The correctness of machine code.
• The meaning of code should not change.
• It has good error detection.
• Checking the code correctly according to grammar.
14
Uses of Compilers
Helps to make the code independent of the platform.
Makes the code free of syntax and semantic errors.
Generate executable files of code.
Translates the code from one language to another.
15
Difference Between Compiler and
Interpreter
Compiler
A compiler checks the whole program at once. It displays all the
errors at a place once the whole program is checked.
Interpreter
On the other hand, an interpreter checks the program line by
line. If an error is detected the execution stops.
16
Difference Between Compiler and
Interpreter
Here are some differences between a compiler and an interpreter:
17
The End
18