SlideShare a Scribd company logo
1 of 87
Compiler Design
Unit 1
Course Objectives
• The student should be made to:
• To learn the various phases of compiler
• To learn the various parsing techniques
• To understand intermediate code generation and
run-time environment
• To learn to implement front-end of the compiler
• To learn to implement code generator
Course Outcomes
• At the end of the course, the student should be able to:
• Explain the process of compilation and building lexical
analyzer with the help of RE, FA and Lex tool
• Describe the process of syntax analysis and build a parser
using top-down method
• Design a parser using bottom-up method and YACC tool
• Develop semantic analyzer for type checking and
intermediate code generator for translating the source
program into an intermediate code
• Explain the principles of runtime environment and issues
in code generation
• Apply various compiler optimization techniques for code
generation
Topic 1
Translators
1.1 Introduction
• Interpreter
• Compiler
1.2 Compilation and Interpretation
• Compiler: It is a computer program 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).
• The most common reason for wanting to
transform source code is to create an
executable program.
Compilation and Interpretation
• Interpreter: In computing, an interpreter is a
computer program that reads the source code
of another computer program and executes
that program directly.
• It is interpreted line by line
• It is a much slower way of running a program
Compiler
Executable
Program
Source
Program
Executable
Program
Data Output
Errors
Interpreter
Source
Program
Data
Output
Compiler Vs. Interpreter
Difference Between Compiler and
Interpreter
S. No Compiler Interpreter
1 Translates source program into an
executable (machine readable) program
Converts a source program and
executes it at the same time
2 Compiles all the statements together
(whole compilation)
Interprets every statement
separately (line by line)
3 Input is given only to the executable file Source program and input data are
given together to an interpreter
4 More space (object code is generated) Requires less space (doesn’t
generate intermediate object code)
5 Fast execution Slower execution
6 E.g. FORTRAN, COBOL, C, C++, Pascal E.g. Lisp, Perl, Python
Classifications of Compilers
• Classified based on construction and
functional behavior:
i. Single pass
ii. Multi pass (Construction)
iii. Load and Go
iv. Debugging and Optimizing – (Functional)
Classifications of Compilers
• The basic tasks that any compiler must
perform are essentially the same.
• By understanding these tasks, we can
construct compilers for a wide variety of
source languages and target machines using
the same basic techniques.
Analysis – Synthesis Model of Compilation
• There are two parts of Compilation:
1. Analysis: It breaks up the source program into
constituent pieces and creates an intermediate
representation of the source program.
2. Synthesis: It constructs the desired target
program from the intermediate representation.
Two Pass Compiler
Analysis – Synthesis Model of Compilation
• During analysis, the operations implied by the
source program are determined and recorded
in a hierarchical structure called a tree.
• Often, a special kind of tree called a syntax
tree is used.
• In syntax tree each node represents an
operation and the children of the node
represent the arguments of the operation.
Analysis – Synthesis Model of Compilation
• For example, a syntax tree of an assignment
statement is shown below.
Topic 3
Language Processors
1.3 The Context of a Compiler
• In addition to a compiler, several other
programs may be required to create an
executable target program.
Language-Processing System
Skeleton Source Program
Pre-Processor
1
Compiler
2
Assembler
3
Relocatable
Machine Code
4
Loader
Link/Editor
5
Absolute machine code
(Executable)
Library,
relocatable
object files
Source program
Target Assembly
program
Topic 4 & 5
Phases of a Compiler
Errors Encountered in Different
Phases
1.4 The Phases of a Compiler
• A compiler operates in phases.
• Each of which transforms the source program
from one representation to another.
• A typical decomposition of a compiler is
shown in fig given below:
24
Lexical
Analysis
Syntax
Analysis
Symbol Table
Semantic
Analysis
Intermediate
Code
Generation
Code
Optimization
Code
Generation
Error Handler
Source
Program
Target
Program
The
Phases of
a
Compiler
https://www.youtube.com/watch?v=P1bQUyl__t0&pp=ygUbcGhhc2VzIG9mIGNvbXBpbGVyIGFu
aW1hdGVk
Lexical or Linear Analysis/ Scanning
Source Program
Lexical Analysis
Tokens
The Phases of a Compiler:
1. Linear Analysis:
• In which the stream of characters making up the
source program is read from left-to-right and
grouped into tokens that are sequences of
characters having a collective meaning.
• The blanks separating the characters of these
tokens would normally be eliminated during the
lexical analysis.
28
The Phases of a Compiler:
• In a compiler, linear analysis is called lexical
analysis or scanning.
• For example, in lexical analysis the characters
in the assignment statement
Position: = initial + rate * 60
• Would be grouped into the following tokens:
29
The Phases of a Compiler
• The identifier, position.
• The assignment symbol :=
• The identifier initial.
• The plus sign.
• The identifier rate.
• The multiplication sign.
• The number 60.
30
The Phases of a Compiler
• The character sequence forming a token is
called the lexeme for the token.
Syntax or Hierarchical Analysis/ Parsing
Tokens
Syntax Analysis
Syntax Tree
The Phases of a Compiler
2. Hierarchical Analysis:
• In which characters or tokens are grouped
hierarchically into nested collections with
collective meaning.
The Phases of a Compiler
• Hierarchical analysis is called parsing or syntax
analysis.
• It involves grouping the tokens of the source
program into grammatical phases that are
used by the compiler to synthesize output.
• The grammatical phrases of the source
program are represented by a parse tree.
The Phases of a Compiler
35
identifier
identifier
expression
identifier
expression
number
expression
expression
expression
assignment
statement
position
:=
+
*
60
initial
rate
The Phases of a Compiler
• In the expression initial + rate * 60, the phrase
rate * 60 is a logical unit because the usual
conventions of arithmetic expressions tell us
that the multiplication is performed before
addition.
• Because the expression initial + rate is
followed by a *, it is not grouped into a single
phrase by itself.
36
The Phases of a Compiler
• The hierarchical structure of a program is
usually expressed by recursive rules.
• For example, we might have the following
rules, as part of the definition of expression:
37
The Phases of a Compiler
• Any identifier is an expression
• Any number is an expression
• If expression1 and expression2 are expressions,
then so are,
– Expression1 + expression2
– Expression1 * expression2
– (Expression1)
38
The Phases of a Compiler
• Example for statements:
1. If identifier1 is an identifier and expression2 is an
expression, then identifier1 = expression2 is a
statement.
2. If expression1 is an expression and statement2 is
a statement, then the following are statements:
while ( expression1 ) statement2
if ( expression1 ) statement2
Semantic Analysis
Syntax Tree
Semantic Analysis
Annotated Parse Tree
The Phases of a Compiler
3. Semantic Analysis:
• In which certain checks are performed to ensure
that the components of a program fit together
meaningfully.
• The semantic analysis phase checks the source
program for semantic errors and gathers type
information for the subsequent code-generation
phase.
41
The Phases of a Compiler
• It uses the hierarchical structure determined
by the syntax-analysis phase to identify the
operators and operand of expressions and
statements.
• An important component of semantic analysis
is type checking.
• The compiler checks that each operator has
operands that are permitted by the source
language specification.
42
The Phases of a Compiler
• For example, when a binary arithmetic
operator is applied to an integer and real. In
this case, the compiler may need to be
converting the integer to a real. As shown in
figure given below:
43
Intermediate Code Generation
Annotated Parse Tree
Intermediate Code Generation
Intermediate Code
(3 Address Code)
The Phases of a Compiler
4. Intermediate Code Generation:
• After Syntax and semantic analysis, some
compilers generate an explicit intermediate
representation of the source program.
• We can think of this intermediate representation
as a program for an abstract machine.
45
The Phases of a Compiler
• This intermediate representation should have
two important properties;
• it should be easy to produce
• easy to translate into the target program
• We consider an intermediate form called
“three address code,” which is like the
assembly language for a machine in which
every memory location can act like a register.
46
The Phases of a Compiler
• Three-address code consists of a sequence of
instructions, each of which has at most three
operands.
• The source statement Position: = initial + rate
* 60 might appear in three-address code as.,
temp1 := inttoreal (60)
temp2 := id3 * temp1
temp3 := id2 + temp2
id1 := temp3
47
The Phases of a Compiler
• The I.R. has several properties:
• Each 3 address instruction has at most one
operator in addition to the assignment.
• The compiler must generate a temporary name to
hold the value computed by each instruction.
Code Optimization
Intermediate Code
Code Optimization
Optimized Intermediate Code
The Phases of a Compiler
5. Code Optimization:
• This phase attempts to improve the intermediate
code, so that faster-running machine code will
result.
• Some optimizations are trivial.
50
The Phases of a Compiler
• For example, a natural algorithm generates
the intermediate code, using an instruction for
each operator in the tree representation after
semantic analysis, even though there is a
better way to perform the same calculation,
using the two instructions.
51
The Phases of a Compiler
• temp1 := id3 * 60.0
• id1 := id2 + temp1
• There is nothing wrong with this simple
algorithm, since the problem can be fixed
during the code-optimization phase.
52
The Phases of a Compiler
• That is, the compiler can deduce that the
conversion of 60 from integer to real
representation can be done once and for all at
compile time, so the inttoreal operation can
be eliminated.
53
The Phases of a Compiler
• Besides, temp3 is used only once, to transmit
its value to id1. It then becomes safe to
substitute id1 for temp3, whereupon the last
statement of I.R. is not needed and the code
above results.
• Significant fraction of amount is spent in
compiler on this phase.
54
Code Generation
Optimized Intermediate Code
Code Generation
Assembly Program/ Machine
Program
The Phases of a Compiler
6. Code Generation:
• The final phase of the compiler is the generation
of target code consisting normally of relocatable
machine code or assembly code.
56
The Phases of a Compiler
• Memory locations are selected for each of the
variables used by the program.
• Then, intermediate instructions are each
translated into a sequence of machine
instructions that perform the same task.
• A crucial aspect is the assignment of variables
to registers.
57
The Phases of a Compiler
• For example, using registers 1 and 2, the
translation of the optimized I.C might
become.,
MOVF id3, r2
MULF #60.0, r2
MOVF id2, r1
ADDF r2, r1
MOVF r1, id1
58
The Phases of a Compiler
• The first and second operands of each
instruction specify a source and destination,
respectively.
• The F in each instruction tells us that
instructions deal with floating-point numbers.
• This code moves the contents of the address
id3 into register 2, and then multiplies it with
the real-constant 60.0.
59
The Phases of a Compiler
• The # signifies that 60.0 is to be treated as a
constant.
• The third instruction moves id2 into register 1
and adds to it the value previously computed
in register 2
• Finally, the value in register 1 is moved into
the address of id1.
60
The Phases of a Compiler
• Symbol Table Management:
• An essential function of a compiler is to record the
identifiers used in the source program and collect
information about various attributes of each
identifier.
• These attributes may provide information about
the storage allocated for an identifier, its type, its
scope.
61
The Phases of a Compiler
• The symbol table is a data structure containing a
record for each identifier with fields for the
attributes of the identifier.
• When an identifier in the source program is
detected by the lexical analyzer, the identifier is
entered into the symbol table.
• However, the attributes of an identifier cannot
normally be determined during lexical analysis.
62
The Phases of a Compiler
• For example, in a Pascal declaration like
• Var position, initial, rate : real;
• The type real is not known when position, initial
and rate are seen by the lexical analyzer.
• The code generator typically enters and uses
detailed information about the storage assigned
to identifiers.
63
The Phases of a Compiler
• The remaining phases gets information about
identifiers into the symbol table and then use this
information in various ways.
• For example, when doing semantic analysis and
intermediate code generation, we need to know
what the types of identifiers are, so we can check
that the source program uses them in valid ways,
and so that we can generate the proper
operations on them.
64
The Phases of a Compiler
• Error Detection and Reporting:
• Each phase can encounter errors.
• However, after detecting an error, a phase must
somehow deal with that error, so that compilation
can proceed, allowing further errors in the source
program to be detected.
• A compiler that stops when it finds the first error
is not as helpful as it could be.
65
The Phases of a Compiler
• The syntax and semantic analysis phases
usually handle a large fraction of the errors
detectable by the compiler.
• Errors where the token stream violates the
structure rules (syntax) of the language are
determined by the syntax analysis phase.
• The lexical phase can detect errors where the
characters remaining in the input do not form
any token of the language.
66
Phase Output Sample
Programmer (source code producer) Source string A=B+C;
Scanner (performs lexical analysis) Token string ‘A’, ‘=’, ‘B’, ‘+’, ‘C’, ‘;’
And symbol table with names
Parser (performs syntax analysis
based on the grammar of the
programming language)
Parse tree or abstract syntax
tree
;
|
=
/ 
A +
/ 
B C
Semantic analyzer (type checking,
etc)
Annotated parse tree or abstract
syntax tree
Intermediate code generator Three-address code, quads, or
RTL
int2fp B t1
+ t1 C t2
:= t2 A
Optimizer Three-address code, quads, or
RTL
int2fp B t1
+ t1 #2.3 A
Code generator Assembly code MOVF #2.3,r1
ADDF2 r1,r2
MOVF r2,A
Peephole optimizer Assembly code ADDF2 #2.3,r2
MOVF r2,A
Phases of a Compiler: An Example
Topic 6
The Grouping of Phases
69
Front and Back Ends
• The phases are collected into a front end and
a back end.
• The front end consists of those phases that
depend primarily on the source language and
are largely independent of the target machine.
70
Front and Back Ends
• These normally include lexical and syntactic
analysis, the creating of the symbol table,
semantic analysis, and the generation of
intermediate code.
• A certain amount of code optimization can be
done by the front end as well.
• The front end also includes the error handling
that goes along with each of these phases.
71
Front and Back Ends
• The back end includes those portions of the
compiler that depend on the target machine.
• And generally, these portions do not depend
on the source language, depend on just the
intermediate language.
72
Front and Back Ends
• In the back end, we find aspects of the code
optimization phase, and we find code
generation, along with the necessary error
handling and symbol table operations.
73
Front and Back Ends
For
Different
Machines
One High
Level
Language
Front
End
Back End Back End
Front and Back Ends
Front End Front End Front End
I.R. I.R. I.R.
Back End
Front End for different
languages
Intermediate
Representation of
different languages
(Same for all
languages)
Common Back End for
different languages
I.R. Stands for Intermediate
Representation
Front End : Analysis + Intermediate Code Generation
Back End : Code Generation + Optimization
vs.
Number of Passes:
A pass: requires r/w intermediate files
Fewer passes: more efficiency.
However: fewer passes require more sophisticated
memory management and compiler phase
interaction.
Front and Back Ends
Passes
• Several phases of compilation are usually
implemented in a single pass consisting of
reading an input file and writing an output
file.
• In practice, there is a great variation in the
way the phases of a compiler are grouped into
passes, so we prefer to organize our discussion
of compiling around passes rather than
phases.
77
Reducing the no. of Passes
• It is desirable to have relatively a few passes,
since it takes time to read and write
intermediate files.
• On the other hand, if we group several phases
into one pass, we may be forced to keep the
entire program in memory, because one phase
may need information in a different order
than a previous phase produces it.
78
Reducing the no. of Passes
• Grouping into one pass has the following
problems:
• Interface between lexical and syntax analyzers can
often be limited to a single token.
• On the other hand, it is very hard to perform code
generation until the IR has been completely
generated.
• The concept of back patching is used.
79
Topic 7
Compiler Construction Tools
Compiler Construction Tools
• The compiler writer, like any programmer, can
profitably use tools such as,
• Debuggers,
• Version managers,
• Profilers and so on.
• In addition to these software-development
tools, other more specialized tools have been
developed for helping implement various
phases of a compiler.
81
Compiler Construction Tools
• The following is a list of some useful compiler-
construction tools:
i. Parser generators
ii. Scanner generators
iii. Syntax directed translation engines
iv. Automatic code generators
v. Data-flow engines
85
Compiler Construction Tools
i. Parser generators
• These produce syntax analyzers, normally from
input that is based on a context-free grammar.
• In early compilers, syntax analysis consumed not
only a large fraction of the running time of a
compiler, but a large fraction of the intellectual
effort of writing a compiler.
• This phase is considered one of the easiest to
implement.
86
Compiler Construction Tools
• Parser Generators
• Input: context-free grammar
• Output: syntax analyzer
• The task of the syntax analyzer is to produce a
representation of the source program in a
form directly representing its syntax structure.
• This representation is usually in the form of a
binary tree or similar data structure.
Compiler Construction Tools
ii. Scanner generators:
• These tools automatically generate lexical
analyzers, normally from a specification based on
regular expressions.
• The basic organization of the resulting lexical
analyzer is in effect a finite automaton.
88
Compiler Construction Tools
• Scanner generators
• Input: source program
• Output: lexical analyzer
• Task of reading characters from source
program and recognizing tokens or basic
syntactic components.
• Maintains a list of reserved words.
Compiler Construction Tools
iii. Syntax directed translation engines:
• These produce collections of routines that walk
the parse tree, generating intermediate code.
• The basic idea is that one or more “translations”
are associated with each node of the parse tree,
and each translation is defined in terms of
translations at its neighbor nodes in the tree.
90
Compiler Construction Tools
• Syntax-directed translators
• Input: parse tree
• Output: routines to generate I-code
• “The role of the semantic analyzer is to derive
methods by which the structures constructed by
the syntax analyzer may be evaluated or
executed.“
• Type checker
• Two common tactics:
• flatten the semantic analyzer’s parse tree
• embed semantic analyzer w/in syntax analyzer
(syntax-driven translation)
Compiler Construction Tools
iv. Automatic code generators:
• Such a tool takes a collection of rules that define
the translation of each operation of the
intermediate language into the machine language
for the target machine.
92
Compiler Construction Tools
• Automatic code generators
• Input: Intermediate code rules
• Output: crude target machine program
• “The task of the code generator is to traverse
this tree, producing functionally equivalent
object code.”
• Three address code is one type.
Compiler Construction Tools
v. Data-flow engines:
• Much of the information needed to perform good
code optimization involves “data-flow analysis,”
the gathering of information how values are
transmitted from one part of a program to each
other part.
94
Compiler Construction Tools
• Data flow engines
• Input: Intermediate code
• Output: Transformed code
• “This improvement is achieved by program
transformations that are traditionally called
optimizations, although the term
‘optimization’ is a misnomer because there is
rarely a guarantee that the resulting code is
the best possible.”

