Cse 321 Programming Languages and Compilers Lecture 4

  • Slides: 30
Download presentation
Cse 321, Programming Languages and Compilers Lecture #4, Jan. 24, 2007 • Homework 3

Cse 321, Programming Languages and Compilers Lecture #4, Jan. 24, 2007 • Homework 3 • Representing sets as lists • the cross product of two sets • epsilon transitions • epsilon - closure • Interpreting an NFA • NFA to DFA Labs scheduled: 1/31/2022 Thursday 4: 00 pm, Friday at 9: 00 am 1

Cse 321, Programming Languages and Compilers Assignments • Read Chapter 2, pages 27 -72

Cse 321, Programming Languages and Compilers Assignments • Read Chapter 2, pages 27 -72 – There will be a 5 minute quiz next lecture • Programming exercise 4 is listed on the web page. It is due next Monday. – exercise writing accumulating parameter functions – a guided exercise to write the e-closure in 10 easy steps 1/31/2022 2

Cse 321, Programming Languages and Compilers Homework 3 fun reverse [] = [] |

Cse 321, Programming Languages and Compilers Homework 3 fun reverse [] = [] | reverse (x: : xs) = reverse xs @ [x]; fun reverse 2 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; 1/31/2022 3

Cse 321, Programming Languages and Compilers Fully Parenthesized fun | | and to. Str

Cse 321, Programming Languages and Compilers Fully Parenthesized fun | | and to. Str Empty = "#" to. Str (C c) = implode [c] to. Str (Concat(x, y)) = to. Str. P x ^ to. Str. P y to. Str (Union(x, y)) = to. Str. P x ^"+"^to. Str. P y to. Str (Star x) = to. Str. P x ^ "*" to. Str. P x = "("^to. Str x^")"; - to. Str re 1; val it = "((+)+((-)+(#)))((D)*))" : string 1/31/2022 4

Cse 321, Programming Languages and Compilers Not parenthesizing leaf REs fun | | and

Cse 321, Programming Languages and Compilers Not parenthesizing leaf REs fun | | and | | to. Str 2 Empty = "#" to. Str 2 (C c) = implode [c] to. Str 2 (Concat(x, y)) = to. Str 2 P x ^ to. Str 2 P y to. Str 2 (Union(x, y)) = to. Str 2 P x ^"+"^to. Str 2 P y to. Str 2 (Star x) = to. Str 2 P x ^ "*" to. Str 2 P (x as (C _)) = to. Str 2 x to. Str 2 P (x as Empty) = to. Str 2 x to. Str 2 P x = "("^to. Str 2 x^")"; - to. Str 2 re 1; val it = "(++(-+#))(D(D*))" : string 1/31/2022 5

Cse 321, Programming Languages and Compilers Escaping special characters fun | | escape escape

Cse 321, Programming Languages and Compilers Escaping special characters fun | | escape escape #"+" = "\+" #"*" = "\*" #"(" = "\(" #")" = "\)" c = implode [c]; fun | | and | | to. Str 3 Empty = "#" to. Str 3 (C c) = escape c to. Str 3 (Concat(x, y)) = to. Str 3 P x ^ to. Str 3 P y to. Str 3 (Union(x, y)) = to. Str 3 P x ^"+"^to. Str 3 P y to. Str 3 (Star x) = to. Str 3 P x ^ "*" to. Str 3 P (x as (C _)) = to. Str 3 x to. Str 3 P (x as Empty) = to. Str 3 x to. Str 3 P x = "("^to. Str 3 x^")"; - to. Str 3 re 1; val it = "(\++(-+#))(D(D*))" : string 1/31/2022 6

Cse 321, Programming Languages and Compilers A separate rule for each sub-RE fun |

