L2 - Phases of Compiler
L2 - Phases of Compiler
PHASES OF COMPILER
Lecture - 2
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
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.
b *
c 2
a +
b *
c Inttoreal
Example: t1=t2+t3
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
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