Air University, Multan Campus
A Faderally Charted Public Sector University

Introduction
Course: Compiler Construction
Tutor: Dr. Muhammad Sanaullah
muhammad.sanaullah@polito.it
Quotations
“If you don’t understand compilers, you can still write
programs – you can even be a competent
programmer – but you can’t be a master. ”



Hal Abelson, MIT

“If you don’t know how compilers work, then you
don’t know how computer work. If you’re not 100%
sure whether you know how compilers work, then
you don’t know how they work.”



Steve Yegge

2
Translator & Compiler
A translator or language processor is a program
that translate an input program written in a
programming language into an equivalent program in
another language.
Compiler is a type of translator, which takes a
program written in high-level programming language
as input (source program) and translates into an
equivalent program in low-level language (target
program), such as machine language or assembly
Source
Target
language.
Compiler





Program

3

Program
Compilers
Compilers traces the errors in the source program and
generates the error report.
 Every programming language have different compilers.
 Without compilation, no program written in a high-level
language can be executed.
 After compilation only the program in machine language
is loaded into the memory for execution.
 During execution, the target program is firstly loaded
into the memory and then the user interacts to generate
output.
Input supplied by
Output produced


the user
4

Compiler

after execution
Preprocessors
Before compilation, the source program is processed
by the preprocessor to prepare it for compilation.
The preprocessor program creates modified source
program from the original source program by
replacing the preprocessor directives with the
suitable content.
The modified source program act as an input to the
compiler.






5
Preprocessors
Preprocessor performs the following tasks:







It permits the user to include the header files in the
program and user can make use of the functions defined
in these header files.
It permits the user to include macros in the program.




Source
Program
6

Macros are the small set of instructions that are used in a
program respectively. Macros have two attributes: macro name
and macro definition.
Whenever the macro name is encountered in the program then
it is replaced by the macro definition (set of statements
correspond to the macro).
Preprocessor

Modified
Source
Program

Compiler

Target
Program
Interpreter
Interpreter directly executes the source program line
by line according to the given inputs.
Translation and execution of each statement are
carried out side by side. Thus, separate execution of
the program is not required.
The line by line execution of the program provides
better debugging environment than a compiler.






Source
Program
Interpreter
Input

7

Output
Compilers Vs Interpreter
The interpreter takes one statement then translates it and
executes it and then takes another statement. While the
compiler translates the entire program in one go and then
executes it.
Compiler generates the error report after the translation
of the entire page while an interpreter will stop the
translation after it gets the first error.
Compiler takes a larger amount of time in analyzing and
processing the high level language code comparatively
interpreter takes lesser time in the same process.
Besides the processing and analyzing time the overall
execution time of a code is faster for compiler relative to
the interpreter.









8
Compilers Vs Interpreter

9
Hybrid Compiler




Java language processors
combine compilation and
interpretation
To achieve faster
processing of inputs to
outputs, some java
compilers, called just-intime compilers, translate
the bytecodes into
machine language
immediately before they
run the intermediate
program to process the
input.

10

Source
Program

Translator

Compiler

ByteCodes
Intermediate
Program
Input

Interpreter
Virtual
Machine

Output
Assemblers


In some cases, compiler generates the target
program in assembly language, and then this
intermediate (assembly program – Mnemonics) is
passed to assembler which convert it into machine
code.

Source
Program

11

Compiler

Assembly
Language
Program

Assembler

Machine
Language
Code
Analysis of a Source Program
Some time, a source program may be divided into
modules stored in separate files.
 The task for collecting the source program (or
macros) from separate files is called preprocessor.
 The modified source program is then fed to a
compiler.
 The compiler may produce an assembly-language
program as its output., because Assembly language
is easier to produce as output and is easier to
debug.
 The assembly language program is then fed to
assembler that produces relocatable machine code
12
as its output.

Analysis of a Source Program






Larger programs are often compiled in pieces, so the
relocatable machine code may have to be linked
together with other relacateble object files and library
files into the code that actually runs on the machine.
The linker resolves external memory
addresses, where the code in one file may refer to a
location in another file.
The loader then puts together all of the executable
object files into memory for execution.

13
Analysis of a Source Program
Source Program
Preprocessor
Modified Source Program
Compiler
If target program is in machine language
then it execute directly

If target program is in assembly language
then it passed to assembler

Assembler
Relocatable Machine Code
Linker/loader

Target Machine Code
14

Library Files and
Relocatable Object Files
References





A. V. Aho, Compilers: principles, techniques, and
tools, Pearson Education India, 2007.
ITL Education Solutions Limited, Principles of
compiler design, Dorling Kindersley (India) Pvt.
Ltd, Pearson Education in South Asia, 2012.
P.H. Dave, H.B. Dave, Compilers: Principles and
Practice, Dorling Kindersley (India) Pvt. Ltd, Pearson
Education in South Asia, 2012.

15
16

