Phase of A Compiler
Phase of A Compiler
1
The Structure of compiler is divided into two parts:
1. Analysis
2. Synthesis
2
The Structure of a Compiler
• The analysis part breaks up the source program into constituent pieces and
imposes a grammatical structure on them. It then uses this structure to create
an intermediate representation of the source program.
• If the analysis part detects that the source program is either syntactically ill
formed or semantically unsound, then it must provide informative messages,
so the user can take corrective action.
• The analysis part also collects information about the source program and
stores it in a data structure called a symbol table, which is passed along with
the intermediate representation to the synthesis part.
• The synthesis part constructs the desired target program from the intermediate
representation and the information in the symbol table. The analysis part is
often called the front end of the compiler; the synthesis part is the back end.
3
Phases of a compiler
4
Phase of a Compiler
5
Phases of a compiler
6
For example, suppose a source program contains the assignment statement
position = initial + rate * 60………………………………………………………. (1.1)
The characters in this assignment could be grouped into the following lexemes and mapped into the
following tokens passed on to the syntax analyzer:
1. position 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 position. The symbol-table entry for an identifier
holds information about the identifier, such as its name and type.
2. The assignment symbol = is a lexeme that is mapped into the token {=}. Since this token needs no
attribute-value, we have omitted the second component
3. initial is a lexeme that is mapped into the token (id, 2 ) , where 2 points to the symbol-table entry for
initial .
4. + is a lexeme that is mapped into the token (+).
5. rate is a lexeme that is mapped into the token ( id, 3 ) , where 3 points to the symbol-table entry for
rate.
6. * is a lexeme that is mapped into the token (* ) .
7. 60 is a lexeme that is mapped into the token (60) .
Blanks separating the lexemes would be discarded by the lexical analyzer. Figure 1.7 shows the
representation of the assignment statement (1.1) after lexical analysis as the sequence of tokens
( id, l) ( = ) (id, 2) (+) (id, 3) ( * ) (60) ………………………..(1.2)
In this representation, the token names =, +, and * are abstract symbols for the assignment, addition, and
multiplication By Jaoydpeeepr aPattoilrs, respectively. 7
Phases of a compiler
8
Phases of a compiler
Syntax Analysis: The second phase of the compiler is syntax analysis or parsing.
The parser uses the first components 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 is a syntax
tree in which each interior node represents an operation and the children of the
node represent the arguments of the operation.
9
Phases of a compiler
10
Phases of a compiler
• 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.
• An important part of semantic analysis is type checking, where the compiler checks
that each operator has matching operands. For example, many programming
language definitions require an array index to be an integer; the compiler must
report an error if a floating-point number is used to index an array.
• The language specification may permit some type conversions called coercions.
• For example, a binary arithmetic operator may be applied to either a pair of
integers or to a pair of floating-point numbers. If the operator is applied to a
floating-point number and an integer, the compiler may convert or coerce the
integer into a floating-point number.
11
Phases of a compiler
12
Phases of a compiler
13
Phases of a compiler
14
Phases of a compiler
• Code Optimization: The machine-independent code-optimization phase
attempts to improve the intermediate code so that better target code will
result. Usually better means faster, but other objectives may be desired,
such as shorter code, or target code that consumes less power. For example,
a straightforward algorithm generates the intermediate code, using an
instruction for each operator in the tree representation that comes from
the semantic analyzer.
• A simple intermediate code generation algorithm followed by code
optimization is a reasonable way to generate good target code. The
optimizer can deduce that the conversion of 60 from integer to floating
point can be done once and for all at compile time, so the inttofloat
operation can be eliminated by replacing the integer 60 by the floating-
point number 60.0. Moreover, t3 is used only once to transmit its value to
id1 so the optimizer can transform into the shorter sequence
• t 1 = id3 * 60 . 0
• id1 = id2 + t 1
15
Phases of a compiler
16
Phases of a compiler
17
Phases of a compiler
18
Phases of a compiler
19
Phases of a compiler
20
Compiler- Construction Tools
1. Parser generators that automatically produce syntax analyzers from a
grammatical description of a programming language.
2. Scanner generators that produce lexical analyzers from a regular expression
description of the tokens of a language.
3. Syntax-directed translation engines that produce collections of routines
for walking a parse tree and generating intermediate code.
4. Code-generator generators that produce a code generator from a collection
of rules for translating each operation of the intermediate language into the
machine language for a target machine.
5. Data-flow analysis engines that facilitate the gathering of information about
how values are transmitted from one part of a program to each 1other part.
Data-flow analysis is a key part of code optimization.
6. Compiler- construction toolkits that provide an integrated set of routines
for constructing various phases of a compiler.
21
Runtime Environments
22
Runtime Environments
Notes: An activation may manipulate data objects allocated for its use.
23
THANK YOU
24