You are on page 1of 21

Compiler Design CS224 Chapter 1: Compiler definition

Compiler Design
CS224

Dr. Galal Eldin Abbas


June 2019

Dr. Galal Eldin Abbas Computer Science Department Page 1 of 21


Compiler Design CS224 Chapter 1: Compiler definition

Course Description:
Course Title and Code - Compiler Design CS224
Pre-requisite: CS213: Course Level: 6, Credit Hours: 3(3+0)
 Compiler introduction and background. Phases and cousins of compiler
 Lexical analysis: regular expressions, finite automata and its implementation. ‫ مبادئ وتقنيات وأدوات هو كتاب شهير في مجال علوم الحاسوب من تأليف الفريد‬:‫المترجمات‬
‫يعرف أيضا الكتاب‬. ‫ رافي سيتي وجيفري المان يختص بشرح كيفية بناء المترجمات‬،‫فينو اهو‬
 Syntax analysis: top-down and bottom up parsing. Derivation trees. Writing ‫( نظرا للصورة الموجودة على غالفه التي تحتوي‬Dragon Book) ‫باسم كتاب التنين‬
context-free grammar of a sample computer language. Recursive descent ‫ يسمى اإلصدار األول من الكتاب باسم كتاب التنين األحمر‬،‫على فارسيحمل سيفا ويقاتل تنين‬
parsing. LL parsing. Non-recursive predictive parsing. SLR parsing. )‫بينما يسمى الجزء الثاني باسم كتاب التنين االرجواني (حسب لون الغالف‬
 Syntax-directed translation. S-attributed and L-attributed grammars. Abstract
syntax trees. :‫مواضيع الكتاب‬
 Semantic analysis. Type checking. ‫ بنية المترجمات‬
 Runtime environments. Activation trees, activation records, calling sequence. ‫النحوي‬ ‫التحليل‬ +‫الداللي‬ ‫التحليل‬ + ‫الصرفي‬ ‫التحليل‬ :‫المفردات‬ ‫ تحليل‬
)‫ تجزئة (لغة‬
 Intermediate code generation. Three-address code. Flow-of-control statements
‫ الترجمة الموجهة نحو بناء الجملة‬
translation. )‫ توليد الكود (توضيح‬
 Code generation and optimization. Register allocation, basic block and ‫ أمثلة البرمجيات‬
peephole optimizations.
 About the Textbook: Will be available in PDF format on the LMS for you to download for free. (QU Blackboard)
 Text: = Compilers: Principles, techniques and tools. Aho, Lam, Sethi and Ullman
 Ref: = Introduction to Compiler Design. T. Mogensen
Marks distribution: About Me
50 Marks Final Exam Name : Dr. Galal Eldin Abbas Eltayb (Sudan)
20 Marks First Midterm B.Sc., M.Sc.: Computer science , Ph.D.: Information technology
20 Marks Second Midterm University Staff since 1995 (Khartoum, Sudan Tech, Nile valley, Qassim)
10 Marks Quizzes an assignments

Dr. Galal Eldin Abbas Computer Science Department Page 2 of 21


Compiler Design CS224 Chapter 1: Compiler definition

Chapter 1: Introduction

Computer Language
A language is consider as a finite set of strings over
some finite set of alphabets. Computer languages are
consider as finite sets, and mathematically set
operations can be perform on them. Finite languages
can be describe by means of regular expressions.

Dr. Galal Eldin Abbas Computer Science Department Page 3 of 21


Compiler Design CS224 Chapter 1: Compiler definition

Compiler1
A compiler is a special program that processes statements written in a particular programming
language and turns them into machine language or "code" that a
computer's processor uses. Typically, a programmer writes
language statements in a language such as Pascal or C one line at a
time using an editor. The file has been created contains what are called the source statements. The
programmer then runs the appropriate language compiler, specifying the name of the file that
contains the source statements.

1
https://searchdatacenter.techtarget.com/answer/What-is-the-difference-between-a-compiler-and-an-interpreter
https://www.thefreecountry.com/compilers/

Dr. Galal Eldin Abbas Computer Science Department Page 4 of 21


Compiler Design CS224 Chapter 1: Compiler definition

When executing (running), the compiler first parses (or analyzes) all of the language statements
syntactically one after the other and then, in one or more successive stages or "passes", builds the
output code, making sure that statements that refer to other statements are referred to correctly in
the final code. Traditionally, the output of
the compilation has been called object code
or sometimes an object module. (Note that
the term "object" here is not related to
object-oriented programming.) The object
code is machine code that the processor can
execute one instruction at a time.

Dr. Galal Eldin Abbas Computer Science Department Page 5 of 21


Compiler Design CS224 Chapter 1: Compiler definition

History of Compiler

Important Landmark of Compiler's history are as follows:


 The "compiler" word was first used in the early 1950s by
Grace Murray Hopper.
 The first compiler was built by John Backum and his
group between 1954 and 1957 at IBM.
 COBOL was the first programming language which was
compiled on multiple platforms in 1960.
 The study of the scanning and parsing issues were pursued
in the 1960s and 1970s to provide a complete solution.

Dr. Galal Eldin Abbas Computer Science Department Page 6 of 21


Compiler Design CS224 Chapter 1: Compiler definition

Compiler name

The original meaning of “to compile” is “to select representative material and add it to a
collection”; makers of compilation CDs use the term in its proper meaning. In its early days
programming language translation was viewed in the same way: when the input contained for
example “a + b”, a prefabricated code fragment “load a in register; add b to register” was selected
and added to the output. A compiler compiled a list of code fragments to be added to the translated
program. Today’s compilers, especially those for the non-imperative programming paradigms,
often perform much more radical transformations on the input program.

Dr. Galal Eldin Abbas Computer Science Department Page 7 of 21


Compiler Design CS224 Chapter 1: Compiler definition

Features of Compilers

 Correctness : Is a compiler behaves according to its language specification


 Speed of compilation.
 Preserve the correct the meaning of the code.
 The speed of the target code.
 Recognize legal and illegal program constructs.
 Good error reporting/handling.
 Code debugging help.

Dr. Galal Eldin Abbas Computer Science Department Page 8 of 21


Compiler Design CS224 Chapter 1: Compiler definition

Types of Compiler
Single Pass Compilers Two Pass Compilers Multipass Compilers

Single Pass Compiler


In single pass Compiler source code directly transforms into machine code. For example, Pascal
language.

Dr. Galal Eldin Abbas Computer Science Department Page 9 of 21


Compiler Design CS224 Chapter 1: Compiler definition

Two Pass Compiler


Two pass Compiler is divided into two sections, viz.
Front end: It maps legal code into Intermediate Representation (IR).
Back end: It maps IR onto the target machine
The Two pass compiler method also simplifies the retargeting process. It also allows multiple front
ends.

Dr. Galal Eldin Abbas Computer Science Department Page 10 of 21


Compiler Design CS224 Chapter 1: Compiler definition

Multipass Compilers

The multipass compiler processes the source code or syntax tree of a program several times. It
divided a large program into multiple small programs and process them. It develops multiple
intermediate codes. All of these multipass take the output of the previous phase as an input. So it
requires less memory. It is also known as 'Wide Compiler'.

Dr. Galal Eldin Abbas Computer Science Department Page 11 of 21


Compiler Design CS224 Chapter 1: Compiler definition

Tasks of Compiler

Main tasks performed by the Compiler are:


 Breaks up the up the source program into pieces and impose grammatical structure on them
 Allows you to construct the desired target program from the intermediate representation and
also create the symbol table
 Compiles source code and detects errors in it
 Manage storage of all variables and codes.
 Support for separate compilation
 Read, analyze the entire program, and translate to semantically equivalent
 Translating the source code into object code depending upon the type of machine

Dr. Galal Eldin Abbas Computer Science Department Page 12 of 21


Compiler Design CS224 Chapter 1: Compiler definition

Role of Compiler

 Compliers reads the source code, outputs executable code


 Translates software written in a higher-level language into instructions that computer can
understand. It converts the text that a programmer writes into a format the CPU can
understand.
 The process of compilation is relatively complicated. It spends a lot of time analyzing and
processing the program.
 The executable result is some form of machine-specific binary code.

Dr. Galal Eldin Abbas Computer Science Department Page 13 of 21


Compiler Design CS224 Chapter 1: Compiler definition

Application of Compilers

 Compiler design helps full implementation Of High-Level Programming Languages (H.L.L.).


 Support optimization for Computer Architecture Parallelism.
 Design of New Memory Hierarchies of Machines.
 Widely used for Translating Programs.
 Used with other Software Productivity Tools.

Dr. Galal Eldin Abbas Computer Science Department Page 14 of 21


Compiler Design CS224 Chapter 1: Compiler definition

Interpreter
An interpreter is a computer program that is used to directly execute program instructions written
using one of the many high-level programming languages. The interpreter transforms the high-
level program into an intermediate language that it then executes, or it could parse the high-level
source code and then performs the commands directly, which is done line by line or statement by
statement.

Role of Interpreter
 The interpreter converts the source code line-by-line during RUN Time.
 Interpret completely translates a program written in a high-level language into machine level
language.
 Interpreter allows evaluation and modification of the program while it is executing.
 Relatively less time spent for analyzing and processing the program
 Program execution is relatively slow compared to compiler

Dr. Galal Eldin Abbas Computer Science Department Page 15 of 21


Compiler Design CS224 Chapter 1: Compiler definition

What is the difference between a compiler and an interpreter?


They are both similar as they achieve similar purposes, but inherently different as to how they
achieve that purpose. Compiled code takes programs (source) written in some kind of programming
language, and then ultimately translates it into object code or machine language. Compiled code
does the work much more efficiently, because it produces a complete machine language program,
which can then be executed. The interpreter translates instructions one at a time, and then executes
those instructions immediately. The compiler is itself a computer program written usually in some
implementation language.

Dr. Galal Eldin Abbas Computer Science Department Page 16 of 21


Compiler Design CS224 Chapter 1: Compiler definition

No Compiler Interpreter
1 Performs the translation of a program as a Performs statement by statement translation.
whole.
2 Execution is faster. Execution is slower.
3 Requires more memory as linking is Memory usage is efficient as no intermediate
needed for the generated intermediate object code is generated.
object code.
4 Debugging is hard as the error messages It stops translation when the first error is met.
are generated after scanning the entire Hence, debugging is easy.
program only.
5 Programming languages like C, C++ uses Programming languages like Python, BASIC, and
compilers. Ruby uses interpreters.

Dr. Galal Eldin Abbas Computer Science Department Page 17 of 21


Compiler Design CS224 Chapter 1: Compiler definition

The structure of compiler consists of two parts:


Analysis part
• Analysis part breaks the source program into constituent pieces and imposes a grammatical
structure on them which further uses this structure to create an intermediate representation of the
source program.
• It is also termed as front end of compiler.
• Information about the source program is collected and stored in a data structure called symbol
table.

Dr. Galal Eldin Abbas Computer Science Department Page 18 of 21


Compiler Design CS224 Chapter 1: Compiler definition

Intermediate Representation (IR).


An intermediate representation is a representation of a program “between” the source and target
languages. A good IR is one that is fairly independent of the source and target languages, so that it
maximizes its ability to be used in a retargetable compiler.

We use intermediate representations for at least four reasons:


1. Because translation appears to inherently require analysis and synthesis.
2. To break the difficult problem of translation into two simpler, more manageable pieces.
3. To build retargetable compilers:
o We can build new back ends for an existing front end (making the source language more
portable across machines).

Dr. Galal Eldin Abbas Computer Science Department Page 19 of 21


Compiler Design CS224 Chapter 1: Compiler definition

o We can build a new front-end for an existing back end (so a new machine can quickly get a
set of compilers for different source languages).
o We only have to write 2n2n half-compilers instead of n(n−1)n(n−1) full compilers. (Though
this might be a bit of an exaggeration in practice!)
4. To perform machine independent optimizations.

Dr. Galal Eldin Abbas Computer Science Department Page 20 of 21


Compiler Design CS224 Chapter 1: Compiler definition

Synthesis part
• Synthesis part takes the intermediate representation as input and transforms it to the target
program.
• It is also termed as back end of compiler.

The design of compiler can be decomposed into several phases, each of which converts one form of
source program into another.

Dr. Galal Eldin Abbas Computer Science Department Page 21 of 21

You might also like