CD_Unit1_Lecture2-3
CD_Unit1_Lecture2-3
Language Translator
Machine code
Recap
Source code / HLL code
Machine code
Recap
Compiler
Pure HLL
Assembly language
Source Program / High-level Language
Pure HLL
Phases of Compiler
X=a+b*c
Compiler
Phases of Compiler Lexical Analysis
Semantic Analysis
Lexemes Tokens
id = E ;
x Identifier
= Operator
E + T
a Identifier Syntax Analysis
+ Operator T T * F
b Identifier
* Operator F F id
c Identifier S id = E ;
E E+T|T id id
T T*F|F
X=a+b*c F id
Parse Tree
Semantic Analyzer
S
id = E ;
E + T Semantically
Semantic Analyzer Verified
T T * F Parse Tree
F F id
id id
Parse Tree
Intermediate Code Generator
Semantically
Verified Intermediate Code Generator Intermediate Code
Parse Tree
Three address code
(TAC)
id = id + id * id ;
1. t0 = b * c;
2. t1 = a + t0;
X=a+b*c; 3. x = t1;
Code Optimizer
Target Code
Optimized Code
Generation
Semantic Analysis
Semantic Analysis
Intermediate Code
Generation
Code Optimization
Target Code
Generation
Tools for Practical Implementation
Lexical Analysis
Semantic Analysis
Intermediate Code
Generation
Back-end
Code Optimization
Target Code
Generation
Practice Question
§ Price = amount + rate * 50
§ Write the answer of each phases of the compiler (till code optimization)
Compiler: Internal Architecture
Lexical Analysis
Syntax Analysis
Semantic Analysis
Symbol Table Error
Intermediate Code
Manager Handler
Generation
Code Optimization
Target Code
Generation
Symbol Table
§ Data Structure
Lexical Analysis
§ Variable and Function names
§ Objects Syntax Analysis
§ Classes
§ Interfaces Semantic Analysis
Symbol Table Intermediate Code
Manager Generation
Code Optimization
Target Code
Generation
Parts of the Compiler Phases
§ Analysis: Analysis part breaks up the program into
constituent pieces and creates an intermediate Lexical Analysis
representation of the source program . It consists the
Syntax Analysis
phases that are
§ Lexical Analysis Semantic Analysis
§ Syntax Analysis
§ Semantic Analysis Intermediate Code
§ Intermediate Generation
Code Optimization
§ Synthesis: Synthesis parts construct the desired
target from intermediate representation. It consists Target Code
the phases that are Generation
§ Code Optimization
§ Code Generation
Symbol Table
Usage:
Lexical Analysis § Create entries for identifiers.
x char 10 1 -- -- --
Symbol Table: Operations
§ Non-Block Structured Language:
o Contains single instance of the variable declaration.
o Operations:
i. Insert()
ii. Lookup()
§ Block Structured Language:
o Variable declaration may happen multiple times.
o Operations:
i. Insert()
ii. Lookup()
iii. Set()
iv. Reset()
Error Handler
§ Error Handler detects each phase error.
§ An error is a blank entry in the symbol table.
§ Parser generator
§ Scanner generator