4
Most read
11
Most read
12
Most read
1
Phases of
a Compiler
2
Phases of a Compiler
 Compiler operates in phases
 Each phase transforms source program
from one representation to naother
3
Phases of the Compiler
Source Program
Lexical Analyzer
1
Syntax Analyzer
2
Semantic Analyzer
3
Intermediate
Code Generator
4
Code Optimizer
5
Code Generator
6
Target Program
Symbol-table
Manager
Error Handler
4
Lexical Analysis
 Linear Analysis / Scanning
 Stream of characters are read and grouped into
tokens
 Tokens – Sequence of characters having a collective
meaning
Input: position := initial + rate * 60
Tokens
Position := initial + rate * 60 ;
5
Lexical Analysis
 During Scanning
 Blanks separating tokens are eliminated
 Char sequences forming a token – lexeme
 Entered into the symbol table
id1 = id2 + id3 * 60
6
Syntax Analysis
 Hierarchical Analysis/Parsing
 Tokens are grouped hierarchically into
nested collections with collective
meaning – grammatical phrases that
are used by the compiler to synthesize
the output
 Grammatical phrases are represented
by Parse Trees
7
Syntax Analysis
 Hierarchical structure is expressed using recursive
rules
 Any identifier is an expr
 Any number is an expr
 If expr1 and expr2 are expressions

expr1 + expr2, expr1 * expr2; (expr1) are all expressions
 If id1 is and identifier and exp2 is an expression id1 = exp2
is a statement
 If exp1 is an expression and stmt2 is a statement
while (exp1) do stmt2
if (exp1) then stmt2 are all statements
8
Syntax Analysis
 Division between lexical and syntax analysis is
arbitrary
 Constructs of the source language can be used to
decide
 Non-recursive – lexical analysis – Recursion is not required
to recognize identifiers.
 Identifiers are strings of letters and digits beginning with a
letter.
 Identifiers are recognized by a simple scan of the input
stream.
 Recursive – Syntactic analysis , begin-end statements;
parenthesis matching in expressions etc.
9
Syntax Analysis
identifier
identifier
expression
identifier
expression
number
expression
expression
expression
assignment
statement
position
:=
+
*
60
initial
rate
10
Syntax Analysis
 Compressed representation – Syntax tree
 In Parse tree
 interior node: record with an operator field and 2
fields for children
 Leaf: record with 2/more fields; one for token and
other information about token
position
initial
rate
:=
+
*
60
11
Semantic Analysis
 Ensures that the components of a program fit
together meaningfully
 Performs Type checking
 Gathers type information and checks for type
compatibility
 Checks operands are permitted by the source
language specification
position
initial
rate
:=
+
*
inttoreal
60
12
Intermediate Code Generation
 Intermediate representation is a Program for an
abstract machine
 Two Properties
 Should be easy to produce

Easy to translate into target program
 3-address code is a common form
 It is a sequence of instructions
 Each instruction has at most 3 operands
13
Intermediate Code Generation
 Properties of Three address code in
Intermediate form
 1. Each three address instruction has at most one
operator in addition to the assignment
 Order of operations must be decided
 Multiplication precedes the addition
 2. Compiler must generate temporary names to hold
the value computed by each instruction
 3. Some instructions may have less than 3 operands
14
Intermediate Code Generation
 Example
temp1 := inttoreal(60)
temp2 := id3 * temp1
temp3 := id2 + temp2
id1 := temp3
15
Code Optimization
 Aims to produce faster running machine code
 Perform the same calculation using the two
instructions
 Optimizing Compilers
 The fraction of time the compiler is spent in this phase
 Amount of optimization varies
 Optimized code
temp1 := id3 * 60.0
id1 := id2 + temp1
-int to real conversion at compile time
-temp3 can be eliminated
16
Code Generation
 Generates target code
 Relocatable machine code / Assembly code
 Memory locations are selected for variables
 Assignment of variables to registers is done
MOVF id3, R2
MULF #60.0, R2
MOVF id2, R1
ADDF R2, R1
MOVF R1, id
F -> floating point numbers, # -> Constant
Source
Destination
17
Symbol Table Management
 Essential function of a compiler is to record for
each identifier and fields for the various attribute
of each identifier
 Attributes Stored
 Identifiers - Storage allocated, type, Scope
 Procedures – Number and type of arguments,
Passing by reference/ value, return type
 Lexical Analysis stage makes entries, other
phases fill in details and refer
18
Error detection and Reporting
 All phases may encounter errors
 Lexical
 Syntax
 Semantic
 Must not stop on first error
19
Example
E
r
r
o
r
s
position := initial + rate * 60
lexical analyzer
syntax analyzer
semantic analyzer
intermediate code generator
id1 := id2 + id3 * 60
:=
id1
id2
id3
+
*
60
:=
id1
id2l
id3
+
*
inttoreal
60
Symbol
Table
position ....
initial ….
rate….
20
Example
E
r
r
o
r
s
intermediate code generator
code optimizer
final code generator
temp1 := inttoreal(60)
temp2 := id3 * temp1
temp3 := id2 + temp2
id1 := temp3
temp1 := id3 * 60.0
id1 := id2 + temp1
MOVF id3, R2
MULF #60.0, R2
MOVF id2, R1
ADDF R2, R1
MOVF R1, id1
position ....
initial ….
rate….
Symbol Table
3 address code

