0
|
1 |
package RexpRelated
|
|
2 |
import scala.language.implicitConversions
|
|
3 |
import scala.language.reflectiveCalls
|
|
4 |
import scala.annotation.tailrec
|
|
5 |
import scala.util.Try
|
|
6 |
|
|
7 |
abstract class Bit
|
|
8 |
case object Z extends Bit
|
|
9 |
case object S extends Bit
|
|
10 |
case class C(c: Char) extends Bit
|
|
11 |
|
|
12 |
|
|
13 |
abstract class Rexp
|
|
14 |
case object ZERO extends Rexp
|
|
15 |
case object ONE extends Rexp
|
|
16 |
case class CHAR(c: Char) extends Rexp
|
|
17 |
case class ALTS(rs: List[Rexp]) extends Rexp
|
|
18 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
|
|
19 |
case class STAR(r: Rexp) extends Rexp
|
|
20 |
case class RECD(x: String, r: Rexp) extends Rexp
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
object Rexp{
|
|
25 |
type Bits = List[Bit]
|
|
26 |
// abbreviations
|
|
27 |
type Mon = (Char, Rexp)
|
|
28 |
type Lin = Set[Mon]
|
|
29 |
def ALT(r1: Rexp, r2: Rexp) = ALTS(List(r1, r2))
|
|
30 |
def PLUS(r: Rexp) = SEQ(r, STAR(r))
|
|
31 |
def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2))
|
|
32 |
|
|
33 |
|
|
34 |
def distinctBy[B, C](xs: List[B], f: B => C, acc: List[C] = Nil): List[B] = xs match {
|
|
35 |
case Nil => Nil
|
|
36 |
case (x::xs) => {
|
|
37 |
val res = f(x)
|
|
38 |
if (acc.contains(res)) distinctBy(xs, f, acc)
|
|
39 |
else x::distinctBy(xs, f, res::acc)
|
|
40 |
}
|
|
41 |
}
|
|
42 |
// some convenience for typing in regular expressions
|
|
43 |
def charlist2rexp(s : List[Char]): Rexp = s match {
|
|
44 |
case Nil => ONE
|
|
45 |
case c::Nil => CHAR(c)
|
|
46 |
case c::s => SEQ(CHAR(c), charlist2rexp(s))
|
|
47 |
}
|
|
48 |
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
|
|
49 |
|
|
50 |
implicit def RexpOps(r: Rexp) = new {
|
|
51 |
def | (s: Rexp) = ALT(r, s)
|
|
52 |
def % = STAR(r)
|
|
53 |
def ~ (s: Rexp) = SEQ(r, s)
|
|
54 |
}
|
|
55 |
|
|
56 |
implicit def stringOps(s: String) = new {
|
|
57 |
def | (r: Rexp) = ALT(s, r)
|
|
58 |
def | (r: String) = ALT(s, r)
|
|
59 |
def % = STAR(s)
|
|
60 |
def ~ (r: Rexp) = SEQ(s, r)
|
|
61 |
def ~ (r: String) = SEQ(s, r)
|
|
62 |
def $ (r: Rexp) = RECD(s, r)
|
|
63 |
}
|
|
64 |
|
|
65 |
// translation into ARexps
|
|
66 |
def fuse(bs: Bits, r: ARexp) : ARexp = r match {
|
|
67 |
case AZERO => AZERO
|
|
68 |
case AONE(cs) => AONE(bs ++ cs)
|
|
69 |
case ACHAR(cs, f) => ACHAR(bs ++ cs, f)
|
|
70 |
case AALTS(cs, rs) => AALTS(bs ++ cs, rs)
|
|
71 |
case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2)
|
|
72 |
case ASTAR(cs, r) => ASTAR(bs ++ cs, r)
|
|
73 |
}
|
|
74 |
|
|
75 |
def internalise(r: Rexp) : ARexp = r match {
|
|
76 |
case ZERO => AZERO
|
|
77 |
case ONE => AONE(Nil)
|
|
78 |
case CHAR(c) => ACHAR(Nil, c)
|
|
79 |
case ALTS(List(r1, r2)) =>
|
|
80 |
AALTS(Nil, List(fuse(List(Z), internalise(r1)), fuse(List(S), internalise(r2))))
|
|
81 |
case ALTS(r1::rs) => {
|
|
82 |
val AALTS(Nil, rs2) = internalise(ALTS(rs))
|
|
83 |
AALTS(Nil, fuse(List(Z), internalise(r1)) :: rs2.map(fuse(List(S), _)))
|
|
84 |
}
|
|
85 |
case SEQ(r1, r2) => ASEQ(Nil, internalise(r1), internalise(r2))
|
|
86 |
case STAR(r) => ASTAR(Nil, internalise(r))
|
|
87 |
case RECD(x, r) => internalise(r)
|
|
88 |
}
|
|
89 |
|
|
90 |
internalise(("a" | "ab") ~ ("b" | ""))
|
5
|
91 |
|
0
|
92 |
def decode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match {
|
|
93 |
case (ONE, bs) => (Empty, bs)
|
5
|
94 |
case (CHAR(f), C(c)::bs) => (Chr(c), bs)
|
0
|
95 |
case (ALTS(r::Nil), bs) => decode_aux(r, bs)//this case seems tailor made for those who want to simplify the regex before der or simp
|
|
96 |
case (ALTS(rs), bs) => bs match {
|
|
97 |
case Z::bs1 => {
|
|
98 |
val (v, bs2) = decode_aux(rs.head, bs1)
|
|
99 |
(Left(v), bs2)
|
|
100 |
}
|
|
101 |
case S::bs1 => {
|
|
102 |
val (v, bs2) = decode_aux(ALTS(rs.tail), bs1)
|
|
103 |
(Right(v), bs2)
|
|
104 |
}
|
|
105 |
}
|
|
106 |
case (SEQ(r1, r2), bs) => {
|
|
107 |
val (v1, bs1) = decode_aux(r1, bs)
|
|
108 |
val (v2, bs2) = decode_aux(r2, bs1)
|
|
109 |
(Sequ(v1, v2), bs2)
|
|
110 |
}
|
|
111 |
case (STAR(r1), S::bs) => {
|
|
112 |
val (v, bs1) = decode_aux(r1, bs)
|
|
113 |
//println(v)
|
|
114 |
val (Stars(vs), bs2) = decode_aux(STAR(r1), bs1)
|
|
115 |
(Stars(v::vs), bs2)
|
|
116 |
}
|
|
117 |
case (STAR(_), Z::bs) => (Stars(Nil), bs)
|
|
118 |
case (RECD(x, r1), bs) => {
|
|
119 |
val (v, bs1) = decode_aux(r1, bs)
|
|
120 |
(Rec(x, v), bs1)
|
|
121 |
}
|
|
122 |
}
|
|
123 |
|
|
124 |
def decode(r: Rexp, bs: Bits) = decode_aux(r, bs) match {
|
|
125 |
case (v, Nil) => v
|
|
126 |
case _ => throw new Exception("Not decodable")
|
|
127 |
}
|
5
|
128 |
|
0
|
129 |
|
|
130 |
//erase function: extracts the regx from Aregex
|
|
131 |
def erase(r:ARexp): Rexp = r match{
|
|
132 |
case AZERO => ZERO
|
|
133 |
case AONE(_) => ONE
|
|
134 |
case ACHAR(bs, f) => CHAR(f)
|
|
135 |
case AALTS(bs, rs) => ALTS(rs.map(erase(_)))
|
|
136 |
case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2))
|
|
137 |
case ASTAR(cs, r)=> STAR(erase(r))
|
|
138 |
}
|
|
139 |
|
|
140 |
//--------------------------------------------------------------------------------------------------------START OF NON-BITCODE PART
|
|
141 |
// nullable function: tests whether the regular
|
|
142 |
// expression can recognise the empty string
|
|
143 |
def nullable (r: Rexp) : Boolean = r match {
|
|
144 |
case ZERO => false
|
|
145 |
case ONE => true
|
|
146 |
case CHAR(_) => false
|
|
147 |
case ALTS(rs) => rs.exists(nullable)
|
|
148 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2)
|
|
149 |
case STAR(_) => true
|
|
150 |
case RECD(_, r) => nullable(r)
|
|
151 |
//case PLUS(r) => nullable(r)
|
|
152 |
}
|
|
153 |
|
|
154 |
// derivative of a regular expression w.r.t. a character
|
|
155 |
def der (c: Char, r: Rexp) : Rexp = r match {
|
|
156 |
case ZERO => ZERO
|
|
157 |
case ONE => ZERO
|
|
158 |
case CHAR(f) => if (c == f) ONE else ZERO
|
|
159 |
case ALTS(List(r1, r2)) => ALTS(List(der(c, r1), der(c, r2)))
|
|
160 |
case SEQ(r1, r2) =>
|
|
161 |
if (nullable(r1)) ALTS(List(SEQ(der(c, r1), r2), der(c, r2)))
|
|
162 |
else SEQ(der(c, r1), r2)
|
|
163 |
case STAR(r) => SEQ(der(c, r), STAR(r))
|
|
164 |
case RECD(_, r1) => der(c, r1)
|
|
165 |
//case PLUS(r) => SEQ(der(c, r), STAR(r))
|
|
166 |
}
|
|
167 |
|
|
168 |
def flatten(v: Val) : String = v match {
|
|
169 |
case Empty => ""
|
|
170 |
case Chr(c) => c.toString
|
|
171 |
case Left(v) => flatten(v)
|
|
172 |
case Right(v) => flatten(v)
|
|
173 |
case Sequ(v1, v2) => flatten(v1) + flatten(v2)
|
|
174 |
case Stars(vs) => vs.map(flatten).mkString
|
|
175 |
case Rec(_, v) => flatten(v)
|
|
176 |
}
|
|
177 |
|
|
178 |
// extracts an environment from a value
|
|
179 |
def env(v: Val) : List[(String, String)] = v match {
|
|
180 |
case Empty => Nil
|
|
181 |
case Chr(c) => Nil
|
|
182 |
case Left(v) => env(v)
|
|
183 |
case Right(v) => env(v)
|
|
184 |
case Sequ(v1, v2) => env(v1) ::: env(v2)
|
|
185 |
case Stars(vs) => vs.flatMap(env)
|
|
186 |
case Rec(x, v) => (x, flatten(v))::env(v)
|
|
187 |
}
|
|
188 |
|
|
189 |
|
|
190 |
// injection part
|
|
191 |
def mkeps(r: Rexp) : Val = r match {
|
|
192 |
case ONE => Empty
|
|
193 |
case ALTS(List(r1, r2)) =>
|
|
194 |
if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
|
|
195 |
case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
|
|
196 |
case STAR(r) => Stars(Nil)
|
|
197 |
case RECD(x, r) => Rec(x, mkeps(r))
|
|
198 |
//case PLUS(r) => Stars(List(mkeps(r)))
|
|
199 |
}
|
|
200 |
|
|
201 |
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
|
|
202 |
case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
|
|
203 |
case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
|
|
204 |
case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
|
|
205 |
case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
|
|
206 |
case (ALTS(List(r1, r2)), Left(v1)) => Left(inj(r1, c, v1))
|
|
207 |
case (ALTS(List(r1, r2)), Right(v2)) => Right(inj(r2, c, v2))
|
|
208 |
case (CHAR(_), Empty) => Chr(c)
|
|
209 |
case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
|
|
210 |
//case (PLUS(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
|
|
211 |
}
|
|
212 |
def lex(r: Rexp, s: List[Char]) : Val = s match {
|
|
213 |
case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
|
|
214 |
case c::cs => inj(r, c, lex(der(c, r), cs))
|
|
215 |
}
|
|
216 |
|
|
217 |
def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
|
|
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 |
val each_simp_time = scala.collection.mutable.ArrayBuffer.empty[Long]
|
|
271 |
val each_simp_timeb = scala.collection.mutable.ArrayBuffer.empty[Long]
|
|
272 |
*/
|
|
273 |
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
|
|
274 |
case Nil => {
|
|
275 |
if (nullable(r)) {
|
|
276 |
mkeps(r)
|
|
277 |
}
|
|
278 |
else throw new Exception("Not matched")
|
|
279 |
}
|
|
280 |
case c::cs => {
|
|
281 |
val (r_simp, f_simp) = simp(der(c, r))
|
|
282 |
inj(r, c, f_simp(lex_simp(r_simp, cs)))
|
|
283 |
}
|
|
284 |
}
|
|
285 |
|
|
286 |
def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList)
|
|
287 |
|
|
288 |
//println(lexing_simp(("a" | "ab") ~ ("b" | ""), "ab"))
|
|
289 |
|
|
290 |
// filters out all white spaces
|
|
291 |
def tokenise(r: Rexp, s: String) =
|
|
292 |
env(lexing_simp(r, s)).filterNot { _._1 == "w"}
|
|
293 |
|
|
294 |
|
|
295 |
//reads the string from a file
|
|
296 |
def fromFile(name: String) : String =
|
|
297 |
io.Source.fromFile(name).mkString
|
|
298 |
|
|
299 |
def tokenise_file(r: Rexp, name: String) =
|
|
300 |
tokenise(r, fromFile(name))
|
|
301 |
|
|
302 |
// Testing
|
|
303 |
//============
|
|
304 |
|
|
305 |
def time[T](code: => T) = {
|
|
306 |
val start = System.nanoTime()
|
|
307 |
val result = code
|
|
308 |
val end = System.nanoTime()
|
|
309 |
println((end - start)/1.0e9)
|
|
310 |
result
|
|
311 |
}
|
|
312 |
|
|
313 |
//--------------------------------------------------------------------------------------------------------END OF NON-BITCODE PART
|
|
314 |
|
|
315 |
// bnullable function: tests whether the aregular
|
|
316 |
// expression can recognise the empty string
|
|
317 |
def bnullable (r: ARexp) : Boolean = r match {
|
|
318 |
case AZERO => false
|
|
319 |
case AONE(_) => true
|
|
320 |
case ACHAR(_,_) => false
|
|
321 |
case AALTS(_, rs) => rs.exists(bnullable)
|
|
322 |
case ASEQ(_, r1, r2) => bnullable(r1) && bnullable(r2)
|
|
323 |
case ASTAR(_, _) => true
|
|
324 |
}
|
|
325 |
|
|
326 |
def mkepsBC(r: ARexp) : Bits = r match {
|
|
327 |
case AONE(bs) => bs
|
|
328 |
case AALTS(bs, rs) => {
|
|
329 |
val n = rs.indexWhere(bnullable)
|
|
330 |
bs ++ mkepsBC(rs(n))
|
|
331 |
}
|
|
332 |
case ASEQ(bs, r1, r2) => bs ++ mkepsBC(r1) ++ mkepsBC(r2)
|
|
333 |
case ASTAR(bs, r) => bs ++ List(Z)
|
|
334 |
}
|
|
335 |
|
|
336 |
// derivative of a regular expression w.r.t. a character
|
|
337 |
def bder(c: Char, r: ARexp) : ARexp = r match {
|
|
338 |
case AZERO => AZERO
|
|
339 |
case AONE(_) => AZERO
|
|
340 |
case ACHAR(bs, f) => if (c == f) AONE(bs:::List(C(c))) else AZERO
|
|
341 |
case AALTS(bs, rs) => AALTS(bs, rs.map(bder(c, _)))
|
|
342 |
case ASEQ(bs, r1, r2) =>
|
|
343 |
if (bnullable(r1)) AALT(bs, ASEQ(Nil, bder(c, r1), r2), fuse(mkepsBC(r1), bder(c, r2)))
|
|
344 |
else ASEQ(bs, bder(c, r1), r2)
|
|
345 |
case ASTAR(bs, r) => ASEQ(bs, fuse(List(S), bder(c, r)), ASTAR(Nil, r))
|
|
346 |
}
|
|
347 |
|
|
348 |
|
|
349 |
def ders (s: List[Char], r: Rexp) : Rexp = s match {
|
|
350 |
case Nil => r
|
|
351 |
case c::s => ders(s, der(c, r))
|
|
352 |
}
|
|
353 |
|
|
354 |
// derivative w.r.t. a string (iterates bder)
|
|
355 |
@tailrec
|
|
356 |
def bders (s: List[Char], r: ARexp) : ARexp = s match {
|
|
357 |
case Nil => r
|
|
358 |
case c::s => bders(s, bder(c, r))
|
|
359 |
}
|
|
360 |
|
|
361 |
def flats(rs: List[ARexp]): List[ARexp] = rs match {
|
|
362 |
case Nil => Nil
|
|
363 |
case AZERO :: rs1 => flats(rs1)
|
|
364 |
case AALTS(bs, rs1) :: rs2 => rs1.map(fuse(bs, _)) ::: flats(rs2)
|
|
365 |
case r1 :: rs2 => r1 :: flats(rs2)
|
|
366 |
}
|
|
367 |
def rflats(rs: List[Rexp]): List[Rexp] = rs match {
|
|
368 |
case Nil => Nil
|
|
369 |
case ZERO :: rs1 => rflats(rs1)
|
|
370 |
case ALTS(rs1) :: rs2 => rs1 ::: rflats(rs2)
|
|
371 |
case r1 :: rs2 => r1 :: rflats(rs2)
|
|
372 |
}
|
|
373 |
var flats_time = 0L
|
|
374 |
var dist_time = 0L
|
|
375 |
|
|
376 |
def bsimp(r: ARexp): ARexp = r match {
|
|
377 |
case ASEQ(bs1, r1, r2) => (bsimp(r1), bsimp(r2)) match {
|
|
378 |
case (AZERO, _) => AZERO
|
|
379 |
case (_, AZERO) => AZERO
|
|
380 |
case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
|
|
381 |
case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
|
|
382 |
}
|
|
383 |
case AALTS(bs1, rs) => {
|
|
384 |
val rs_simp = rs.map(bsimp)
|
|
385 |
val flat_res = flats(rs_simp)
|
|
386 |
val dist_res = distinctBy(flat_res, erase)
|
|
387 |
dist_res match {
|
|
388 |
case Nil => AZERO
|
|
389 |
case s :: Nil => fuse(bs1, s)
|
|
390 |
case rs => AALTS(bs1, rs)
|
|
391 |
}
|
|
392 |
}
|
5
|
393 |
//case ASTAR(bs, r) => ASTAR(bs, bsimp(r))
|
0
|
394 |
case r => r
|
|
395 |
}
|
5
|
396 |
def super_bsimp(r: ARexp): ARexp = r match {
|
|
397 |
case ASEQ(bs1, r1, r2) => (super_bsimp(r1), super_bsimp(r2)) match {
|
0
|
398 |
case (AZERO, _) => AZERO
|
|
399 |
case (_, AZERO) => AZERO
|
|
400 |
case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
|
5
|
401 |
case (AALTS(bs2, rs), r2) => AALTS(bs1 ++ bs2, rs.map(r => r match {case AONE(bs3) => fuse(bs3, r2) case r => ASEQ(Nil, r, r2)}) )
|
0
|
402 |
case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
|
|
403 |
}
|
|
404 |
case AALTS(bs1, rs) => {
|
5
|
405 |
val rs_simp = rs.map(super_bsimp)
|
0
|
406 |
val flat_res = flats(rs_simp)
|
|
407 |
val dist_res = distinctBy(flat_res, erase)
|
|
408 |
dist_res match {
|
|
409 |
case Nil => AZERO
|
|
410 |
case s :: Nil => fuse(bs1, s)
|
|
411 |
case rs => AALTS(bs1, rs)
|
|
412 |
}
|
|
413 |
}
|
5
|
414 |
//case ASTAR(bs, r) => ASTAR(bs, bsimp(r))
|
0
|
415 |
case r => r
|
|
416 |
}
|
|
417 |
|
5
|
418 |
|
0
|
419 |
def simp_weakened(r: Rexp): Rexp = r match {
|
|
420 |
case SEQ(r1, r2) => (simp_weakened(r1), r2) match {
|
|
421 |
case (ZERO, _) => ZERO
|
|
422 |
case (_, ZERO) => ZERO
|
|
423 |
case (ONE, r2s) => r2s
|
|
424 |
case (r1s, r2s) => SEQ(r1s, r2s)
|
|
425 |
}
|
|
426 |
case ALTS(rs) => {
|
|
427 |
val rs_simp = rs.map(simp_weakened)
|
|
428 |
val flat_res = rflats(rs_simp)
|
|
429 |
val dist_res = rs_simp.distinct
|
|
430 |
dist_res match {
|
|
431 |
case Nil => ZERO
|
|
432 |
case s :: Nil => s
|
|
433 |
case rs => ALTS(rs)
|
|
434 |
}
|
|
435 |
}
|
|
436 |
case STAR(r) => STAR(simp_weakened(r))
|
|
437 |
case r => r
|
|
438 |
}
|
|
439 |
|
|
440 |
|
|
441 |
//----------------------------------------------------------------------------experiment bsimp
|
|
442 |
/*
|
|
443 |
def bders_simp (s: List[Char], r: ARexp) : ARexp = s match {
|
|
444 |
case Nil => r
|
|
445 |
case c::s => bders_simp(s, bsimp(bder(c, r)))
|
|
446 |
}
|
|
447 |
*/
|
|
448 |
/*
|
|
449 |
def time[T](code: => T) = {
|
|
450 |
val start = System.nanoTime()
|
|
451 |
val result = code
|
|
452 |
val end = System.nanoTime()
|
|
453 |
println((end - start)/1.0e9)
|
|
454 |
result
|
|
455 |
}
|
|
456 |
*/
|
|
457 |
// main unsimplified lexing function (produces a value)
|
|
458 |
def blex(r: ARexp, s: List[Char]) : Bits = s match {
|
|
459 |
case Nil => if (bnullable(r)) mkepsBC(r) else throw new Exception("Not matched")
|
|
460 |
case c::cs => {
|
|
461 |
val der_res = bder(c,r)
|
|
462 |
blex(der_res, cs)
|
|
463 |
}
|
|
464 |
}
|
|
465 |
|
|
466 |
def bpre_lexing(r: Rexp, s: String) = blex(internalise(r), s.toList)
|
|
467 |
//def blexing(r: Rexp, s: String) : Val = decode(r, blex(internalise(r), s.toList))
|
|
468 |
|
|
469 |
var bder_time = 0L
|
|
470 |
var bsimp_time = 0L
|
|
471 |
var mkepsBC_time = 0L
|
|
472 |
var small_de = 2
|
|
473 |
var big_de = 5
|
|
474 |
var usual_de = 3
|
|
475 |
|
|
476 |
def blex_simp(r: ARexp, s: List[Char]) : Bits = s match {
|
|
477 |
case Nil => {
|
|
478 |
if (bnullable(r)) {
|
|
479 |
//println(asize(r))
|
5
|
480 |
mkepsBC(r)
|
0
|
481 |
}
|
|
482 |
else throw new Exception("Not matched")
|
|
483 |
}
|
|
484 |
case c::cs => {
|
|
485 |
val der_res = bder(c,r)
|
|
486 |
val simp_res = bsimp(der_res)
|
|
487 |
blex_simp(simp_res, cs)
|
|
488 |
}
|
|
489 |
}
|
5
|
490 |
def super_blex_simp(r: ARexp, s: List[Char]): Bits = s match {
|
|
491 |
case Nil => {
|
|
492 |
if (bnullable(r)) {
|
|
493 |
mkepsBC(r)
|
|
494 |
}
|
|
495 |
else throw new Exception("Not matched")
|
|
496 |
}
|
|
497 |
case c::cs => {
|
|
498 |
super_blex_simp(super_bsimp(bder(c,r)), cs)
|
|
499 |
}
|
|
500 |
}
|
0
|
501 |
def blex_real_simp(r: ARexp, s: List[Char]): ARexp = s match{
|
|
502 |
case Nil => r
|
|
503 |
case c::cs => blex_real_simp(bsimp(bder(c, r)), cs)
|
|
504 |
}
|
|
505 |
|
|
506 |
|
|
507 |
//size: of a Aregx for testing purposes
|
|
508 |
def size(r: Rexp) : Int = r match {
|
|
509 |
case ZERO => 1
|
|
510 |
case ONE => 1
|
|
511 |
case CHAR(_) => 1
|
|
512 |
case SEQ(r1, r2) => 1 + size(r1) + size(r2)
|
|
513 |
case ALTS(rs) => 1 + rs.map(size).sum
|
|
514 |
case STAR(r) => 1 + size(r)
|
|
515 |
}
|
|
516 |
|
|
517 |
def asize(a: ARexp) = size(erase(a))
|
|
518 |
|
|
519 |
|
|
520 |
// decoding does not work yet
|
|
521 |
def blexing_simp(r: Rexp, s: String) = {
|
|
522 |
val bit_code = blex_simp(internalise(r), s.toList)
|
5
|
523 |
decode(r, bit_code)
|
|
524 |
}
|
|
525 |
def super_blexing_simp(r: Rexp, s: String) = {
|
|
526 |
decode(r, super_blex_simp(internalise(r), s.toList))
|
0
|
527 |
}
|
|
528 |
|
|
529 |
|
|
530 |
|
|
531 |
|
|
532 |
|
|
533 |
// Lexing Rules for a Small While Language
|
|
534 |
|
|
535 |
//symbols
|
|
536 |
/*
|
|
537 |
val SYM = PRED("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".contains(_))
|
|
538 |
|
|
539 |
//digits
|
|
540 |
val DIGIT = PRED("0123456789".contains(_))
|
|
541 |
//identifiers
|
|
542 |
val ID = SYM ~ (SYM | DIGIT).%
|
|
543 |
//numbers
|
|
544 |
val NUM = STAR(DIGIT)
|
|
545 |
//keywords
|
|
546 |
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false"
|
|
547 |
val AKEYWORD: Rexp = ALTS(List("skip" , "while" , "do" , "if" , "then" , "else" , "read" , "write" , "true" , "false"))
|
|
548 |
//semicolons
|
|
549 |
val SEMI: Rexp = ";"
|
|
550 |
//operators
|
|
551 |
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
|
|
552 |
val AOP: Rexp = ALTS(List(":=" , "==" , "-" , "+" , "*" , "!=" , "<" , ">" , "<=" , ">=" , "%" , "/"))
|
|
553 |
//whitespaces
|
|
554 |
val WHITESPACE = PLUS(" " | "\n" | "\t")
|
|
555 |
//parentheses
|
|
556 |
val RPAREN: Rexp = ")"
|
|
557 |
val LPAREN: Rexp = "("
|
|
558 |
val BEGIN: Rexp = "{"
|
|
559 |
val END: Rexp = "}"
|
|
560 |
//strings...but probably needs not
|
|
561 |
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
|
|
562 |
|
|
563 |
|
|
564 |
|
|
565 |
val WHILE_REGS = (("k" $ KEYWORD) |
|
|
566 |
("i" $ ID) |
|
|
567 |
("o" $ OP) |
|
|
568 |
("n" $ NUM) |
|
|
569 |
("s" $ SEMI) |
|
|
570 |
("str" $ STRING) |
|
|
571 |
("p" $ (LPAREN | RPAREN)) |
|
|
572 |
("b" $ (BEGIN | END)) |
|
|
573 |
("w" $ WHITESPACE)).%
|
|
574 |
|
|
575 |
val AWHILE_REGS = (
|
|
576 |
ALTS(
|
|
577 |
List(
|
|
578 |
("k" $ AKEYWORD),
|
|
579 |
("i" $ ID),
|
|
580 |
("o" $ AOP) ,
|
|
581 |
("n" $ NUM) ,
|
|
582 |
("s" $ SEMI) ,
|
|
583 |
("str" $ STRING),
|
|
584 |
("p" $ (LPAREN | RPAREN)),
|
|
585 |
("b" $ (BEGIN | END)),
|
|
586 |
("w" $ WHITESPACE)
|
|
587 |
)
|
|
588 |
)
|
|
589 |
).%
|
|
590 |
|
|
591 |
*/
|
|
592 |
|
|
593 |
|
|
594 |
//--------------------------------------------------------------------------------------------------------START OF NON-BITCODE PART (TESTING)
|
|
595 |
/*
|
|
596 |
// Two Simple While programs
|
|
597 |
//========================
|
|
598 |
println("prog0 test")
|
|
599 |
|
|
600 |
val prog0 = """read n"""
|
|
601 |
println(env(lexing_simp(WHILE_REGS, prog0)))
|
|
602 |
println(tokenise(WHILE_REGS, prog0))
|
|
603 |
|
|
604 |
println("prog1 test")
|
|
605 |
|
|
606 |
val prog1 = """read n; write (n)"""
|
|
607 |
println(tokenise(WHILE_REGS, prog1))
|
|
608 |
|
|
609 |
*/
|
|
610 |
// Bigger Tests
|
|
611 |
//==============
|
|
612 |
|
|
613 |
def escape(raw: String): String = {
|
|
614 |
import scala.reflect.runtime.universe._
|
|
615 |
Literal(Constant(raw)).toString
|
|
616 |
}
|
|
617 |
|
|
618 |
val prog2 = """
|
|
619 |
write "Fib";
|
|
620 |
read n;
|
|
621 |
minus1 := 0;
|
|
622 |
minus2 := 1;
|
|
623 |
while n > 0 do {
|
|
624 |
temp := minus2;
|
|
625 |
minus2 := minus1 + minus2;
|
|
626 |
minus1 := temp;
|
|
627 |
n := n - 1
|
|
628 |
};
|
|
629 |
write "Result";
|
|
630 |
write minus2
|
|
631 |
"""
|
|
632 |
|
|
633 |
val prog3 = """
|
|
634 |
start := 1000;
|
|
635 |
x := start;
|
|
636 |
y := start;
|
|
637 |
z := start;
|
|
638 |
while 0 < x do {
|
|
639 |
while 0 < y do {
|
|
640 |
while 0 < z do {
|
|
641 |
z := z - 1
|
|
642 |
};
|
|
643 |
z := start;
|
|
644 |
y := y - 1
|
|
645 |
};
|
|
646 |
y := start;
|
|
647 |
x := x - 1
|
|
648 |
}
|
|
649 |
"""
|
|
650 |
/*
|
|
651 |
for(i <- 400 to 400 by 1){
|
|
652 |
println(i+":")
|
|
653 |
blexing_simp(WHILE_REGS, prog2 * i)
|
|
654 |
} */
|
|
655 |
|
|
656 |
/*
|
|
657 |
for (i <- 2 to 5){
|
|
658 |
for(j <- 1 to 3){
|
|
659 |
println(i,j)
|
|
660 |
small_de = i
|
|
661 |
usual_de = i + j
|
|
662 |
big_de = i + 2*j
|
|
663 |
blexing_simp(AWHILE_REGS, prog2 * 100)
|
|
664 |
}
|
|
665 |
}*/
|
|
666 |
|
|
667 |
/*
|
|
668 |
println("Tokens of prog2")
|
|
669 |
println(tokenise(WHILE_REGS, prog2).mkString("\n"))
|
|
670 |
|
|
671 |
val fib_tokens = tokenise(WHILE_REGS, prog2)
|
|
672 |
fib_tokens.map{case (s1, s2) => (escape(s1), escape(s2))}.mkString(",\n")
|
|
673 |
|
|
674 |
|
|
675 |
val test_tokens = tokenise(WHILE_REGS, prog3)
|
|
676 |
test_tokens.map{case (s1, s2) => (escape(s1), escape(s2))}.mkString(",\n")
|
|
677 |
*/
|
|
678 |
|
|
679 |
/*
|
|
680 |
println("time test for blexing_simp")
|
|
681 |
for (i <- 1 to 1 by 1) {
|
|
682 |
lexing_simp(WHILE_REGS, prog2 * i)
|
|
683 |
blexing_simp(WHILE_REGS, prog2 * i)
|
|
684 |
for( j <- 0 to each_simp_timeb.length - 1){
|
|
685 |
if( each_simp_timeb(j)/each_simp_time(j) >= 10.0 )
|
|
686 |
println(j, each_simp_timeb(j), each_simp_time(j))
|
|
687 |
}
|
|
688 |
}
|
|
689 |
*/
|
|
690 |
|
|
691 |
|
|
692 |
//--------------------------------------------------------------------------------------------------------END OF NON-BITCODE PART (TESTING)
|
|
693 |
|
|
694 |
|
|
695 |
|
|
696 |
def clear() = {
|
|
697 |
print("")
|
|
698 |
//print("\33[H\33[2J")
|
|
699 |
}
|
|
700 |
|
|
701 |
//testing the two lexings produce the same value
|
|
702 |
//enumerates strings of length n over alphabet cs
|
|
703 |
def strs(n: Int, cs: String) : Set[String] = {
|
|
704 |
if (n == 0) Set("")
|
|
705 |
else {
|
|
706 |
val ss = strs(n - 1, cs)
|
|
707 |
ss ++
|
|
708 |
(for (s <- ss; c <- cs.toList) yield c + s)
|
|
709 |
}
|
|
710 |
}
|
|
711 |
def enum(n: Int, s: String) : Stream[Rexp] = n match {
|
|
712 |
case 0 => ZERO #:: ONE #:: s.toStream.map(CHAR)
|
|
713 |
case n => {
|
|
714 |
val rs = enum(n - 1, s)
|
|
715 |
rs #:::
|
|
716 |
(for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) #:::
|
|
717 |
(for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) #:::
|
|
718 |
(for (r1 <- rs) yield STAR(r1))
|
|
719 |
}
|
|
720 |
}
|
|
721 |
|
|
722 |
//tests blexing and lexing
|
|
723 |
def tests_blexer_simp(ss: Set[String])(r: Rexp) = {
|
|
724 |
clear()
|
|
725 |
//println(s"Testing ${r}")
|
|
726 |
for (s <- ss.par) yield {
|
|
727 |
val res1 = Try(Some(lexing_simp(r, s))).getOrElse(None)
|
5
|
728 |
val res2 = Try(Some(super_blexing_simp(r, s))).getOrElse(None)
|
0
|
729 |
if (res1 != res2) println(s"Disagree on ${r} and ${s}")
|
|
730 |
if (res1 != res2) println(s" ${res1} != ${res2}")
|
|
731 |
if (res1 != res2) Some((r, s)) else None
|
|
732 |
}
|
|
733 |
}
|
|
734 |
|
|
735 |
|
|
736 |
|
5
|
737 |
|
0
|
738 |
/*
|
|
739 |
def single_expression_explorer(ar: ARexp, ss: Set[String]): Unit = {
|
|
740 |
for (s <- ss){
|
|
741 |
|
|
742 |
val der_res = bder(c, ar)
|
|
743 |
val simp_res = bsimp(der_res)
|
|
744 |
println(asize(der_res))
|
|
745 |
println(asize(simp_res))
|
|
746 |
single_expression_explorer(simp_res, (sc - c))
|
|
747 |
}
|
|
748 |
}*/
|
|
749 |
|
|
750 |
//single_expression_explorer(internalise(("c"~("a"+"b"))%) , Set('a','b','c'))
|
|
751 |
|
|
752 |
|
|
753 |
}
|
|
754 |
|
|
755 |
import Rexp.Bits
|
|
756 |
abstract class ARexp
|
|
757 |
case object AZERO extends ARexp
|
|
758 |
case class AONE(bs: Bits) extends ARexp
|
|
759 |
case class ACHAR(bs: Bits, f: Char) extends ARexp
|
|
760 |
case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp
|
|
761 |
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp
|
|
762 |
case class ASTAR(bs: Bits, r: ARexp) extends ARexp
|
|
763 |
|
|
764 |
|
|
765 |
|
|
766 |
abstract class Val
|
|
767 |
case object Empty extends Val
|
|
768 |
case class Chr(c: Char) extends Val
|
|
769 |
case class Sequ(v1: Val, v2: Val) extends Val
|
|
770 |
case class Left(v: Val) extends Val
|
|
771 |
case class Right(v: Val) extends Val
|
|
772 |
case class Stars(vs: List[Val]) extends Val
|
|
773 |
case class Rec(x: String, v: Val) extends Val
|
|
774 |
//case class Pos(i: Int, v: Val) extends Val
|
|
775 |
case object Prd extends Val
|