Cse 321, Programming Languages and Compilers A separate rule for each sub-RE fun | | | | and | | to. Str 4 to. Str. C to. Str. U to. Str. S Empty = "#" (C c) = escape c (Concat(x, y)) = to. Str. C x ^ to. Str. C y (Union(x, y)) = to. Str. U x ^"+"^to. Str. U y (Star x) = to. Str. S x ^ "*" (x as (C _)) = to. Str 4 x (x as Empty) = to. Str 4 x (x as (Concat _)) = to. Str 4 x (x as (Star _)) = to. Str 4 x x = "("^to. Str 4 x^")" (x as (C _)) = to. Str 4 x (x as Empty) = to. Str 4 x (x as (Union _)) = to. Str 4 x x = "("^to. Str 4 x^")" (x as (C _)) = to. Str 4 x (x as Empty) = to. Str 4 x x = "("^to. Str 4 x^")"; - to. Str 4 re 1; val it = "(\++-+#)DD*" : string 1/31/2022 7

Cse 321, Programming Languages and Compilers Representing Sets in ML • We do it

Cse 321, 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 fun mem x [] = false | mem x (y: : ys) = if x=y then true else mem x ys; datatype order = EQUAL | LESS | GREATER; fun set. Add x [] = [x] Note the use of the Int library compare function. | set. Add x (y: : ys) = case Int. compare (x, y) of EQUAL => (y: : ys) | LESS => x: : ys | GREATER => y : : set. Add x ys; 1/31/2022 8

Cse 321, Programming Languages and Compilers Union of two sets • Take advantage of

Cse 321, Programming Languages and Compilers Union of two sets • Take advantage of the fact that the lists are ordered, this allows union with time proportional to what? fun | | | set. Union [] [] = [] set. Union [] ys = ys set. Union xs [] = xs set. Union (x: : xs) (y: : ys) = case Int. compare (x, y) of EQUAL => set. Union xs (y: : ys) | LESS => x: : set. Union xs (y: : ys) | GREATER => y : : set. Union (x: : xs) ys; fun set. Concat [] = [] | set. Concat (x: : xs) = set. Union x (set. Concat xs); 1/31/2022 9

Cse 321, Programming Languages and Compilers Can we turn a normal list into a

Cse 321, Programming Languages and Compilers Can we turn a normal list into a set? (* Turn a list into a set *) (* sort and remove duplicates. *) fun sort [] = [] | sort (x: : xs) = set. Add x (sort xs); fun rem. Dup. From. Ordered [] = [] | rem. Dup. From. Ordered [x] = [x] | rem. Dup. From. Ordered (x: : y: : zs) = if x=y then rem. Dup. From. Ordered (y: : zs) else x: : rem. Dup. From. Ordered (y: : zs); fun norm xs = rem. Dup. From. Ordered(sort xs); 1/31/2022 10

Cse 321, Programming Languages and Compilers Cross Product • Consider the two sets –

Cse 321, 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)] • How could we compute this? In pseudo code ( ans : = [ ] ; while not (null xs) do let val x = hd xs val ptr = y in while not (null ptr) do let val y = hd ptr in ans : = (x, y): : ans ptr : = tl ptr end; xs : = tl xs end) 1/31/2022 This has the pattern of a nested set of accumulating parameter functions! 11

Cse 321, Programming Languages and Compilers ML code fun inner. While x [] ans

Cse 321, Programming Languages and Compilers ML code fun inner. While x [] ans = ans | inner. While x (y: : ys) ans = inner. While x ys ((x, y): : ans) ; fun outer. While ys [] ans = ans | outer. While ys (x: : xs) ans = outer. While ys xs (inner. While x ys ans); fun cross xs ys = outer. While ys xs []; 1/31/2022 ( 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) 12

Cse 321, Programming Languages and Compilers The power of a set of strings if