More Related Content

Similar to Unit 1.pptx

1-Phases of compiler-26-04-2023.pptx
1-Phases of compiler-26-04-2023.pptx1-Phases of compiler-26-04-2023.pptx
1-Phases of compiler-26-04-2023.pptxvenkatapranaykumarGa
 
System software module 4 presentation file
System software module 4 presentation fileSystem software module 4 presentation file
System software module 4 presentation filejithujithin657
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction Thapar Institute
 
Chapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfChapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfDrIsikoIsaac
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compilerAbha Damani
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction Sarmad Ali
 
unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfDrIsikoIsaac
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overviewamudha arul
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler ConstructionAhmed Raza
 
Valuable Information on Lexical Analysis in Compiler Design
Valuable Information on Lexical Analysis in Compiler DesignValuable Information on Lexical Analysis in Compiler Design
Valuable Information on Lexical Analysis in Compiler DesignLesa Cote
 
Comiler construction Notes
Comiler construction NotesComiler construction Notes
Comiler construction NotesNadeem Khan
 
Compiler Construction Lecture One .pptx
Compiler Construction Lecture One  .pptxCompiler Construction Lecture One  .pptx
Compiler Construction Lecture One .pptxانشال عارف
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptxssuser3b4934
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design BasicsAkhil Kaushik
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compilerIffat Anjum
 

