Vous êtes sur la page 1sur 28

COMPILER DESIGN

JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION COMPILER CONSTRUCTION
SCANNING

Principles and Methods

Johnpaul C I

Compiler Design(IS0426)

Department of Information Science and Engineering


National Institute of Engineering, Mysore

August 2015

JOHNPAUL C I () COMPILER DESIGN August 2015 1 / 28


COMPILER DESIGN
Contents
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

Why Compiler....
Introduction
Translation Process
Scanning

JOHNPAUL C I () COMPILER DESIGN August 2015 2 / 28


COMPILER DESIGN

JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

Never ever depend on PPTS, They are just a tool


to organise the pointers of subject. Read text books
and your class notes for better understanding

JOHNPAUL C I () COMPILER DESIGN August 2015 3 / 28


COMPILER DESIGN
Contents
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

Why Compiler....
Introduction
Translation Process
Scanning

JOHNPAUL C I () COMPILER DESIGN August 2015 4 / 28


COMPILER DESIGN
Why Compiler....
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

To make a machine understandable language.


Machine understandable language is the Binary
language(Object Code).
There by asking the machine to do predefined
set of operations.
It is analogous to the interaction of human
beings in their own languages to carry out their
daily routines.

JOHNPAUL C I () COMPILER DESIGN August 2015 5 / 28


COMPILER DESIGN
Why compiler ....
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

It is a natural process where a human being


understands his language.
But it is a gradual process of learning based on
the understanding capability of our brain.
We are adopting this natural learning process
to create a machine understandable language.
It is done by the compiler.
Hence it is a highly complex task.

JOHNPAUL C I () COMPILER DESIGN August 2015 6 / 28


COMPILER DESIGN
The Dragon Book ....
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

Figure: 2 The Cover page of Dragon book

A world famous compiler design book by Alfred


V Aho and J. D. Ullmann
The dragon shows the complexity in designing
the compilers.

JOHNPAUL C I () COMPILER DESIGN August 2015 7 / 28


COMPILER DESIGN
Contents
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

Why Compiler....
Introduction
Translation Process
Scanning

JOHNPAUL C I () COMPILER DESIGN August 2015 8 / 28


COMPILER DESIGN
Text book and Reference books .....
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

Text Book:
Compiler Construction: Principles and Practice
by Kenneth C Louden, 1997 edition
Reference Books:
Compilers Principles, Techniques and Tools by
Aho, J. D. Ullman,Monica S Lam
Compiler Construction, by Niklaus Wirth

JOHNPAUL C I () COMPILER DESIGN August 2015 9 / 28


COMPILER DESIGN
Introduction
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

Figure: A basic functionality of a Compiler

Compiler: It is also a program.


Source programs: It can be in any high level
languages like C, C++ etc
Target programs: It is a machine specific
Object code.
Basically Compiler will make the machine
understand about human programming.

JOHNPAUL C I () COMPILER DESIGN August 2015 10 / 28


COMPILER DESIGN
History of Compiler programs...
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
Late in 1940s....After the stored program
SCANNING concept of John Von Newman
Initially Binary language is used to make the
machine to perform actions !!!
It is not human readable and not at all
user-friendly
Then Assembly language is used (eg MOV AX,
BX)
It has also got demerits
Lastly high level languages came which is
human friendly and not machine friendly
To make it machine friendly Compilers
originated

JOHNPAUL C I () COMPILER DESIGN August 2015 11 / 28


COMPILER DESIGN
History of Compiler Programs...
JOHNPAUL C I

WHY COMPILER??
High-level languages Human friendly
INTRODUCTION
TRANSLATION
languages
SCANNING
Similar to the communication language of
human beings, these high level languages also
have their own origin, grammar and usage rules.
Noam Chomsky did a great research in the
grammar and usage scenarios of these High
level languages.
He classified the grammar of Programming
languages can be in the form of four basic
groups
I Type 0 REL - Recursive Enumerated
LanguageTuring Machine
I Type 1 CSL - Context Sensitive Language
Linear Bounded Automata
I Type 2 CFL - Context Free Language PDA
I Type 3 Regular Language Finite Automata
JOHNPAUL C I () COMPILER DESIGN August 2015 12 / 28
COMPILER DESIGN
History of Compiler Programs...
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION Each of These Languages have their own
SCANNING
associated grammar
I Type 0 - REL REG
I Type 1 - CSL CSG
I Type 2 - CFL CFG
I Type 3 - RL RG
The Language of our interest will be Type 2
(Context Free Language)
Since most of the programming languages will
be coming in this group, CFG will be also a
part of our exploration
Regular expressions are one of the
representation of these programming languages

JOHNPAUL C I () COMPILER DESIGN August 2015 13 / 28


