Beruflich Dokumente
Kultur Dokumente
Introduction to Compiler
Design
B.Sc.(CS)-4th Year, First
Semester
(Session-2015-16)
Dr. Deepak K.
Sinha, JIT, JU
Syllabus
Prerequisites: Formal Language and
Automata Theory
Textbook: Compilers: Principles, Techniques,
and Tools by Aho, Sethi, and Ullman, 2nd
edition
Other material: class handouts
Grade breakdown:
Exams (two midterm, one final) (30%+50%)
Lab/Project assignments (15%)
Assignment (5%)
Dr. Deepak K.
Sinha, JIT, JU
Objectives
Be able to build a compiler for a (simplified)
(programming) language
Know how to use compiler construction tools,
such as generators of scanners and parsers
Be familiar with assembly code and virtual
machines, such as the JVM, and bytecode
Be familiar with compiler analysis and
optimization techniques
Dr. Deepak K.
Sinha, JIT, JU
Introductions.
Programming languages are notations for
describing computations to people and to
machine.
The world as we know it depends on
programming language, because all the S/W
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 execute by computer.
Dr. Deepak K.
Sinha, JIT, JU
Dr. Deepak K.
Sinha, JIT, JU
Language Processors
Simply stated, a compiler is a program that
can read a program into one language-(the
source language) and translate it into an
equivalent program into another language(the target language)
Input
Source
Program
Dr. Deepak K.
Sinha, JIT, JU
Compiler
Error messages
Target
Program
Output
Dr. Deepak K.
Sinha, JIT, JU
Error messages
Output
Dr. Deepak K.
Sinha, JIT, JU
A Hybrid Compilers
10
Source Program
Translato
r
Intermediate
Program
Inputs
Dr. Deepak K.
Sinha, JIT, JU
VM
Output
11
Contd..
12
Dr. Deepak K.
Sinha, JIT, JU
Preprocessors, Compilers,
Assemblers, and Linkers
13
Source Code
Preprocessor
Modified Source Code
Compiler
Target Assembly Program
Assembler
Relocatable Object Code
Linker/Loader
Dr. Deepak K.
Sinha, JIT, JU
Libraries and
Relocatable Object Files
Dr. Deepak K.
Sinha, JIT, JU
14
15
Analysis
Synthesis
16
17
Contd
The synthesis part construct the desired
target program from the intermediate
representation and information in the symbol
table
Synthesis takes the tree structure and
translates the operations therein into the
target program
The analysis part is often called the front end
and the synthesis part is the back end of the
compiler.
Dr. Deepak K.
Sinha, JIT, JU
Character
stream
Lexical
Analyzer
Syntax
Analyzer
Token
Syntax
Stream
Tree
Symbol
Table
MachineCode
Dependent
Generator
Code Optimizer
IR
Target
machine
Target Machine
code
Deepak K.
CodeDr.
Sinha, JIT, JU
18
Semantic
Analyzer
Syntax
tree
Intermediat
e code
Generator
Intermediate
representation
MachineIndependent
Code
Optimizer
19
Code Generator
[Intermediate Code Generator]
Scanner
[Lexical Analyzer]
Tokens
Code Optimizer
Parser
[Syntax Analyzer]
Parse tree
Code Optimizer
Semantic Process
[Semantic analyzer]
Abstract Syntax Tree w/ Attributes
Dr. Deepak K.
Sinha, JIT, JU
Lexical Analysis
20
21
p=i+r*60
p is a lexeme that would be mapped into a
token <id, 1>, where id is an abstract symbol
standing for identifier and 1 points to the
symbol-table entry for p.
The symbol-table entry for an identifier holds
information about the identifier, such as its
name and type 1 p
-------2 i
3 r
symbol table
Dr. Deepak K.
Sinha, JIT, JU
22
23
Dr. Deepak K.
Sinha, JIT, JU
Syntax Analysis
24
25
Dr. Deepak K.
Sinha, JIT, JU
Semantic Analysis
The semantic analyzer uses the syntax tree
and the information in the symbol table to
check the source program for semantic
consistency with the language definition.
It also gathers type information and saves
it in either the syntax tree or the symbol
table, for subsequent use during
intermediate code generation.
Dr. Deepak K.
Sinha, JIT, JU
26
27
28
Dr. Deepak K.
Sinha, JIT, JU
Dr. Deepak K.
Sinha, JIT, JU
29
30
Code Optimization
The code optimization attempts to improve
the intermediate code so that better target
code will result.
Usually better means faster, but some times
shorter code or target code that consumes
less resources
Dr. Deepak K.
Sinha, JIT, JU
31
Code Generation
The code generation takes as input an
intermediate representation of the source
program and maps it into the target
language.
If the target language is machine code,
registers or memory location are selected
for each variables used by the program
Dr. Deepak K.
Sinha, JIT, JU
32
Compiler-Construction Tools
Software development tools are available to
implement one or more compiler phases
Parser generators:
That automatically produces syntax analyzer
from a grammatical description of a
programming language.
Scanner generators:
That produce lexical analyzers from a regularexpression description of the token of a
language.
Syntax-directed translation engines:
That produce collection of routines for walking a
Dr. Deepak K.
parse
Sinha,
JIT, JU tree and generating intermediate code.
33
34
Automatic code generators:
that produces a code generator from a
collection of rule for translating each operation
of the intermediate language into the machine
language for a target machine.
Data-flow engines:
That facilitate the gathering of information about
how values are transmitted from one part of a
program to each other part. This is key part of
code optimization.
-Compiler Construction toolkit:
That provides an integral set of the routine for
contracting various phases of a compiler
Dr. Deepak K.
Sinha, JIT, JU
Programming Language
Basics
Static/Dynamic Distinction:
35
Dr. Deepak K.
Sinha, JIT, JU
int i;
void f()
{
int i;
i=3;
}
x=x+i;
36
37
Dr. Deepak K.
Sinha, JIT, JU
B3
B2
B4
B1
38
39
Declaration
Scope
int a=1
B1-B3
int b=1
B1-B2
int b=2
B2-B4
int a=3
B3
int b=4
B4
Dr. Deepak K.
Sinha, JIT, JU
Passing Parameter
Mechanism
Call by Value
Call by Reference
Dr. Deepak K.
Sinha, JIT, JU
40