UNIT I Lecture 2 Phases of Compiler
UNIT I Lecture 2 Phases of Compiler
Sharma
UNIT I
Phases of Compiler
Learn Compiler Design: From B. K. Sharma
Unit I: Syllabus
• Introduction to Compiler
• Major Data Structures in Compiler
• Types of Compiler
• Front-End and Back-End of Compiler
• Compiler Structure:
– Analysis-synthesis model of compilation
• Various phases of Compiler
Learn Compiler Design: From B. K. Sharma
Unit I: Syllabus
• Lexical Analysis:
– Input Buffering
– Specification and Recognition of Tokens
– Design of a Lexical Analyzer Generator
• Lex
Learn Compiler Design: From B. K. Sharma
Meaning of Phase
A stage in the development of something
किसी वस्तु िे कविास िी अवस्था या चरण
P and Q are in the same Phase
A phase of a compiler is a
distinguishable stage, which takes input
from the previous stage, processes and
produces output that can be used as
input for the next stage.
Learn Compiler Design: From B. K. Sharma
1. Lexical Analysis
2. Syntax Analysis
3. Semantic Analysis
4. IR Code Gen
5. Code Optimization
Target Program
Learn Compiler Design: From B. K. Sharma
1. Lexical Analysis(Scanner)
2. Syntax Analysis(Parser)
4. IR Code Gen
5. Code Optimization
Target Program
Learn Compiler Design: From B. K. Sharma
1. Lexical Analysis(Scanner)
Tokens
2. Syntax Analysis(Parser)
Syntax Tree
3. Semantic Analysis(Semantic Analyzer)
Annotated Tree
4. IR Code Gen
Intermediate Code
5. Code Optimization
Optimized Code
6. Target Code Generation
Target Code
Target Program
Learn Compiler Design: From B. K. Sharma
The Translation Process: Phases of Compiler
c) Parser d) None
Learn Compiler Design: From B. K. Sharma
Synthesis:
1. Lexical Analysis
Analysis of
Source program 2. Syntax Analysis
3. Semantic Analysis
4. IR Code Gen
Synthesis of
Target instruction 5. Code Optimization
code
6. Target Code Generation
Target Program
Learn Compiler Design: From B. K. Sharma
Errors
Learn Compiler Design: From B. K. Sharma
1. Lexical Analysis
Front-End
Machine 2. Syntax Analysis
Independent
3. Semantic Analysis
Target Program
Learn Compiler Design: From B. K. Sharma
c) Middle-end d) None
Learn Compiler Design: From B. K. Sharma
a Identifier y Identifier
:= Assignment Operator Multiplication Operator
*
x Identifier 2.5 Real Literal
+ Plus Operator ; Delimiter Semicolon
Learn Compiler Design: From B. K. Sharma
What is Literal?
Semantic Analysis:
The semantic analyzer does the following:
Checks the static semantics of the language
Annotates the syntax tree with type
information
Learn Compiler Design: From B. K. Sharma
Semantic Analysis:
The semantics of a program are its meaning
as opposed to syntax or structure.
Semantic Analysis:
Type checking.
Learn Compiler Design: From B. K. Sharma
Errors
Code Optimization
produces better/semantically equivalent code.
temp1 := int2real(y)
Before
temp2 := temp1 real * 2.5 Optimization
temp3 := x real+ temp2
a := temp3