COMPILER DESIGN
Instances of Compiler Linked Programs...
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING
Interpreters Python, Lisp
Assemblers as
Linkers Library and header files
Loaders Memory allocation etc
Preprocessors Unfold the source code as much
as possible
Debuggers gdb
Profilers memory usage
Project Managers Manage and monitor
projects

JOHNPAUL C I () COMPILER DESIGN August 2015 14 / 28


COMPILER DESIGN
Contents
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

Why Compiler....
Introduction
Translation Process
Scanning

JOHNPAUL C I () COMPILER DESIGN August 2015 15 / 28


COMPILER DESIGN
Translation process
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

Translation Word meaning itself is converting


a language from one form to another
In compilers Translation means converting the
high level language in to target code
Translation has a well defined sequence of steps
These steps includes the elements of Finite
Automta, Data structures, Programming,
Algorithms etc.

JOHNPAUL C I () COMPILER DESIGN August 2015 16 / 28


COMPILER DESIGN
Translation Process..
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

JOHNPAUL C I () COMPILER DESIGN August 2015 17 / 28


COMPILER DESIGN
Highlights of each phases
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING Scanner Lexical Analysis Tokens
Parser Parse tree creations Syntax tree
Semantic Analysis Meaning of statements
Annotated tree
Source Code Optimizer Which modifies the
Source code Optimized source code
Code generator Generated Assembly
Language Instructions Assembly
language(Target code)
Target Code Optimizer Optimizes the
assembly language Optimized Target code

JOHNPAUL C I () COMPILER DESIGN August 2015 18 / 28


COMPILER DESIGN
Data structures in translation process...
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

Tokens From lexical analysis


Syntax Tree From parser
Symbol table For storing the tokens
Literal table For storing constants
Intermediate code
Temporary files

JOHNPAUL C I () COMPILER DESIGN August 2015 19 / 28


COMPILER DESIGN
Contents
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

Why Compiler....
Introduction
Translation Process
Scanning

JOHNPAUL C I () COMPILER DESIGN August 2015 20 / 28


COMPILER DESIGN
Scanning: The lexical analysis...
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

Figure: Scanning process

The primary step in compilation


Reading the source program as a file of
characters and dividing it into tokens
JOHNPAUL C I () COMPILER DESIGN August 2015 21 / 28
COMPILER DESIGN
Scanning process......
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING The basic mechanism of tokenisation is pattern
matching.
The most common token classifications are
I Reserved words IF, ELSE, WHILE, DO, ......
I Special Symbols PLUS, MINUS, MUL, DIV,
.....
I Multiple Strings NUM, ID
The value of IF token is if, similarly THEN is
then,....
To avoid the ambiguity in their token name and
value, usually the if, then, else.... etc are known
as lexemes.

JOHNPAUL C I () COMPILER DESIGN August 2015 22 / 28


COMPILER DESIGN
Scanning process
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION Any value associated with a token is called
SCANNING
attributes
For instance if we type any number say 5655.
That character sequence will be a string 5655.
Apart from that string value it also has a
numeric value of five thousand six hundred and
fifty five; ie a numeric value.
If we type +, It has a character value + and
operative meaning of addition
These string values and numeric values are
known as attributes.
A token can have as many number of attributes.

JOHNPAUL C I () COMPILER DESIGN August 2015 23 / 28


COMPILER DESIGN
Scanning process...
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

In a high level view these tokens can be


compared to Structures in C.
Since every token has their own name and
values of different types, structure data type
will be best suited for them.
A scanner will identify as much as attributes
from the token and stores it in the symbol table.
Some attributes can also be calculated from
other attributes.

JOHNPAUL C I () COMPILER DESIGN August 2015 24 / 28


COMPILER DESIGN
Scanning process...
JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

gettoken()....skip...gettoken()....gettoken()

Scanning process in action


gettoken() function is called again and again to
scan and store the tokens.

JOHNPAUL C I () COMPILER DESIGN August 2015 25 / 28


COMPILER DESIGN
Role of Finite automata and RE in
JOHNPAUL C I

WHY COMPILER??
Scanning...
INTRODUCTION
TRANSLATION
SCANNING
Scanning will give rise to tokens.
It is made possible by pattern matching.
For pattern matching Regular Expressions are
used.
So to give life to regular expression based
pattern matching, there should be a Finite
Automata.
Finite Automata will tell the compiler designer
where to use the Conditions, from which input
where to go..
Finally he will convert the Finite Automata in
to a scanning program.

JOHNPAUL C I () COMPILER DESIGN August 2015 26 / 28


COMPILER DESIGN

JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

will be updated soon ......

JOHNPAUL C I () COMPILER DESIGN August 2015 27 / 28


COMPILER DESIGN

JOHNPAUL C I

WHY COMPILER??
INTRODUCTION
TRANSLATION
SCANNING

THANK YOU.

JOHNPAUL C I () COMPILER DESIGN August 2015 28 / 28

Vous aimerez peut-être aussi