0% found this document useful (0 votes)
76 views12 pages

L2 - Phases of Compiler

The document discusses the phases of a compiler in processing a source code program. There are six main phases: 1) lexical analysis, 2) syntax analysis, 3) semantic analysis, 4) intermediate code generation, 5) code optimization, and 6) code generation. Each phase transforms the program representation further. The phases work together to translate high-level source code into low-level machine-executable code.

Uploaded by

Moupriya Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views12 pages

L2 - Phases of Compiler

The document discusses the phases of a compiler in processing a source code program. There are six main phases: 1) lexical analysis, 2) syntax analysis, 3) semantic analysis, 4) intermediate code generation, 5) code optimization, and 6) code generation. Each phase transforms the program representation further. The phases work together to translate high-level source code into low-level machine-executable code.

Uploaded by

Moupriya Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Introduction to Compiling

PHASES OF COMPILER
Lecture - 2

8/19/2020 Compiled by Mithun Roy, Department of CSE, SIT, Siliguri 1


A Compiler operates in phases, each of which transforms the source program from one
representation into another.

The following are the phases of the compiler: Source Program

Main phases:
1)Lexical analysis
2)Syntax analysis
3)Semantic analysis
4)Intermediate code generation
5)Code optimization
6)Code generation

Sub-Phases:
1)Symbol table management
Target Program
2)Error handling

8/19/2020 Compiled by Mithun Roy, Department of CSE, SIT, Siliguri 2


LEXICAL ANALYSIS:

• It is the first phase of the compiler.


• It gets input from the source program and produces tokens as output.
• It reads the characters one by one, starting from left to right and forms the tokens.
• Token: It represents a logically cohesive sequence of characters such as keywords, operators,
identifiers, special symbols etc.

Example: a + b = 20 Here, a, b,+,=,20 are all separate tokens. Group of characters forming a
token is called the Lexeme.
• The lexical analyser not only generates a token but also enters the lexeme into the symbol table if
it is not already there.

8/19/2020 Compiled by Mithun Roy, Department of CSE, SIT, Siliguri 3


SYNTAX ANALYSIS:

• It is the second phase of the compiler. It is also known as parser.


• It gets the token stream as input from the lexical analyser of the compiler and
generates syntax tree as the output.
• Syntax tree: It is a tree in which interior nodes are operators and exterior nodes are
operands.
=
Example: For a = b + c * 2, syntax tree is
a +

b *

c 2

8/19/2020 Compiled by Mithun Roy, Department of CSE, SIT, Siliguri 4


SEMANTIC ANALYSIS:

• It is the third phase of the compiler.


• It gets input from the syntax analysis as parse tree and checks whether the given
syntax is correct or not.
• It performs type conversion of all the data types into real data types.

a +

b *

c Inttoreal

8/19/2020 Compiled by Mithun Roy, Department of CSE, SIT, Siliguri 5


INTERMEDIATE CODE GENERATION:

• It is the fourth phase of the compiler.


• It gets input from the semantic analysis and converts the input into output as
intermediate code such as three-address code.
• The three-address code consists of a sequence of instructions, each of which has at
most three operands.

Example: t1=t2+t3

8/19/2020 Compiled by Mithun Roy, Department of CSE, SIT, Siliguri 6


CODE OPTIMIZATION:
• It is the fifth phase of the compiler.
• It gets the intermediate code as input and produces optimized intermediate code as output.
• This phase reduces the redundant code and attempts to improve the intermediate code so
that faster-running machine code will result.
• During the code optimization, the result of the program is not affected.
To improve the code generation, the optimization involves
-deduction and removal of dead code(unreachable code).
-calculation of constants in expressions and terms.
-collapsing of repeated expression into temporary string.
-loop unrolling.
-moving code outside the loop.
-removal of unwanted temporary variables.

8/19/2020 Compiled by Mithun Roy, Department of CSE, SIT, Siliguri 7


CODE GENERATION:

• It is the final phase of the compiler.


• It gets input from code optimization phase and produces the target code or object code
as result.
• Intermediate instructions are translated into a sequence of machine instructions that
perform the same task.

The code generation involves


-allocation of register and memory
-generation of correct references
-generation of correct data types
-generation of missing code

8/19/2020 Compiled by Mithun Roy, Department of CSE, SIT, Siliguri 8


SYMBOL TABLE MANAGEMENT:
• Symbol table is used to store all the information about identifiers used in the program.
• It is a data structure containing a record for each identifier, with fields for the attributes of the
identifier.
• It allows to find the record for each identifier quickly and to store or retrieve data from that record.
Whenever an identifier is detected in any of the phases, it is stored in the symbol table.

ERROR HANDLING:
• Each phase can encounter errors. After detecting an error, a phase must handle the error so that
compilation can proceed.
• In lexical analysis, errors occur in separation of tokens.
• In syntax analysis, errors occur during construction of syntax tree.
• In semantic analysis, errors occur when the compiler detects constructs with right syntactic structure
but no meaning and during type conversion.
• In code optimization, errors occur when the result is affected by the optimization.
• In code generation, it shows error when code is missing etc.
8/19/2020 Compiled by Mithun Roy, Department of CSE, SIT, Siliguri 9
To illustrate the translation of source code through each phase, consider the statement a = b + c * 2.
The figure shows the representation of this statement after each phase:

Step 2: Step 3:
Step 1:
=
a = b + c * 2 (input) id1 = id2 + id3 * 2 (input)
a +
(input)
b *
Lexical Analyser Syntax Analyser c 2

Symantic
Analyser
id1 = id2 + id3 * 2 (output) =

a +
(Symbol Table) (output)
=

b *
a id1 a +
(output)
c 2
b id2 b *

c id3 c Inttoreal

8/19/2020 Compiled by Mithun Roy, Department of CSE, SIT, Siliguri 10


To illustrate the translation of source code through each phase, consider the statement a = b + c * 2.
The figure shows the representation of this statement after each phase:

Step 5: Step 6:
Step 4:
=
temp1 = inttoreal(2) temp1 = id3 * 2 (input)
id1 = id2 + temp1
a + temp2 = id3 * temp1
(input) (input)
b * temp3 = id2 + temp2
c Inttoreal
id1 = temp3 Code Generator

Intermediate Code Optimiser


MOV id3, R2
Code Generator MUL 2.0, R2
MOV id2, R1 (output)
ADD R2, R1
temp1 = inttoreal(2) temp1 = id3 * 2
(output) MOV R1, id1
id1 = id2 + temp1
temp2 = id3 * temp1 (output)
temp3 = id2 + temp2
id1 = temp3

8/19/2020 Compiled by Mithun Roy, Department of CSE, SIT, Siliguri 11


HomeWork –II

To illustrate the translation of source code to target code through


each phase, consider the statement a = b + c * ((d ^ e) / 15).
(where all the operands are real)

8/19/2020 Compiled by Mithun Roy, Department of CSE, SIT, Siliguri 12

You might also like