Similar to Unit 1.pptx (20)

1-Phases of compiler-26-04-2023.pptx
1-Phases of compiler-26-04-2023.pptx1-Phases of compiler-26-04-2023.pptx
1-Phases of compiler-26-04-2023.pptx
 
COMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptxCOMPILER DESIGN PPTS.pptx
COMPILER DESIGN PPTS.pptx
 
System software module 4 presentation file
System software module 4 presentation fileSystem software module 4 presentation file
System software module 4 presentation file
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction
 
Chapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfChapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdf
 
The Phases of a Compiler
The Phases of a CompilerThe Phases of a Compiler
The Phases of a Compiler
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
 
unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdf
 
Compiler an overview
Compiler  an overviewCompiler  an overview
Compiler an overview
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Valuable Information on Lexical Analysis in Compiler Design
Valuable Information on Lexical Analysis in Compiler DesignValuable Information on Lexical Analysis in Compiler Design
Valuable Information on Lexical Analysis in Compiler Design
 
Comiler construction Notes
Comiler construction NotesComiler construction Notes
Comiler construction Notes
 
Compiler Construction Lecture One .pptx
Compiler Construction Lecture One  .pptxCompiler Construction Lecture One  .pptx
Compiler Construction Lecture One .pptx
 
Chap01-Intro.ppt
Chap01-Intro.pptChap01-Intro.ppt
Chap01-Intro.ppt
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
 
Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 

