0% found this document useful (0 votes)
31 views

Compiler Design: Dr. Eng. Ahmed Moustafa Elmahalawy

The document discusses compiler design and outlines topics such as programming languages, translators, and different levels of languages from machine-level to high-level. It describes advantages of high-level languages and defines compilers as programs that accept source code and generate object code.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Compiler Design: Dr. Eng. Ahmed Moustafa Elmahalawy

The document discusses compiler design and outlines topics such as programming languages, translators, and different levels of languages from machine-level to high-level. It describes advantages of high-level languages and defines compilers as programs that accept source code and generate object code.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Compiler

Design
Dr. Eng. Ahmed Moustafa Elmahalawy
Computer Science and Engineering Department
‫ميثاق المحاضرة‬

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


Outlines
1- Introduction.

2- Model of a Compiler.

3- Language Specification.

4- Grammars and Their Classification.


Evaluation
1- Reports = 5 Marks.

2- Mid Term exam = 15 Marks.

3- Section Work = 10 Marks.

4- Final Exam = 70 Marks.


References
1- Jean-Paul Tremblay and Paul G. Sorenson “The
Theory and Practice of Compiler Writing” BS
Publications 4-4-309, Giriraj Lane, Sultan Bazar,
Hyderabad - 500 095 A.P. 2008.

2- Torben Ægidius Mogensen “Basics of Compiler


Design”, Published through lulu.com.
http://www.diku.dk/ torbenm/Basics, 2000-2010.
ISBN 978-87-993154-0-6

3- Andrew W. “Modern Compiler Implementation in


Java” Appel and Jens Palsberg, Cambridge
University Press © 2002. ISBN:052182060x
Chapter 1
Introduction
Compiler Design Chapter 1: Introduction

Contents:-

1- Programming Languages

2- Translators
Compiler Design Chapter 1: Introduction

Users of modern computing systems


can be divided into two broad categories.

_There are those who never develop their


own programs, but simply use ones
developed by others.

_There are those who are concerned as


much with the development of programs as
with their subsequent use.
Compiler Design Chapter 1: Introduction

1.1 Programming Languages


In computer programming, a
programming language serves as a means
of communication between the person with a
problem and the computer used to help
solve it.
Compiler Design Chapter 1: Introduction

An effective programming language


enhances both the development and the
expression of computer programs.

It must bridge the gap between the


often unstructured nature of human thought
and the precision required for computer
execution.
Compiler Design Chapter 1: Introduction

A program solution to a given problem


will be easier and more natural to obtain if
the programming language used is close to
the problem.

Such a programming language is


usually high-level.
Compiler Design Chapter 1: Introduction

Digital computers accept and


understand only their own special low-level
language, consisting typically of long
sequences of zeros and ones.

These sequences are generally


unintelligible to humans.
Compiler Design Chapter 1: Introduction

Programmers use tools as compilers,


editors and operating systems to remove all
burdens, or all possibilities for error, as the
reader will be well aware.

Much program development was done


in machine language where programs
written as almost meaningless collections of
binary or hexadecimal digits.
Compiler Design Chapter 1: Introduction

However, in order for high-level


languages to be usable, one must be able to
convert programs written in them into the
binary or hexadecimal digits and bit strings
that a machine will understand.
Compiler Design Chapter 1: Introduction

At an early stage, the development of


translators or compilers - programs which
accept (as data) a textual representation of
an algorithm expressed in a source
language, and which produce (as primary
output) a representation of the same
algorithm expressed in another language,
the object or target language.
Compiler Design Chapter 1: Introduction

A hierarchy of programming languages


based on increasing machine independence
includes the following:

1. Machine-level languages.

2. Assembly languages.

3. Higher-level or user-oriented
languages.

4. Problem-oriented language.
Compiler Design Chapter 1: Introduction

1. Machine-level languages are the lowest


form of computer language.

Each instruction in a program is


represented by a numeric code, and
numerical addresses are used throughout
the program to refer to memory locations in
the computer's memory.
Compiler Design Chapter 1: Introduction

Also included as machine-level


programs are programs written in microcode
(i.e., microprograms).

Microcode allows for the expression of


some of the more powerful machine-level
instructions in terms of a set of basic
machine instructions.
Compiler Design Chapter 1: Introduction

2. Assembly languages are essentially a


symbolic version of a machine-level
language.

Each operation code is given a


symbolic code such as ADD for addition and
MUL for multiplication.

Moreover, memory locations are given


symbolic names such as PAY and RATE.
Compiler Design Chapter 1: Introduction

Some assembly languages contain


macroinstructions which are at a higher level
than assembly-languages instructions.

Assembly language systems offer


certain diagnostic and debugging assistance
that is normally not available at the machine
level.
Compiler Design Chapter 1: Introduction

3- Higher-level or user-oriented languages


such as FORTRAN, PASCAL, or PL/I offers
most of the features of an assembly language.
While some facilities for accessing system
level features may not be provided, a high-level
language offers a more enriched set of language
features such as structured control constructs,
nested statements, blocks, and procedures.
Compiler Design Chapter 1: Introduction

4- Problem-oriented language provides for the


expression of problems in a specific application
or problem area.

Examples of such languages are SEQUEL


for database retrieval applications and COGO for
civil engineering applications.
Compiler Design Chapter 1: Introduction

Advantages of high-level languages over


low-level languages such as machine and
assembly languages include the following:

1. High-level languages are easier to learn than


their lower-level counterparts.

2. These languages are closer to their problem


areas than lower-level languages.
Compiler Design Chapter 1: Introduction

3. The learning of many high-level languages


requires little or no computer hardware
background because such languages are
relatively machine-independent.

4. The programmer does not have to be


concerned with clerical tasks involving
numerical or symbolic references to
instructions, memory locations, constants etc.
Compiler Design Chapter 1: Introduction

5. A programmer is not required to know how to


convert data from external forms to various
internal forms within the memory of a
computer.

6. Most high-level languages offer a programmer


a variety of control 'structures which are not
available in low-level languages.
Compiler Design Chapter 1: Introduction

High-level languages offer several of the


following language constructs:

• Conditional statements (such as IF-THEN-


ELSE and CASE statements)

• Looping statements (for both counted and


conditional loops)

• Nested statements

• Block structures
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.

You might also like