Introduction To Compiler Design: B.Sc. (SE) - 3rd Year (Session-2017-18)
Introduction To Compiler Design: B.Sc. (SE) - 3rd Year (Session-2017-18)
Introduction To Compiler Design: B.Sc. (SE) - 3rd Year (Session-2017-18)
B.Sc.(SE)-3rd Year
(Session-2017-18)
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) (35%+50%)
– Lab/Project assignments (10%)
– Attendence (5%)
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
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,
AASTU, Addis Ababa
5
• The S/W system that do this translation are called
Compilers.
• The study of Compilers Writing touches upon:
– Programming Languages
– Machine Architecture
– Language Theory
– Algorithms
– S/W Engineering
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 Target
Compiler
Program Program
Source
Program
Interpreter Output
Input
A Hybrid Compilers
Source Program
Translator
Intermediate Program
VM Output
Inputs
Preprocessor
Modified Source Code
Compiler
Target Assembly Program
Assembler
Relocatable Object Code
Linker/Loader Libraries and
Relocatable Object Files
Dr. Deepak K. Sinha,
AASTU, Addis Ababa
Target Machine Code
The structure of a compiler
14
Compiler
Analysis Synthesis
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.
Code Generator
[Intermediate Code Generator]
Tokens
Code Optimizer
Parser
[Syntax Analyzer]
Optimized Intermediate Code
Parse tree
Code Optimizer
Semantic Process
[Semantic analyzer] Target machine code
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,
AASTU, Addis Ababa
23
Syntax Analysis
• The second phase of the compiler is syntax
analysis or parsing
• The parser uses the first component of the tokens
produced by the lexical analyzer to create a tree-
like intermediate representation that depicts the
grammatical structure of the token stream
• A typical representation of a syntax tree in which
each interior node represents an operation and
the children of the node represent the arguments
of the operation.
Dr. Deepak K. Sinha,
AASTU, Addis Ababa
25
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.
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