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