author | Christian Urban <urbanc@in.tum.de> |
Wed, 20 Feb 2019 00:00:30 +0000 | |
changeset 312 | 8b0b414e71b0 |
parent 311 | 8b8db9558ecf |
child 314 | 20a57552d722 |
permissions | -rw-r--r-- |
298 | 1 |
|
2 |
import scala.language.implicitConversions |
|
3 |
import scala.language.reflectiveCalls |
|
4 |
import scala.annotation.tailrec |
|
5 |
import scala.util.Try |
|
6 |
||
7 |
def escape(raw: String) : String = { |
|
8 |
import scala.reflect.runtime.universe._ |
|
9 |
Literal(Constant(raw)).toString |
|
10 |
} |
|
11 |
||
12 |
def esc2(r: (String, String)) = (escape(r._1), escape(r._2)) |
|
13 |
||
14 |
def distinctBy[B, C](xs: List[B], f: B => C, acc: List[C] = Nil): List[B] = xs match { |
|
15 |
case Nil => Nil |
|
16 |
case (x::xs) => { |
|
17 |
val res = f(x) |
|
18 |
if (acc.contains(res)) distinctBy(xs, f, acc) |
|
19 |
else x::distinctBy(xs, f, res::acc) |
|
20 |
} |
|
21 |
} |
|
22 |
||
23 |
abstract class Bit |
|
24 |
case object Z extends Bit |
|
25 |
case object S extends Bit |
|
26 |
case class C(c: Char) extends Bit |
|
27 |
||
28 |
type Bits = List[Bit] |
|
29 |
||
30 |
// usual regular expressions |
|
31 |
abstract class Rexp |
|
32 |
case object ZERO extends Rexp |
|
33 |
case object ONE extends Rexp |
|
305 | 34 |
case class PRED(f: Char => Boolean, s: String = "_") extends Rexp |
298 | 35 |
case class ALTS(rs: List[Rexp]) extends Rexp |
36 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
|
37 |
case class STAR(r: Rexp) extends Rexp |
|
38 |
case class RECD(x: String, r: Rexp) extends Rexp |
|
39 |
||
40 |
||
41 |
// abbreviations |
|
305 | 42 |
def CHAR(c: Char) = PRED(_ == c, c.toString) |
298 | 43 |
def ALT(r1: Rexp, r2: Rexp) = ALTS(List(r1, r2)) |
44 |
def PLUS(r: Rexp) = SEQ(r, STAR(r)) |
|
312
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
45 |
val ANYCHAR = PRED(_ => true, ".") |
298 | 46 |
|
47 |
// annotated regular expressions |
|
48 |
abstract class ARexp |
|
49 |
case object AZERO extends ARexp |
|
50 |
case class AONE(bs: Bits) extends ARexp |
|
305 | 51 |
case class APRED(bs: Bits, f: Char => Boolean, s: String = "_") extends ARexp |
298 | 52 |
case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp |
53 |
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp |
|
54 |
case class ASTAR(bs: Bits, r: ARexp) extends ARexp |
|
55 |
||
56 |
// abbreviations |
|
57 |
def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2)) |
|
58 |
||
59 |
// values |
|
60 |
abstract class Val |
|
61 |
case object Empty extends Val |
|
62 |
case class Chr(c: Char) extends Val |
|
63 |
case class Sequ(v1: Val, v2: Val) extends Val |
|
64 |
case class Left(v: Val) extends Val |
|
65 |
case class Right(v: Val) extends Val |
|
66 |
case class Stars(vs: List[Val]) extends Val |
|
67 |
case class Rec(x: String, v: Val) extends Val |
|
68 |
||
69 |
||
70 |
||
71 |
// some convenience for typing in regular expressions |
|
72 |
def charlist2rexp(s : List[Char]): Rexp = s match { |
|
73 |
case Nil => ONE |
|
74 |
case c::Nil => CHAR(c) |
|
75 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
|
76 |
} |
|
77 |
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList) |
|
78 |
||
79 |
implicit def RexpOps(r: Rexp) = new { |
|
80 |
def | (s: Rexp) = ALT(r, s) |
|
81 |
def % = STAR(r) |
|
82 |
def ~ (s: Rexp) = SEQ(r, s) |
|
83 |
} |
|
84 |
||
85 |
implicit def stringOps(s: String) = new { |
|
86 |
def | (r: Rexp) = ALT(s, r) |
|
87 |
def | (r: String) = ALT(s, r) |
|
88 |
def % = STAR(s) |
|
89 |
def ~ (r: Rexp) = SEQ(s, r) |
|
90 |
def ~ (r: String) = SEQ(s, r) |
|
91 |
def $ (r: Rexp) = RECD(s, r) |
|
92 |
} |
|
93 |
||
94 |
||
300 | 95 |
// string of a regular expressions - for testing purposes |
305 | 96 |
def string(r: Rexp): String = r match { |
97 |
case ZERO => "0" |
|
98 |
case ONE => "1" |
|
99 |
case PRED(_, s) => s |
|
100 |
case ALTS(rs) => rs.map(string).mkString("[", "|", "]") |
|
101 |
case SEQ(r1, r2) => s"(${string(r1)} ~ ${string(r2)})" |
|
102 |
case STAR(r) => s"{${string(r)}}*" |
|
103 |
case RECD(x, r) => s"(${x}! ${string(r)})" |
|
104 |
} |
|
105 |
||
106 |
// string of an annotated regular expressions - for testing purposes |
|
107 |
||
108 |
def astring(a: ARexp): String = a match { |
|
109 |
case AZERO => "0" |
|
110 |
case AONE(_) => "1" |
|
111 |
case APRED(_, _, s) => s |
|
112 |
case AALTS(_, rs) => rs.map(astring).mkString("[", "|", "]") |
|
113 |
case ASEQ(_, r1, r2) => s"(${astring(r1)} ~ ${astring(r2)})" |
|
114 |
case ASTAR(_, r) => s"{${astring(r)}}*" |
|
115 |
} |
|
116 |
||
300 | 117 |
|
306
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
118 |
//-------------------------------------------------------------- |
298 | 119 |
// START OF NON-BITCODE PART |
120 |
// |
|
121 |
||
122 |
// nullable function: tests whether the regular |
|
123 |
// expression can recognise the empty string |
|
124 |
def nullable (r: Rexp) : Boolean = r match { |
|
125 |
case ZERO => false |
|
126 |
case ONE => true |
|
305 | 127 |
case PRED(_, _) => false |
298 | 128 |
case ALTS(rs) => rs.exists(nullable) |
129 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
130 |
case STAR(_) => true |
|
131 |
case RECD(_, r) => nullable(r) |
|
132 |
} |
|
133 |
||
134 |
// derivative of a regular expression w.r.t. a character |
|
135 |
def der (c: Char, r: Rexp) : Rexp = r match { |
|
136 |
case ZERO => ZERO |
|
137 |
case ONE => ZERO |
|
305 | 138 |
case PRED(f, _) => if (f(c)) ONE else ZERO |
298 | 139 |
case ALTS(List(r1, r2)) => ALTS(List(der(c, r1), der(c, r2))) |
140 |
case SEQ(r1, r2) => |
|
141 |
if (nullable(r1)) ALTS(List(SEQ(der(c, r1), r2), der(c, r2))) |
|
142 |
else SEQ(der(c, r1), r2) |
|
143 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
144 |
case RECD(_, r1) => der(c, r1) |
|
145 |
} |
|
146 |
||
147 |
||
148 |
def flatten(v: Val) : String = v match { |
|
149 |
case Empty => "" |
|
150 |
case Chr(c) => c.toString |
|
151 |
case Left(v) => flatten(v) |
|
152 |
case Right(v) => flatten(v) |
|
153 |
case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
|
154 |
case Stars(vs) => vs.map(flatten).mkString |
|
155 |
case Rec(_, v) => flatten(v) |
|
156 |
} |
|
157 |
||
158 |
// extracts an environment from a value |
|
159 |
def env(v: Val) : List[(String, String)] = v match { |
|
160 |
case Empty => Nil |
|
161 |
case Chr(c) => Nil |
|
162 |
case Left(v) => env(v) |
|
163 |
case Right(v) => env(v) |
|
164 |
case Sequ(v1, v2) => env(v1) ::: env(v2) |
|
165 |
case Stars(vs) => vs.flatMap(env) |
|
166 |
case Rec(x, v) => (x, flatten(v))::env(v) |
|
167 |
} |
|
168 |
||
169 |
||
170 |
// injection part |
|
171 |
def mkeps(r: Rexp) : Val = r match { |
|
172 |
case ONE => Empty |
|
173 |
case ALTS(List(r1, r2)) => |
|
174 |
if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
|
175 |
case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
|
176 |
case STAR(r) => Stars(Nil) |
|
177 |
case RECD(x, r) => Rec(x, mkeps(r)) |
|
178 |
} |
|
179 |
||
180 |
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
|
181 |
case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
|
182 |
case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
|
183 |
case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
|
184 |
case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
|
185 |
case (ALTS(List(r1, r2)), Left(v1)) => Left(inj(r1, c, v1)) |
|
186 |
case (ALTS(List(r1, r2)), Right(v2)) => Right(inj(r2, c, v2)) |
|
305 | 187 |
case (PRED(_, _), Empty) => Chr(c) |
298 | 188 |
case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
189 |
} |
|
190 |
||
191 |
// lexing without simplification |
|
192 |
def lex(r: Rexp, s: List[Char]) : Val = s match { |
|
193 |
case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched") |
|
194 |
case c::cs => inj(r, c, lex(der(c, r), cs)) |
|
195 |
} |
|
196 |
||
197 |
def lexing(r: Rexp, s: String) : Val = lex(r, s.toList) |
|
198 |
||
199 |
//println(lexing(("ab" | "ab") ~ ("b" | ONE), "ab")) |
|
200 |
||
201 |
// some "rectification" functions for simplification |
|
202 |
def F_ID(v: Val): Val = v |
|
203 |
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v)) |
|
204 |
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v)) |
|
205 |
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
|
206 |
case Right(v) => Right(f2(v)) |
|
207 |
case Left(v) => Left(f1(v)) |
|
208 |
} |
|
209 |
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
|
210 |
case Sequ(v1, v2) => Sequ(f1(v1), f2(v2)) |
|
211 |
} |
|
212 |
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = |
|
213 |
(v:Val) => Sequ(f1(Empty), f2(v)) |
|
214 |
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = |
|
215 |
(v:Val) => Sequ(f1(v), f2(Empty)) |
|
216 |
def F_RECD(f: Val => Val) = (v:Val) => v match { |
|
217 |
case Rec(x, v) => Rec(x, f(v)) |
|
218 |
} |
|
219 |
def F_ERROR(v: Val): Val = throw new Exception("error") |
|
220 |
||
221 |
// simplification of regular expressions returning also an |
|
222 |
// rectification function; no simplification under STAR |
|
223 |
def simp(r: Rexp): (Rexp, Val => Val) = r match { |
|
224 |
case ALTS(List(r1, r2)) => { |
|
225 |
val (r1s, f1s) = simp(r1) |
|
226 |
val (r2s, f2s) = simp(r2) |
|
227 |
(r1s, r2s) match { |
|
228 |
case (ZERO, _) => (r2s, F_RIGHT(f2s)) |
|
229 |
case (_, ZERO) => (r1s, F_LEFT(f1s)) |
|
230 |
case _ => if (r1s == r2s) (r1s, F_LEFT(f1s)) |
|
231 |
else (ALTS(List(r1s, r2s)), F_ALT(f1s, f2s)) |
|
232 |
} |
|
233 |
} |
|
234 |
case SEQ(r1, r2) => { |
|
235 |
val (r1s, f1s) = simp(r1) |
|
236 |
val (r2s, f2s) = simp(r2) |
|
237 |
(r1s, r2s) match { |
|
238 |
case (ZERO, _) => (ZERO, F_ERROR) |
|
239 |
case (_, ZERO) => (ZERO, F_ERROR) |
|
240 |
case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s)) |
|
241 |
case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s)) |
|
242 |
case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s)) |
|
243 |
} |
|
244 |
} |
|
245 |
case RECD(x, r1) => { |
|
246 |
val (r1s, f1s) = simp(r1) |
|
247 |
(RECD(x, r1s), F_RECD(f1s)) |
|
248 |
} |
|
249 |
case r => (r, F_ID) |
|
250 |
} |
|
251 |
||
299
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
252 |
def ders_simp(s: List[Char], r: Rexp) : Rexp = s match { |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
253 |
case Nil => r |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
254 |
case c::s => ders_simp(s, simp(der(c, r))._1) |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
255 |
} |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
256 |
|
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
257 |
|
298 | 258 |
def lex_simp(r: Rexp, s: List[Char]) : Val = s match { |
259 |
case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched") |
|
260 |
case c::cs => { |
|
261 |
val (r_simp, f_simp) = simp(der(c, r)) |
|
262 |
inj(r, c, f_simp(lex_simp(r_simp, cs))) |
|
263 |
} |
|
264 |
} |
|
265 |
||
266 |
def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList) |
|
267 |
||
306
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
268 |
//println(lexing_simp(("a" | "ab") ~ ("b" | ""), "ab")) |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
269 |
|
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
270 |
|
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
271 |
def tokenise_simp(r: Rexp, s: String) = |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
272 |
env(lexing_simp(r, s)).map(esc2) |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
273 |
|
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
274 |
//-------------------------------------------------------------------- |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
275 |
// Partial Derivatives |
298 | 276 |
|
277 |
||
306
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
278 |
def pder(c: Char, r: Rexp): Set[Rexp] = r match { |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
279 |
case ZERO => Set() |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
280 |
case ONE => Set() |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
281 |
case PRED(f, _) => if (f(c)) Set(ONE) else Set() |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
282 |
case ALTS(rs) => rs.toSet.flatMap(pder(c, _)) |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
283 |
case SEQ(r1, r2) => |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
284 |
(for (pr1 <- pder(c, r1)) yield SEQ(pr1, r2)) ++ |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
285 |
(if (nullable(r1)) pder(c, r2) else Set()) |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
286 |
case STAR(r1) => |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
287 |
for (pr1 <- pder(c, r1)) yield SEQ(pr1, STAR(r1)) |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
288 |
case RECD(_, r1) => pder(c, r1) |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
289 |
} |
298 | 290 |
|
306
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
291 |
def pders(cs: List[Char], r: Rexp): Set[Rexp] = cs match { |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
292 |
case Nil => Set(r) |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
293 |
case c::cs => pder(c, r).flatMap(pders(cs, _)) |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
294 |
} |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
295 |
|
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
296 |
def pders_simp(cs: List[Char], r: Rexp): Set[Rexp] = cs match { |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
297 |
case Nil => Set(r) |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
298 |
case c::cs => pder(c, r).flatMap(pders_simp(cs, _)).map(simp(_)._1) |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
299 |
} |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
300 |
|
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
301 |
def psize(rs: Set[Rexp]) = |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
302 |
rs.map(size).sum |
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
303 |
|
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
304 |
//-------------------------------------------------------------------- |
298 | 305 |
// BITCODED PART |
306 |
||
307 |
||
308 |
def fuse(bs: Bits, r: ARexp) : ARexp = r match { |
|
309 |
case AZERO => AZERO |
|
310 |
case AONE(cs) => AONE(bs ++ cs) |
|
305 | 311 |
case APRED(cs, f, s) => APRED(bs ++ cs, f, s) |
298 | 312 |
case AALTS(cs, rs) => AALTS(bs ++ cs, rs) |
313 |
case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2) |
|
314 |
case ASTAR(cs, r) => ASTAR(bs ++ cs, r) |
|
315 |
} |
|
316 |
||
317 |
// translation into ARexps |
|
318 |
def internalise(r: Rexp) : ARexp = r match { |
|
319 |
case ZERO => AZERO |
|
320 |
case ONE => AONE(Nil) |
|
305 | 321 |
case PRED(f, s) => APRED(Nil, f, s) |
298 | 322 |
case ALTS(List(r1, r2)) => |
323 |
AALTS(Nil, List(fuse(List(Z), internalise(r1)), fuse(List(S), internalise(r2)))) |
|
324 |
case ALTS(r1::rs) => { |
|
325 |
val AALTS(Nil, rs2) = internalise(ALTS(rs)) |
|
326 |
AALTS(Nil, fuse(List(Z), internalise(r1)) :: rs2.map(fuse(List(S), _))) |
|
327 |
} |
|
328 |
case SEQ(r1, r2) => ASEQ(Nil, internalise(r1), internalise(r2)) |
|
329 |
case STAR(r) => ASTAR(Nil, internalise(r)) |
|
330 |
case RECD(x, r) => internalise(r) |
|
331 |
} |
|
332 |
||
333 |
internalise(("a" | "ab") ~ ("b" | "")) |
|
334 |
||
335 |
// decoding of values from bit sequences |
|
336 |
def decode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match { |
|
337 |
case (ONE, bs) => (Empty, bs) |
|
305 | 338 |
case (PRED(f, _), C(c)::bs) => (Chr(c), bs) |
298 | 339 |
case (ALTS(r::Nil), bs) => decode_aux(r, bs) |
340 |
case (ALTS(rs), bs) => bs match { |
|
341 |
case Z::bs1 => { |
|
342 |
val (v, bs2) = decode_aux(rs.head, bs1) |
|
343 |
(Left(v), bs2) |
|
344 |
} |
|
345 |
case S::bs1 => { |
|
346 |
val (v, bs2) = decode_aux(ALTS(rs.tail), bs1) |
|
347 |
(Right(v), bs2) |
|
348 |
} |
|
349 |
} |
|
350 |
case (SEQ(r1, r2), bs) => { |
|
351 |
val (v1, bs1) = decode_aux(r1, bs) |
|
352 |
val (v2, bs2) = decode_aux(r2, bs1) |
|
353 |
(Sequ(v1, v2), bs2) |
|
354 |
} |
|
355 |
case (STAR(r1), S::bs) => { |
|
356 |
val (v, bs1) = decode_aux(r1, bs) |
|
357 |
val (Stars(vs), bs2) = decode_aux(STAR(r1), bs1) |
|
358 |
(Stars(v::vs), bs2) |
|
359 |
} |
|
360 |
case (STAR(_), Z::bs) => (Stars(Nil), bs) |
|
361 |
case (RECD(x, r1), bs) => { |
|
362 |
val (v, bs1) = decode_aux(r1, bs) |
|
363 |
(Rec(x, v), bs1) |
|
364 |
} |
|
365 |
} |
|
366 |
||
367 |
def decode(r: Rexp, bs: Bits) = decode_aux(r, bs) match { |
|
368 |
case (v, Nil) => v |
|
369 |
case _ => throw new Exception("Not decodable") |
|
370 |
} |
|
371 |
||
372 |
||
373 |
//erase function: extracts a Rexp from Arexp |
|
374 |
def erase(r: ARexp) : Rexp = r match{ |
|
375 |
case AZERO => ZERO |
|
376 |
case AONE(_) => ONE |
|
305 | 377 |
case APRED(bs, f, s) => PRED(f, s) |
298 | 378 |
case AALTS(bs, rs) => ALTS(rs.map(erase(_))) |
379 |
case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2)) |
|
380 |
case ASTAR(cs, r)=> STAR(erase(r)) |
|
381 |
} |
|
382 |
||
383 |
||
384 |
// bnullable function: tests whether the aregular |
|
385 |
// expression can recognise the empty string |
|
386 |
def bnullable (r: ARexp) : Boolean = r match { |
|
387 |
case AZERO => false |
|
388 |
case AONE(_) => true |
|
305 | 389 |
case APRED(_,_,_) => false |
298 | 390 |
case AALTS(_, rs) => rs.exists(bnullable) |
391 |
case ASEQ(_, r1, r2) => bnullable(r1) && bnullable(r2) |
|
392 |
case ASTAR(_, _) => true |
|
393 |
} |
|
394 |
||
395 |
def bmkeps(r: ARexp) : Bits = r match { |
|
396 |
case AONE(bs) => bs |
|
397 |
case AALTS(bs, rs) => { |
|
398 |
val n = rs.indexWhere(bnullable) |
|
399 |
bs ++ bmkeps(rs(n)) |
|
400 |
} |
|
401 |
case ASEQ(bs, r1, r2) => bs ++ bmkeps(r1) ++ bmkeps(r2) |
|
402 |
case ASTAR(bs, r) => bs ++ List(Z) |
|
403 |
} |
|
404 |
||
405 |
// derivative of a regular expression w.r.t. a character |
|
406 |
def bder(c: Char, r: ARexp) : ARexp = r match { |
|
407 |
case AZERO => AZERO |
|
408 |
case AONE(_) => AZERO |
|
305 | 409 |
case APRED(bs, f, _) => if (f(c)) AONE(bs:::List(C(c))) else AZERO |
298 | 410 |
case AALTS(bs, rs) => AALTS(bs, rs.map(bder(c, _))) |
411 |
case ASEQ(bs, r1, r2) => |
|
412 |
if (bnullable(r1)) AALT(bs, ASEQ(Nil, bder(c, r1), r2), fuse(bmkeps(r1), bder(c, r2))) |
|
413 |
else ASEQ(bs, bder(c, r1), r2) |
|
414 |
case ASTAR(bs, r) => ASEQ(bs, fuse(List(S), bder(c, r)), ASTAR(Nil, r)) |
|
415 |
} |
|
416 |
||
417 |
||
418 |
// derivative w.r.t. a string (iterates bder) |
|
419 |
@tailrec |
|
420 |
def bders (s: List[Char], r: ARexp) : ARexp = s match { |
|
421 |
case Nil => r |
|
422 |
case c::s => bders(s, bder(c, r)) |
|
423 |
} |
|
424 |
||
425 |
def flats(rs: List[ARexp]): List[ARexp] = rs match { |
|
426 |
case Nil => Nil |
|
427 |
case AZERO :: rs1 => flats(rs1) |
|
428 |
case AALTS(bs, rs1) :: rs2 => rs1.map(fuse(bs, _)) ::: flats(rs2) |
|
429 |
case r1 :: rs2 => r1 :: flats(rs2) |
|
430 |
} |
|
431 |
||
432 |
def bsimp(r: ARexp): ARexp = r match { |
|
433 |
case ASEQ(bs1, r1, r2) => (bsimp(r1), bsimp(r2)) match { |
|
434 |
case (AZERO, _) => AZERO |
|
435 |
case (_, AZERO) => AZERO |
|
436 |
case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s) |
|
437 |
case (r1s, r2s) => ASEQ(bs1, r1s, r2s) |
|
438 |
} |
|
439 |
case AALTS(bs1, rs) => distinctBy(flats(rs.map(bsimp)), erase) match { |
|
440 |
case Nil => AZERO |
|
300 | 441 |
case r :: Nil => fuse(bs1, r) |
298 | 442 |
case rs => AALTS(bs1, rs) |
443 |
} |
|
300 | 444 |
//case ASTAR(bs1, r1) => ASTAR(bs1, bsimp(r1)) |
298 | 445 |
case r => r |
446 |
} |
|
447 |
||
448 |
def bders_simp (s: List[Char], r: ARexp) : ARexp = s match { |
|
449 |
case Nil => r |
|
450 |
case c::s => bders_simp(s, bsimp(bder(c, r))) |
|
451 |
} |
|
452 |
||
453 |
def blex_simp(r: ARexp, s: List[Char]) : Bits = s match { |
|
454 |
case Nil => if (bnullable(r)) bmkeps(r) |
|
455 |
else throw new Exception("Not matched") |
|
456 |
case c::cs => blex_simp(bsimp(bder(c, r)), cs) |
|
457 |
} |
|
458 |
||
459 |
||
460 |
def blexing_simp(r: Rexp, s: String) : Val = |
|
299
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
461 |
decode(r, blex_simp(internalise(r), s.toList)) |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
462 |
|
298 | 463 |
|
311 | 464 |
def btokenise_simp(r: Rexp, s: String) = |
465 |
env(blexing_simp(r, s)).map(esc2) |
|
298 | 466 |
|
467 |
||
468 |
||
300 | 469 |
// INCLUDING SIMPLIFICATION UNDER STARS |
470 |
||
471 |
def bsimp_full(r: ARexp): ARexp = r match { |
|
472 |
case ASEQ(bs1, r1, r2) => (bsimp_full(r1), bsimp_full(r2)) match { |
|
473 |
case (AZERO, _) => AZERO |
|
474 |
case (_, AZERO) => AZERO |
|
475 |
case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s) |
|
476 |
case (r1s, r2s) => ASEQ(bs1, r1s, r2s) |
|
477 |
} |
|
478 |
case AALTS(bs1, rs) => distinctBy(flats(rs.map(bsimp_full)), erase) match { |
|
479 |
case Nil => AZERO |
|
480 |
case r :: Nil => fuse(bs1, r) |
|
481 |
case rs => AALTS(bs1, rs) |
|
482 |
} |
|
483 |
case ASTAR(bs1, r1) => ASTAR(bs1, bsimp_full(r1)) |
|
484 |
case r => r |
|
485 |
} |
|
486 |
||
487 |
def bders_simp_full(s: List[Char], r: ARexp) : ARexp = s match { |
|
488 |
case Nil => r |
|
489 |
case c::s => bders_simp_full(s, bsimp_full(bder(c, r))) |
|
490 |
} |
|
491 |
||
492 |
def blex_simp_full(r: ARexp, s: List[Char]) : Bits = s match { |
|
493 |
case Nil => if (bnullable(r)) bmkeps(r) |
|
494 |
else throw new Exception("Not matched") |
|
495 |
case c::cs => blex_simp_full(bsimp_full(bder(c, r)), cs) |
|
496 |
} |
|
497 |
||
498 |
||
499 |
def blexing_simp_full(r: Rexp, s: String) : Val = |
|
500 |
decode(r, blex_simp_full(internalise(r), s.toList)) |
|
501 |
||
502 |
||
503 |
def btokenise_simp_full(r: Rexp, s: String) = env(blexing_simp_full(r, s)).map(esc2) |
|
504 |
||
311 | 505 |
// bders2 for strings in the ALTS case |
506 |
||
507 |
def bders2_simp(s: List[Char], a: ARexp) : ARexp = { |
|
508 |
//println(s"s = ${s.length} a = ${asize(a)}") |
|
509 |
//Console.readLine |
|
510 |
(s, a) match { |
|
511 |
case (Nil, r) => r |
|
512 |
case (s, AZERO) => AZERO |
|
513 |
case (s, AONE(_)) => AZERO |
|
514 |
case (s, APRED(bs, f, _)) => |
|
515 |
if (f(s.head) && s.tail == Nil) AONE(bs:::List(C(s.head))) else AZERO |
|
516 |
case (s, AALTS(bs, rs)) => bsimp(AALTS(bs, rs.map(bders2_simp(s, _)))) |
|
517 |
case (c::s, r) => bders2_simp(s, bsimp(bder(c, r))) |
|
518 |
}} |
|
519 |
||
520 |
||
521 |
||
522 |
def blexing2_simp(r: Rexp, s: String) : Val = { |
|
523 |
val bder = bders2_simp(s.toList, internalise(r)) |
|
524 |
if (bnullable(bder)) decode(r, bmkeps(bder)) else |
|
525 |
throw new Exception("Not matched") |
|
526 |
||
527 |
} |
|
528 |
||
529 |
def btokenise2_simp(r: Rexp, s: String) = |
|
530 |
env(blexing2_simp(r, s)).map(esc2) |
|
531 |
||
300 | 532 |
|
312
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
533 |
// Parser for regexes |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
534 |
|
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
535 |
case class Parser(s: String) { |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
536 |
var i = 0 |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
537 |
|
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
538 |
def peek() = s(i) |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
539 |
def eat(c: Char) = |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
540 |
if (c == s(i)) i = i + 1 else throw new Exception("Expected " + c + " got " + s(i)) |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
541 |
def next() = { i = i + 1; s(i - 1) } |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
542 |
def more() = s.length - i > 0 |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
543 |
|
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
544 |
def Regex() : Rexp = { |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
545 |
val t = Term(); |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
546 |
if (more() && peek() == '|') { |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
547 |
eat ('|') ; |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
548 |
ALT(t, Regex()) |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
549 |
} |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
550 |
else t |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
551 |
} |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
552 |
|
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
553 |
def Term() : Rexp = { |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
554 |
var f : Rexp = |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
555 |
if (more() && peek() != ')' && peek() != '|') Factor() else ONE; |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
556 |
while (more() && peek() != ')' && peek() != '|') { |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
557 |
f = SEQ(f, Factor()) ; |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
558 |
} |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
559 |
f |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
560 |
} |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
561 |
|
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
562 |
def Factor() : Rexp = { |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
563 |
var b = Base(); |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
564 |
while (more() && peek() == '*') { |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
565 |
eat('*') ; |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
566 |
b = STAR(b) ; |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
567 |
} |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
568 |
while (more() && peek() == '?') { |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
569 |
eat('?') ; |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
570 |
b = ALT(b, ONE) ; |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
571 |
} |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
572 |
while (more() && peek() == '+') { |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
573 |
eat('+') ; |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
574 |
b = SEQ(b, STAR(b)) ; |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
575 |
} |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
576 |
b |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
577 |
} |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
578 |
|
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
579 |
def Base() : Rexp = { |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
580 |
peek() match { |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
581 |
case '(' => { eat('(') ; val r = Regex(); eat(')') ; r } // if groups should be groups RECD("",r) } |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
582 |
case '.' => { eat('.'); ANYCHAR } |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
583 |
case _ => CHAR(next()) |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
584 |
} |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
585 |
} |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
586 |
} |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
587 |
|
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
588 |
// two simple examples for the regex parser |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
589 |
|
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
590 |
println("two simple examples for the regex parser") |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
591 |
|
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
592 |
println(string(Parser("a|(bc)*").Regex())) |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
593 |
println(string(Parser("(a|b)*(babab(a|b)*bab|bba(a|b)*bab)(a|b)*").Regex())) |
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
594 |
|
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
595 |
|
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
596 |
|
8b0b414e71b0
added size bounds for partial derivatives
Christian Urban <urbanc@in.tum.de>
parents:
311
diff
changeset
|
597 |
System.exit(0) |
299
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
598 |
|
298 | 599 |
// Testing |
600 |
//============ |
|
601 |
||
602 |
def time[T](code: => T) = { |
|
603 |
val start = System.nanoTime() |
|
604 |
val result = code |
|
605 |
val end = System.nanoTime() |
|
606 |
((end - start)/1.0e9).toString |
|
607 |
//result |
|
608 |
} |
|
609 |
||
299
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
610 |
def timeR[T](code: => T) = { |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
611 |
val start = System.nanoTime() |
300 | 612 |
for (i <- 1 to 10) code |
299
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
613 |
val result = code |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
614 |
val end = System.nanoTime() |
300 | 615 |
(result, (end - start)) |
299
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
616 |
} |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
617 |
|
298 | 618 |
//size: of a Aregx for testing purposes |
619 |
def size(r: Rexp) : Int = r match { |
|
620 |
case ZERO => 1 |
|
621 |
case ONE => 1 |
|
305 | 622 |
case PRED(_,_) => 1 |
298 | 623 |
case SEQ(r1, r2) => 1 + size(r1) + size(r2) |
624 |
case ALTS(rs) => 1 + rs.map(size).sum |
|
625 |
case STAR(r) => 1 + size(r) |
|
299
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
626 |
case RECD(_, r) => size(r) |
298 | 627 |
} |
628 |
||
629 |
def asize(a: ARexp) = size(erase(a)) |
|
630 |
||
631 |
||
632 |
// Lexing Rules for a Small While Language |
|
633 |
||
634 |
//symbols |
|
305 | 635 |
val SYM = PRED("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".contains(_), "SYM") |
298 | 636 |
//digits |
305 | 637 |
val DIGIT = PRED("0123456789".contains(_), "NUM") |
298 | 638 |
//identifiers |
639 |
val ID = SYM ~ (SYM | DIGIT).% |
|
640 |
//numbers |
|
641 |
val NUM = STAR(DIGIT) |
|
642 |
//keywords |
|
643 |
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false" |
|
644 |
//semicolons |
|
645 |
val SEMI: Rexp = ";" |
|
646 |
//operators |
|
647 |
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/" |
|
648 |
//whitespaces |
|
649 |
val WHITESPACE = PLUS(" " | "\n" | "\t") |
|
650 |
//parentheses |
|
651 |
val RPAREN: Rexp = ")" |
|
652 |
val LPAREN: Rexp = "(" |
|
653 |
val BEGIN: Rexp = "{" |
|
654 |
val END: Rexp = "}" |
|
655 |
//strings...but probably needs not |
|
656 |
val STRING: Rexp = "\"" ~ SYM.% ~ "\"" |
|
657 |
||
658 |
||
659 |
||
660 |
val WHILE_REGS = (("k" $ KEYWORD) | |
|
661 |
("i" $ ID) | |
|
662 |
("o" $ OP) | |
|
663 |
("n" $ NUM) | |
|
664 |
("s" $ SEMI) | |
|
665 |
("str" $ STRING) | |
|
666 |
("p" $ (LPAREN | RPAREN)) | |
|
667 |
("b" $ (BEGIN | END)) | |
|
668 |
("w" $ WHITESPACE)).% |
|
669 |
||
670 |
||
671 |
// Some Small Tests |
|
672 |
//================== |
|
673 |
||
307
ee1caac29bb2
updated to Isabelle 2018
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
674 |
println("Small tests") |
ee1caac29bb2
updated to Isabelle 2018
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
675 |
|
ee1caac29bb2
updated to Isabelle 2018
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
676 |
val re1 = STAR("a" | "aa") |
ee1caac29bb2
updated to Isabelle 2018
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
677 |
println(astring(bders_simp("".toList, internalise(re1)))) |
ee1caac29bb2
updated to Isabelle 2018
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
678 |
println(astring(bders_simp("a".toList, internalise(re1)))) |
ee1caac29bb2
updated to Isabelle 2018
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
679 |
println(astring(bders_simp("aa".toList, internalise(re1)))) |
ee1caac29bb2
updated to Isabelle 2018
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
680 |
println(astring(bders_simp("aaa".toList, internalise(re1)))) |
ee1caac29bb2
updated to Isabelle 2018
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
681 |
println(astring(bders_simp("aaaaaa".toList, internalise(re1)))) |
ee1caac29bb2
updated to Isabelle 2018
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
682 |
println(astring(bders_simp("aaaaaaaaa".toList, internalise(re1)))) |
ee1caac29bb2
updated to Isabelle 2018
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
683 |
println(astring(bders_simp("aaaaaaaaaaaa".toList, internalise(re1)))) |
ee1caac29bb2
updated to Isabelle 2018
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
684 |
println(astring(bders_simp("aaaaaaaaaaaaaaaaaaaaaaaaa".toList, internalise(re1)))) |
ee1caac29bb2
updated to Isabelle 2018
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
685 |
println(astring(bders_simp("aaaaaabaaaabbbbbaaaaaaaaaaaaaaa".toList, internalise(re1)))) |
ee1caac29bb2
updated to Isabelle 2018
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
686 |
|
ee1caac29bb2
updated to Isabelle 2018
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
687 |
|
311 | 688 |
for (i <- 0 to 100 by 5) { |
689 |
//print("Old: " + time(tokenise_simp(re1, "a" * i))) |
|
690 |
print(" Bit: " + time(btokenise_simp(re1, "a" * i))) |
|
691 |
print(" Bit full simp: " + time(btokenise_simp_full(re1, "a" * i))) |
|
692 |
println(" Bit2: " + time(btokenise2_simp(re1, "a" * i))) |
|
693 |
} |
|
307
ee1caac29bb2
updated to Isabelle 2018
Christian Urban <urbanc@in.tum.de>
parents:
306
diff
changeset
|
694 |
|
311 | 695 |
Console.readLine |
298 | 696 |
|
697 |
||
698 |
// Bigger Tests |
|
699 |
//============== |
|
700 |
||
701 |
||
311 | 702 |
println("Big tests") |
703 |
||
298 | 704 |
val fib_prog = """ |
705 |
write "Fib"; |
|
706 |
read n; |
|
707 |
minus1 := 0; |
|
708 |
minus2 := 1; |
|
709 |
while n > 0 do { |
|
710 |
temp := minus2; |
|
711 |
minus2 := minus1 + minus2; |
|
712 |
minus1 := temp; |
|
713 |
n := n - 1 |
|
714 |
}; |
|
715 |
write "Result"; |
|
716 |
write minus2 |
|
717 |
""" |
|
718 |
||
719 |
||
720 |
println("fib prog tests :") |
|
721 |
println(tokenise_simp(WHILE_REGS, fib_prog)) |
|
722 |
println(btokenise_simp(WHILE_REGS, fib_prog)) |
|
723 |
println("equal? " + (tokenise_simp(WHILE_REGS, fib_prog) == btokenise_simp(WHILE_REGS, fib_prog))) |
|
724 |
||
300 | 725 |
for (i <- 1 to 20) { |
299
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
726 |
print("Old: " + time(tokenise_simp(WHILE_REGS, fib_prog * i))) |
300 | 727 |
print(" Bit: " + time(btokenise_simp(WHILE_REGS, fib_prog * i))) |
728 |
println(" Bit full simp: " + time(btokenise_simp_full(WHILE_REGS, fib_prog * i))) |
|
311 | 729 |
//println(" Bit2: " + time(btokenise2_simp(WHILE_REGS, fib_prog * i))) |
299
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
730 |
} |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
731 |
|
305 | 732 |
|
299
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
733 |
println("Original " + size(WHILE_REGS)) |
305 | 734 |
println("Size Bit " + asize(bders_simp((fib_prog * 1).toList, internalise(WHILE_REGS)))) |
735 |
println("Size Bitf " + asize(bders_simp_full((fib_prog * 1).toList, internalise(WHILE_REGS)))) |
|
311 | 736 |
println("Size Bit2 " + asize(bders2_simp((fib_prog * 1).toList, internalise(WHILE_REGS)))) |
305 | 737 |
println("Size Old " + size(ders_simp((fib_prog * 1).toList, WHILE_REGS))) |
306
6756b026c5fe
added partial derivatives to compare sizes
Christian Urban <urbanc@in.tum.de>
parents:
305
diff
changeset
|
738 |
println("Size Pder " + psize(pders_simp((fib_prog * 1).toList, WHILE_REGS))) |
298 | 739 |
|
305 | 740 |
System.exit(0) |
300 | 741 |
|
299
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
742 |
println("Internal sizes test OK or strange") |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
743 |
|
300 | 744 |
def perc(p1: Double, p2: Double) : String = |
745 |
f"${(((p1 - p2) / p2) * 100.0) }%5.0f" + "%" |
|
746 |
||
299
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
747 |
def ders_test(n: Int, s: List[Char], r: Rexp, a: ARexp) : (Rexp, ARexp) = s match { |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
748 |
case Nil => (r, a) |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
749 |
case c::s => { |
300 | 750 |
// derivative |
751 |
val (rd1, tr1) = timeR(der(c, r)) |
|
752 |
val (ad1, ta1) = timeR(bder(c, a)) |
|
753 |
val trs1 = f"${tr1}%.5f" |
|
754 |
val tas1 = f"${ta1}%.5f" |
|
755 |
if (tr1 < ta1) println(s"Time strange der (step) ${n} ${perc(ta1, tr1)} sizes der ${size(rd1)} ${asize(ad1)}") |
|
756 |
//simplification |
|
757 |
val (rd, tr) = timeR(simp(rd1)._1) |
|
758 |
val (ad, ta) = timeR(bsimp(ad1)) |
|
299
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
759 |
val trs = f"${tr}%.5f" |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
760 |
val tas = f"${ta}%.5f" |
300 | 761 |
//full simplification |
762 |
val (adf, taf) = timeR(bsimp_full(ad1)) |
|
763 |
if (tr < ta) println(s"Time strange simp (step) ${n} ${perc(ta, tr)} sizes simp ${size(rd)} ${asize(ad)}") |
|
764 |
if (n == 1749 || n == 1734) { |
|
765 |
println{s"Aregex before bder (size: ${asize(a)})\n ${string(erase(a))}"} |
|
766 |
println{s"Aregex after bder (size: ${asize(ad1)})\n ${string(erase(ad1))}"} |
|
767 |
println{s"Aregex after bsimp (size: ${asize(ad)})\n ${string(erase(ad))}"} |
|
768 |
println{s"Aregex after bsimp_full (size: ${asize(adf)})\n ${string(erase(adf))}"} |
|
769 |
} |
|
299
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
770 |
ders_test(n + 1, s, rd, ad) |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
771 |
} |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
772 |
} |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
773 |
|
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
774 |
val prg = (fib_prog * 10).toList |
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
775 |
ders_test(0, prg, WHILE_REGS, internalise(WHILE_REGS)) |
298 | 776 |
|
777 |
||
778 |
//testing the two lexings produce the same value |
|
779 |
//enumerates strings of length n over alphabet cs |
|
780 |
def strs(n: Int, cs: String) : Set[String] = { |
|
781 |
if (n == 0) Set("") |
|
782 |
else { |
|
783 |
val ss = strs(n - 1, cs) |
|
784 |
ss ++ |
|
785 |
(for (s <- ss; c <- cs.toList) yield c + s) |
|
786 |
} |
|
787 |
} |
|
788 |
def enum(n: Int, s: String) : Stream[Rexp] = n match { |
|
789 |
case 0 => ZERO #:: ONE #:: s.toStream.map(CHAR) |
|
790 |
case n => { |
|
791 |
val rs = enum(n - 1, s) |
|
792 |
rs #::: |
|
793 |
(for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) #::: |
|
794 |
(for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) #::: |
|
795 |
(for (r1 <- rs) yield STAR(r1)) |
|
796 |
} |
|
797 |
} |
|
798 |
||
799 |
//tests blexing and lexing |
|
800 |
def tests(ss: Set[String])(r: Rexp) = { |
|
801 |
//println(s"Testing ${r}") |
|
802 |
for (s <- ss.par) yield { |
|
803 |
val res1 = Try(Some(lexing_simp(r, s))).getOrElse(None) |
|
804 |
val res2 = Try(Some(blexing_simp(r, s))).getOrElse(None) |
|
805 |
if (res1 != res2) |
|
806 |
{ println(s"Disagree on ${r} and ${s}") |
|
807 |
println(s" ${res1} != ${res2}") |
|
808 |
Some((r, s)) } else None |
|
809 |
} |
|
810 |
} |
|
811 |
||
812 |
||
813 |
println("Partial searching: ") |
|
814 |
enum(2, "abc").map(tests(strs(3, "abc"))).toSet |
|
299
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
815 |
|
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
816 |
|
cae7eab03018
added some timing and size tests when doing the derivatives
Christian Urban <urbanc@in.tum.de>
parents:
298
diff
changeset
|
817 |
|
305 | 818 |