Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

Compiler

Design
Assoc. Prof. Ahmed Moustafa Elmahalawy
Communication and Computer Engineering Department
‫ميثاق المحاضرة‬

‫األحترام المتبادل‬ ‫إغالق المحمول‬ ‫تحديد الهدف‬ ‫المشاركة‬ ‫اإللتزام بالوقت‬


Chapter 1
Introduction
Compiler Design Chapter 1: Introduction

Contents:-

1- Programming Languages

2- Translators

3- Why learn about compilers?


Compiler Design Chapter 1: Introduction

1.2 Translators

A translator inputs and then converts a


source program into an object or target program.

The source program is written in a source


language and the object program belongs to an
object language.
Compiler Design Chapter 1: Introduction

A translator, being a program in its own


right, must itself be written in a computer
language, known as its host or implementation
language.

A translator may formally be defined as a


function, whose domain is a source language,
and whose range is contained in an object or
target language.
Compiler Design Chapter 1: Introduction
Compiler Design Chapter 1: Introduction

In fact, at least three languages are


involved in the development of translators:

1) The source language to be translated, the


object or target language to be generated.

2) The host language to be used for


implementing the translator.

3) The intermediate languages, if the translation


takes place in several stages.
Compiler Design Chapter 1: Introduction

If the source language being translated is


assembly language and the object program is
machine language, the translator is called an
assembler.

Assembly language resembles closely


machine language.
Compiler Design Chapter 1: Introduction

In an elementary assembly language, most


of its instructions are symbolic representations of
corresponding machine-language instructions.

Each assembly instruction contains an


ordered set of fields.

For example, the first field might represent


a label which is followed immediately by an
operation field.
Compiler Design Chapter 1: Introduction

A translator which transforms a high-level


language such as FORTRAN, PASCAL, or
COBOL into a particular computer's machine or
assembly language is called a compiler.

The time at which the conversion of a


source program to an object program occurs is
called compile time.

The object program is executed at run time.


Compiler Design Chapter 1: Introduction

The next figure illustrates the compilation


process.

Note that the source program and data are


processed at different times, namely, compile
time and run time, respectively.
Compiler Design Chapter 1: Introduction
Compiler Design Chapter 1: Introduction

Another kind of translator, called an


interpreter, processes an internal form of the
source program and data at the same time.

That is, interpretation of the internal


source form occurs at run time and no object
program is generated.
Compiler Design Chapter 1: Introduction

The next figure illustrates this


interpretive process.

Some interpreters analyze each source


statement every time it is to be executed.

Such an approach is very time-


consuming and is seldom used.
Compiler Design Chapter 1: Introduction
Compiler Design Chapter 1: Introduction

An interpreter is a translator that


effectively accepts a source program and
executes it directly, without, seemingly,
producing any object code first.

It does this by fetching the source


program instructions one by one, analyzing
them one by one, and then "executing" them
one by one.
Compiler Design Chapter 1: Introduction

Complex program structures such as


nested procedures or compound statements
do not lend themselves easily to such
treatment.

But, one-line queries made of a data


base, or simple manipulations of a row or
column of a spreadsheet, can be handled
very effectively.
Compiler Design Chapter 1: Introduction

Interpretation shares many aspects


with compiling. Lexing, parsing and type-
checking are in an interpreter done just as in
a compiler.

But instead of generating code from the


syntax tree, the syntax tree is processed
directly to evaluate expressions and execute
statements, and so on.
Compiler Design Chapter 1: Introduction

An interpreter may need to process the


same piece of the syntax tree (for example,
the body of a loop) many times and, hence,
interpretation is typically slower than
executing a compiled program.
Compiler Design Chapter 1: Introduction

But writing an interpreter is often


simpler than writing a compiler and the
interpreter is easier to move to a different
machine, so for applications where speed is
not of essence, interpreters are often used.
Compiler Design Chapter 1: Introduction

