66fe65b5746f9CCWeek-02Lecture03
66fe65b5746f9CCWeek-02Lecture03
(CSC-320)
Lecture # 03
Course Instructor: M. Ramzan Shahid Khan
2
What are the Phases of Compiler Design?
• Compiler Operates in various phases
• Every phase takes inputs from its previous stage and feeds its output
to the next phase of the compiler.
3
The Phases of a Compiler
Phase Output Sample
Programmer Source string A=B+C;
Scanner (performs lexical Token string ‘A’, ‘=’, ‘B’, ‘+’, ‘C’, ‘;’
analysis) And symbol table for identifiers
;
Parser (performs syntax Parse tree or abstract syntax tree |
analysis based on the grammar =
/ \
of the programming language) A +
/ \
B C
5
The Analysis-Synthesis Model of Compilation
•Synthesis takes the tree structure and translates the operations therein
into the target program
6
Other Tools that Use the Analysis-Synthesis
Model
• Editors (syntax highlighting)
• Pretty printers (e.g. doxygen)
• Static checkers (e.g. lint and splint)
• Interpreters
• Text formatters (e.g. TeX and LaTeX)
• Silicon compilers (e.g. VHDL)
• Query interpreters/compilers (Databases)
7
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 other 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. 8
Compiler-Construction Tools
Software development tools are available to implement one or
more compiler phases:
• Scanner generators (JFlex)
https://www.jflex.de/#:~:text=JFlex%20is%20designed%20to%20
work,or%20as%20a%20standalone%20tool.
• Parser generators
• Syntax-directed translation engines
• Automatic code generators
• Data-flow engines
9
Phases of Compiler
• There are 6 phases involved in the construction of a Compiler.
• Each of this phase help in converting the high-level language into the machine code.
10
Phases of Compiler
Symbol Table
Error Handler
11
Phases of Compiler
• All these phases convert the Source Code by
12
Phase 1: Lexical Analysis
• Lexical Analysis is the first phase when compiler scans the source
code.
13
Phase 1: Lexical Analysis
• Here, the character stream from the source program is grouped in
meaningful sequences by identifying the tokens.
• It makes the entry of the corresponding tickets into the symbol table
and passes that token to next phase.
14
Phase 1: Lexical Analysis
The primary functions of this phase are:
15
Phase 1: Lexical Analysis - Example
X = Y + 10
Tokens:
X Identifier
= Assignment Operator
Y Identifier
+ Addition Operator
10 Number
16
Phase 2: Syntax Analysis
• Syntax Analysis is all about discovering structure in code.
• The main aim of this phase is to make sure that the source code was
written by the programmer is correct or not.
17
Phase 2: Syntax Analysis
• Syntax analysis is based on the rules based on the specific
programming language by constructing the Parse Tree with the help
of tokens.
18
Phase 2: Syntax Analysis
Here, is a list of tasks performed in this phase:
19
Phase 2: Syntax Analysis - Example
Any identifier/ number is an expression
Then
X = Y + 10 is a statement.
20
Phase 2: Syntax Analysis - Example
Consider Parse Tree for the Following Example:
(a+b)*c
*
+ c
a b
21
Phase 2: Syntax Analysis - Example
In Parse Tree
• Interior Node: Record with an operator filed and two files for children
• Leaf: Records with 2/ more fields; One for Token and Other Information about the Token
22
Phase 3: Semantic Analysis
• Semantic analysis checks the semantic consistency of the code.
• It uses the syntax tree of the previous phase along with the symbol
table to verify that the given source code is semantically consistent.
23
Phase 3: Semantic Analysis
• Semantic Analyser will check for
• Type Mismatches,
• Incompatible Operands,
24
Phase 3: Semantic Analysis
Functions of Semantic Analyses Phase are:
26
Phase 3: Semantic Analysis - Example
float X = 20.2;
float Y = X * 30;
In the above code, the semantic analyse will typecast the integer 30 to
float 30.0 before multiplication.
27
Phase 4: Intermediate Code Generation
• Once the semantic analysis phase is over the compiler, generates
intermediate code for the target machine.
29
Phase 4: Intermediate Code Generation
• Allows you to maintain precedence ordering of the source language
30
Phase 4: Intermediate Code Generation –
Example
total = count + rate * 5
t1 := int_to_float(5)
t2 := rate * t1
t3 := count + t2
total := t3
31
Phase 5: Code Optimization
• The next phase is Code Optimization
32
Phase 5: Code Optimization
The primary functions of this phase are:
33
Phase 5: Code Optimization
• Removing unreachable code and getting rid of unused variables
34
Phase 5: Code Optimization - Example
a = int_to_float(10)
b=c*a
d=e+b
f=d
Can become
b = c * 10.0
f=e+b
35
Phase 6: Code Generation
• Code Generation is the last and final phase of a compiler.
• It gets inputs from code optimization phases and produces the page
code or object code as a result.
36
Phase 6: Code Generation
• It also allocates memory locations for the variable.
• This phase converts the optimize or intermediate code into the target
language.
37
Phase 6: Code Generation
• The target language is the machine code.
• Therefore, all the memory locations and registers are also selected
and allotted during this phase.
38
Phase 6: Code Generation - Example
a = b + 60.0
MOVF a, R1
MULF #60.0, R2
ADDF R1, R2
39
Symbol Table Management
• A Symbol Table contains a record for each identifier with fields for the
attributes of the identifier.
• 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.
40
Error Handling Routine
In the compiler design process error may occur in all the below-given
phases:
41
Error Handling Routine
• Code Optimizer: When the statement is not reachable
• Code Generator: When the memory is full or proper registers are not
allocated
42
Error Handling Routine
• Most common errors are:
43
Error Handling Routine
• 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.
45
Summary
• Lexical Analysis is the first phase when compiler scans the source
code.
• Code Generation Phase gets inputs from code optimisation phase and
produces the page code or object code as a result
• A Symbol Table contains a record for each identifier with fields for the
attributes of the identifier
• Error Handling routine handles error and reports during many phases
47