0% found this document useful (0 votes)
13 views

Introduction To Compilers

COMPILER DESIGN STUDY MATERIAL - INTRODUCTION TO COMPILERS

Uploaded by

deeplyfwind7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Introduction To Compilers

COMPILER DESIGN STUDY MATERIAL - INTRODUCTION TO COMPILERS

Uploaded by

deeplyfwind7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Introduction To Compilers

CSO844
Phases of a compiler
Lexical• Recognize
Analysis words.
This is a sentence.
• Note the
• Capital “T” (start of sentence symbol)
• Blank “ ” (word separator)
• Period “.” (end of sentence symbol)

• Lexical analyzer divides program text into “tokens”


If x == y then z = 1; else z = 2;
• Units:
if, x, ==, y, then, z, =, 1, ;, else, z, =, 2, ;
Parsing: Diagramming a Sentence
This line is a longer sentence.

adjective noun verb article adjective noun

subject object

sentence
Parsing Programs
A parser recognizes higher-level structure from a token sequence.
• Consider:
If x == y then z = 1; else z = 2;
• Diagrammed:

x == y z 1 z 2
relation assign assign

predicate then-stmt else-stmt


if-then-else
Semantic Analysis in English
• Understanding meaning and performing consistency checks.
• Example:
Jack said Jerry left his assignment at home.
What does “his” refer to? Jack or Jerry?

• Even worse:
Jack said Jack left his assignment at home?
How many Jacks are there?
Which one left the assignment?
Semantic Analysis in Programming
• Programming {
languages define strict int Jack = 3;
rules to avoid such
ambiguities {
• Scope Rules int Jack = 4;
cout << Jack;
• This C++ code prints }
“4”; the inner }
definition is used
More Semantic Analysis
• Compilers perform many semantic checks besides
variable bindings.
• Example:
Jack left his homework at home.
Jack left her homework at home.
• A “type mismatch” between her and Jack; we know
they are different people.
• Presumably, Jack is male.
• Example:
int i, j; float x; j = (int) ((i + j) * x);
Optimization
• No strong counterpart in English, but akin to editing.
• Automatically modify programs so that the equivalent program
• Runs faster.
• Uses less memory.
• In general, improves resource utilization.
• Simple Example:
X = Y + Y is the same as X = 2*Y
(Implemented as arithmetic left-shift operation)
Code Generation
• Produces assembly code (usually).
• Many compilers perform translations between
successive intermediate forms.
• All but the first and the last are ILs internal to the
compiler, typically ordered in descending level of
abstraction.
• Highest is the source language.
• Lowest is the assembly language.
• Lower levels expose features such as registers, memory layouts,
etc hidden by higher-levels.
• Lower levels obscure high-level meaning.

You might also like