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

Advanced Computer Systems: Compiler Design & Implementation

The document discusses the structure and phases of a compiler. A compiler translates a program written in a source language into an equivalent program in a target language. It has two main parts - analysis and synthesis. The analysis part breaks down the source program into pieces, imposes a grammatical structure, and creates an intermediate representation. It also collects information in a symbol table. The synthesis part constructs the target program from the intermediate representation and symbol table. A compiler typically has several phases that each transform the program representation, such as lexical analysis, syntax analysis, semantic analysis, optimization, and code generation. The symbol table is used across all phases.

Uploaded by

xliizbethx
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Advanced Computer Systems: Compiler Design & Implementation

The document discusses the structure and phases of a compiler. A compiler translates a program written in a source language into an equivalent program in a target language. It has two main parts - analysis and synthesis. The analysis part breaks down the source program into pieces, imposes a grammatical structure, and creates an intermediate representation. It also collects information in a symbol table. The synthesis part constructs the target program from the intermediate representation and symbol table. A compiler typically has several phases that each transform the program representation, such as lexical analysis, syntax analysis, semantic analysis, optimization, and code generation. The symbol table is used across all phases.

Uploaded by

xliizbethx
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Advanced Computer Systems

COMPILER DESIGN & IMPLEMENTATION

5th Computer
at Modern Academy

Dr. Osama Mohamed Mowafy

1
1. Compiler : Introduction
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 first
must be translated into a form in which it can be executed by a
computer.
The software systems that do this translation are called
compilers.
This course is about how to design and implement compilers.
We shall discover that 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.
2
1. Compiler : Introduction

Why Study Compilers?


Central role of compilers in
software development.
Prime importance of compilers
in determining performance.
The microcosm of computer
science found in compilers.
3
1. Compiler : Introduction

Fundamentals of Compiling
The compiler must preserve the
meaning of the program.
The compiler must improve the
source code in some noticeable
manner.
4
1. Compiler : Introduction

Desirable Properties
Correct output code
Compile-time efficiency
Better code (Optimized)
Minimum Space
Feedback
Debugging
5
1. Compiler : Introduction

Fundamentals of Compiling
The compiler must preserve the
meaning of the program.
The compiler must improve the
source code in some noticeable
manner.
6
1.1 Language Processors
Simply stated, 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; see Fig. 1.1. An
important role of the compiler is to report any errors in the source
program that it detects during the translation process.

If the target program is an executable machine-language program, it can


then be called by the user to process inputs and produce outputs; see Fig. 1.2.

.
7
1.1 Language Processors
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, as shown in Fig. 1.3.

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.

8
1.1 Language Processors

Example 1.1 : Java language processors combine compilation and


interpretation, as shown in Fig. 1.4. 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.
9
1.1 Language Processors
In addition to a compiler, several other programs may be required to
create an executable target program, as shown in Fig. 1.5. A source
program may be divided into modules stored in separate files. The task of
collecting the source program is sometimes entrusted to a separate
program, called a preprocessor. The preprocessor may also expand
shorthands, called macros, into source language statements.
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 is then processed by a program called an
assembler that produces relocatable machine code as its output.
Large programs are often compiled in pieces, so the relocatable
machine code may have to be linked together with other relocatable
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. 10
1.1 Language Processors
 1.11 Exercises for Section 1.1
Exercise 1.1.1 : What is the difference between a compiler
and an interpreter?
Exercise 1.1.2 : What are the advantages of (a) a compiler
over an interpreter (b) an interpreter over a compiler?
Exercise 1.1.3 : What advantages are there to a language-
processing system in which the compiler produces
assembly language rather than machine language?
Exercise 1.1.4 : A compiler that translates a high-level
language into another high-level language is called a
source-to-source translator. What advantages are there to
using C as a target language for a compiler?
Exercise 1.1.5 : Describe some of the tasks that an assembler
needs to perform.

11
1.2 The Structure of a Compiler
Up to this point we have treated a compiler as a single box
that maps a source program into a semantically equivalent
target program. If we open up this box a little, we see that
there are two parts to this mapping: analysis and synthesis.
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. If the analysis part
detects that the source program is either syntactically ill
formed or semantically unsound, then it must provide
informative messages, so the user can take corrective action.
The analysis part also collects information about the source
program and stores it in a data structure called a symbol
table, which is passed along with the intermediate
representation to the synthesis part.
12
1.2 The Structure of a Compiler
The synthesis part constructs the desired target program
from the intermediate representation and the information in
the symbol table. The analysis part is often called the front
end of the compiler; the synthesis part is the back end.
If we examine the compilation process in more detail, we
see that it operates as a sequence of phases, each of which
transforms one representation of the source program to
another. A typical decomposition of a compiler into phases is
shown in Fig. 1.6. In practice, several phases may be grouped
together, and the intermediate representations between the
grouped phases need not be constructed explicitly. The
symbol table, which stores information about the entire
source program, is used by all phases of the compiler.

13
1.2 The Structure of a Compiler
Jobs for a Compiler…
Determine whether input is a well constructed sentence in
the language.
Lexical analysis or Scanning
Break the input into tokens (words)
Syntax analysis or Parsing
Analyze the phrase or sentence structure of the
input
Determine whether the program (input) has a well-defined
meaning.
Semantic or context-sensitive analysis
Perform type checking, analyze the hierarchical
structure of statements and report errors
W W*2*X*Y*Z 14
1.2 The Structure of a Compiler
Jobs for a Compiler…
Improve the code.
Optimization
Improve some important aspect of the code, e.g.,
speed or size.
W W*2*2 becomes W W*4
Generate an executable program.
Code Generation
Select the instructions to implement the transformed
code on the target machine.
Allocate program values to the limited set of registers.
Order or schedule the selected instructions to
minimize the execution time.
15
1.2 The Structure of a Compiler
Simplified Modern Compiler Organization

Modern Optimizing Compiler

16
1.2 The Structure of a Compiler

17
1.2 The Structure of a Compiler
Lexical Analysis
Determine the words (token) in the input.
What are words in computer programs?
How do we separate the input string into words?
How does this facilitate compilation?

Syntactic Analysis
Determine the sentences in the input.
What are sentences in computer programs?
How do we group the words into sentences?
How does this facilitate compilation?

18
1.2 The Structure of a Compiler
Intermediate Representations
Represent the program in an easily transformable manner.
Representation strongly tied to the algorithms
trees ➫ recursion
linear ➫ iteration
How do IRs support compilation?
Abstract Syntax Trees
Form of intermediate representation.
Tree basis for representing the essence of the computer
program being translated.
Form reflects the form of the program.
Representation for each programming structure mimics the
language. 19
1.2 The Structure of a Compiler
Semantic Analysis
Determine the meaning of the input.
What are sentences in computer program supposed to do?
Are all of the interactions valid?
How does this support compilation?

Code Generation
Translate the internal representation into a form that can be
executed by a machine.
Optimization
Improve the generated code in some measurable way (speed,
size, …)
20

You might also like