Cse321, Programming Languages and Compilers 1 7/14/2015 Lecture #3b, Jan. 22, 2007 References While...
-
date post
22-Dec-2015 -
Category
Documents
-
view
215 -
download
2
Transcript of Cse321, Programming Languages and Compilers 1 7/14/2015 Lecture #3b, Jan. 22, 2007 References While...
Cse321, Programming Languages and Compilers
104/19/23
Lecture #3b, Jan. 22, 2007•References•While Loops•Accumulating parameter functions
•Regular Expressions as programs•RE to NFA•Patterns for RE’s
Cse321, Programming Languages and Compilers
204/19/23
Libraries
• There are lots of predefined types and functions in SML when it starts up.
• You can find out about them at:http://www.smlnj.org//basis/pages/top-level-chapter.html
• Many more can be found in the other libraries.– http://www.standardml.org/Basis/manpages.html
• Libraries are encapsulated in Structures which are classified by Signatures (a list of what is in the structure).
Cse321, Programming Languages and Compilers
304/19/23
Peeking inside a Library
• To see what is inside a Structure you can open it.
• This is somewhat of a hack, but it is useful.Standard ML of New Jersey v110.57 [built: Mon Nov 21 21:46:28 2005]- open Int;opening Int type int = ?.int val precision : Int31.int option val minInt : int option val maxInt : int option val toLarge : int -> IntInf.int val fromLarge : IntInf.int -> int val toInt : int -> Int31.int val fromInt : Int31.int -> int val div : int * int -> int val mod : int * int -> int val quot : int * int -> int val rem : int * int -> int val min : int * int -> int val max : int * int -> int
Cse321, Programming Languages and Compilers
404/19/23
The List library- open List;opening List datatype 'a list = :: of 'a * 'a list | nil exception Empty val null : 'a list -> bool val hd : 'a list -> 'a val tl : 'a list -> 'a list val last : 'a list -> 'a val getItem : 'a list -> ('a * 'a list) option val nth : 'a list * int -> 'a val take : 'a list * int -> 'a list val drop : 'a list * int -> 'a list val length : 'a list -> int val rev : 'a list -> 'a list val @ : 'a list * 'a list -> 'a list val concat : 'a list list -> 'a list val revAppend : 'a list * 'a list -> 'a list val app : ('a -> unit) -> 'a list -> unit val map : ('a -> 'b) -> 'a list -> 'b list val mapPartial : ('a -> 'b option) -> 'a list -> 'b list val find : ('a -> bool) -> 'a list -> 'a option val filter : ('a -> bool) -> 'a list -> 'a list val partition : ('a -> bool) -> 'a list -> 'a list * 'a list val foldr : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b val foldl : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b val exists : ('a -> bool) -> 'a list -> bool val all : ('a -> bool) -> 'a list -> bool val tabulate : int * (int -> 'a) -> 'a list val collate : ('a * 'a -> order) -> 'a list * 'a list -> order-
Cse321, Programming Languages and Compilers
504/19/23
References• References allow one to write programs with
mutable variables.• The interface to assignment and update is slightly
different from other languages– val r = (ref 5) Create a new reference that can be updated– !r Get the value stored in the reference– (ref n) => … Pattern match to get value
» fun ! (ref n) = n– r := 6 + z Update a reference with a new value
• Later today we will use the following:val next = ref 0;fun new () = let val ref n = next in (next := n+1; n) end;
Alternatively fun new () = let val n = !next in (next := n+1; n) end;
Cse321, Programming Languages and Compilers
604/19/23
While Loops
• While loops are similar to other languages, they usually require use of references (to get the condition to eventually change).
• Statements are inside ()’s and separated by “;”
val n = ref 4;
val w1 =
while (!n > 0)
do (print (Int.toString (!n) ^ "\n");
n := (!n) – 1 );
Semicolon to separate
statements
Semicolon to end the
“val w1 = …”declaration
Cse321, Programming Languages and Compilers
704/19/23
Factorial as a while loop• Factorial
fun fact3 n =
let val ans = ref 1
val count = ref n
in while (!count > 0)
do (ans := !ans * !count
; count := !count - 1);
!ans
end;
fun fact1 n = if n=0 then 1 else n * (fact1 (n-1));
fun fact2 0 = 1
| fact2 n = n * (fact2 (n-1));
Inside a let between “in” and “end” we don’t need to surround statements
with ()s, but we still separate with “;”
Return the value stored in the reference ans
compare with the recursive
versions
Cse321, Programming Languages and Compilers
804/19/23
Accumulating parameters• Many loops look like this
{ ans = init; While test do stuff; Return ans}
• There is a pattern that mimics this in ML called functions with accumulating parameter.
• The pattern consists of a recursive function with two (or more) parameters. The first parameter drives the loop (usually by pattern matching), the second accumulates an answer (like ans in example above).
• We call the function with init as the value of the second argument to get started.
• We return the second argument when the function is done recurring.
Cse321, Programming Languages and Compilers
904/19/23
Fact as an accumulating function
fun fact4 n =
let fun loop 0 ans = ans
| loop n ans = loop (n-1) (n*ans)
in loop n 1 end;
{ ans = init;
While test do stuff;
Return ans}
Cse321, Programming Languages and Compilers
1004/19/23
Flat as an accumulating function
datatype Tree =
Tip | Node of Tree * int * Tree;
fun flat3 x =
let fun help Tip ans = ans
| help (Node(x,y,z)) ans =
help x (y::(help z ans))
in help x [] end;
{ ans = init; While test do stuff; Return ans}
Cse321, Programming Languages and Compilers
1104/19/23
Regular Expressions• Regular Languages and Regular expressions
are used to describe the patterns which describe lexemes.
• Regular expressions are composed of empty-string, concatenation, union, and closure.
• Examples:
A(A | D)* where A is alphabetic and
D is a digit
(+ | - | ε ) D D*
closure
union
Empty-string
Concatenation is implicit
Cse321, Programming Languages and Compilers
1204/19/23
Meaning of Regular Expressions
Let A,B be sets of strings:
The empty string: ""
ε= { "" }
(sometimes <empty> )
Concatenation by juxtaposition:
AB = a^b where a in A and b in B A = {"x", "qw"} and B = {"v", "A"}
then AB = { "xv", "xA", "qwv", "qwA"}
Cse321, Programming Languages and Compilers
1304/19/23
Meaning of Regular Expressions (cont.)
Union by | (or other symbols like U etc)
A = {"x", "qw"} and B = {"v", "A"}
then A|B = {"x", "qw", "v", "A"}
Closure by *
Thus A* = {""} | A | AA | AAA | ...
= A0 | A1 | A2 | A3 | ...
A = {"x", "qw"}
then A* = { "" } | {"x", "qw"}
| {"xqw", "qwx","xx", "qwqw"} | ...
Cse321, Programming Languages and Compilers
1404/19/23
Regular Expressions as a language• We can treat regular expressions as a
programming language.• Each expression is a new program.• Programs can be compiled.
• How do we represent the regular expression language? By using a datatype.
datatype RE = Empty | Union of RE * RE | Concat of RE * RE | Star of RE | C of char;
Cse321, Programming Languages and Compilers
1504/19/23
Example RE program
(+ | - | ε ) D D*
val re1 =
Concat(Union(C #”+”,Union(C #”-”,Empty))
,Concat(C #”D”,Star (C #”D”)))
Cse321, Programming Languages and Compilers
1604/19/23
R.E.’s and FSA’s• Algorithm that constructs a FSA from a regular expression.• FSA
– alphabet , A
– set of states, S
– a transition function, A x S -> S
– a start state, S0
– a set of accepting states, SF subset of S
• Defined by cases over the structure of regular expressions• Let A,B be R.E.’s, “x” in A, then
– ε is a R.E.
– “x” is a R.E.
– AB is a R.E.
– A|B is a R.E.
– A* is a R.E.
1 Rule for each case
Cse321, Programming Languages and Compilers
1704/19/23
Rules• ε
• “x”
• AB
• A|B
• A*
ε
x
BA
A
B
ε
ε
ε
ε
A
ε
ε
ε ε
Cse321, Programming Languages and Compilers
1804/19/23
Example: (a|b)*abb
ε
ε ε
ε
8
a
b
ε
a
bb
1
•Note the many ε transitions
•Loops caused by the *
•Non-Determinism, many paths out of a state on “a”
2 36
4 57
10 9
0
ε
εε
Cse321, Programming Languages and Compilers
1904/19/23
Building an NFA from a REdatatype Label = Epsilon | Char of char;
type Start = int;
type Finish = int;
datatype Edge = Edge of Start * Label * Finish;
val next = ref 0;fun new () = let val ref n = next in (next := n+1; n) end;
Ref makes a mutable variable
Semi colon separates commands (inside parenthesis)
Cse321, Programming Languages and Compilers
2004/19/23
fun nfa Empty = let val s = new() val f = new() in (s,f,[Edge(s,Epsilon,f)]):Nfa end
| nfa (C x) = let val s = new() val f = new() in (s,f,[Edge(s,Char x,f)]) end
| nfa (Union(x,y)) = let val (sx,fx,xes) = nfa x val (sy,fy,yes) = nfa y val s = new() val f = new() val newes = [Edge(s,Epsilon,sx) ,Edge(s,Epsilon,sy) ,Edge(fx,Epsilon,f) ,Edge(fy,Epsilon,f)] in (s,f,newes @ xes @ yes) end
ε
x
A
B
ε
ε
ε
ε
Cse321, Programming Languages and Compilers
2104/19/23
| nfa (Concat(x,y)) = let val (sx,fx,xes) = nfa x val (sy,fy,yes) = nfa y in (sx,fy,(Edge(fx,Epsilon,sy)):: (xes @ yes)) end
| nfa (Star r) = let val (sr,fr,res) = nfa r val s = new() val f = new() val newes = [Edge(s,Epsilon,sr) ,Edge(fr,Epsilon,f) ,Edge(s,Epsilon,f) ,Edge(f,Epsilon,s)] in (s,f,newes @ res) end
A
ε
ε
ε ε
BA
Cse321, Programming Languages and Compilers
2204/19/23
Example useval re1 =
Concat(Union(C #”+”,Union(C #”-”,Empty))
,Concat(C #”D”,Star (C #”D”)))
Val ex6 = nfa re1;
val ex6 =
(8,15,
[Edge (9,Epsilon,10),Edge (8,Epsilon,0)
,Edge (8,Epsilon,6),Edge (1,Epsilon,9)
,Edge (7,Epsilon,9),Edge (0,Char #,1)
,Edge (6,Epsilon,2),Edge (6,Epsilon,4)
,Edge (3,Epsilon,7),Edge (5,Epsilon,7)
,Edge (2,Char #,3),Edge (4,Epsilon,5),...]) : Nfa
Cse321, Programming Languages and Compilers
2304/19/23
Assignment #3CS321 Prog Lang & Compilers Assignment # 3 Assigned: Jan 22, 2007 Due: Wed. Jan 24, 2007Turn in a listing, and a transcript that shows you have tested your code. A minimum of 3 tests is necessary.Some functions may require more than 3 tests to receive full credit.
1) Write the following functions over lists. You must use pattern matching and recursion.
A. reverse a list so that its elements appear in the oposite order. reverse [1,2,3,4] ----> [4,3,2,1]
B. Count the number of occurrences of an element in a listcount 4 [1,2,3,4,5,4] ---> 2 count 4 [1,2,3,2,1] ---> 0
C. concatenate together a list of listsconcat [[1,2],[],[5,6]] ----> [1,2,5,6]
2) Using the datatype for Regular Expressions we defined in class
datatype RE = Empty | Union of RE * RE | Concat of RE * RE | Star of RE | C of char;
Write a function that turns a RE into a string, so that it can beprinted. Minimize the number of parenthesis, but keep the stringunambigouous by using the following rules. 1) Star has highest precedence so: ab* means a(b*) 2) Concat has the next highest precedence so: a+bc means a+(bc) 3) Union has lowest precedence so: a+bc+c* means a+(bc)+(c*) 4) Use the hash mark (#) as the empty string. 5) Special characters *+()\ should be escaped by using a preceeding backslash. So (Concat (C #"+") (C #"a")) should be "\+a"
Hints:1) The string concatenation operator is usefull: "abc" ^ "zx" -----> "abczx"2) Write this is two steps. First, fully paranethesize every RE Second, Change the function to not add the parenthesis which the rules don't require.