Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

CS327 - Compilers

Compiler Phases

Abhishek Bichhawat 10/01/2024


Compiler Phases

Abstract
Source Lexical Parsing
Tokens Syntax
Program Analysis
Tree
Semantic
Analysis

Attributed
Target Code Optimized AST
Code Generation Code Optimization (Intermediate
Code)
Lexical Analysis
Lexical Analysis - Analogy
● Recognize words
Space

Thew orl disro und.


Punctuation
Lexical Analysis - Analogy
● More than that - make sense out of the words
Space

The world is round.


Punctuation
Lexical Analysis
● Divides programs into tokens
Space

The world is round.


Punctuation
Lexical Analysis
● Divides programs into tokens
Variables Operators

if x == 0 then { y = 1; } else { z = 2; }

Keywords
Constants
Lexical Analysis
● Divides programs into tokens

● How do we distinguish “==” from “=” ?


if x == 0 then { y = 1; } else { z = 2; }

● Are there other tokens?


Parsing
Parsing - Analogy
● Making sense of a sentence

Round, the world is.


Parsing - Analogy
● Recognize sentences with diagramming
● Each word is identified by a type or role

The world is round .

article noun verb adjective period

subject object

sentence
Parsing
● Parsing program expressions with “Parse Tree” or AST

if x == 0 then { y = 1; } else { z = 2; }
x == 0 y 1 z 2

relation assign-stmt assign-stmt

pred then-stmt else-stmt

If-then-else-stmt
Semantic Analysis
Semantic Analysis - Analogy
● Understand the meaning once parsing is done

Ram and Sita are going to her village.


Semantic Analysis - Analogy
● Understand the meaning once parsing is done
○ May have ambiguities

Ram and Shyam are going to his village.


Semantic Analysis
● Understand the meaning once parsing is done
○ Stricter rules for resolving ambiguities

x = 1; x = 1;
{ {
x = 2; z = 2;
print x; print x;
} }
Semantic Analysis
● Understand the meaning once parsing is done
○ Stricter rules for resolving ambiguities
● Compilers perform semantic checks
○ Type checking and scope analysis to find inconsistencies
Optimization
Optimization - Analogy
● Edit sentences
○ Revise sentences to shorter sentences while keeping the meaning
○ Remove redundant sentences

Decisions about the policies of the institute


are taken by the Director of the institute.

The Director decides the institutional policies.


Optimization
● Change programs to
○ Run faster
○ Lower resource usages
● Many optimization techniques have been proposed
○ Not all compilers use all optimization techniques

y = 0; x = y + 1; print x;

print 1;
Code Generation
Code Generation - Analogy
● Generate “equivalent” sentence in another language

The Director decides the institutional policies.

नदे शक संस्थागत नी तयों का नणर्णय


करता है ।
Code Generation
● Produces code in another language
○ Mostly, low-level languages
○ Some sort of intermediate representations
● Lower level languages expose more features like registers,
memory etc.
● Higher level languages are easier to understand
Almost all Compilers
follow the same
structure and we will
study each of these
phases in detail.

You might also like