More Related Content

PPT
Lecture 1 - Lexical Analysis.ppt
PPTX
Lexical analysis - Compiler Design
PPTX
Intermediate code generator
PPT
Lexical analyzer
PPTX
Input-Buffering
PPTX
Datatype in c++ unit 3 -topic 2
PPTX
Type checking in compiler design
PPT
Parsing
Lecture 1 - Lexical Analysis.ppt
Lexical analysis - Compiler Design
Intermediate code generator
Lexical analyzer
Input-Buffering
Datatype in c++ unit 3 -topic 2
Type checking in compiler design
Parsing

What's hot (20)

PPTX
Specification-of-tokens
PPTX
Code generation
PDF
Intermediate code generation in Compiler Design
PPTX
Structure of the compiler
PPT
Bottom - Up Parsing
PPTX
Compiler design
DOC
PDF
Code optimization in compiler design
PPTX
RECURSIVE DESCENT PARSING
PPTX
Intermediate code
PPTX
Top down parsing
PDF
Syntax analysis
PPTX
Input buffering
PPTX
Phases of compiler
PPTX
Symbol Table
PPT
Software Metrics
PPTX
Phases of Compiler
PDF
Intermediate code generation
PPTX
Top Down Parsing, Predictive Parsing
PPTX
Finite Automata in compiler design
Specification-of-tokens
Code generation
Intermediate code generation in Compiler Design
Structure of the compiler
Bottom - Up Parsing
Compiler design
Code optimization in compiler design
RECURSIVE DESCENT PARSING
Intermediate code
Top down parsing
Syntax analysis
Input buffering
Phases of compiler
Symbol Table
Software Metrics
Phases of Compiler
Intermediate code generation
Top Down Parsing, Predictive Parsing
Finite Automata in compiler design
Ad

Similar to phases of a compiler (20)

KEY
Unit 1 cd
PPT
Compiler Construction
PPT
what is compiler and five phases of compiler
PPT
Cpcs302 1
PDF
Lecture 2.1 - Phase of a Commmmpiler.pdf
PPT
1 - Introduction to Compilers.ppt
PPTX
The Phases of a Compiler
PPT
Phases of compiler
DOC
Chapter 1 1
DOCX
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
PPT
Compiler design computer science engineering.ppt
PPTX
Compiler Construction-2 for bs computer science.pptx
PPT
Concept of compiler in details
PPTX
Ss ui lecture 2
PPTX
A Lecture of Compiler Design Subject.pptx
PPTX
Compiler Design
PDF
PDF
Compilers Design
DOCX
Compiler Design Material
PPT
Module 2
Unit 1 cd
Compiler Construction
what is compiler and five phases of compiler
Cpcs302 1
Lecture 2.1 - Phase of a Commmmpiler.pdf
1 - Introduction to Compilers.ppt
The Phases of a Compiler
Phases of compiler
Chapter 1 1
2-Design Issues, Patterns, Lexemes, Tokens-28-04-2023.docx
Compiler design computer science engineering.ppt
Compiler Construction-2 for bs computer science.pptx
Concept of compiler in details
Ss ui lecture 2
A Lecture of Compiler Design Subject.pptx
Compiler Design
Compilers Design
Compiler Design Material
Module 2
Ad

Recently uploaded (20)

PDF
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PPTX
Software Engineering and software moduleing
PPTX
ASME PCC-02 TRAINING -DESKTOP-NLE5HNP.pptx
PDF
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
PDF
Applications of Equal_Area_Criterion.pdf
PDF
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
PDF
Soil Improvement Techniques Note - Rabbi
PPTX
CyberSecurity Mobile and Wireless Devices
PDF
Design of Material Handling Equipment Lecture Note
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PPTX
ai_satellite_crop_management_20250815030350.pptx
PPTX
tack Data Structure with Array and Linked List Implementation, Push and Pop O...
PDF
20250617 - IR - Global Guide for HR - 51 pages.pdf
PDF
MLpara ingenieira CIVIL, meca Y AMBIENTAL
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PPTX
Petroleum Refining & Petrochemicals.pptx
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
Java Basics-Introduction and program control
PPTX
wireless networks, mobile computing.pptx
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
Software Engineering and software moduleing
ASME PCC-02 TRAINING -DESKTOP-NLE5HNP.pptx
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
Applications of Equal_Area_Criterion.pdf
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
Soil Improvement Techniques Note - Rabbi
CyberSecurity Mobile and Wireless Devices
Design of Material Handling Equipment Lecture Note
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
ai_satellite_crop_management_20250815030350.pptx
tack Data Structure with Array and Linked List Implementation, Push and Pop O...
20250617 - IR - Global Guide for HR - 51 pages.pdf
MLpara ingenieira CIVIL, meca Y AMBIENTAL
distributed database system" (DDBS) is often used to refer to both the distri...
Petroleum Refining & Petrochemicals.pptx
"Array and Linked List in Data Structures with Types, Operations, Implementat...
Java Basics-Introduction and program control
wireless networks, mobile computing.pptx

