Phases of a Compiler with an example:
Example:
total = sum + rate * 5
Lexical Analysis:
Input:
total = sum + rate * 5
Lexemes Tokens
total variable / identifier
= Assignment operator
sum variable / identifier
+ Addition operator
rate variable / identifier
* Multiplication operator
5 Number / Constant
Output:
id1 = id2 + id3 * 5
1
Syntax Analysis
Input:
id1 = id2 + id3 * 5
Output:
id1 +
Parse Tree
id2 *
id3 5
Semantic Analysis
Input:
id1 = id2 + id3 * 5
Output:
id1 +
id2 *
id3 int_to_float
2
Intermediate Code Generation
Input:
id1 = id2 + id3 * 5
Output:
t1 := int_to_float(5)
t2 := id3 * t1
t3 := id2 + t2
id1 := t3
[Here, Intermediate code is created with the help of three address code]
Code Optimization
Input:
t1 := int_to_float(5)
t2 := id3 * t1
t3 := id2 + t2
id1 := t3
Output:
t2 := id3 * 5.0
id1 := id2 + t2
3
Code Generation
Input:
t2 := id3 * 5.0
id1 := id2 + t2
Output:
Comment
MOV R2, id3 (R2) ← id3
MUL R2, 5.0 (R2) ← 5.0 * (R2)
MOV R1, id2 (R1) ← id2
ADD R1, R2 (R1) ← (R2) + (R1)
MOV id1, R1 id1 ← (R1)
In this example, Compiler is Converting a High – level language statement into an assembly
language statement
Symbol Table Management:
A symbol table contains a record for each identifier with fields for the attributes of the identifier.
This component makes it easier for the compiler to search the identifier record and retrieve it
quickly. The symbol table also helps you for the scope management. The symbol table and error
handler interact with all the phases and symbol table update correspondingly.
Error Handling Routine:
In the compiler design process error may occur in all the below-given phases:
Lexical analyzer: Wrongly spelled tokens
Syntax analyzer: Missing parenthesis
Intermediate code generator: Mismatched operands for an operator
Code Optimizer: When the statement is not reachable
Code Generator: Unreachable statements
Symbol tables: Error of multiple declared identifiers
4
Most common errors are invalid character sequence in scanning, invalid token sequences in type,
scope error, and parsing in semantic analysis.
The error may be encountered in any of the above phases. After finding errors, the phase needs
to deal with the errors to continue with the compilation process. These errors need to be reported
to the error handler which handles the error to perform the compilation process. Generally, the
errors are reported in the form of message.