You are on page 1of 20

Compiler Design

Phases of Compiler
Session Objectives
• To learn
– Phases of the Compiler with an example

2 v 1.2
Session Outcomes
• At the end of this session, participants will be able to
– How phases of the compiler works?

3 v 1.2
Agenda
• Phases of the Compiler

4 v 1.2
Phases of Compiler

Dr. B. Prabavathy
SSNCE

5 v 1.2
Phases of compiler - Example
• Lexical Phase
– Reads characters from source program
– Formulate a stream of tokens
– Tokens may be identifiers, keywords (int, float,…), operators,
etc.
– Sequence of characters forming a token is called lexeme
• Syntactic phase
– Syntax tree is formed from stream of tokens
– Tree in which interior node represents the operator and children
represents the tokens

6 v 1.2
Syntax tree

7 v 1.2
Phases of compiler - Example

Practically, when the


compiler is
constructed, some of
the phases may be
grouped together

8 v 1.2
Phases of compiler - Example

9 v 1.2
Phases of compiler - Example

10 v 1.2
Symbol table manager
• Essential function
– Record identifiers that are found from source program along with
the attributes
• Attributes for Identifier
– Storage allocated
– Type
– Scope
• Attributes for procedures
– Name
– Number of arguments
– Types of arguments
– Method passing the argument
– Type of return

11 v 1.2
Symbol Table
• Symbol table - data structure created and maintained by
compilers in order to store information about the
occurrence of various entities such as variable names,
objects, classes, interfaces
• Symbol table is used by both the analysis and the
synthesis parts of a compiler
– To store the names of all entities in a structured form at one
place.
– To verify if a variable has been declared.
– To implement type checking, by verifying assignments and
expressions in the source code are semantically correct.
– To determine the scope of a name (scope resolution).

12 v 1.2
Implementation and Operation
• If a compiler is to handle a small amount of data, then
the symbol table can be implemented as an unordered
list
• A symbol table can be implemented in one of the
following ways:
– Linear (sorted or unsorted) list
– Binary Search Tree
– Hash table
• Operations
– Insert() – To insert an entity in symbol table
– Lookup() – To search for an entity

13 v 1.2
Scope Management
void pro_one() void pro_two()
{ {
int one_1; int two_1;
int one_2; int two_2;
{ {
int one_3; int two_3;
int one_4; int two_4;
} }
int one_5; int two_5;
{ int one_6; }
int one_7;
}
}

14 v 1.2
Hierarchical Structure of Symbol Table

15 v 1.2
Lookup Operation
• This symbol table data structure hierarchy is stored in
the semantic analyzer and whenever a name needs to
be searched in a symbol table, it is searched using the
following algorithm
– first a symbol will be searched in the current scope, i.e. current
symbol table.
– if a name is found, then search is completed, else it will be
searched in the parent symbol table until,
– either the name is found or global symbol table has been
searched for the name.

16 v 1.2
Symbol table manager

• Lexical Analysis
– Creates new table entries in the table, example like entries about
token.
• Syntax Analysis
– Adds information regarding attribute type, scope, dimension, line
of reference, use, etc in the table.
• Semantic Analysis
– Uses available information in the table to check for semantics
i.e. to verify that expressions and assignments are semantically
correct(type checking) and update it accordingly.

17 v 1.2
Symbol table manager
• Intermediate Code generation
– Refers symbol table for knowing how much and what type of
run-time is allocated and table helps in adding temporary
variable information
• Code Optimization
– Uses information present in symbol table for machine dependent
optimization
• Target Code generation 
– Generates code by using address information of identifier
present in the table

18 v 1.2
Simple Questions
1. If function name is the LHS of the expression
statement, which phase will detect the error and what is
the type of error?
2. Why Intermediate code generation is important?
3. Which intermediate code is popular?

19 v 1.2
Summary
• Language Processing System
• Phases of the Compiler

20 v 1.2

You might also like