Download Standard ML - Compiler Design - Lecture Slides and more Slides Computer Science in PDF only on Docsity!
Standard ML
• In this course we will use an implementation of
the language Standard ML
• The SML/NJ Homepage has lots of useful
information:
• 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
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.
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.
Interacting
• The normal style for interaction is to start SML,
and then type definitions into the window.
• Types of commands
– 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”;
The SML Read-Typecheck-Eval-Print Loop Standard ML of New Jersey v110.57 [built: Mon Nov 21 21:46:28 2005]
**-
- 3+5; val it = 8 : int
- print "Hi there\n"; Hi there val it = () : unit
- val x = 22; val x = 22 : int
- x+ 5; val it = 27 : int -**
- val pwd = OS.FileSys.getDir;
- **val pwd = fn : unit -> string
- val cd = OS.FileSys.chDir; val cd = fn : string -> unit -**
Note the semicolon when you’re ready to evaluate. Otherwise commands can spread across several lines.
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
- mymap plusone [2,3,4] [3, 4, 5]
- filter even [1,2,3,4,5,6] [2, 4, 6]
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,
- Next Quarter in the second class of the sequence
- Intermediate representations
- Backend analysis
- Transformations and optimizations for a number of different kinds of languages
The Top to Bottom Example
text:
tokens:
syntax tree:
id(z) eql id(x) plus id(pi) times float(12.0)
z = x + pi * 12.
Id(z) +
float(12.0)
=
Id(z)
Id(x)^ *****
Id(pi)
Passes (cont)
Three address code:
temp1 := pi * 12. z := x * temp
Assembly level code:
ld r1,x ld r2,pi add r1,r ldi r2,12. mul r1,r st r1,z
Tokens, Patterns & Lexemes
- Many strings from the input may produce the
same TOKEN i.e. identifiers, integers
constants, floats
- A PATTERN describes a rule which describes
which strings are assigned to a token.
- A LEXEME is the exact sequence of input
characters matched by a PATTERN.
Examples
• lexeme pattern token
- x * Id "x"
- abc * Id "abc"
- 152 + 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)
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
Recursive Grammar Examples
Recursive Grammars describe infinite languages
list ::= [ num morenum ]
morenum ::= , num morenum
|
derives [ 2 ], [2,4], [2,4,6] ...
Exp ::= id
| Exp + Exp
| Exp * Exp
| ( Exp ) derives x, x+x, x+x+x, ...
Syntax Directed Translations
- A syntax directed translation traverses a syntax tree and
builds a translation in the process.
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
Example Translation Process Translation as an abstract syntax to abstract syntax transformer
We represent this as a grammar with “actions” { ... }. The action is
performed when that production is reduced.
Exp ::= Term terms
terms ::= + Term { print "+" } term
|
Term ::= Factor factors
factors ::= * Factor { print "*" } factors
|
Factor ::= id { print id.name }
| ( Exp )