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