|
1 import scala.language.implicitConversions |
|
2 import scala.language.reflectiveCalls |
|
3 import scala.annotation.tailrec |
|
4 |
|
5 abstract class Rexp |
|
6 case object ZERO extends Rexp |
|
7 case object ONE extends Rexp |
|
8 case class CHAR(c: Char) extends Rexp |
|
9 case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
|
10 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
|
11 case class STAR(r: Rexp) extends Rexp |
|
12 case class RECD(x: String, r: Rexp) extends Rexp |
|
13 |
|
14 abstract class Val |
|
15 case object Empty extends Val |
|
16 case class Chr(c: Char) extends Val |
|
17 case class Sequ(v1: Val, v2: Val) extends Val |
|
18 case class Left(v: Val) extends Val |
|
19 case class Right(v: Val) extends Val |
|
20 case class Stars(vs: List[Val]) extends Val |
|
21 case class Rec(x: String, v: Val) extends Val |
|
22 |
|
23 // some convenience for typing in regular expressions |
|
24 def charlist2rexp(s : List[Char]): Rexp = s match { |
|
25 case Nil => ONE |
|
26 case c::Nil => CHAR(c) |
|
27 case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
|
28 } |
|
29 implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList) |
|
30 |
|
31 implicit def RexpOps(r: Rexp) = new { |
|
32 def | (s: Rexp) = ALT(r, s) |
|
33 def % = STAR(r) |
|
34 def ~ (s: Rexp) = SEQ(r, s) |
|
35 } |
|
36 |
|
37 implicit def stringOps(s: String) = new { |
|
38 def | (r: Rexp) = ALT(s, r) |
|
39 def | (r: String) = ALT(s, r) |
|
40 def % = STAR(s) |
|
41 def ~ (r: Rexp) = SEQ(s, r) |
|
42 def ~ (r: String) = SEQ(s, r) |
|
43 def $ (r: Rexp) = RECD(s, r) |
|
44 } |
|
45 |
|
46 // nullable function: tests whether the regular |
|
47 // expression can recognise the empty string |
|
48 def nullable (r: Rexp) : Boolean = r match { |
|
49 case ZERO => false |
|
50 case ONE => true |
|
51 case CHAR(_) => false |
|
52 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
53 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
54 case STAR(_) => true |
|
55 case RECD(_, r1) => nullable(r1) |
|
56 } |
|
57 |
|
58 // derivative of a regular expression w.r.t. a character |
|
59 def der (c: Char, r: Rexp) : Rexp = r match { |
|
60 case ZERO => ZERO |
|
61 case ONE => ZERO |
|
62 case CHAR(d) => if (c == d) ONE else ZERO |
|
63 case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
64 case SEQ(r1, r2) => |
|
65 if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
66 else SEQ(der(c, r1), r2) |
|
67 case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
68 case RECD(_, r1) => der(c, r1) |
|
69 } |
|
70 |
|
71 // derivative w.r.t. a string (iterates der) |
|
72 def ders (s: List[Char], r: Rexp) : Rexp = s match { |
|
73 case Nil => r |
|
74 case c::s => ders(s, der(c, r)) |
|
75 } |
|
76 |
|
77 // extracts a string from value |
|
78 def flatten(v: Val) : String = v match { |
|
79 case Empty => "" |
|
80 case Chr(c) => c.toString |
|
81 case Left(v) => flatten(v) |
|
82 case Right(v) => flatten(v) |
|
83 case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
|
84 case Stars(vs) => vs.map(flatten(_)).mkString |
|
85 case Rec(_, v) => flatten(v) |
|
86 } |
|
87 |
|
88 |
|
89 // extracts an environment from a value |
|
90 def env(v: Val) : List[(String, String)] = v match { |
|
91 case Empty => Nil |
|
92 case Chr(c) => Nil |
|
93 case Left(v) => env(v) |
|
94 case Right(v) => env(v) |
|
95 case Sequ(v1, v2) => env(v1) ::: env(v2) |
|
96 case Stars(vs) => vs.flatMap(env) |
|
97 case Rec(x, v) => (x, flatten(v))::env(v) |
|
98 } |
|
99 |
|
100 // injection part |
|
101 def mkeps(r: Rexp) : Val = r match { |
|
102 case ONE => Empty |
|
103 case ALT(r1, r2) => |
|
104 if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
|
105 case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
|
106 case STAR(r) => Stars(Nil) |
|
107 case RECD(x, r) => Rec(x, mkeps(r)) |
|
108 } |
|
109 |
|
110 |
|
111 def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
|
112 case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
|
113 case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
|
114 case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
|
115 case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
|
116 case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1)) |
|
117 case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
|
118 case (CHAR(d), Empty) => Chr(c) |
|
119 case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
|
120 } |
|
121 |
|
122 // main unsimplified lexing function (produces a value) |
|
123 def lex(r: Rexp, s: List[Char]) : Val = s match { |
|
124 case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched") |
|
125 case c::cs => inj(r, c, lex(der(c, r), cs)) |
|
126 } |
|
127 |
|
128 def lexing(r: Rexp, s: String) : Val = lex(r, s.toList) |
|
129 |
|
130 |
|
131 // some "rectification" functions for simplification |
|
132 def F_ID(v: Val): Val = v |
|
133 def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v)) |
|
134 def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v)) |
|
135 def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
|
136 case Right(v) => Right(f2(v)) |
|
137 case Left(v) => Left(f1(v)) |
|
138 } |
|
139 def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
|
140 case Sequ(v1, v2) => Sequ(f1(v1), f2(v2)) |
|
141 } |
|
142 def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = (v:Val) => Sequ(f1(Empty), f2(v)) |
|
143 def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = (v:Val) => Sequ(f1(v), f2(Empty)) |
|
144 def F_RECD(f: Val => Val) = (v:Val) => v match { |
|
145 case Rec(x, v) => Rec(x, f(v)) |
|
146 } |
|
147 def F_ERROR(v: Val): Val = throw new Exception("error") |
|
148 |
|
149 // simplification of regular expressions returning also an |
|
150 // rectification function; no simplification under STAR |
|
151 def simp(r: Rexp): (Rexp, Val => Val) = r match { |
|
152 case ALT(r1, r2) => { |
|
153 val (r1s, f1s) = simp(r1) |
|
154 val (r2s, f2s) = simp(r2) |
|
155 (r1s, r2s) match { |
|
156 case (ZERO, _) => (r2s, F_RIGHT(f2s)) |
|
157 case (_, ZERO) => (r1s, F_LEFT(f1s)) |
|
158 case _ => if (r1s == r2s) (r1s, F_LEFT(f1s)) |
|
159 else (ALT (r1s, r2s), F_ALT(f1s, f2s)) |
|
160 } |
|
161 } |
|
162 case SEQ(r1, r2) => { |
|
163 val (r1s, f1s) = simp(r1) |
|
164 val (r2s, f2s) = simp(r2) |
|
165 (r1s, r2s) match { |
|
166 case (ZERO, _) => (ZERO, F_ERROR) |
|
167 case (_, ZERO) => (ZERO, F_ERROR) |
|
168 case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s)) |
|
169 case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s)) |
|
170 case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s)) |
|
171 } |
|
172 } |
|
173 case RECD(x, r1) => { |
|
174 val (r1s, f1s) = simp(r1) |
|
175 (RECD(x, r1s), F_RECD(f1s)) |
|
176 } |
|
177 case r => (r, F_ID) |
|
178 } |
|
179 |
|
180 def lex_simp(r: Rexp, s: List[Char]) : Val = s match { |
|
181 case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched") |
|
182 case c::cs => { |
|
183 val (r_simp, f_simp) = simp(der(c, r)) |
|
184 inj(r, c, f_simp(lex_simp(r_simp, cs))) |
|
185 } |
|
186 } |
|
187 |
|
188 def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList) |
|
189 |
|
190 |
|
191 // Some Tests |
|
192 //============ |
|
193 |
|
194 def time[T](code: => T) = { |
|
195 val start = System.nanoTime() |
|
196 val result = code |
|
197 val end = System.nanoTime() |
|
198 println((end - start)/1.0e9) |
|
199 result |
|
200 } |
|
201 |
|
202 val r0 = ("a" | "ab") ~ ("b" | "") |
|
203 println(lexing(r0, "ab")) |
|
204 println(lexing_simp(r0, "ab")) |
|
205 |
|
206 val r1 = ("a" | "ab") ~ ("bcd" | "cd") |
|
207 println(lexing_simp(r1, "abcd")) |
|
208 |
|
209 println(lexing_simp((("" | "a") ~ ("ab" | "b")), "ab")) |
|
210 println(lexing_simp((("" | "a") ~ ("b" | "ab")), "ab")) |
|
211 println(lexing_simp((("" | "a") ~ ("c" | "ab")), "ab")) |
|
212 |
|
213 |
|
214 |
|
215 // Two Simple Tests for the While Language |
|
216 //======================================== |
|
217 |
|
218 // Lexing Rules |
|
219 |
|
220 def PLUS(r: Rexp) = r ~ r.% |
|
221 val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" |
|
222 val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
|
223 val ID = SYM ~ (SYM | DIGIT).% |
|
224 val NUM = PLUS(DIGIT) |
|
225 val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false" |
|
226 val SEMI: Rexp = ";" |
|
227 val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/" |
|
228 val WHITESPACE = PLUS(" " | "\n" | "\t") |
|
229 val RPAREN: Rexp = ")" |
|
230 val LPAREN: Rexp = "(" |
|
231 val BEGIN: Rexp = "{" |
|
232 val END: Rexp = "}" |
|
233 val STRING: Rexp = "\"" ~ SYM.% ~ "\"" |
|
234 |
|
235 |
|
236 val WHILE_REGS = (("k" $ KEYWORD) | |
|
237 ("i" $ ID) | |
|
238 ("o" $ OP) | |
|
239 ("n" $ NUM) | |
|
240 ("s" $ SEMI) | |
|
241 ("str" $ STRING) | |
|
242 ("p" $ (LPAREN | RPAREN)) | |
|
243 ("b" $ (BEGIN | END)) | |
|
244 ("w" $ WHITESPACE)).% |
|
245 |
|
246 /* |
|
247 val WHILE_REGS = (KEYWORD | |
|
248 ID | |
|
249 OP | |
|
250 NUM | |
|
251 SEMI | |
|
252 LPAREN | RPAREN | |
|
253 BEGIN | END | |
|
254 WHITESPACE).% |
|
255 */ |
|
256 |
|
257 |
|
258 println("prog0 test") |
|
259 |
|
260 val prog0 = """read n""" |
|
261 println(env(lexing_simp(WHILE_REGS, prog0))) |
|
262 |
|
263 println("prog1 test") |
|
264 |
|
265 val prog1 = """read n; write (n)""" |
|
266 println(env(lexing_simp(WHILE_REGS, prog1))) |
|
267 |
|
268 |
|
269 // Big Test |
|
270 //========== |
|
271 val prog2 = """ |
|
272 i := 2; |
|
273 max := 100; |
|
274 while i < max do { |
|
275 isprime := 1; |
|
276 j := 2; |
|
277 while (j * j) <= i + 1 do { |
|
278 if i % j == 0 then isprime := 0 else skip; |
|
279 j := j + 1 |
|
280 }; |
|
281 if isprime == 1 then write i else skip; |
|
282 i := i + 1 |
|
283 }""" |
|
284 |
|
285 println("prog2 test - tokens") |
|
286 println(env(lexing_simp(WHILE_REGS, prog2))) |
|
287 |
|
288 |
|
289 val prog3 = """ |
|
290 write "fib"; |
|
291 read n; |
|
292 minus1 := 0; |
|
293 minus2 := 1; |
|
294 while n > 0 do { |
|
295 temp := minus2; |
|
296 minus2 := minus1 + minus2; |
|
297 minus1 := temp; |
|
298 n := n - 1 |
|
299 }; |
|
300 write "result"; |
|
301 write minus2 |
|
302 """ |
|
303 |
|
304 println("prog3 test - tokens") |
|
305 println(env(lexing_simp(WHILE_REGS, prog3))) |
|
306 |
|
307 /* |
|
308 for (i <- 1 to 80) { |
|
309 print(i.toString + ": ") |
|
310 time(lexing_simp(WHILE_REGS, prog2 * i)) |
|
311 } |
|
312 */ |
|
313 |