Compilation and interpretation may be


combined to implement a programming
language.

The compiler may produce


intermediate-level code which is then
interpreted rather than compiled to machine
code.
Compiler Design Chapter 1: Introduction

In some systems, there may even be


parts of a program that are compiled to
machine code, some parts that are compiled
to intermediate code, which is interpreted at
runtime while other parts may be kept as a
syntax tree and interpreted directly.
Compiler Design Chapter 1: Introduction

Each choice is a compromise between


speed and space: Compiled code tends to
be bigger than intermediate code, which
tend to be bigger than syntax, but each step
of translation improves running speed.
Compiler Design Chapter 1: Introduction

Using an interpreter is also useful


during program development, where:

_ it is more important to be able to test a


program modification quickly rather than
run the program efficiently.

_ They do less work on the program before


execution starts.
Compiler Design Chapter 1: Introduction

_ They are able to start running the program


more quickly.

_ An interpreter is closer to the source code


than is compiled code, error messages
can be more precise and informative.
Compiler Design Chapter 1: Introduction

Interpreters have become increasingly


popular lately, particularly in microcomputer
environments where the overhead of
interpretation seems to be significantly less
for the user.
Compiler Design Chapter 1: Introduction

For example, a main reason why


languages such as BASIC, APL. LISP, and
SMALL T ALK-80 have become very popular
is because they have been implemented in
an interpretive environment.
Compiler Design Chapter 1: Introduction

Some translators for high-level languages,


known as interpretive compilers, produce (as
output) intermediate code which is simple
enough to satisfy the constraints imposed by a
practical interpreter.

Even though it may still be quite a long


way from the machine code of the system on
which it is desired to execute the original
program.
Compiler Design Chapter 1: Introduction

You should be able to see that a


program could be written to allow one real
machine to emulate any other real machine,
albeit perhaps slowly, simply by writing an
interpreter - or, as it is more usually called,
an emulator - for the second machine.
Compiler Design Chapter 1: Introduction

For example, we might develop an


emulator that runs on a Sun SPARC
machine and makes it appear to be an IBM
PC (or the other way around).
Compiler Design Chapter 1: Introduction

This an interpretive approach may have


several points in its favour:

• It is far easier to generate hypothetical


machine code than real machine code.

• It can more easily be made "user


friendly" than can the native code approach.
Compiler Design Chapter 1: Introduction

• A compiler written to produce (as output)


well-defined pseudo-machine code
capable of easy interpretation on a range
of machines can be made highly portable.

• A whole range of languages may quickly


be implemented in a useful form on a wide
range of different machines relatively
easily.
Compiler Design Chapter 1: Introduction

• It proves to be useful in connection with


cross-translators to be tested more effectively
by simulated execution on the donor
machine, rather than after transfer to the
target machine.

• Lastly, intermediate languages are often very


compact, allowing large programs to be
handled, even on relatively small machines.
Compiler Design Chapter 1: Introduction

For all these advantages, interpretive


systems carry fairly obvious overheads in
execution speed, because execution of
intermediate code effectively carries with it
the cost of virtual translation into machine
code each time a hypothetical machine
instruction is obeyed.
Compiler Design Chapter 1: Introduction

1.3 Why learn about compilers?

Few people will ever be required to


write a compiler for a general-purpose
language like C, Pascal or SML. So why do
most computer science institutions offer
compiler courses and often make these
mandatory?
Compiler Design Chapter 1: Introduction

Some typical reasons are:

a) It is considered a topic that you should


know in order to be “well-cultured” in
computer science.

b) A good craftsman should know his tools,


and compilers are important tools for
programmers and computer scientists.
Compiler Design Chapter 1: Introduction

c) The techniques used for constructing a


compiler are useful for other purposes as
well.

d) There is a good chance that a


programmer or computer scientist will
need to write a compiler or interpreter for
a domain-specific language.

You might also like