Principles of Compiler Design
Document Sample


Principles of Compiler Design
P. A. Ozarkar
410444 Principles of Compiler Design
Syllabus
1. Introduction to Compiler
Translator issues, why to write compiler,
compilation process in brief, front end and
backend model, compiler construction tools,
Interpreter and the related issues, Cross compiler,
Incremental compiler, Boot strapping.
Lexical Analysis
Review of lexical analysis: alphabet, token,
lexical error, Block schematic of lexical analyser,
Automatic construction of lexical analyser (LEX),
LEX specification details.
Unit 1 Introduction to Compiler 2
2. Syntax Analysis
Introduction: Role of parsers, Parsing
technique: Top down-RD parser, Predictive LL (k)
parser, Bottom up-shift-Reduce, SLR, LR(k), LALR
etc. using ambiguous grammars, Error detection
and recovery, Automatic construction of parser
(YACC), YACC specifications.
semantic analysis
Need of semantic analysis, type checking and
type conversation.
Unit 1 Introduction to Compiler 3
3. Syntax directed translation
Syntax directed definitions, construction of
syntax trees, bottom-up evaluation of S-attribute
definition, L-attributed definition , Top-down
translation, Bottom-up evaluation of inherited
attributes.
Intermediate code Generation:
Intermediate code generation for declaration,
assignment, iterative statements, case
statements, arrays, structures, conditional
statements, Boolean expressions, procedure calls,
Intermediate code Generation using YACC
Unit 1 Introduction to Compiler 4
4. Run Time Storage Organisation
Storage allocation strategies, static, dynamic
storage allocation, allocation strategies for block
structured and non-block structured languages;
O.S. support required for IO statements. (e.g.
printf, scanf) and memory allocation deallocation
related statement. (e.g. new, malloc)
Unit 1 Introduction to Compiler 5
5. Code Generation
Introduction: Issues in code generation, Target
machine description, Basic blocks and flow
graphs, next use representation of basic blocks,
Peephole optimisation, DAG generating code from
a DAG, Dynamic programming, Code generator-
generator concept.
Unit 1 Introduction to Compiler 6
6. Code Optimisation
Introduction, classification of Optimisation,
principle sources of Optimisation, m/c dependent
Optimisation, m/c independent optimisation,
Optimisation of basic blocks, loops in flowgraphs,
Optimising transformation: compile time
evaluation, Common sub-expression elimination,
variable propagation, code Movement, strength
reduction, dead code elimination and loop
optimisation, local optimisation, DAG based local
optimisation. Global optimisation: control and data
flow analysis, control flow analysis-concepts and
definition, data flow analysis, data flow analysis,
Computing data flow information, meet over
paths,Data flow equations. Iterative data flow
analysis: Available exprns, live range identification.
Unit 1 Introduction to Compiler 7
Reference Books
1. Aho, A. V., R. Sethi and J. D. Ullman, Compiler
principle, techniques and tools-, Addison wesley.
2. Barrent W. A., J. D. Couch, Compiler construction
theory and practice-, Computer science series, Asian
student edition.
3. Dhamdhere D. M., Compiler construction principle and
practice-, Mac. Millan India, New Delhi.
4. Gress D.. Compiler construction for digital computer,
Wiley New York.
5. Holub A. J., Compiler design in C-Printice Hall.
6. Tremblay J. P. and R G. Sorenson Theory and Practice
of compilers.
7. Lex and Yece-O'relly.
8. Muchnlk -Advanced compiler design & Implementation.
Unit 1 Introduction to Compiler 8
Definition
A compiler is a computer program (or set of
programs) that transforms source code
written in a programming language (the
source language) into another computer
language (the target language, often having a
binary form known as object code).
Compiler
Source Target
code code
Errors
Unit 1 Introduction to Compiler 9
Why ?
The most common reason for wanting to
transform source code is to create an
executable program.
Unit 1 Introduction to Compiler 10
The Analysis-Synthesis Model
of Compilation
There are two parts to compilation:
Analysis determines the operations
implied by the source program which are
recorded in a tree structure
Synthesis takes the tree structure and
translates the operations therein into the
target program
Unit 1 Introduction to Compiler 11
Other Tools that Use the
Analysis-Synthesis Model
Editors (syntax highlighting)
Pretty printers (e.g. Doxygen)
Static checkers (e.g. Lint and Splint)
Interpreters
Text formatters (e.g. TeX and LaTeX)
Silicon compilers (e.g. VHDL)
Query interpreters/compilers (Databases)
Unit 1 Introduction to Compiler 12
Grouping of phases
Unit 1 Introduction to Compiler 13
A compiler consists of three main parts:
frontend,
middle-end,
backend.
Unit 1 Introduction to Compiler 14
Frontend
Frontend checks whether the program is
correctly written in terms of the programming
language syntax and semantics. Here legal
and illegal programs are recognized. Errors
are reported, if any, in a useful way. Type
checking is also performed by collecting type
information. Frontend generates IR
(intermediate representation) for the middle-
end. Optimization of this part is almost
complete so much are already automated.
There are efficient algorithms typically in O(n)
or O(nlog n).
Unit 1 Introduction to Compiler 15
Middle-end
Middle-end is where the optimizations for
performance take place. Typical
transformations for optimization are
1) removal of useless or unreachable code,
2) discovering and propagating constant
values
3) relocation of computation to a less
frequently executed place (e.g., out of a
loop), 3) specializing a computation based on
the context. Middle-end generates IR for the
following backend. Most optimization efforts
are focused on this part.
Unit 1 Introduction to Compiler 16
Backend
Backend is responsible for translation of
IR into the target assembly code. The
target instruction(s) are chosen for each
IR instruction. Variables are also selected
for the registers. Backend utilizes the
hardware by figuring out how to keep
parallel FUs busy, filling delay slots, and
so on. Although most algorithms for
optimization are in NP, heuristic
techniques are well-developed.
Unit 1 Introduction to Compiler 17
Analysis consists of 3 phases
Linear Analysis
Hierarchical Analysis
Semantic Analysis
Unit 1 Introduction to Compiler 18
cross compiler
A cross compiler is a compiler capable of
creating executable code for a platform
other than the one on which the compiler
is run.
Cross compiler tools are used to generate
executables for embedded system or
multiple platforms.
It is used to compile for a platform upon
which it is not feasible to do the compiling,
like microcontrollers that don't support an
operating system.
Unit 1 Introduction to Compiler 19
incremental compiler
The term incremental compiler may refer to
two different types of compiler.
Imperative programming
Interactive Programming
Unit 1 Introduction to Compiler 20
incremental compiler
In imperative programming and software development, an
incremental compiler is one that when invoked, takes only
the changes of a known set of source files and updates any
corresponding output files (in the compiler's target
language, often bytecode) that may already exist from
previous compilations. By effectively building upon
previously compiled output files, the incremental compiler
avoids the wasteful recompilation entire source files, where
most of the code remains unchanged. For most incremental
compilers, compiling a program with small changes to its
source code is usually near instantaneous. It can be said
that an incremental compiler reduces the granularity of a
language's traditional compilation units while maintaining
the language's semantics, such that the compiler can
append and replace smaller parts.
Unit 1 Introduction to Compiler 21
incremental compiler
In the interactive programming paradigm, and
particularly in Prolog related literature, an incremental
compiler refers to a compiler that is actually a part of
the runtime system of the source language. The
compiler can be invoked at runtime on some source code
or data structure managed by the program, which then
produces a new compiled program fragment that is then
immediately available for use by the runtime system.
This scheme allows for a degree of self-modifying code
and requires metaprogramming language features. The
ability to add, remove and delete code while running is
known as hot swapping. Some interactive programming
platforms mix the use of interpreted and compiled code
to achieve the illusion that any changes to code are
accessible by the program immediately.
Unit 1 Introduction to Compiler 22
Phases of a Compiler
Source Program
1
Lexical Analyzer
2
Syntax Analyzer
3
Semantic Analyzer
Symbol-table Error Handler
Manager
4 Intermediate Code
Generator
5
Code Optimizer
6
Code Generator
Target Program
Unit 1 Introduction to Compiler 23
Lexical Analysis
Easiest Analysis - Identify tokens which are the
basic building blocks
Position := initial + rate * 60 ;
All are tokens
Blanks, Line breaks, etc. are
scanned out
Unit 1 Introduction to Compiler 24
Block schematic
Unit 1 Introduction to Compiler 25
Examples of Token
Token: A sequence of characters to be treated as a
single unit.
• Examples of tokens.
– Reserved words (e.g. begin, end, struct, if etc.)
– Keywords (integer, true etc.)
– Operators (+, &&, ++ etc)
– Identifiers (variable names, procedure names,
parameter names)
– Literal constants (numeric, string, character constants
etc.)
– Punctuation marks (:, , etc.)
Unit 1 Introduction to Compiler 26
The End
Unit 1 Introduction to Compiler 27
Get documents about "