Cse 321, Programming Languages and Compilers The power of a set of strings if A = [“x”, “y”] what is AA, or AAAA fun power 0 x = [""] | power n x = map (op ^) (cross x (power (n-1) x)); power 3 ["a", "xy"]; Val it = ["xyaxy", "xyaa", "xyxyxy", "xyxya“ , "aaxy", "aaa", "axyxy", "axya"] 1/31/2022 13

Cse 321, Programming Languages and Compilers Concat(Union(C #”+”, Union(C #”-”, Empty)) , Concat(C #”D”,

Cse 321, Programming Languages and Compilers Concat(Union(C #”+”, Union(C #”-”, Empty)) , Concat(C #”D”, Star (C #”D”))) (+|-| ε)DD* ε + 0 1 ε 8 val ex 6 = ε (8, 15, ε [Edge (9, Epsilon, 10) 6 , 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) , Edge (11, Epsilon, 14) , Edge (10, Char #"D", 11) , Edge (14, Epsilon, 12) , Edge (13, Epsilon, 15) , Edge (14, Epsilon, 15) , Edge (15, Epsilon, 14) , Edge (12, Char #"D", 13)]) : Nfa 1/31/2022 2 4 ε 9 3 5 ε ε ε 10 D 7 11 ε 14 ε 12 ε D 13 ε 15 14 ε

Cse 321, Programming Languages and Compilers Epsilon transitions + 0 ε 1 ε 8

Cse 321, Programming Languages and Compilers Epsilon transitions + 0 ε 1 ε 8 ε 2 ε - 6 ε 4 ε 9 3 5 ε ε • Note that in state 8, one can get to {0, 6, 2, 4, 5, 7, 9, 10} by not consuming any characters. • We need only take epsilon transitions • This set is called the epsilon-closure 1/31/2022 10 D 7 11 ε 14 ε 12 ε D 13 – eclosure [8] = {0, 6, 2, 4, 5, 7, 9, 10} • How do we compute this? ε ε 15 15 ε

Cse 321, Programming Languages and Compilers 1 -step epsilon + 0 ε 1 ε

Cse 321, Programming Languages and Compilers 1 -step epsilon + 0 ε 1 ε 8 ε 2 ε - 6 ε 4 ε 9 3 5 • Note that every state has a 1 -step epsilon set. many states have empty • one. Step 8 = [0, 6] epsilon sets • one. Step 0 = [ ] • one. Step 4 = [5] • one. Step 6 = [2, 4] • . . . 1/31/2022 ε ε ε 10 D 7 11 ε 14 ε 12 ε D 13 ε 15 16 ε

Cse 321, Programming Languages and Compilers Recall how an NFA is represented val ex

Cse 321, Programming Languages and Compilers Recall how an NFA is represented val ex 6 = (8, 15, [Edge (9, Epsilon, 10) , 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) , Edge (11, Epsilon, 14) , Edge (10, Char #"D", 11) , Edge (14, Epsilon, 12) , Edge (13, Epsilon, 15) , Edge (14, Epsilon, 15) , Edge (15, Epsilon, 14) , Edge (12, Char #"D", 13)]) 1/31/2022 fun one. Step n (Edge(s, Epsilon, f)) = if n=s then [f] else [] | one. Step n (Edge(s, Char _, f)) = [] • Given a list of edges we apply this function to every edge. fun one. Step. From. Edges es n = set. Concat(map (one. Step n) es); • How does this work? 17

Cse 321, Programming Languages and Compilers Step – by - step val edges =

Cse 321, Programming Languages and Compilers Step – by - step val edges = [Edge (9, Epsilon, 10) , Edge (8, Epsilon, 6) , Edge (1, Epsilon, 9) , Edge (7, Epsilon, 9) , Edge (0, Char #"+", 1) , Edge (6, Epsilon, 2) , Edge (6, Epsilon, 4)] - one. Step 8 (Edge(5, Epsilon, 6)); val it = [ ] - one. Step 8 (Edge(8, Epsilon, 6)); val it = [6] we only get output if the start state matches n fun one. Step. From. Edges n es = List. concat (map (one. Step n) es); - map (one. Step 8) edges; val it = [[], [0], [6], [], [], []] - set. Concat [[], [0], [6], [], [], []]; val it = [0, 6] : int list 1/31/2022 18

Cse 321, Programming Languages and Compilers One step from a set of states •

Cse 321, Programming Languages and Compilers One step from a set of states • Note one. Step. From. Edge only gives the e-states from a single starting state. • What if I wanted all the reachable states on epsilon from both 8 or 9? 10 + 0 ε D 1 ε ε ε 8 ε 2 ε - 6 ε 4 ε 14 9 3 5 ε ε ε 11 7 ε 12 D 13 ε fun one. Step. From. Set es states = set. Concat (map (one. Step. From. Edges es) states); 15 1/31/2022 19 ε

Cse 321, Programming Languages and Compilers Iterate fun one. Step. From. Set es states

Cse 321, Programming Languages and Compilers Iterate fun one. Step. From. Set es states = set. Concat (map (one. Step. From. Edges es) states); - one. Step. From. Set edges [8]; val it = [0, 6] : int list - one. Step. From. Set edges (set. Union [8] [0, 6]); val it = [0, 2, 4, 6] : int list - one. Step. From. Set edges (set. Union [0, 6, 8] [0, 2, 4, 6]); val it = [0, 2, 4, 5, 6] : int list - one. Step. From. Set edges (set. Union [0, 2, 4, 6, 8] [0, 2, 4, 5, 6]); val it = [0, 2, 4, 5, 6, 7] : int list - one. Step. From. Set edges (set. Union [0, 2, 4, 5, 6, 8] [0, 2, 4, 5, 6, 7]); val it = [0, 2, 4, 5, 6, 7, 9] : int list - one. Step. From. Set edges (set. Union [0, 2, 4, 5, 6, 7, 8] [0, 2, 4, 5, 6, 7, 9]); val it = [0, 2, 4, 5, 6, 7, 9, 10] : int list - one. Step. From. Set edges (set. Union [0, 2, 4, 5, 6, 7, 8, 9] [0, 2, 4, 5, 6, 7, 9, 10]); val it = [0, 2, 4, 5, 6, 7, 9, 10] : int list 1/31/2022 20

Cse 321, Programming Languages and Compilers eclose fun eclose edges states = let val

Cse 321, Programming Languages and Compilers eclose fun eclose edges states = let val new = one. Step. From. Set edges states val union = set. Union new states in if union = states then states else ( print (set. To. String states) ; print (set. To. String new) ; print (set. To. String union) ; print "-------n" ; eclose edges union ) end; 1/31/2022 - eclose edges [8]; [8] [0, 6, 8] -----------[0, 6, 8] [0, 2, 4, 6, 8] -----------[0, 2, 4, 6, 8] [0, 2, 4, 5, 6, 8] -----------[0, 2, 4, 5, 6, 8] [0, 2, 4, 5, 6, 7, 8] -----------[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] 21

Cse 321, Programming Languages and Compilers Fixed-point functions fun fix f init = let

Cse 321, 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; fun eclose 2 edges xs = let fun step x = set. Union x (one. Step. From. Set edges x) in fix step xs end; - eclose 2 edges [8]; val it = [0, 2, 4, 5, 6, 7, 8, 9, 10] : int list 1/31/2022 22

Cse 321, Programming Languages and Compilers Simulating an NFA ε 0 ε ε 1

Cse 321, Programming Languages and Compilers Simulating an NFA ε 0 ε ε 1 ε 2 4 a 3 ε 6 b ε 5 ε ε 7 a 8 b 9 b 10 • Given a string, say “ababb” use the NFA to determine if the NFA “accepts” the string. • ε -closure , All those states reachable from a given set on transitions via ε transitions only • Initial state is ε-closure of {0} • For character in the input string keep track of what set-of-states the machine could possibly be in. 1/31/2022 23

Cse 321, Programming Languages and Compilers Example: “ababb” ε 0 ε ε 1 ε

Cse 321, Programming Languages and Compilers Example: “ababb” ε 0 ε ε 1 ε 2 4 state a 3 ε 6 b ε 5 ε ε input 7 a 8 b 9 “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” {9, 5; 6, 7, 0, 1, 2, 4} “b” }; {10, 5; 6, 7, 0, 1, 2, 4} ““ if S is in F 10, so the string is accepted. 1/31/2022 10 Accepting Algorithm S : = S 0; {0; 1, 2, 4, 7} Final state includes the accepting state, b C : = nextchar() then return “yes” else return “no” 24

Cse 321, Programming Languages and Compilers fun transition. On c states edges = let

Cse 321, Programming Languages and Compilers fun transition. On c states edges = let fun good (Edge(s, Char x, f)) = (c=x) andalso (mem s states) | good _ = false fun finish (Edge(s, _, f)) = f in map finish (List. filter good edges) end; fun nfa edges final states [] = mem final states | nfa edges final states (c: : cs) = let val _ = print ("State = "^set. To. String states) val _ = print ("Input = "^implode(c: : cs)^"n") val new = eclose 2 edges (transition. On c states edges) val _ = print ("On '"^implode [c]^ "' we can get to "^ set. To. String new) in if new = [] then false else nfa edges final new cs end; 1/31/2022 25

Cse 321, Programming Languages and Compilers Code fun accept (start, final, edges) input =

Cse 321, Programming Languages and Compilers Code fun accept (start, final, edges) input = nfa edges final (eclose 2 edges [start]) (explode input) - accept ex 6 "+DDD"; State = [0, 2, 4, 5, 6, 7, 8, 9, 10] Input = +DDD On '+' we can get to [1, 9, 10] State = [1, 9, 10] Input = DDD On 'D' we can get to [11, 12, 14, 15] State = [11, 12, 14, 15] Input = DD On 'D' we can get to [12, 13, 14, 15] State = [12, 13, 14, 15] Input = D On 'D' we can get to [12, 13, 14, 15] val it = true : bool 1/31/2022 26

Cse 321, Programming Languages and Compilers A failing examples - accept ex 6 "+Da.

Cse 321, Programming Languages and Compilers A failing examples - accept ex 6 "+Da. D“; State = [0, 2, 4, 5, 6, 7, 8, 9, 10] Input = +Da. D On '+' we can get to [1, 9, 10] State = [1, 9, 10] Input = Da. D On 'D' we can get to [11, 12, 14, 15] State = [11, 12, 14, 15] Input = a. D On 'a' we can get to [] val it = false : bool - accept ex 6 "+"; State = [0, 2, 4, 5, 6, 7, 8, 9, 10] Input = + On '+' we can get to [1, 9, 10] val it = false : bool 1/31/2022 27

Cse 321, Programming Languages and Compilers Making a DFA ε 0 ε ε 1

Cse 321, Programming Languages and Compilers Making a DFA ε 0 ε ε 1 a 2 ε 3 ε 6 b 4 ε ε 5 ε 7 a 8 b 9 b 10 To turn an NFA into a DFA Simulate all possible transitions simultaneously. Algorithm is a famous one and is called “the subset construction”. DStates : = { (e-clos(S 0), unmarked) }; while exists (T, unmarked) in DStates do { mark T; for-each input symbol { U a do : = e-clos(move(T, a)); if U is not in DStates then add (U, unmarked) to Dstates Dtran[T, a] : = U 1/31/2022 } } 28

Cse 321, Programming Languages and Compilers ε 0 ε ε 1 ε 2 4

Cse 321, Programming Languages and Compilers ε 0 ε ε 1 ε 2 4 a 3 ε 6 b ε 5 ε ε 7 a 8 b 9 b 10 • 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 1/31/2022 29

Cse 321, Programming Languages and Compilers Example Algorithm use ε 0 ε ε 1

Cse 321, Programming Languages and Compilers Example Algorithm use ε 0 ε ε 1 ε a 2 3 6 b 4 ε ε 5 Dstates a 7 8 Dtran on a b 9 b 10 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 a 1/31/2022 b a D b E a 30