Professional Documents
Culture Documents
08/01/21 1
Cse321, Programming Languages and Compilers
Assignments
• Read Chapter 2, pages 27-72
– There will be a 5 minute quiz next lecture
08/01/21 2
Cse321, Programming Languages and Compilers
Homework 3
fun reverse [] = []
| reverse (x::xs) = reverse xs @ [x];
fun reverse2 xs =
let fun revonto [] ans = ans
| revonto (x::xs) ans = revonto xs (x::ans)
in revonto xs [] end;
fun count n [] = 0
| count n (x::xs) =
if n=x then 1 + count n xs else count n xs;
fun concatenate [] = []
| concatenate (xs::xss) = xs @ concatenate xss;
08/01/21 3
Cse321, Programming Languages and Compilers
Fully Parenthesized
fun toStr Empty = "#"
| toStr (C c) = implode [c]
| toStr (Concat(x,y)) = toStrP x ^ toStrP y
| toStr (Union(x,y)) = toStrP x ^"+"^toStrP y
| toStr (Star x) = toStrP x ^ "*"
and toStrP x = "("^toStr x^")";
- toStr re1;
val it = "((+)+((-)+(#)))((D)((D)*))" : string
08/01/21 4
Cse321, Programming Languages and Compilers
- toStr2 re1;
val it = "(++(-+#))(D(D*))" : string
08/01/21 5
Cse321, Programming Languages and Compilers
- toStr3 re1;
val it = "(\\++(-+#))(D(D*))" : string
08/01/21 6
Cse321, Programming Languages and Compilers
- toStr4 re1;
val it = "(\\++-+#)DD*" : string
08/01/21 7
Cse321, Programming Languages and Compilers
Representing Sets in ML
• We do it for ‘int’ others would be similar
• Key – Represent a set as an ordered list without duplicates
08/01/21 8
Cse321, Programming Languages and Compilers
fun setUnion [] [] = []
| setUnion [] ys = ys
| setUnion xs [] = xs
| setUnion (x::xs) (y::ys) =
case Int.compare (x,y) of
EQUAL => setUnion xs (y::ys)
| LESS => x:: setUnion xs (y::ys)
| GREATER => y :: setUnion (x::xs) ys;
fun setConcat [] = []
| setConcat (x::xs) = setUnion x (setConcat xs);
08/01/21 9
Cse321, Programming Languages and Compilers
fun sort [] = []
| sort (x::xs) = setAdd x (sort xs);
fun remDupFromOrdered [] = []
| remDupFromOrdered [x] = [x]
| remDupFromOrdered (x::y::zs) =
if x=y then remDupFromOrdered (y::zs)
else x:: remDupFromOrdered (y::zs);
08/01/21 10
Cse321, Programming Languages and Compilers
Cross Product
• Consider the two sets
– [1,2,6]
– [3,5]
• The cross product is a set of pairs
– all possible pairs (x,y) where x comes from [1,2,6] and y comes from [3,5]
– [(1,3),(1,5),(2,3),(2,5),(6,3),(6,5)]
ML code
fun innerWhile x [] ans = ans
| innerWhile x (y::ys) ans =
innerWhile x ys ((x,y)::ans) ;
fun outerWhile ys [] ans = ans
| outerWhile ys (x::xs) ans =
outerWhile ys xs (innerWhile x ys ans);
( ans := [ ]
; while not (null xs) do
let val x = hd xs
in while not (null ys) do
let val y = hd ys
in ans := (x,y)::ans
xs := tl xs
ys := tl ys
end
end)
08/01/21 12
Cse321, Programming Languages and Compilers
power 3 ["a","xy"];
Val it = ["xyaxy","xyaa","xyxyxy","xyxya“
,"aaxy","aaa","axyxy","axya"]
08/01/21 13
Cse321, Programming Languages and Compilers
Concat(Union(C #”+”,Union(C #”-”,Empty))
,Concat(C #”D”,Star (C #”D”)))
(+|-| ε)DD*
0 + 1
ε
ε
8
val ex6 = 9 ε 10
(8,15, ε -
[Edge (9,Epsilon,10)
ε 2 3 ε D
,Edge (8,Epsilon,0) 6 7
,Edge (8,Epsilon,6) ε ε 11
,Edge (1,Epsilon,9) 4 ε 5
,Edge (7,Epsilon,9) ε
,Edge (0,Char #"+",1)
14
,Edge (6,Epsilon,2)
,Edge (6,Epsilon,4) ε
,Edge (3,Epsilon,7)
,Edge (5,Epsilon,7) ε 12
,Edge (2,Char #"-",3)
,Edge (4,Epsilon,5) D ε
,Edge (11,Epsilon,14)
,Edge (10,Char #"D",11)
13
,Edge (14,Epsilon,12) ε
,Edge (13,Epsilon,15)
,Edge (14,Epsilon,15) 15
,Edge (15,Epsilon,14)
,Edge (12,Char #"D",13)]) : Nfa
08/01/21 14
Cse321, Programming Languages and Compilers
Epsilon transitions
0 + 1
ε
ε
8
9 ε 10
ε -
ε 2 3 ε D
6 7
ε ε 11
4 ε 5
ε
• Note that in state 8, one can get to 14
{0,6,2,4,5,7,9,10} by not consuming any ε
characters.
ε 12
• We need only take epsilon transitions
D ε
• This set is called the epsilon-closure 13
– eclosure [8] = {0,6,2,4,5,7,9,10} ε
• How do we compute this? 15
08/01/21 15
Cse321, Programming Languages and Compilers
1-step epsilon
0 + 1
ε
ε
8
9 ε 10
ε -
ε 2 3 ε D
6 7
ε ε 11
4 ε 5
ε
• Note that every state has a 1-step epsilon 14
set. ε
many states
• oneStep 8 = [0,6] have empty 12
epsilon sets ε
• oneStep 0 = [ ] D ε
• oneStep 4 = [5] 13
ε
• oneStep 6 = [2,4]
15
• ...
08/01/21 16
Cse321, Programming Languages and Compilers
08/01/21 17
Cse321, Programming Languages and Compilers
Step – by - step
val edges = - oneStep 8 (Edge(5,Epsilon,6));
[Edge (9,Epsilon,10)
,Edge (8,Epsilon,0) val it = [ ]
,Edge (8,Epsilon,6)
,Edge (1,Epsilon,9)
,Edge (7,Epsilon,9)
,Edge (0,Char #"+",1) - oneStep 8 (Edge(8,Epsilon,6));
,Edge (6,Epsilon,2)
,Edge (6,Epsilon,4)] val it = [6] we only get output if
the start state
matches n
fun oneStepFromEdges n es =
List.concat - map (oneStep 8) edges;
(map (oneStep n) es);
val it = [[],[0],[6],[],[],[],[],[]]
- setConcat [[],[0],[6],[],[],[],[],[]];
val it = [0,6] : int list
08/01/21 18
Cse321, Programming Languages and Compilers
08/01/21 19
Cse321, Programming Languages and Compilers
Iterate
fun oneStepFromSet es states =
setConcat (map (oneStepFromEdges es) states);
08/01/21 20
Cse321, Programming Languages and Compilers
eclose
- eclose edges [8];
[8]
[0,6]
[0,6,8]
fun eclose edges states =
-----------------------
let val new = oneStepFromSet edges states [0,6,8]
val union = setUnion new states [0,2,4,6]
[0,2,4,6,8]
in if union = states -----------------------
then states [0,2,4,6,8]
else ( print (setToString states) [0,2,4,5,6]
[0,2,4,5,6,8]
; print (setToString new) -----------------------
; print (setToString union) [0,2,4,5,6,8]
[0,2,4,5,6,7]
; print "-------------\n" [0,2,4,5,6,7,8]
; eclose edges union ) -----------------------
end; [0,2,4,5,6,7,8]
[0,2,4,5,6,7,9]
[0,2,4,5,6,7,8,9]
-----------------------
[0,2,4,5,6,7,8,9]
[0,2,4,5,6,7,9,10]
[0,2,4,5,6,7,8,9,10]
-----------------------
val it = [0,2,4,5,6,7,8,9,10]
08/01/21 21
Cse321, Programming Languages and Compilers
Fixed-point functions
fun fix f init =
let val new = f init
in if new=init then new else fix f new end;
08/01/21 22
Cse321, Programming Languages and Compilers
Simulating an NFA
ε
a ε
2 3
ε
ε 6 ε a b b
0 1
b
7 8 9 10
ε 4 5 ε
ε
08/01/21 23
Cse321, Programming Languages and Compilers
Example: “ababb”
ε
a ε
2 3
ε
ε 6 ε a b b
0 1
b
7 8 9 10
ε 4 5 ε
ε
Accepting Algorithm
state input S := S0;
{0;1,2,4,7} “ababb” C := nextchar();
{3,8;6,7,0,1,2,4} “babb” while C <> eof do
{9,5;6,7,0,1,2,4} “abb” { S := move(S,C);
{3,8;6,7,0,1,2,4} “bb” C := nextchar()
{9,5;6,7,0,1,2,4} “b” };
{10,5;6,7,0,1,2,4} ““ if S is in F
then return “yes”
Final state includes the accepting state,
else return “no”
10, so the string is accepted.
08/01/21 24
Cse321, Programming Languages and Compilers
08/01/21 25
Cse321, Programming Languages and Compilers
Code
fun accept (start,final,edges) input =
nfa edges final
(eclose2 edges [start])
(explode input)
08/01/21 26
Cse321, Programming Languages and Compilers
A failing examples
- accept ex6 "+DaD“;
State = [0,2,4,5,6,7,8,9,10]
Input = +DaD
On '+' we can get to [1,9,10]
State = [1,9,10]
Input = DaD
On 'D' we can get to [11,12,14,15]
State = [11,12,14,15]
Input = aD
On 'a' we can get to []
val it = false : bool
08/01/21 27
Cse321, Programming Languages and Compilers
Making a DFA
ε
a ε
2 3
ε
ε 6 ε a b b
0 1
b
7 8 9 10
ε 4 5 ε
ε
a ε
2 3
ε
ε 6 ε a b b
0 1
b
7 8 9 10
ε 4 5 ε
ε
• Initial state is 0
• ε –closure of 0 is {0;1,2,4,7}
• From any of {0;1,2,4,7}
– We can make a transition on “a” to {3,8}
– We can make a transition on “b” to {5}
• ε –closure of {3,8} is {3,8;6,7,0,1,2,4}
• ε –closure of {5} is {5;6,7,0,1,2,4}
• From any of {3,8;6,7,0,1,2,4}
– We can make a transition on “a” to {3,8} -- which we’ve seen before
– We can make a transition on “b” to {9,5} -- which is new
• From any of {4;6,7,0,1,2,4}
– We can make a transition on “a” to {3,8} -- which we’ve seen before
– We can make a transition on “b” to {5} – which we’ve seen before
ε –closure of {9,5} is {9;6,7,0,1,2,4}
• From any of {9;6,7,0,1,2,4}
– We can make a transition on “a” to {3,8} -- which we’ve seen before
– We can make a transition on “b” to {10,5} – which is new
08/01/21 29
Cse321, Programming Languages and Compilers
a ε
2 3
ε
ε 6 ε a b b
0 1
b
7 8 9 10
ε 4 5 ε
ε
Dstates Dtran on a Dtran on b
A{0;1,2,4,7} B {3,8;1,2,4,6,7} C{5;1,2,4,6,7}
B{1,2,3,4,6,7,8} B{3,8;1,2,4,6,7} D{5,9;6,7,1,2,4}
C{1,2,4,5,6,7} B{3,8;1,2,4,6,7} C{5;1,2,4,6,7}
D{1,2,4,5,6,7,9} B{3,8;1,2,4,6,7} E{5,10;1,2,4,6,7}
E{1,2,4,5,6,7,10} B{3,8;1,2,4,6,7} C{5;1,2,4,6,7}
b
C
b
b
A a b b
B D
a E
a a
08/01/21 30