Compilers Introduction Alex Aiken
Compilers
Introduction
Alex Aiken
Intro to Compilers
Main Text Book:Compilers: Principles, Techniques & Tools, 2nd ed.,Aho, Lam, Sethi, and Ullman, 2007
Evaluation:Midterm Exam 32.5%Final Exam 32.5%Assignments and Quizzes 15%Project 20%
Intro to Compilers
• Compilers
• Interpreters
• a program that translates an executable program in a source language (usually high level)into an equivalent executable program in a target language(usually low level)
• a program that reads an executable program and produces the results of running that program
• usually, this involves executing the source program in some fashion
Intro to Compilers
• Compilers
CompilerSource
Program
Target
Program
Errors
Target ProgramInput Output
Intro to Compilers
• Interpreters
Interpreter
Input
Output
Source
Program
Translates line by line
Executes each translated line immediately
Execution is slower because translation is repeated
But, usually give better error diagnostics than a compiler
Intro to Compilers
• 1954 IBM develops the 704
– Successor to the 701
– customers found that the software costs exceeded the
hardware costs.
Alex Aiken
Intro to Compilers
• “Speedcoding”
JohnBackus
Image used with permission of The History of Computing Project(www.thocp.net)Alex Aiken
– an early example of an interpreter
– developed in 1953 by John Backus
– much faster way of developing programs
– programs were ten to twenty times slower
Intro to Compilers
• FORTRAN I
Image used with permission of The History of Computing Project(www.thocp.net)Alex Aiken
– FORmula TRANslation Project
– FORTRAN ran from 1954 To1957
– By 1958, over 50 percent of all of programs
were in FORTRAN
JohnBackus
Alex Aiken
Intro to Compilers
• The first compiler
– Huge impact on computer science
• Led to an enormous body of theoreticalwork
• Modern compilers preserve the outline of FORTRAN I
Alex Aiken
Intro to Compilers
1. LexicalAnalysis
2. Parsing
3. SemanticAnalysis
4. Optimization
5. Code Generation
Alex Aiken
Intro to Compilers
• First step: recognize words.
– Smallest unit aboveletters
This is a sentence.
Alex Aiken
Intro to Compilers
ist his ase nte nce
Alex Aiken
Intro to Compilers
• Lexical analysis divides program text into “words” or
“tokens”
if x == y then z = 1; else z = 2;
Alex Aiken
Intro to Compilers
• Once words are understood, the next step is tounderstand sentence structure
• Parsing = Diagramming Sentences
– The diagram is a tree
Intro to Compilers
This sentenceline is a longer
Alex Aiken
Intro to Compilers
This sentence
article
line is
noun verb
a longer
article adjective noun
Alex Aiken
Intro to Compilers
This sentence
article
line is
noun verb
a longer
article adjective noun
subject object
Alex Aiken
Intro to Compilers
This sentence
article
line is
noun verb
a longer
article adjective noun
subject object
sentence
Alex Aiken
Intro to Compilers
if x == y then z = 1; else z = 2;
if-then-elseAlex Aiken
Intro to Compilers
predicate else-stmtthen-stmt
if-then-elseAlex Aiken
if x == y then z = 1; else z = 2;
Intro to Compilers
x == y
relation
predicate
z 2
assign
else-stmt
z 1
assign
then-stmt
if-then-elseAlex Aiken
if x == y then z = 1; else z = 2;
Alex Aiken
Intro to Compilers
• Once sentence structure is understood, we can try to
understand “meaning”
– This is hard!
• Compilers perform limited semantic analysis to catch
inconsistencies
Alex Aiken
Intro to Compilers
• Example:
Jack said Jerry left his assignment at home.
• Even worse:
Jack said Jack left his assignment at home?
Alex Aiken
Intro to Compilers
• Programming languages
define strict rules to
avoid such ambiguities
{
int Jack = 3 ;
{
int Jack = 4;
cout << Jack;
}
}
Alex Aiken
Intro to Compilers
• Compilers perform many semantic checks besides variable bindings
• Example:
Jack left her homework at home.
• A “type mismatch” between her and Jack; we know they are different people
Alex Aiken
Intro to Compilers
• Optimization has no strong counterpart in English
– But a little bit likeediting
– But akin to editing
• Automatically modify programs so that they– Runfaster
– Use less memory
– Use less power
– Use less network messages, database accesses, etc.
Alex Aiken
Intro to Compilers
X = Y *0 is the same as X =0
NOT ALWAYS CORRECT
NaN
Alex Aiken
Intro to Compilers
• Produces assembly code (usually)
• A translation into another language
– Analogous to humantranslation
Alex Aiken
Intro to Compilers
• The overall structure of almost everycompiler
adheres to our outline
• The proportions have changed since FORTRAN
L P S O C
L P S O C
Intro to Compilers
Intro to Compilers
Errors
position = initial + rate * 60
lexical analyzer
syntax analyzer
semantic analyzer
intermediate code generator
<id, 1> < = > <id, 2> < + > <id, 3> < * > < 60 >
:=
<id, 1><id, 2>
<id, 3>
+
*
60
:=
<id, 1>
<id, 2>
<id, 3>
+
*
inttofloat
60
Symbol Table
1 position ....
2 initial ….
3 rate….
Intro to Compilers
Errors
intermediate code generator
code optimizer
final code generator
t1 := inttofloat(60)
t2 := id3 * t1
t3 := id2 + t2
id1 := t3
t1 := id3 * 60.0
id1 := id2 + t1
LDF R2, id3
MULF R2, R2, #60.0
LDF R1, id2
ADDF R1, R1, R2
STF id1, R1
1 position ....
2 initial ….
3 rate….
Symbol Table
3 address codes
AlexAiken
Economy
• Why are there so many programming languages?
• Why are there new programminglanguages?
• What is a good programminglanguage?
AlexAiken
Economy
Why are there so many programming languages?
Application domains have distinctive/conflictingneeds
o Scientific computing
• good floating point support
• good arrays
• parallelism
o Business applications
• persistence
• report generation
• data analysis
o Systems programming
• control of resources
• Real time constraints
AlexAiken
Economy
Why are there new programminglanguages?
Claim:Programmertraining is thedominantcost fora programming language.
o widely used languages are slow to change
o it's easy to start a new language
o new languages are born to fill a void
New languages tend to look like old languages
o Java vsC++
AlexAiken
Economy
What is a goodprogramming language?
o There is NO universally accepted metric for language
• Popularity? VB was once a very popular language
AlexAiken
Economy
Application domains have conflictingneeds.
Programmer training isthe dominant cost for a
programming language.