S01 Intro ML
S01 Intro ML
6/3/2013
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
6/3/2013
Todays 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
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 %)
6/3/2013
Examinations
Entrance Exam.
Do you know your REs and CFGs?
Final exam
Monday, Mar. 19, 2007. Time: 6:00-7:50.
6/3/2013
Text Book
Text: Engineering a Compiler
Keith D. Cooper, and Linda Torczon
Class Handouts
Each class, a copy of that days 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.
6/3/2013
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 Manangements 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!
6/3/2013
Installing SML
Software can be obtained at:
http://www.smlnj.org/
Browse the documentation and Literature section of the SML web page. Find some resources that you can use. SML also runs on the PSU linux and Intel labs
linux usepkg sml then logout, or start a new shell type: sm Intel In a commnd window p:\programs\smlnj\addpkg.cmd then logout, or start a new command window then just type: N:\>sml
6/3/2013
Entrance Exam
CS321 has some pretty serious prerequisites.
1. Write a regular expression for the set of strings that begins with an a which is followed by an arbitrary number of bs or cs, and is ended by a d. e.g. ad, abbbd, abcbcbcd, etc. 2. Transform your regular expression into a DFA 3. Write a context free grammar that recognizes the same set of strings as your RE 4 Transform your CFG into a CFG that is left-recursion free.
6/3/2013
10
Academic Integrity
Students are expected to be honest in their academic dealings. Dishonesty is dealt with severely. Homework. Pass in only your own work. Program assignments. Program independently. Examinations. Notes and such, only as each instructor allows.
OK to discuss how to solve problems with other students, but each student should write up, debug, and turn in his own solution.
6/3/2013
11
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 dont do them - you wont learn
Youre 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.
6/3/2013
12
6/3/2013
13
Standard ML
In this course we will use an implementation of the language Standard ML The SML/NJ Homepage has lots of useful information: http://www.smlnj.org// You can get a version to install on your own machine there.
I will use the version 110.57 or 110.60 of SML. Earlier versions probably will work as well. I dont 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.
6/3/2013
14
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.
6/3/2013
15
Syntactic Elements
Identifiers start with a letter followed by digits or other letters or primes or underscores.
Valid Examples: a a3 Invalid Examples: 12A ab aF
Identifiers can also be constructed with a sequence of operators like: !@#$%^&*+~ Reserved words include
fun val datatype if then else if of let in end type
6/3/2013
16
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. val pwd = OS.FileSys.getDir; val cd = OS.FileSys.chDir; To load a file that has a sml program type
Use file.sml;
6/3/2013
17
18
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) fun prefix x = rev (tl (rev x)) Change directory to where the file resides Load the file ( use S01code.html ) Test the function
Standard ML of New Jersey v110.57 - K; - val cd = OS.FileSys.chDir; val cd = fn : string -> unit - cd "D:/work/sheard/courses/PsuCs321/web/notes"; - use "S01code.html"; [opening S01code.html] val lastone = fn : 'a list -> 'a val prefix = fn : 'a list -> 'a list val it = () : unit - lastone [1,2,3,4]; val it = 4 : int
6/3/2013
19
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]
Sample Session
- mymap plusone [2,3,4] [3, 4, 5] - filter even [1,2,3,4,5,6] [2, 4, 6]
6/3/2013
20
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,
21
Each phase is from one form to another, OR from one form to the same form, which is often called a source to source transformation.
6/3/2013
22
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)
6/3/2013
+ *
float(12.0)
23
Passes (cont)
Three address code:
temp1 := z := pi * 12.0 x * temp1
6/3/2013
24
Lexical Analysis
Produces Tokens and Deals with:
white space comments reserved word identification symbol table interface
Tokens are the terminals of grammars. Lexical analysis reads the whole program, character by character thus it needs to be efficient. This implies fancy buffering techniques etc. Modern lexical generators handle these problems so we will ignore them.
6/3/2013
25
6/3/2013
26
Examples
lexeme
x abc 152 then
pattern
<alpha><alpha>* <alpha><alpha>* <digit>+ then
token
Id "x" Id "abc" Constant(152) 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)
6/3/2013
27
Informal Definitions of: Regular: concatenation, union, star Context Free: only one symbol on the lhs of a production
6/3/2013
28
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 Start Symbol = Sentence Example sentence: tim ate the big ball
6/3/2013
29
Exp ::= | | |
6/3/2013
Parse Trees
Each nonterminal on the lhs of a production "roots" a tree:
Exp Exp
Exp
Id
Id
Each node in a tree with all its immediate children is derived from a single production of the grammar We desire a program which constructs a parse tree from a string. Such programs are different for every grammar, we some times use tools to construct such programs (yacc).
6/3/2013
31
Considerations
Tree Traversal orders
Left to right? right to left? in-order, pre-order, or post-order
Where does the information about what to do in the traversal come from?
Attribute grammars
Inherited attributes Synthesized attributes
6/3/2013
32
33
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
Very useful in writing compilers since they give a reference when trying to decide what the compiler should do in particular cases.
6/3/2013
34
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.
6/3/2013
35