|
1 // uses bitcode sequences and annotated regular |
|
2 // expressions |
|
3 // |
|
4 // for basic regular expressions and RECD |
|
5 // |
|
6 // uses a non-standard extraction method for generating |
|
7 // tokens (this is tail-recursive) |
|
8 // |
|
9 // can match 60 copies of the fib-program (size 10500) |
|
10 // in about 20 secs |
|
11 |
|
12 |
|
13 import scala.language.implicitConversions |
|
14 import scala.language.reflectiveCalls |
|
15 import scala.annotation.tailrec |
|
16 |
|
17 // standard regular expressions |
|
18 abstract class Rexp |
|
19 case object ZERO extends Rexp |
|
20 case object ONE extends Rexp |
|
21 case class CHAR(c: Char) extends Rexp |
|
22 case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
|
23 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
|
24 case class STAR(r: Rexp) extends Rexp |
|
25 case class RECD(x: String, r: Rexp) extends Rexp |
|
26 |
|
27 abstract class Bit |
|
28 case object Z extends Bit |
|
29 case object S extends Bit |
|
30 |
|
31 type Bits = List[Bit] |
|
32 |
|
33 // annotated regular expressions |
|
34 abstract class ARexp |
|
35 case object AZERO extends ARexp |
|
36 case class AONE(bs: Bits) extends ARexp |
|
37 case class ACHAR(bs: Bits, c: Char) extends ARexp |
|
38 case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp |
|
39 case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp |
|
40 case class ASTAR(bs: Bits, r: ARexp) extends ARexp |
|
41 |
|
42 // an abbreviation for binary alternatives |
|
43 def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2)) |
|
44 |
|
45 abstract class Val |
|
46 case object Empty extends Val |
|
47 case class Chr(c: Char) extends Val |
|
48 case class Sequ(v1: Val, v2: Val) extends Val |
|
49 case class Left(v: Val) extends Val |
|
50 case class Right(v: Val) extends Val |
|
51 case class Stars(vs: List[Val]) extends Val |
|
52 case class Recd(x: String, v: Val) extends Val |
|
53 |
|
54 // some convenience for typing in regular expressions |
|
55 def charlist2rexp(s: List[Char]): Rexp = s match { |
|
56 case Nil => ONE |
|
57 case c::Nil => CHAR(c) |
|
58 case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
|
59 } |
|
60 implicit def string2rexp(s: String) : Rexp = charlist2rexp(s.toList) |
|
61 |
|
62 implicit def RexpOps(r: Rexp) = new { |
|
63 def | (s: Rexp) = ALT(r, s) |
|
64 def % = STAR(r) |
|
65 def ~ (s: Rexp) = SEQ(r, s) |
|
66 } |
|
67 |
|
68 implicit def stringOps(s: String) = new { |
|
69 def | (r: Rexp) = ALT(s, r) |
|
70 def | (r: String) = ALT(s, r) |
|
71 def % = STAR(s) |
|
72 def ~ (r: Rexp) = SEQ(s, r) |
|
73 def ~ (r: String) = SEQ(s, r) |
|
74 def $ (r: Rexp) = RECD(s, r) |
|
75 } |
|
76 |
|
77 def size(r: Rexp) : Int = r match { |
|
78 case ZERO => 1 |
|
79 case ONE => 1 |
|
80 case CHAR(_) => 1 |
|
81 case ALT(r1, r2) => 1 + size(r1) + size(r2) |
|
82 case SEQ(r1, r2) => 1 + size(r1) + size(r2) |
|
83 case STAR(r) => 1 + size(r) |
|
84 case RECD(_, r) => 1 + size(r) |
|
85 } |
|
86 |
|
87 |
|
88 |
|
89 // Bitcoded + Annotation |
|
90 //======================= |
|
91 |
|
92 //erase function: extracts the Rexp from ARexp |
|
93 def erase(r:ARexp): Rexp = r match{ |
|
94 case AZERO => ZERO |
|
95 case AONE(_) => ONE |
|
96 case ACHAR(bs, c) => CHAR(c) |
|
97 case AALTS(bs, Nil) => ZERO |
|
98 case AALTS(bs, r::Nil) => erase(r) |
|
99 case AALTS(bs, r::rs) => ALT(erase(r), erase(AALTS(bs, rs))) |
|
100 case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2)) |
|
101 case ASTAR(cs, r)=> STAR(erase(r)) |
|
102 } |
|
103 |
|
104 def fuse(bs: Bits, r: ARexp) : ARexp = r match { |
|
105 case AZERO => AZERO |
|
106 case AONE(cs) => AONE(bs ++ cs) |
|
107 case ACHAR(cs, c) => ACHAR(bs ++ cs, c) |
|
108 case AALTS(cs, rs) => AALTS(bs ++ cs, rs) |
|
109 case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2) |
|
110 case ASTAR(cs, r) => ASTAR(bs ++ cs, r) |
|
111 } |
|
112 |
|
113 def internalise(r: Rexp) : ARexp = r match { |
|
114 case ZERO => AZERO |
|
115 case ONE => AONE(Nil) |
|
116 case CHAR(c) => ACHAR(Nil, c) |
|
117 case ALT(r1, r2) => AALT(Nil, fuse(List(Z), internalise(r1)), fuse(List(S), internalise(r2))) |
|
118 case SEQ(r1, r2) => ASEQ(Nil, internalise(r1), internalise(r2)) |
|
119 case STAR(r) => ASTAR(Nil, internalise(r)) |
|
120 case RECD(_, r) => internalise(r) |
|
121 } |
|
122 |
|
123 // example |
|
124 // internalise(("a" | "ab") ~ ("b" | "")) |
|
125 |
|
126 |
|
127 // decoding of a value from a bitsequence |
|
128 // (this is not tail-recursive and therefore a potential bottleneck) |
|
129 def vdecode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match { |
|
130 case (ONE, bs) => (Empty, bs) |
|
131 case (CHAR(c), bs) => (Chr(c), bs) |
|
132 case (ALT(r1, r2), Z::bs) => { |
|
133 val (v, bs1) = vdecode_aux(r1, bs) |
|
134 (Left(v), bs1) |
|
135 } |
|
136 case (ALT(r1, r2), S::bs) => { |
|
137 val (v, bs1) = vdecode_aux(r2, bs) |
|
138 (Right(v), bs1) |
|
139 } |
|
140 case (SEQ(r1, r2), bs) => { |
|
141 val (v1, bs1) = vdecode_aux(r1, bs) |
|
142 val (v2, bs2) = vdecode_aux(r2, bs1) |
|
143 (Sequ(v1, v2), bs2) |
|
144 } |
|
145 case (STAR(r1), Z::bs) => { |
|
146 val (v, bs1) = vdecode_aux(r1, bs) |
|
147 val (Stars(vs), bs2) = vdecode_aux(STAR(r1), bs1) |
|
148 (Stars(v::vs), bs2) |
|
149 } |
|
150 case (STAR(_), S::bs) => (Stars(Nil), bs) |
|
151 case (RECD(s, r1), bs) => |
|
152 val (v, bs1) = vdecode_aux(r1, bs) |
|
153 (Recd(s, v), bs1) |
|
154 } |
|
155 |
|
156 def vdecode(r: Rexp, bs: Bits) = vdecode_aux(r, bs) match { |
|
157 case (v, Nil) => v |
|
158 case _ => throw new Exception("Not decodable") |
|
159 } |
|
160 |
|
161 // decoding of sequence of string tokens from a bitsequence |
|
162 // tail-recursive version using an accumulator (alternative for |
|
163 // vdecode) |
|
164 @tailrec |
|
165 def sdecode_aux(rs: List[Rexp], bs: Bits, acc: List[String]) : List[String] = (rs, bs) match { |
|
166 case (Nil, _) => acc |
|
167 case (_, Nil) => acc |
|
168 case (ONE::rest, bs) => sdecode_aux(rest, bs, acc) |
|
169 case (CHAR(c)::rest, bs) => |
|
170 sdecode_aux(rest, bs, (acc.head ++ c.toString)::acc.tail) |
|
171 case (ALT(r1, r2)::rest, Z::bs) => sdecode_aux(r1::rest, bs, acc) |
|
172 case (ALT(r1, r2)::rest, S::bs) => sdecode_aux(r2::rest, bs, acc) |
|
173 case (SEQ(r1, r2)::rest, bs) => sdecode_aux(r1::r2::rest, bs, acc) |
|
174 case (STAR(r1)::rest, Z::bs) => sdecode_aux(r1::STAR(r1)::rest, bs, acc) |
|
175 case (STAR(_)::rest, S::bs) => sdecode_aux(rest, bs, acc) |
|
176 case (RECD(s, r1)::rest, bs) => |
|
177 sdecode_aux(r1::rest, bs, s"$s:"::acc) |
|
178 } |
|
179 |
|
180 def sdecode(r: Rexp, bs: Bits) : List[String] = |
|
181 sdecode_aux(List(r), bs, List("")).reverse.tail |
|
182 |
|
183 |
|
184 |
|
185 // nullable function: tests whether the an (annotated) |
|
186 // regular expression can recognise the empty string |
|
187 def bnullable (r: ARexp) : Boolean = r match { |
|
188 case AZERO => false |
|
189 case AONE(_) => true |
|
190 case ACHAR(_,_) => false |
|
191 case AALTS(_, rs) => rs.exists(bnullable) |
|
192 case ASEQ(_, r1, r2) => bnullable(r1) && bnullable(r2) |
|
193 case ASTAR(_, _) => true |
|
194 } |
|
195 |
|
196 def bmkeps(r: ARexp) : Bits = r match { |
|
197 case AONE(bs) => bs |
|
198 case AALTS(bs, r::Nil) => bs ++ bmkeps(r) |
|
199 case AALTS(bs, r::rs) => |
|
200 if (bnullable(r)) bs ++ bmkeps(r) else bmkeps(AALTS(bs, rs)) |
|
201 case ASEQ(bs, r1, r2) => bs ++ bmkeps(r1) ++ bmkeps(r2) |
|
202 case ASTAR(bs, r) => bs ++ List(S) |
|
203 } |
|
204 |
|
205 // derivative of a regular expression w.r.t. a character |
|
206 def bder(c: Char, r: ARexp) : ARexp = r match { |
|
207 case AZERO => AZERO |
|
208 case AONE(_) => AZERO |
|
209 case ACHAR(bs, d) => if (c == d) AONE(bs) else AZERO |
|
210 case AALTS(bs, rs) => AALTS(bs, rs.map(bder(c, _))) |
|
211 case ASEQ(bs, r1, r2) => |
|
212 if (bnullable(r1)) AALT(bs, ASEQ(Nil, bder(c, r1), r2), fuse(bmkeps(r1), bder(c, r2))) |
|
213 else ASEQ(bs, bder(c, r1), r2) |
|
214 case ASTAR(bs, r) => ASEQ(bs, fuse(List(Z), bder(c, r)), ASTAR(Nil, r)) |
|
215 } |
|
216 |
|
217 // derivative w.r.t. a string (iterates bder) |
|
218 @tailrec |
|
219 def bders (s: List[Char], r: ARexp) : ARexp = s match { |
|
220 case Nil => r |
|
221 case c::s => bders(s, bder(c, r)) |
|
222 } |
|
223 |
|
224 // main unsimplified lexing function (produces a bitsequence) |
|
225 def blex(r: ARexp, s: List[Char]) : Bits = s match { |
|
226 case Nil => if (bnullable(r)) bmkeps(r) else throw new Exception("Not matched") |
|
227 case c::cs => blex(bder(c, r), cs) |
|
228 } |
|
229 |
|
230 // calls blex and decodes the value |
|
231 def blexing(r: Rexp, s: String) : Val = |
|
232 vdecode(r, blex(internalise(r), s.toList)) |
|
233 |
|
234 |
|
235 // example by Tudor |
|
236 //val reg = (STAR("a") ~ ("b" | "c")).% |
|
237 |
|
238 //println(blexing(reg, "aab")) |
|
239 |
|
240 |
|
241 //======================= |
|
242 // simplification |
|
243 // |
|
244 |
|
245 def flts(rs: List[ARexp]) : List[ARexp] = rs match { |
|
246 case Nil => Nil |
|
247 case AZERO :: rs => flts(rs) |
|
248 case AALTS(bs, rs1) :: rs => rs1.map(fuse(bs, _)) ++ flts(rs) |
|
249 case r1 :: rs => r1 :: flts(rs) |
|
250 } |
|
251 |
|
252 def bsimp(r: ARexp): ARexp = r match { |
|
253 case ASEQ(bs1, r1, r2) => (bsimp(r1), bsimp(r2)) match { |
|
254 case (AZERO, _) => AZERO |
|
255 case (_, AZERO) => AZERO |
|
256 case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s) |
|
257 // needed in order to keep the size down |
|
258 case (AALTS(bs, rs), r2) => AALTS(bs1 ++ bs, rs.map(ASEQ(Nil, _, r2))) |
|
259 case (r1s, r2s) => ASEQ(bs1, r1s, r2s) |
|
260 } |
|
261 // distinctBy deletes copies of the same "erased" regex |
|
262 case AALTS(bs1, rs) => (flts(rs.map(bsimp))).distinctBy(erase) match { |
|
263 case Nil => AZERO |
|
264 case r::Nil => fuse(bs1, r) |
|
265 case rs => AALTS(bs1, rs) |
|
266 } |
|
267 case r => r |
|
268 } |
|
269 |
|
270 def blex_simp(r: ARexp, s: List[Char]) : Bits = s match { |
|
271 case Nil => if (bnullable(r)) bmkeps(r) |
|
272 else throw new Exception("Not matched") |
|
273 case c::cs => blex_simp(bsimp(bder(c, r)), cs) |
|
274 } |
|
275 |
|
276 // blexing_simp decodes a value from the bitsequence (potentially slow) |
|
277 // blexing2_simp decodes a string-list from the bitsequence |
|
278 def blexing_simp(r: Rexp, s: String) : Val = |
|
279 vdecode(r, blex_simp(internalise(r), s.toList)) |
|
280 |
|
281 def blexing2_simp(r: Rexp, s: String) : List[String] = |
|
282 sdecode(r, blex_simp(internalise(r), s.toList)) |
|
283 |
|
284 |
|
285 //println(blexing_simp(reg, "aab")) |
|
286 |
|
287 |
|
288 // extracts a string from value |
|
289 def flatten(v: Val) : String = v match { |
|
290 case Empty => "" |
|
291 case Chr(c) => c.toString |
|
292 case Left(v) => flatten(v) |
|
293 case Right(v) => flatten(v) |
|
294 case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
|
295 case Stars(vs) => vs.map(flatten).mkString |
|
296 } |
|
297 |
|
298 // extracts an environment from a value |
|
299 def env(v: Val) : List[(String, String)] = v match { |
|
300 case Empty => Nil |
|
301 case Chr(c) => Nil |
|
302 case Left(v) => env(v) |
|
303 case Right(v) => env(v) |
|
304 case Sequ(v1, v2) => env(v1) ::: env(v2) |
|
305 case Stars(vs) => vs.flatMap(env) |
|
306 case Recd(x, v) => (x, flatten(v))::env(v) |
|
307 } |
|
308 |
|
309 def bsize(a: ARexp) = size(erase(a)) |
|
310 |
|
311 // Some Tests |
|
312 //============ |
|
313 |
|
314 |
|
315 def time_needed[T](i: Int, code: => T) = { |
|
316 val start = System.nanoTime() |
|
317 for (j <- 1 to i) code |
|
318 val end = System.nanoTime() |
|
319 (end - start)/(i * 1.0e9) |
|
320 } |
|
321 |
|
322 /* |
|
323 val evil1 = STAR(STAR("a")) ~ "b" |
|
324 val evil2 = STAR(STAR(STAR("a"))) ~ "b" |
|
325 val evil3 = STAR("aa" | "a") |
|
326 |
|
327 for(i <- 0 to 10000 by 1000) { |
|
328 println(time_needed(1, blexing2_simp(evil1, "a"*i ++ "b"))) |
|
329 } |
|
330 |
|
331 for(i <- 0 to 10000 by 1000) { |
|
332 println(time_needed(1, blexing2_simp(evil2, "a"*i ++ "b"))) |
|
333 } |
|
334 |
|
335 for(i <- 0 to 10000 by 1000) { |
|
336 println(time_needed(1, blexing2_simp(evil3, "a"*i))) |
|
337 } |
|
338 */ |
|
339 |
|
340 // WHILE LANGUAGE |
|
341 //================ |
|
342 def PLUS(r: Rexp) = r ~ r.% |
|
343 |
|
344 def Range(s : List[Char]) : Rexp = s match { |
|
345 case Nil => ZERO |
|
346 case c::Nil => CHAR(c) |
|
347 case c::s => ALT(CHAR(c), Range(s)) |
|
348 } |
|
349 def RANGE(s: String) = Range(s.toList) |
|
350 |
|
351 val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_") |
|
352 val DIGIT = RANGE("0123456789") |
|
353 val ID = SYM ~ (SYM | DIGIT).% |
|
354 val NUM = PLUS(DIGIT) |
|
355 val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" |
|
356 val SEMI: Rexp = ";" |
|
357 val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">" |
|
358 val WHITESPACE = PLUS(" " | "\n" | "\t") |
|
359 val RPAREN: Rexp = "{" |
|
360 val LPAREN: Rexp = "}" |
|
361 val STRING: Rexp = "\"" ~ SYM.% ~ "\"" |
|
362 |
|
363 |
|
364 val WHILE_REGS = (("k" $ KEYWORD) | |
|
365 ("i" $ ID) | |
|
366 ("o" $ OP) | |
|
367 ("n" $ NUM) | |
|
368 ("s" $ SEMI) | |
|
369 ("str" $ STRING) | |
|
370 ("p" $ (LPAREN | RPAREN)) | |
|
371 ("w" $ WHITESPACE)).% |
|
372 |
|
373 |
|
374 // Some Simple While Tests |
|
375 //======================== |
|
376 |
|
377 val prog0 = """read n""" |
|
378 println(s"test: $prog0") |
|
379 println(env(blexing_simp(WHILE_REGS, prog0))) |
|
380 println(blexing2_simp(WHILE_REGS, prog0)) |
|
381 |
|
382 val prog1 = """read n; write n""" |
|
383 println(s"test: $prog1") |
|
384 println(env(blexing_simp(WHILE_REGS, prog1))) |
|
385 println(blexing2_simp(WHILE_REGS, prog1)) |
|
386 |
|
387 val prog2 = """ |
|
388 write "Fib"; |
|
389 read n; |
|
390 minus1 := 0; |
|
391 minus2 := 1; |
|
392 while n > 0 do { |
|
393 temp := minus2; |
|
394 minus2 := minus1 + minus2; |
|
395 minus1 := temp; |
|
396 n := n - 1 |
|
397 }; |
|
398 write "Result"; |
|
399 write minus2 |
|
400 """ |
|
401 |
|
402 println("lexing fib program (once)") |
|
403 println(blexing2_simp(WHILE_REGS, prog2).filter(s => s == "" || !s.startsWith("w"))) |
|
404 |
|
405 val n = 60 |
|
406 println(s"lexing fib program ($n times, size ${prog2.length * n})") |
|
407 println(time_needed(1, blexing2_simp(WHILE_REGS, prog2 * n))) |