phases of a compiler

  • 2. 2 Phases of a Compiler  Compiler operates in phases  Each phase transforms source program from one representation to naother
  • 3. 3 Phases of the Compiler Source Program Lexical Analyzer 1 Syntax Analyzer 2 Semantic Analyzer 3 Intermediate Code Generator 4 Code Optimizer 5 Code Generator 6 Target Program Symbol-table Manager Error Handler
  • 4. 4 Lexical Analysis  Linear Analysis / Scanning  Stream of characters are read and grouped into tokens  Tokens – Sequence of characters having a collective meaning Input: position := initial + rate * 60 Tokens Position := initial + rate * 60 ;
  • 5. 5 Lexical Analysis  During Scanning  Blanks separating tokens are eliminated  Char sequences forming a token – lexeme  Entered into the symbol table id1 = id2 + id3 * 60
  • 6. 6 Syntax Analysis  Hierarchical Analysis/Parsing  Tokens are grouped hierarchically into nested collections with collective meaning – grammatical phrases that are used by the compiler to synthesize the output  Grammatical phrases are represented by Parse Trees
  • 7. 7 Syntax Analysis  Hierarchical structure is expressed using recursive rules  Any identifier is an expr  Any number is an expr  If expr1 and expr2 are expressions  expr1 + expr2, expr1 * expr2; (expr1) are all expressions  If id1 is and identifier and exp2 is an expression id1 = exp2 is a statement  If exp1 is an expression and stmt2 is a statement while (exp1) do stmt2 if (exp1) then stmt2 are all statements
  • 8. 8 Syntax Analysis  Division between lexical and syntax analysis is arbitrary  Constructs of the source language can be used to decide  Non-recursive – lexical analysis – Recursion is not required to recognize identifiers.  Identifiers are strings of letters and digits beginning with a letter.  Identifiers are recognized by a simple scan of the input stream.  Recursive – Syntactic analysis , begin-end statements; parenthesis matching in expressions etc.
  • 10. 10 Syntax Analysis  Compressed representation – Syntax tree  In Parse tree  interior node: record with an operator field and 2 fields for children  Leaf: record with 2/more fields; one for token and other information about token position initial rate := + * 60
  • 11. 11 Semantic Analysis  Ensures that the components of a program fit together meaningfully  Performs Type checking  Gathers type information and checks for type compatibility  Checks operands are permitted by the source language specification position initial rate := + * inttoreal 60
  • 12. 12 Intermediate Code Generation  Intermediate representation is a Program for an abstract machine  Two Properties  Should be easy to produce  Easy to translate into target program  3-address code is a common form  It is a sequence of instructions  Each instruction has at most 3 operands
  • 13. 13 Intermediate Code Generation  Properties of Three address code in Intermediate form  1. Each three address instruction has at most one operator in addition to the assignment  Order of operations must be decided  Multiplication precedes the addition  2. Compiler must generate temporary names to hold the value computed by each instruction  3. Some instructions may have less than 3 operands
  • 14. 14 Intermediate Code Generation  Example temp1 := inttoreal(60) temp2 := id3 * temp1 temp3 := id2 + temp2 id1 := temp3
  • 15. 15 Code Optimization  Aims to produce faster running machine code  Perform the same calculation using the two instructions  Optimizing Compilers  The fraction of time the compiler is spent in this phase  Amount of optimization varies  Optimized code temp1 := id3 * 60.0 id1 := id2 + temp1 -int to real conversion at compile time -temp3 can be eliminated
  • 16. 16 Code Generation  Generates target code  Relocatable machine code / Assembly code  Memory locations are selected for variables  Assignment of variables to registers is done MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R2, R1 MOVF R1, id F -> floating point numbers, # -> Constant Source Destination
  • 17. 17 Symbol Table Management  Essential function of a compiler is to record for each identifier and fields for the various attribute of each identifier  Attributes Stored  Identifiers - Storage allocated, type, Scope  Procedures – Number and type of arguments, Passing by reference/ value, return type  Lexical Analysis stage makes entries, other phases fill in details and refer
  • 18. 18 Error detection and Reporting  All phases may encounter errors  Lexical  Syntax  Semantic  Must not stop on first error
  • 19. 19 Example E r r o r s position := initial + rate * 60 lexical analyzer syntax analyzer semantic analyzer intermediate code generator id1 := id2 + id3 * 60 := id1 id2 id3 + * 60 := id1 id2l id3 + * inttoreal 60 Symbol Table position .... initial …. rate….
  • 20. 20 Example E r r o r s intermediate code generator code optimizer final code generator temp1 := inttoreal(60) temp2 := id3 * temp1 temp3 := id2 + temp2 id1 := temp3 temp1 := id3 * 60.0 id1 := id2 + temp1 MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R2, R1 MOVF R1, id1 position .... initial …. rate…. Symbol Table 3 address code