Introduction To Compilers
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)
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
• 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.