1. CHAPTER # 01
INTRODUCTIONTO COMPILER
DEPARTMENT OF SOFTWARE ENGINEERING
SIR SYED UNIVERSITY OF ENGINEERING & TECHNOLOGY
COURSE INSTRUCTORS:
ENGR. FARHEEN QAZI
ENGR. SANOBER SOOMRO
2. COURSE SCOPE
Aim:
To learn techniques of a modern compiler
Main reference:
Alfred V. Aho, Monica S. Lam, Ravi Sethi & Jeffrey D. Ullman,
Compilers Principles, Techniques and Tools, Pearson, 2nd
Edition (2020), ISBN-13: 978-0321486813.
Supplementary references:
Torben Aegidius Mogensen, Introduction to compiler Design
(Springer), 2nd Edition (2017), ISBN: 978-3-319-66966-3.
4. TODAY’S AGENDA
Overview
Compiler definition
CompilerVS Interpreter
Program execution sequence
History of Compiler
Models of Compiler
Phases of compiler
Software Tools Performing Analysis
Types of compiler
5. COMPILER LEARNING
Isn’t it an old discipline?
Yes, it is a well-established discipline
Algorithms, methods and techniques are researched and
developed in early stages of computer science growth
There are many compilers around and many tools to
generate them automatically
So, why we need to learn it?
Although you may never write a full compiler
But the techniques we learn is useful in many tasks like
writing an interpreter for a scripting language, validation
checking for forms and so on
6. OVERVIEW
Programming languages are notations for describing
computations to people and to machines.
The world as we know it depends on programming languages,
because all the software running on all the computers was
written in some programming language.
But, before a program can be run, it must first be translated
into a form in which it can be executed by a computer.
The software systems that do this translation are called
Language Processing Systems (Compilers, Interpreters and
Assemblers etc.) or Translators and compiler is one of them.
7. CONTD….
This course is about how to design and implement compilers.
A few basic ideas can be used to construct translators for a
wide variety of languages and machines.
Besides compilers, the principles and techniques for compiler
design are applicable to so many other domains that they are
likely to be reused many times in the career of a computer
scientist.
The study of compiler writing touches upon programming
languages, machine architecture, language theory, algorithms,
and software engineering.
8. TERMINOLOGY
Compiler:
A program that converts instructions into a machine-
code or lower-level form so that they can be read and
executed by a computer.
OR
A compiler is a computer program that
translates computer code written in one programming
language (the source language) into another language (the
target language).
The name compiler is primarily used for programs that
translate source code from a high-level programming
language to a lower level language (e.g., assembly
language) to create an executable program.
9. ABSTRACTVIEW
Recognizes legal (and illegal) programs
Generate correct code
Manage storage of all variables and code
Agreement on format for object (or assembly) code
Source
code
Machine
code
Compiler
errors
10. DIFFERENCE BETWEEN COMPILER AND INTERPRETER
Interpreter Compiler
• Translates program one
statement at a time.
• Scans the entire program and
translates it as a whole into
machine code.
• Interpreters usually take less
amount of time to analyze the
source code. However, the
overall execution time is
comparatively slower than
compilers.
• Compilers usually take a large
amount of time to analyze the
source code. However, the
overall execution time is
comparatively faster than
interpreters.
• Programming languages like
JavaScript, Python, Ruby use
interpreters.
• Programming languages like C,
C++, Java use compilers.
13. MODEL OF A COMPILER
This model is collectively called the Analysis-Synthesis model
of compilation.
The analysis part is often called the front end (FE) of the
compiler.
The synthesis part is the back end (BE).
14. ANALYSIS OR FRONT END OF A COMPILER
Analysis element breaks the acknowledgment program into
an essential or characteristic part of something abstract.
pieces and imposes a grammatical ordering on them which
further uses this structure to create an intermediate version
of the source program.
It is also call as front end of compiler.
15. SYNTHESIS OR BACK END OF A COMPILER
Synthesis factor takes the intermediate explanation as input
and transforms it to the intended program.
It is also termed as back end of compiler.
16. Source Code
Lexical Analyzer
Syntax Analyzer
Semantic Analyzer
Intermediate Code Generator
Code Optimizer
Code Generator
Object Code
Symbol
Table
Manager
Error
Handler
Synthesis
Analysis
Synthesis
Tokens
Syntax Tree
Syntax Tree
Intermediate Representation
Intermediate
Representation
PHASES OF COMPILER
17. LEXICAL ANALYZER
Lexical analysis is the first phase of compiler which is also termed
as scanning.
Source program is scanned to read the stream of characters
and those characters are grouped to form a sequence called
lexemes which produces token as output.
Example c=a+b*5;
Token Template:
<token-name, attribute-value>
<id1, c> <=> <id2, a> <+> <id3, b> < *> <num, 5> <; >
18. SYNTAX ANALYZER
Syntax analysis is the second phase of compiler which is
also called as parsing.
Parser converts the tokens gave by lexical analyzer into a
tree like version called parse tree.
A parse tree describes the syntactic structure of the input.
Example: int a
int int a
19. SEMANTIC ANALYZER
It checks for the semantic consistency.
Type information is gathered and stored in symbol table or
in syntax tree.
Performs type checking.
Example : int a;
a=6;
int a;
a=3.142;
20. INTERMEDIATE CODE GENERATOR
Intermediate code nature produces intermediate
representations for the source program.
It generates three address code in which every memory
location acts like register
Example : t1 = inttofloat (5)
t2 = id3* t1
t3 = id2 + t2
id1 = t3
21. CODE OPTIMIZER
Code optimization phase gets the intermediate code as
input and produces optimized intermediate code as output.
It results in faster running machine code.
It can be done by reducing the number of lines of code for
a program.
23. TARGET CODE GENERATOR
Code category is the final phase of a compiler.
It gets input from code optimization phase and produces the
target code as result.
Intermediate instructions are translated into a sequence of
machine instructions that perform the same task.
25. SYMBOLTABLE MANAGER
Important Functions:
Record Identifiers used in program
Collect info about various attributes of each identifier
.
Stores:
Storage Location
Type
Scope
Procedure name
No. of type of arguments
ReturnType
Symbol table is data structure containing record for each
identifiers
with fields for attributes of identifiers.
It is prepared at lexical analysis and later phases add information.
26. ERROR DETECTION & REPORTING
Each phase can encounter errors.
Compilation can proceed only after solving errors generated
by lexical, syntax & semantic analysis.
If code doesn’t form tokens, structure is violated which is
detected by lexical analysis.
If syntax is violated, error is detected by syntax phase.
During semantic analysis, compiler tries to detect constructs
that have right syntactic structure.
27. SOFTWARETOOLS PERFORMINGANALYSIS
Software tools other than compilers that perform analysis
on Source Program are:
Structure Editors: Inputs a sequence of commands to build
a source program.
The structure editor performs text creation, modification
and analyzes program text for putting an appropriate
hieratical structure on the source program.
E.g. Matching do-while and parenthesis { }
28. CONTD….
Pretty Printers:
Analyzes a program and prints it in such a way that the
structure of program becomes clearly visible.
i.e. Indentation
Static Checkers:
Reads a program, analyzes it and attempts to discover
potential bugs without running the program.
E.g. Parts of source program that can never be executed
and using variables without being defining them.
Works on dry run techniques.
29. TYPES OF COMPILER
The types of compiler are
Single Pass Compilers
Two Pass Compilers
Multi-pass Compilers
30. SINGLE PASS COMPILER
If we combine or group all the phases of compiler design in
a single module known as single pass compiler.
A one pass/single pass compiler is that type of compiler that
passes through the part of each compilation unit exactly
once.
Single pass compiler is faster and smaller than the multi pass
compiler.
32. TWO PASS COMPILER
A two pass compiler is a type of compiler that processes
the source code or abstract syntax tree of a program multiple
times.
In two pass compiler we divide phases in two pass.
34. FIRST PASS OFTWO PASS COMPILER
In first pass the included phases are work as front end and
analytic part means all phases analyze the High level
language and convert them three address code.
First pass is platform independent because the output of
first pass is as three address code which is useful for every
system and the requirement is to change the code
optimization and code generator phase which are comes
to the second pass.
35. SECOND PASS OFTWO PASS COMPILER
Second Pass the included phases work as back end and
the synthesis part refers to taking input as three address
code and convert them into Low level language/assembly
language.
Second pass is platform dependent because final stage of a
typical compiler converts the intermediate representation
of program into an executable set of instructions which is
dependent on the system.
36. MULTI PASS COMPILER
If we want to design a compiler for different programming
language for same machine. In this case for each
programming language there is requirement of making Front
end/first pass for each of them and only one Back
end/second pass.
38. MULTI PASS COMPILER
If we want to design a compiler for same programming
language for different machine/system. In this case we make
different Back end for different Machine/system and make
only one Front end for same programming language.