More from NISHASOMSCS113

Building blocks of Algblocks of Alg.pptx
Building blocks of Algblocks of Alg.pptxBuilding blocks of Algblocks of Alg.pptx
Building blocks of Algblocks of Alg.pptxNISHASOMSCS113
 
dokumen.tips_1-cryptography-and-network-security-third-edition-by-william-sta...
dokumen.tips_1-cryptography-and-network-security-third-edition-by-william-sta...dokumen.tips_1-cryptography-and-network-security-third-edition-by-william-sta...
dokumen.tips_1-cryptography-and-network-security-third-edition-by-william-sta...NISHASOMSCS113
 
Exception handling.pptx
Exception handling.pptxException handling.pptx
Exception handling.pptxNISHASOMSCS113
 
Introduction to Information Storage.pptx
Introduction to Information Storage.pptxIntroduction to Information Storage.pptx
Introduction to Information Storage.pptxNISHASOMSCS113
 

More from NISHASOMSCS113 (9)

Building blocks of Algblocks of Alg.pptx
Building blocks of Algblocks of Alg.pptxBuilding blocks of Algblocks of Alg.pptx
Building blocks of Algblocks of Alg.pptx
 
dokumen.tips_1-cryptography-and-network-security-third-edition-by-william-sta...
dokumen.tips_1-cryptography-and-network-security-third-edition-by-william-sta...dokumen.tips_1-cryptography-and-network-security-third-edition-by-william-sta...
dokumen.tips_1-cryptography-and-network-security-third-edition-by-william-sta...
 
