Professional Documents
Culture Documents
Compiler 1
Compiler 1
12/07/21 1
Cse321, Programming Languages and Compilers
Acknowledgements
The material taught in this course was made
possible by many people. Here is a partial list:
• Andrew Tolmach
• Nathan Linger
• Harry Porter
• Jinke Lee
12/07/21 2
Cse321, Programming Languages and Compilers
12/07/21 3
Cse321, Programming Languages and Compilers
Today’s Assignments
Reading
• Engineering a Compiler
– Available In the PSU bookstore
– Chapter 1, pp 1-26
– There will be a 5 minute quiz on the reading Wednesday.
Search
• Find the class webpage
12/07/21 4
Cse321, Programming Languages and Compilers
Course Information
• CS321 - Languages and Compiler Design
– Time: Monday & Wednesday 18:00-19:50 pm
– Place: PCAT 138
– Instructor: Tim Sheard
– office: room 115, CS Dept, 4th Ave Building, Portland State Univ.
– phone: 503-725-2410 (work) 503-649-7242 (home)
– office hours: Before class in my office (5:00-5:50), or by Appt.
• Assignments
– Reading from text and handouts (quizzes on reading)
– Daily, 1 page programming assignments
– 3 part programming project
• Grading:
– midterm exam (25%)
– 3 parts of project (30%)
– Daily 1 page assignments and quizzes (15%)
– Final exam (30 %)
12/07/21 5
Cse321, Programming Languages and Compilers
Examinations
• Entrance Exam.
– Do you know your REs and CFGs?
• Final exam
– Monday, Mar. 19, 2007. Time: 6:00-7:50.
12/07/21 6
Cse321, Programming Languages and Compilers
Text Book
• Text: Engineering a Compiler
– Keith D. Cooper, and Linda Torczon
• Other Reference Materials
– Auxilliary Material
» Elements of Functional Programming (SML book)
by Chris Reade, Addison Wesley, ISBN 0-201-12915-9
» Using the SML/NJ System
http://www.cs.cmu.edu/~petel/smlguide/smlnj.htm
• Class Handouts
– Each class, a copy of that day’s slides will be available as a
handout.
– I will post files that contain the example programs used in each
lecture on the class web page
www.cs.pdx.edu/~sheard/course/Cs321
– I will post Assignments there as well.
12/07/21 7
Cse321, Programming Languages and Compilers
Labs
• Whenever you learn a new language its great to have
someone looking over your shoulder.
• In this spirit I have scheduled some lab times where people
can work on learning ML while I am there to help.
– FAB INTEL Lab (FAB 55-17) downstairs by the Engineering and
Technology Manangement’s departmental offices
– Friday Jan. 12, 2007. 4:00 – 5:30 PM
– Tueday Jan. 16, 2007 4:00 – 5:30
– Friday Jan. 19, 2005. 4:00 – 5:30 PM
• Labs are not required, but attendance of at least one is
highly recommended!
12/07/21 8
Cse321, Programming Languages and Compilers
Installing SML
• Software can be obtained at:
– http://www.smlnj.org/
• I am using the most recent version 110.60
– but it displays the version 110.57 when it runs
• Browse the “documentation and Literature” section of the SML web
page. Find some resources that you can use.
12/07/21 9
Cse321, Programming Languages and Compilers
Entrance Exam
• CS321 has some pretty serious prerequisites.
12/07/21 10
Cse321, Programming Languages and Compilers
Academic Integrity
Students are expected to be honest in their academic
dealings. Dishonesty is dealt with severely.
12/07/21 11
Cse321, Programming Languages and Compilers
Course Thesis
• This course is about programming languages. We study
languages in two ways.
– From the perspective of the user
– From the perspective of the implementer (compiler writer)
• We will learn about some languages you may never have
heard of. We will learn to program in one of them
(Standard ML). Its good to learn a new language in depth.
• This course is also about programming. There will be
extensive programming assignments in SML. If you don’t
do them - you won’t learn
– You’re deluding yourself if you think you can learn the material
without doing the exercises!
• We will write a comiler for a Java subset. Its good to
understand the implementation details of a language you
already know.
12/07/21 12
Cse321, Programming Languages and Compilers
12/07/21 13
Cse321, Programming Languages and Compilers
Standard ML
• In this course we will use an implementation of the
language Standard ML
I will use the version 110.57 or 110.60 of SML. Earlier versions probably will
work as well. I don’t foresee any problems with other versions, but if you want
to use the identical version that I use in class then this is the one.
12/07/21 14
Cse321, Programming Languages and Compilers
Characteristics of SML
• Applicative style
– input output description of problem.
• First class functions
– pass as parameters
– return as value of a function
– store in data-structures
• Less Importantly:
– Automatic memory management (G.C. no new or malloc)
– Use of a strong type system which uses type inference, i.e. no
declarations but still strongly typed.
12/07/21 15
Cse321, Programming Languages and Compilers
Syntactic Elements
12/07/21 16
Cse321, Programming Languages and Compilers
Interacting
• The normal style for interaction is to start SML, and then
type definitions into the window.
• Types of commands
– 4 + 5;
– val x = 34;
– fun f x = x + 1;
• Here are two commands you might find useful.
Use “file.sml”;
12/07/21 17
Cse321, Programming Languages and Compilers
- val cd = OS.FileSys.chDir;
val cd = fn : string -> unit
-
12/07/21 18
Cse321, Programming Languages and Compilers
In Class Exercise 1
• Define prefix and lastone in terms of head tail and reverse.
• First make a file “S01code.sml”
• Start sml fun lastone x = hd (rev x)
• Change directory to fun prefix x = rev (tl (rev x))
where the file resides
• Load the file ( use “S01code.html” )
• Test the function
12/07/21 19
Cse321, Programming Languages and Compilers
In Class Exercise 2
• define map and filter functions
– mymap f [1,2,3] = [f 1, f 2, f 3]
– filter even [1,2,3,4,5] = [2,4]
fun mymap f [] = []
| mymap f (x::xs) = (f x)::(mymap f xs);
fun filter p [] = []
| filter p (x::xs) =
if (p x) then x::(filter p xs) else (filter p xs);
• Sample Session
Course topics
• Programming Language
– Types of languages
– Data types and languages
– Types and languages
• Compilers
– Lexical analysis
– Parsing
– Translation to abstract syntax using modern parser generator
technology.
– Type checking
– identifiers and symbol table organization,
12/07/21 21
Cse321, Programming Languages and Compilers
12/07/21 22
Cse321, Programming Languages and Compilers
text: z = x + pi * 12.0
tokens:
id(z) eql id(x) plus id(pi) times float(12.0)
syntax tree:
=
Id(z)
Id(z) +
Id(x) *
Id(pi) float(12.0)
12/07/21 23
Cse321, Programming Languages and Compilers
Passes (cont)
Three address code:
temp1 := pi * 12.0
z := x * temp1
12/07/21 24
Cse321, Programming Languages and Compilers
Lexical Analysis
• Produces Tokens and Deals with:
» white space
» comments
» reserved word identification
» symbol table interface
12/07/21 25
Cse321, Programming Languages and Compilers
12/07/21 26
Cse321, Programming Languages and Compilers
Examples
• lexeme pattern token
– x <alpha><alpha>* Id "x"
– abc <alpha><alpha>* Id "abc"
– 152 <digit>+ Constant(152)
– then then ThenKeyword
• Many lexemes map to the same token. e.g. “x” and “abc” .
• Note, some lexemes might match many patterns. e.g.
"then" above. Need to resolve ambiguity.
• Since tokens are terminals, they must be "produced" by
the lexical phase with synthesized attributes in place. (e.g.
name of an identifier). e.g. id(“x”) and constant(152)
12/07/21 27
Cse321, Programming Languages and Compilers
12/07/21 28
Cse321, Programming Languages and Compilers
Example Grammar
Sentence ::= Subject Verb Object
Subject ::= Proper-noun
Object ::= Article Adjective Noun
Verb ::= ate | saw | called
Noun ::= cat | ball | dish
Article ::= the | a
Adjective ::= big | bad | pretty
Proper-noun ::= tim | mary
12/07/21 29
Cse321, Programming Languages and Compilers
Exp ::= id
| Exp + Exp
| Exp * Exp
| ( Exp )
derives x, x+x, x+x+x, ...
12/07/21 30
Cse321, Programming Languages and Compilers
Parse Trees
• Each nonterminal on the lhs of a production "roots" a
tree:
Exp
Exp + Exp
Id Id
12/07/21 31
Cse321, Programming Languages and Compilers
Considerations
• Tree Traversal orders
» Left to right?
» right to left?
» in-order, pre-order, or post-order
12/07/21 32
Cse321, Programming Languages and Compilers
| <empty>
Term ::= Factor factors
factors ::= * Factor { print "*" } factors
| <empty>
Factor ::= id { print id.name }
| ( Exp )
12/07/21 33
Cse321, Programming Languages and Compilers
Semantics
• How do we know what to translate the syntax tree into?
• How do we know if it is correct?
• Semantics
» denotational semantics
» operational semantics
» interpreters
12/07/21 34
Cse321, Programming Languages and Compilers
Over view
• Compilation is a large process
• It is often broken into stages
• The theories of computer science guide us in writing
programs at each stage.
• We must understand what a program “means” if we are to
translate it correctly.
• Many phases of the compiler try and optimize by
translating one form into a better (more efficient?) form.
• Most of compiling is about “pattern matching” languages
and tools that support pattern matching are very useful.
12/07/21 35