8/5/2020
Compiler Construction
[SWE-310]
Introduction
Compiler Construction [SWE-310] Introduction
Overview
Programming languages are notations for describing
computations to people and to machines.
The world as we know it depends on programming
languages, because all the software running on all
the computers was written in some programming
language.
But, before a program can be run, it must first be
translated into a form in which it can be executed
by a computer.
1
8/5/2020
Compiler Construction [SWE-310] Introduction
Overview
The software systems that do this translation are called
Language Processing Systems (Compilers, Interpreters and
Assemblers etc.) or Translators and compiler is one of them.
This course is about how to design and implement compilers.
A few basic ideas can be used to construct translators for a
wide variety of languages and machines.
Besides compilers, the principles and techniques for compiler
design are applicable to so many other domains that they are
likely to be reused many times in the career of a computer
scientist.
The study of compiler writing touches upon programming
languages, machine architecture, language theory, algorithms,
and software engineering.
Compiler Construction [SWE-310] Introduction
Overview
This preliminary chapter is about:
• Introduction of different forms of language
translators
• A high level overview of the structure of a
typical compiler
• The trends in programming languages and
machine architecture that are shaping
compilers
2
8/5/2020
Compiler Construction [SWE-310] Introduction
Language Processing System/Translator
A translator inputs and then converts a
source program into an object or target
program.
Source program is written in a source
language
Object program belongs to an object
language
A translators could be: Assembler,
Compiler or Interpreter
Compiler Construction [SWE-310] Introduction
Language Processing System/Translator
3
8/5/2020
Compiler Construction [SWE-310] Introduction
Assembler
Assembler:
Assembler
Source Program Object Program
(In Assembly Language) (In machine Language)
Compiler Construction [SWE-310] Introduction
Compiler
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)
4
8/5/2020
Compiler Construction [SWE-310] Introduction
Compiler
Compiler Construction [SWE-310] Introduction
Compiler
If the target program is an executable
machine-language program, it can then be
called by the user to process inputs and
produce outputs
5
8/5/2020
Compiler Construction [SWE-310] Introduction
Compiler
The compiler also report about errors in
source program.
Compiler
Source Program Target Program
Error Messages
Compiler Construction [SWE-310] Introduction
Interpreter
An interpreter is another common kind of language
processor. Instead of producing a target program as a
translation, an interpreter appears to directly execute
the operations specified in the source program on
inputs supplied by the user
6
8/5/2020
Compiler Construction [SWE-310] Introduction
Compiler vs. Interpreter
The machine-language target program produced by a
compiler is usually much faster than an interpreter at
mapping inputs to outputs .
An interpreter, however, can usually give better error
diagnostics than a compiler, because it executes the
source program statement by statement.
Compiler Construction [SWE-310] Introduction
Compiler vs. Interpreter
Compiler: translates a source program written in a
High-Level Language (HLL) such as Pascal, C++
into computer’s machine language (Low-Level
Language (LLL)).
• The time of conversion from source program into
object program is called compile time
• The object program is executed at run time
Interpreter: processes an internal form of the
source program and data at the same time (at run
time); no object program is generated.
7
8/5/2020
Compiler Construction [SWE-310] Introduction
Compiler vs. Interpreter
Why Interpretation?
• A higher degree of machine independence: high
portability.
• Dynamic execution: modification or addition to
user programs as execution proceeds.
• Dynamic data type: type of object may change
at runtime
• Easier to write: no synthesis part.
• Better diagnostics: more source text
information available
Compiler Construction [SWE-310] Introduction
Compiler vs. Interpreter
Compilation Process:
Interpretive Process:
8
8/5/2020
Compiler Construction [SWE-310] Introduction
Compiler vs. Interpreter
Compilation Process:
Interpretive Process:
Compiler Construction [SWE-310] Introduction
Compiler vs. Interpreter
Java language processors combine compilation and
interpretation.
9
8/5/2020
Compiler Construction [SWE-310] Introduction
Compiler vs. Interpreter
A Java source program may first be compiled into an
intermediate form called bytecodes.
The bytecodes are then interpreted by a virtual
machine.
A benefit of this arrangement is that bytecodes
compiled on one machine can be interpreted on
another machine, perhaps across a network.
In order to achieve faster processing of inputs to
outputs, some Java compilers, called just-in-time
compilers, translate the bytecodes into machine
language immediately before they run the
intermediate program to process the input.
Compiler Construction [SWE-310] Introduction
Model of a Compiler
A compiler must perform two tasks:
Analysis of source program:
The analysis part breaks up the source program
into constituent pieces and imposes a
grammatical structure on them. It then uses this
structure to create an intermediate representation
of the source program.
Synthesis of its corresponding program:
constructs the desired target program from the
intermediate representation and the information
in the symbol table.
10
8/5/2020
Compiler Construction [SWE-310] Introduction
Model of a Compiler
This model is collectively called the
Analysis-Synthesis model of compilation.
The analysis part is often called the front
end (FE) of the compiler
The synthesis part is the back end (SE).
Compiler Construction [SWE-310] Introduction
Analysis-Synthesis Model of a Compiler
Source Program Object Program
Synthesis
Analysis
Code Code
Lexical Syntactic Semantic
Generator optimizer
Analysis Analysis Analysis
Tables
11
8/5/2020
Compiler Construction [SWE-310] Introduction
Software Tools Performing Analysis
Software tools other than compilers that
perform analysis on Source Program are:
Structure Editors: Inputs a sequence of
commands to build a source program.
The structure editor performs text creation,
modification and analyzes program text for
putting an appropriate hieratical structure on
the source program.
E.g. Matching do-while and parenthesis { }
Compiler Construction [SWE-310] Introduction
Software Tools Performing Analysis
Pretty Printers:
Analyzes a program and prints it in such a
way that the structure of program becomes
clearly visible.
i.e. Indentation
12
8/5/2020
Compiler Construction [SWE-310] Introduction
Software Tools Performing Analysis
Static Checkers:
Reads a program, analyzes it and attempts
to discover potential bugs without running
the program.
E.g. Parts of source program that can
never be executed and using variables
without being defining them.
Works on dry run techniques.
Compiler Construction [SWE-310] Introduction
Introduction
The End
13