Design Time Methodology for the Formal Modeling and Verification of Smart Environments
Compiler Construction Course - Introduction

Compiler Construction Course - Introduction

  • 1.
    Air University, MultanCampus A Faderally Charted Public Sector University Introduction Course: Compiler Construction Tutor: Dr. Muhammad Sanaullah muhammad.sanaullah@polito.it
  • 2.
    Quotations “If you don’tunderstand compilers, you can still write programs – you can even be a competent programmer – but you can’t be a master. ”  Hal Abelson, MIT “If you don’t know how compilers work, then you don’t know how computer work. If you’re not 100% sure whether you know how compilers work, then you don’t know how they work.”  Steve Yegge 2
  • 3.
    Translator & Compiler Atranslator or language processor is a program that translate an input program written in a programming language into an equivalent program in another language. Compiler is a type of translator, which takes a program written in high-level programming language as input (source program) and translates into an equivalent program in low-level language (target program), such as machine language or assembly Source Target language. Compiler   Program 3 Program
  • 4.
    Compilers Compilers traces theerrors in the source program and generates the error report.  Every programming language have different compilers.  Without compilation, no program written in a high-level language can be executed.  After compilation only the program in machine language is loaded into the memory for execution.  During execution, the target program is firstly loaded into the memory and then the user interacts to generate output. Input supplied by Output produced  the user 4 Compiler after execution
  • 5.
    Preprocessors Before compilation, thesource program is processed by the preprocessor to prepare it for compilation. The preprocessor program creates modified source program from the original source program by replacing the preprocessor directives with the suitable content. The modified source program act as an input to the compiler.    5
  • 6.
    Preprocessors Preprocessor performs thefollowing tasks:    It permits the user to include the header files in the program and user can make use of the functions defined in these header files. It permits the user to include macros in the program.   Source Program 6 Macros are the small set of instructions that are used in a program respectively. Macros have two attributes: macro name and macro definition. Whenever the macro name is encountered in the program then it is replaced by the macro definition (set of statements correspond to the macro). Preprocessor Modified Source Program Compiler Target Program
  • 7.
    Interpreter Interpreter directly executesthe source program line by line according to the given inputs. Translation and execution of each statement are carried out side by side. Thus, separate execution of the program is not required. The line by line execution of the program provides better debugging environment than a compiler.    Source Program Interpreter Input 7 Output
  • 8.
    Compilers Vs Interpreter Theinterpreter takes one statement then translates it and executes it and then takes another statement. While the compiler translates the entire program in one go and then executes it. Compiler generates the error report after the translation of the entire page while an interpreter will stop the translation after it gets the first error. Compiler takes a larger amount of time in analyzing and processing the high level language code comparatively interpreter takes lesser time in the same process. Besides the processing and analyzing time the overall execution time of a code is faster for compiler relative to the interpreter.     8
  • 9.
  • 10.
    Hybrid Compiler   Java languageprocessors combine compilation and interpretation To achieve faster processing of inputs to outputs, some java compilers, called just-intime compilers, translate the bytecodes into machine language immediately before they run the intermediate program to process the input. 10 Source Program Translator Compiler ByteCodes Intermediate Program Input Interpreter Virtual Machine Output
  • 11.
    Assemblers  In some cases,compiler generates the target program in assembly language, and then this intermediate (assembly program – Mnemonics) is passed to assembler which convert it into machine code. Source Program 11 Compiler Assembly Language Program Assembler Machine Language Code
  • 12.
    Analysis of aSource Program Some time, a source program may be divided into modules stored in separate files.  The task for collecting the source program (or macros) from separate files is called preprocessor.  The modified source program is then fed to a compiler.  The compiler may produce an assembly-language program as its output., because Assembly language is easier to produce as output and is easier to debug.  The assembly language program is then fed to assembler that produces relocatable machine code 12 as its output. 
  • 13.
    Analysis of aSource Program    Larger programs are often compiled in pieces, so the relocatable machine code may have to be linked together with other relacateble object files and library files into the code that actually runs on the machine. The linker resolves external memory addresses, where the code in one file may refer to a location in another file. The loader then puts together all of the executable object files into memory for execution. 13
  • 14.
    Analysis of aSource Program Source Program Preprocessor Modified Source Program Compiler If target program is in machine language then it execute directly If target program is in assembly language then it passed to assembler Assembler Relocatable Machine Code Linker/loader Target Machine Code 14 Library Files and Relocatable Object Files
  • 15.
    References    A. V. Aho,Compilers: principles, techniques, and tools, Pearson Education India, 2007. ITL Education Solutions Limited, Principles of compiler design, Dorling Kindersley (India) Pvt. Ltd, Pearson Education in South Asia, 2012. P.H. Dave, H.B. Dave, Compilers: Principles and Practice, Dorling Kindersley (India) Pvt. Ltd, Pearson Education in South Asia, 2012. 15
  • 16.
    16 Design Time Methodologyfor the Formal Modeling and Verification of Smart Environments