Professional Documents
Culture Documents
Introduction to compiler
design
source COMPILER
target
program program
( Normally a error ( Normally the
program written equivalent program
in a high-level messages in machine code or
programming assembly language)
language)
Chapter – 1 : Introduction to 4 Bahir Dar Institute of
History of compilers
1940’s:
• Early stored-program computers were programmed in
machine language.
• Later, assembly languages were developed
1950’s:
• Early high-level languages were developed, FORTRAN
• Compiler-writing was a huge task, took 18 person year for
FORTRAN compilers
1960’s onwards/Now:
• Intensively studied
• using software tools, can be done in a few months
Chapter – 1 : Introduction to 5 Bahir Dar Institute of
Programs related to compilers (COUSINS OF COMPILER)
There are other translators/programs that are related to or used
together with compilers and that often come together with
compilers in complete language development environment.
As a general,Translator is a program that translates one language
to another.
Types of Translator:
1. Interpreter 2.Compiler 3.Assembler
Interpreters - Directly execute the operations specified in
the source program on inputs supplied by the user
Ideally:
Source code Compiler Executable
Input data Executable Output data
Source code
Interpreter Output data
Input data
i.e.
preprocessor
modified source program
compiler
target assembly program
assembler
relocatable machine code
linker/loader Library
files
target machine code
Assembly language
Software Engineering
Computer Architecture
Discrete Mathematics
more effectively
ÿ To provide solid foundation in parsing theory for parser
writing
Resource allocation, “optimization”, etc.
Youmight even write a compiler some day!
Chapter – 1 : Introduction to 2 Bahir Dar Institute of
Grouping of Phases into Passes /Parts of compilation
Compiler is not a single box that maps a source program into a target program.
There are two parts to this mapping: analysis and synthesis
• Analysis (front part) [Lexical, Syntax, and Semantic analysis]
• breaks up the source program into constituent pieces
• Creates an intermediate representation of the source program
• Reports any error detected
• Stores source program info in a data structure called a symbol table
• Machine Independent/Language Dependent. b/c
they depend primarily on the source language
• Synthesis (Back part)[Code Generation + Code Optimization]
• constructs the desired target program from the intermediate
representation and the information in the symbol table.
• Machine Dependent. b/c they depend on
the target machine/Language
independent
Compilation process operates as a sequence of phases,
• each of which transforms one representation of the source program to
another.
Chapter – 1Intermediate
• NB: : Introduction
codeto 1
generation is betweenBahir
frontDar Institute
end and of
back end
The Phases of a Compiler…
Current trends:
• to obtain smaller, but maybe slower, equivalent code for
embedded systems;
• to reduce power consumption
• to enable parallelism
Chapter – 1 : Introduction to 2 Bahir Dar Institute of
Code Generation
A compiler may generate
• pure machine codes (machine dependent assembly
language) directly, which is rare now ;
• virtual machine code.
Generates object code from (optimized) intermediate
code LDF R2, id3
MULF R2, R2,#60.0
t2=id3*60.0
LDF R1, id2
id1 = id2 + t2
ADDF R1, R1, R2
STF id1, R1