Exception handling.pptx
Exception handling.pptxException handling.pptx
Exception handling.pptx
 
Introduction to Information Storage.pptx
Introduction to Information Storage.pptxIntroduction to Information Storage.pptx
Introduction to Information Storage.pptx
 
recursion.ppt
recursion.pptrecursion.ppt
recursion.ppt
 
social.pptx
social.pptxsocial.pptx
social.pptx
 
Data Management.pptx
Data Management.pptxData Management.pptx
Data Management.pptx
 
intro to sna.ppt
intro to sna.pptintro to sna.ppt
intro to sna.ppt
 
intro.ppt
intro.pptintro.ppt
intro.ppt
 

Recently uploaded

APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Recently uploaded (20)

APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

Unit 1.pptx

  • 2. Course Objectives • The student should be made to: • To learn the various phases of compiler • To learn the various parsing techniques • To understand intermediate code generation and run-time environment • To learn to implement front-end of the compiler • To learn to implement code generator
  • 3. Course Outcomes • At the end of the course, the student should be able to: • Explain the process of compilation and building lexical analyzer with the help of RE, FA and Lex tool • Describe the process of syntax analysis and build a parser using top-down method • Design a parser using bottom-up method and YACC tool • Develop semantic analyzer for type checking and intermediate code generator for translating the source program into an intermediate code • Explain the principles of runtime environment and issues in code generation • Apply various compiler optimization techniques for code generation
  • 6. 1.2 Compilation and Interpretation • Compiler: It is a computer program 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). • The most common reason for wanting to transform source code is to create an executable program.
  • 7. Compilation and Interpretation • Interpreter: In computing, an interpreter is a computer program that reads the source code of another computer program and executes that program directly. • It is interpreted line by line • It is a much slower way of running a program
  • 9. Difference Between Compiler and Interpreter S. No Compiler Interpreter 1 Translates source program into an executable (machine readable) program Converts a source program and executes it at the same time 2 Compiles all the statements together (whole compilation) Interprets every statement separately (line by line) 3 Input is given only to the executable file Source program and input data are given together to an interpreter 4 More space (object code is generated) Requires less space (doesn’t generate intermediate object code) 5 Fast execution Slower execution 6 E.g. FORTRAN, COBOL, C, C++, Pascal E.g. Lisp, Perl, Python
  • 10. Classifications of Compilers • Classified based on construction and functional behavior: i. Single pass ii. Multi pass (Construction) iii. Load and Go iv. Debugging and Optimizing – (Functional)
  • 11. Classifications of Compilers • The basic tasks that any compiler must perform are essentially the same. • By understanding these tasks, we can construct compilers for a wide variety of source languages and target machines using the same basic techniques.
  • 12. Analysis – Synthesis Model of Compilation • There are two parts of Compilation: 1. Analysis: It breaks up the source program into constituent pieces and creates an intermediate representation of the source program. 2. Synthesis: It constructs the desired target program from the intermediate representation. Two Pass Compiler
  • 13. Analysis – Synthesis Model of Compilation • During analysis, the operations implied by the source program are determined and recorded in a hierarchical structure called a tree. • Often, a special kind of tree called a syntax tree is used. • In syntax tree each node represents an operation and the children of the node represent the arguments of the operation.
  • 14. Analysis – Synthesis Model of Compilation • For example, a syntax tree of an assignment statement is shown below.
  • 16. 1.3 The Context of a Compiler • In addition to a compiler, several other programs may be required to create an executable target program.
  • 17. Language-Processing System Skeleton Source Program Pre-Processor 1 Compiler 2 Assembler 3 Relocatable Machine Code 4 Loader Link/Editor 5 Absolute machine code (Executable) Library, relocatable object files Source program Target Assembly program
  • 18. Topic 4 & 5 Phases of a Compiler Errors Encountered in Different Phases
  • 19. 1.4 The Phases of a Compiler • A compiler operates in phases. • Each of which transforms the source program from one representation to another. • A typical decomposition of a compiler is shown in fig given below: 24
  • 22. Lexical or Linear Analysis/ Scanning Source Program Lexical Analysis Tokens
  • 23. The Phases of a Compiler: 1. Linear Analysis: • In which the stream of characters making up the source program is read from left-to-right and grouped into tokens that are sequences of characters having a collective meaning. • The blanks separating the characters of these tokens would normally be eliminated during the lexical analysis. 28
  • 24. The Phases of a Compiler: • In a compiler, linear analysis is called lexical analysis or scanning. • For example, in lexical analysis the characters in the assignment statement Position: = initial + rate * 60 • Would be grouped into the following tokens: 29
  • 25. The Phases of a Compiler • The identifier, position. • The assignment symbol := • The identifier initial. • The plus sign. • The identifier rate. • The multiplication sign. • The number 60. 30
  • 26. The Phases of a Compiler • The character sequence forming a token is called the lexeme for the token.
  • 27. Syntax or Hierarchical Analysis/ Parsing Tokens Syntax Analysis Syntax Tree
  • 28. The Phases of a Compiler 2. Hierarchical Analysis: • In which characters or tokens are grouped hierarchically into nested collections with collective meaning.
  • 29. The Phases of a Compiler • Hierarchical analysis is called parsing or syntax analysis. • It involves grouping the tokens of the source program into grammatical phases that are used by the compiler to synthesize output. • The grammatical phrases of the source program are represented by a parse tree.
  • 30. The Phases of a Compiler 35 identifier identifier expression identifier expression number expression expression expression assignment statement position := + * 60 initial rate
  • 31. The Phases of a Compiler • In the expression initial + rate * 60, the phrase rate * 60 is a logical unit because the usual conventions of arithmetic expressions tell us that the multiplication is performed before addition. • Because the expression initial + rate is followed by a *, it is not grouped into a single phrase by itself. 36
  • 32. The Phases of a Compiler • The hierarchical structure of a program is usually expressed by recursive rules. • For example, we might have the following rules, as part of the definition of expression: 37
  • 33. The Phases of a Compiler • Any identifier is an expression • Any number is an expression • If expression1 and expression2 are expressions, then so are, – Expression1 + expression2 – Expression1 * expression2 – (Expression1) 38
  • 34. The Phases of a Compiler • Example for statements: 1. If identifier1 is an identifier and expression2 is an expression, then identifier1 = expression2 is a statement. 2. If expression1 is an expression and statement2 is a statement, then the following are statements: while ( expression1 ) statement2 if ( expression1 ) statement2
  • 35. Semantic Analysis Syntax Tree Semantic Analysis Annotated Parse Tree
  • 36. The Phases of a Compiler 3. Semantic Analysis: • In which certain checks are performed to ensure that the components of a program fit together meaningfully. • The semantic analysis phase checks the source program for semantic errors and gathers type information for the subsequent code-generation phase. 41
  • 37. The Phases of a Compiler • It uses the hierarchical structure determined by the syntax-analysis phase to identify the operators and operand of expressions and statements. • An important component of semantic analysis is type checking. • The compiler checks that each operator has operands that are permitted by the source language specification. 42
  • 38. The Phases of a Compiler • For example, when a binary arithmetic operator is applied to an integer and real. In this case, the compiler may need to be converting the integer to a real. As shown in figure given below: 43
  • 39. Intermediate Code Generation Annotated Parse Tree Intermediate Code Generation Intermediate Code (3 Address Code)
  • 40. The Phases of a Compiler 4. Intermediate Code Generation: • After Syntax and semantic analysis, some compilers generate an explicit intermediate representation of the source program. • We can think of this intermediate representation as a program for an abstract machine. 45
  • 41. The Phases of a Compiler • This intermediate representation should have two important properties; • it should be easy to produce • easy to translate into the target program • We consider an intermediate form called “three address code,” which is like the assembly language for a machine in which every memory location can act like a register. 46
  • 42. The Phases of a Compiler • Three-address code consists of a sequence of instructions, each of which has at most three operands. • The source statement Position: = initial + rate * 60 might appear in three-address code as., temp1 := inttoreal (60) temp2 := id3 * temp1 temp3 := id2 + temp2 id1 := temp3 47
  • 43. The Phases of a Compiler • The I.R. has several properties: • Each 3 address instruction has at most one operator in addition to the assignment. • The compiler must generate a temporary name to hold the value computed by each instruction.
  • 44. Code Optimization Intermediate Code Code Optimization Optimized Intermediate Code
  • 45. The Phases of a Compiler 5. Code Optimization: • This phase attempts to improve the intermediate code, so that faster-running machine code will result. • Some optimizations are trivial. 50
  • 46. The Phases of a Compiler • For example, a natural algorithm generates the intermediate code, using an instruction for each operator in the tree representation after semantic analysis, even though there is a better way to perform the same calculation, using the two instructions. 51
  • 47. The Phases of a Compiler • temp1 := id3 * 60.0 • id1 := id2 + temp1 • There is nothing wrong with this simple algorithm, since the problem can be fixed during the code-optimization phase. 52
  • 48. The Phases of a Compiler • That is, the compiler can deduce that the conversion of 60 from integer to real representation can be done once and for all at compile time, so the inttoreal operation can be eliminated. 53
  • 49. The Phases of a Compiler • Besides, temp3 is used only once, to transmit its value to id1. It then becomes safe to substitute id1 for temp3, whereupon the last statement of I.R. is not needed and the code above results. • Significant fraction of amount is spent in compiler on this phase. 54
  • 50. Code Generation Optimized Intermediate Code Code Generation Assembly Program/ Machine Program
  • 51. The Phases of a Compiler 6. Code Generation: • The final phase of the compiler is the generation of target code consisting normally of relocatable machine code or assembly code. 56
  • 52. The Phases of a Compiler • Memory locations are selected for each of the variables used by the program. • Then, intermediate instructions are each translated into a sequence of machine instructions that perform the same task. • A crucial aspect is the assignment of variables to registers. 57
  • 53. The Phases of a Compiler • For example, using registers 1 and 2, the translation of the optimized I.C might become., MOVF id3, r2 MULF #60.0, r2 MOVF id2, r1 ADDF r2, r1 MOVF r1, id1 58
  • 54. The Phases of a Compiler • The first and second operands of each instruction specify a source and destination, respectively. • The F in each instruction tells us that instructions deal with floating-point numbers. • This code moves the contents of the address id3 into register 2, and then multiplies it with the real-constant 60.0. 59
  • 55. The Phases of a Compiler • The # signifies that 60.0 is to be treated as a constant. • The third instruction moves id2 into register 1 and adds to it the value previously computed in register 2 • Finally, the value in register 1 is moved into the address of id1. 60
  • 56. The Phases of a Compiler • Symbol Table Management: • An essential function of a compiler is to record the identifiers used in the source program and collect information about various attributes of each identifier. • These attributes may provide information about the storage allocated for an identifier, its type, its scope. 61
  • 57. The Phases of a Compiler • The symbol table is a data structure containing a record for each identifier with fields for the attributes of the identifier. • When an identifier in the source program is detected by the lexical analyzer, the identifier is entered into the symbol table. • However, the attributes of an identifier cannot normally be determined during lexical analysis. 62
  • 58. The Phases of a Compiler • For example, in a Pascal declaration like • Var position, initial, rate : real; • The type real is not known when position, initial and rate are seen by the lexical analyzer. • The code generator typically enters and uses detailed information about the storage assigned to identifiers. 63
  • 59. The Phases of a Compiler • The remaining phases gets information about identifiers into the symbol table and then use this information in various ways. • For example, when doing semantic analysis and intermediate code generation, we need to know what the types of identifiers are, so we can check that the source program uses them in valid ways, and so that we can generate the proper operations on them. 64
  • 60. The Phases of a Compiler • Error Detection and Reporting: • Each phase can encounter errors. • However, after detecting an error, a phase must somehow deal with that error, so that compilation can proceed, allowing further errors in the source program to be detected. • A compiler that stops when it finds the first error is not as helpful as it could be. 65
  • 61. The Phases of a Compiler • The syntax and semantic analysis phases usually handle a large fraction of the errors detectable by the compiler. • Errors where the token stream violates the structure rules (syntax) of the language are determined by the syntax analysis phase. • The lexical phase can detect errors where the characters remaining in the input do not form any token of the language. 66
  • 62.
  • 63. Phase Output Sample Programmer (source code producer) Source string A=B+C; Scanner (performs lexical analysis) Token string ‘A’, ‘=’, ‘B’, ‘+’, ‘C’, ‘;’ And symbol table with names Parser (performs syntax analysis based on the grammar of the programming language) Parse tree or abstract syntax tree ; | = / A + / B C Semantic analyzer (type checking, etc) Annotated parse tree or abstract syntax tree Intermediate code generator Three-address code, quads, or RTL int2fp B t1 + t1 C t2 := t2 A Optimizer Three-address code, quads, or RTL int2fp B t1 + t1 #2.3 A Code generator Assembly code MOVF #2.3,r1 ADDF2 r1,r2 MOVF r2,A Peephole optimizer Assembly code ADDF2 #2.3,r2 MOVF r2,A Phases of a Compiler: An Example
  • 64. Topic 6 The Grouping of Phases 69
  • 65. Front and Back Ends • The phases are collected into a front end and a back end. • The front end consists of those phases that depend primarily on the source language and are largely independent of the target machine. 70
  • 66. Front and Back Ends • These normally include lexical and syntactic analysis, the creating of the symbol table, semantic analysis, and the generation of intermediate code. • A certain amount of code optimization can be done by the front end as well. • The front end also includes the error handling that goes along with each of these phases. 71
  • 67. Front and Back Ends • The back end includes those portions of the compiler that depend on the target machine. • And generally, these portions do not depend on the source language, depend on just the intermediate language. 72
  • 68. Front and Back Ends • In the back end, we find aspects of the code optimization phase, and we find code generation, along with the necessary error handling and symbol table operations. 73
  • 69. Front and Back Ends For Different Machines One High Level Language Front End Back End Back End
  • 70. Front and Back Ends Front End Front End Front End I.R. I.R. I.R. Back End Front End for different languages Intermediate Representation of different languages (Same for all languages) Common Back End for different languages I.R. Stands for Intermediate Representation
  • 71. Front End : Analysis + Intermediate Code Generation Back End : Code Generation + Optimization vs. Number of Passes: A pass: requires r/w intermediate files Fewer passes: more efficiency. However: fewer passes require more sophisticated memory management and compiler phase interaction. Front and Back Ends
  • 72. Passes • Several phases of compilation are usually implemented in a single pass consisting of reading an input file and writing an output file. • In practice, there is a great variation in the way the phases of a compiler are grouped into passes, so we prefer to organize our discussion of compiling around passes rather than phases. 77
  • 73. Reducing the no. of Passes • It is desirable to have relatively a few passes, since it takes time to read and write intermediate files. • On the other hand, if we group several phases into one pass, we may be forced to keep the entire program in memory, because one phase may need information in a different order than a previous phase produces it. 78
  • 74. Reducing the no. of Passes • Grouping into one pass has the following problems: • Interface between lexical and syntax analyzers can often be limited to a single token. • On the other hand, it is very hard to perform code generation until the IR has been completely generated. • The concept of back patching is used. 79
  • 76. Compiler Construction Tools • The compiler writer, like any programmer, can profitably use tools such as, • Debuggers, • Version managers, • Profilers and so on. • In addition to these software-development tools, other more specialized tools have been developed for helping implement various phases of a compiler. 81
  • 77. Compiler Construction Tools • The following is a list of some useful compiler- construction tools: i. Parser generators ii. Scanner generators iii. Syntax directed translation engines iv. Automatic code generators v. Data-flow engines 85
  • 78. Compiler Construction Tools i. Parser generators • These produce syntax analyzers, normally from input that is based on a context-free grammar. • In early compilers, syntax analysis consumed not only a large fraction of the running time of a compiler, but a large fraction of the intellectual effort of writing a compiler. • This phase is considered one of the easiest to implement. 86
  • 79. Compiler Construction Tools • Parser Generators • Input: context-free grammar • Output: syntax analyzer • The task of the syntax analyzer is to produce a representation of the source program in a form directly representing its syntax structure. • This representation is usually in the form of a binary tree or similar data structure.
  • 80. Compiler Construction Tools ii. Scanner generators: • These tools automatically generate lexical analyzers, normally from a specification based on regular expressions. • The basic organization of the resulting lexical analyzer is in effect a finite automaton. 88
  • 81. Compiler Construction Tools • Scanner generators • Input: source program • Output: lexical analyzer • Task of reading characters from source program and recognizing tokens or basic syntactic components. • Maintains a list of reserved words.
  • 82. Compiler Construction Tools iii. Syntax directed translation engines: • These produce collections of routines that walk the parse tree, generating intermediate code. • The basic idea is that one or more “translations” are associated with each node of the parse tree, and each translation is defined in terms of translations at its neighbor nodes in the tree. 90
  • 83. Compiler Construction Tools • Syntax-directed translators • Input: parse tree • Output: routines to generate I-code • “The role of the semantic analyzer is to derive methods by which the structures constructed by the syntax analyzer may be evaluated or executed.“ • Type checker • Two common tactics: • flatten the semantic analyzer’s parse tree • embed semantic analyzer w/in syntax analyzer (syntax-driven translation)
  • 84. Compiler Construction Tools iv. Automatic code generators: • Such a tool takes a collection of rules that define the translation of each operation of the intermediate language into the machine language for the target machine. 92
  • 85. Compiler Construction Tools • Automatic code generators • Input: Intermediate code rules • Output: crude target machine program • “The task of the code generator is to traverse this tree, producing functionally equivalent object code.” • Three address code is one type.
  • 86. Compiler Construction Tools v. Data-flow engines: • Much of the information needed to perform good code optimization involves “data-flow analysis,” the gathering of information how values are transmitted from one part of a program to each other part. 94
  • 87. Compiler Construction Tools • Data flow engines • Input: Intermediate code • Output: Transformed code • “This improvement is achieved by program transformations that are traditionally called optimizations, although the term ‘optimization’ is a misnomer because there is rarely a guarantee that the resulting code is the best possible.”