author | Chengsong |
Wed, 08 May 2019 22:09:59 +0100 | |
changeset 16 | c51178fa85fe |
parent 15 | cd0ceaf89c1d |
child 17 | 3241b1e71633 |
permissions | -rw-r--r-- |
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 |
|
12
768b833d6230
removed C(c) The retrieve and code in the previous version is still not correct and will crash. no prob now.
Chengsong
parents:
11
diff
changeset
|
10 |
//case class C(c: Char) extends Bit |
0 | 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) |
|
12
768b833d6230
removed C(c) The retrieve and code in the previous version is still not correct and will crash. no prob now.
Chengsong
parents:
11
diff
changeset
|
94 |
case (CHAR(f), bs) => (Chr(f), bs) |
768b833d6230
removed C(c) The retrieve and code in the previous version is still not correct and will crash. no prob now.
Chengsong
parents:
11
diff
changeset
|
95 |
case (ALTS(r::Nil), bs) => decode_aux(r, bs)//this case seems only used when we simp a regex before derivatives and it contains things like alt("a") |
0 | 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 |
|
11
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
129 |
def code(v: Val): Bits = v match { |
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
130 |
case Empty => Nil |
12
768b833d6230
removed C(c) The retrieve and code in the previous version is still not correct and will crash. no prob now.
Chengsong
parents:
11
diff
changeset
|
131 |
case Chr(a) => Nil |
11
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
132 |
case Left(v) => Z :: code(v) |
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
133 |
case Right(v) => S :: code(v) |
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
134 |
case Sequ(v1, v2) => code(v1) ::: code(v2) |
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
135 |
case Stars(Nil) => Z::Nil |
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
136 |
case Stars(v::vs) => S::code(v) ::: code(Stars(vs)) |
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
137 |
} |
0 | 138 |
|
11
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
139 |
|
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
140 |
def retrieve(r: ARexp, v: Val): Bits = (r,v) match { |
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
141 |
case (AONE(bs), Empty) => bs |
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
142 |
case (ACHAR(bs, c), Chr(d)) => bs |
14 | 143 |
case (AALTS(bs, a::Nil), v) => bs ++ retrieve(a, v) |
11
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
144 |
case (AALTS(bs, as), Left(v)) => bs ++ retrieve(as.head,v) |
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
145 |
case (AALTS(bs, as), Right(v)) => bs ++ retrieve(AALTS(Nil,as.tail),v) |
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
146 |
case (ASEQ(bs, a1, a2), Sequ(v1, v2)) => bs ++ retrieve(a1, v1) ++ retrieve(a2, v2) |
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
147 |
case (ASTAR(bs, a), Stars(Nil)) => bs ++ List(Z) |
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
148 |
case (ASTAR(bs, a), Stars(v::vs)) => bs ++ List(S) ++ retrieve(a, v) ++ retrieve(ASTAR(Nil, a), Stars(vs)) |
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
149 |
} |
0 | 150 |
//erase function: extracts the regx from Aregex |
151 |
def erase(r:ARexp): Rexp = r match{ |
|
152 |
case AZERO => ZERO |
|
153 |
case AONE(_) => ONE |
|
154 |
case ACHAR(bs, f) => CHAR(f) |
|
155 |
case AALTS(bs, rs) => ALTS(rs.map(erase(_))) |
|
156 |
case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2)) |
|
157 |
case ASTAR(cs, r)=> STAR(erase(r)) |
|
158 |
} |
|
159 |
||
160 |
//--------------------------------------------------------------------------------------------------------START OF NON-BITCODE PART |
|
161 |
// nullable function: tests whether the regular |
|
162 |
// expression can recognise the empty string |
|
163 |
def nullable (r: Rexp) : Boolean = r match { |
|
164 |
case ZERO => false |
|
165 |
case ONE => true |
|
166 |
case CHAR(_) => false |
|
167 |
case ALTS(rs) => rs.exists(nullable) |
|
168 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
169 |
case STAR(_) => true |
|
170 |
case RECD(_, r) => nullable(r) |
|
171 |
//case PLUS(r) => nullable(r) |
|
172 |
} |
|
173 |
||
174 |
// derivative of a regular expression w.r.t. a character |
|
175 |
def der (c: Char, r: Rexp) : Rexp = r match { |
|
176 |
case ZERO => ZERO |
|
177 |
case ONE => ZERO |
|
178 |
case CHAR(f) => if (c == f) ONE else ZERO |
|
179 |
case ALTS(List(r1, r2)) => ALTS(List(der(c, r1), der(c, r2))) |
|
180 |
case SEQ(r1, r2) => |
|
181 |
if (nullable(r1)) ALTS(List(SEQ(der(c, r1), r2), der(c, r2))) |
|
182 |
else SEQ(der(c, r1), r2) |
|
183 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
184 |
case RECD(_, r1) => der(c, r1) |
|
185 |
//case PLUS(r) => SEQ(der(c, r), STAR(r)) |
|
186 |
} |
|
187 |
||
188 |
def flatten(v: Val) : String = v match { |
|
189 |
case Empty => "" |
|
190 |
case Chr(c) => c.toString |
|
191 |
case Left(v) => flatten(v) |
|
192 |
case Right(v) => flatten(v) |
|
193 |
case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
|
194 |
case Stars(vs) => vs.map(flatten).mkString |
|
195 |
case Rec(_, v) => flatten(v) |
|
196 |
} |
|
197 |
||
198 |
// extracts an environment from a value |
|
199 |
def env(v: Val) : List[(String, String)] = v match { |
|
200 |
case Empty => Nil |
|
201 |
case Chr(c) => Nil |
|
202 |
case Left(v) => env(v) |
|
203 |
case Right(v) => env(v) |
|
204 |
case Sequ(v1, v2) => env(v1) ::: env(v2) |
|
205 |
case Stars(vs) => vs.flatMap(env) |
|
206 |
case Rec(x, v) => (x, flatten(v))::env(v) |
|
207 |
} |
|
208 |
||
209 |
||
210 |
// injection part |
|
211 |
def mkeps(r: Rexp) : Val = r match { |
|
212 |
case ONE => Empty |
|
213 |
case ALTS(List(r1, r2)) => |
|
214 |
if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
|
215 |
case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
|
216 |
case STAR(r) => Stars(Nil) |
|
217 |
case RECD(x, r) => Rec(x, mkeps(r)) |
|
218 |
//case PLUS(r) => Stars(List(mkeps(r))) |
|
219 |
} |
|
220 |
||
221 |
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
|
222 |
case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
|
223 |
case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
|
224 |
case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
|
225 |
case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
|
226 |
case (ALTS(List(r1, r2)), Left(v1)) => Left(inj(r1, c, v1)) |
|
227 |
case (ALTS(List(r1, r2)), Right(v2)) => Right(inj(r2, c, v2)) |
|
228 |
case (CHAR(_), Empty) => Chr(c) |
|
229 |
case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
|
230 |
//case (PLUS(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
|
231 |
} |
|
232 |
def lex(r: Rexp, s: List[Char]) : Val = s match { |
|
233 |
case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched") |
|
234 |
case c::cs => inj(r, c, lex(der(c, r), cs)) |
|
235 |
} |
|
236 |
||
237 |
def lexing(r: Rexp, s: String) : Val = lex(r, s.toList) |
|
238 |
||
239 |
// some "rectification" functions for simplification |
|
240 |
def F_ID(v: Val): Val = v |
|
241 |
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v)) |
|
242 |
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v)) |
|
243 |
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
|
244 |
case Right(v) => Right(f2(v)) |
|
245 |
case Left(v) => Left(f1(v)) |
|
246 |
} |
|
247 |
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
|
248 |
case Sequ(v1, v2) => Sequ(f1(v1), f2(v2)) |
|
249 |
} |
|
250 |
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = |
|
251 |
(v:Val) => Sequ(f1(Empty), f2(v)) |
|
252 |
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = |
|
253 |
(v:Val) => Sequ(f1(v), f2(Empty)) |
|
254 |
def F_RECD(f: Val => Val) = (v:Val) => v match { |
|
255 |
case Rec(x, v) => Rec(x, f(v)) |
|
256 |
} |
|
257 |
def F_ERROR(v: Val): Val = throw new Exception("error") |
|
258 |
||
259 |
// simplification of regular expressions returning also an |
|
260 |
// rectification function; no simplification under STAR |
|
261 |
def simp(r: Rexp): (Rexp, Val => Val) = r match { |
|
262 |
case ALTS(List(r1, r2)) => { |
|
263 |
val (r1s, f1s) = simp(r1) |
|
264 |
val (r2s, f2s) = simp(r2) |
|
265 |
(r1s, r2s) match { |
|
266 |
case (ZERO, _) => (r2s, F_RIGHT(f2s)) |
|
267 |
case (_, ZERO) => (r1s, F_LEFT(f1s)) |
|
268 |
case _ => if (r1s == r2s) (r1s, F_LEFT(f1s)) |
|
269 |
else (ALTS(List(r1s, r2s)), F_ALT(f1s, f2s)) |
|
270 |
} |
|
271 |
} |
|
272 |
case SEQ(r1, r2) => { |
|
273 |
val (r1s, f1s) = simp(r1) |
|
274 |
val (r2s, f2s) = simp(r2) |
|
275 |
(r1s, r2s) match { |
|
276 |
case (ZERO, _) => (ZERO, F_ERROR) |
|
277 |
case (_, ZERO) => (ZERO, F_ERROR) |
|
278 |
case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s)) |
|
279 |
case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s)) |
|
280 |
case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s)) |
|
281 |
} |
|
282 |
} |
|
283 |
case RECD(x, r1) => { |
|
284 |
val (r1s, f1s) = simp(r1) |
|
285 |
(RECD(x, r1s), F_RECD(f1s)) |
|
286 |
} |
|
287 |
case r => (r, F_ID) |
|
288 |
} |
|
289 |
/* |
|
290 |
val each_simp_time = scala.collection.mutable.ArrayBuffer.empty[Long] |
|
291 |
val each_simp_timeb = scala.collection.mutable.ArrayBuffer.empty[Long] |
|
292 |
*/ |
|
293 |
def lex_simp(r: Rexp, s: List[Char]) : Val = s match { |
|
294 |
case Nil => { |
|
295 |
if (nullable(r)) { |
|
296 |
mkeps(r) |
|
297 |
} |
|
298 |
else throw new Exception("Not matched") |
|
299 |
} |
|
300 |
case c::cs => { |
|
301 |
val (r_simp, f_simp) = simp(der(c, r)) |
|
302 |
inj(r, c, f_simp(lex_simp(r_simp, cs))) |
|
303 |
} |
|
304 |
} |
|
305 |
||
306 |
def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList) |
|
307 |
||
308 |
//println(lexing_simp(("a" | "ab") ~ ("b" | ""), "ab")) |
|
309 |
||
310 |
// filters out all white spaces |
|
311 |
def tokenise(r: Rexp, s: String) = |
|
312 |
env(lexing_simp(r, s)).filterNot { _._1 == "w"} |
|
313 |
||
314 |
||
315 |
//reads the string from a file |
|
316 |
def fromFile(name: String) : String = |
|
317 |
io.Source.fromFile(name).mkString |
|
318 |
||
319 |
def tokenise_file(r: Rexp, name: String) = |
|
320 |
tokenise(r, fromFile(name)) |
|
321 |
||
322 |
// Testing |
|
323 |
//============ |
|
324 |
||
325 |
def time[T](code: => T) = { |
|
326 |
val start = System.nanoTime() |
|
327 |
val result = code |
|
328 |
val end = System.nanoTime() |
|
329 |
println((end - start)/1.0e9) |
|
330 |
result |
|
331 |
} |
|
332 |
||
333 |
//--------------------------------------------------------------------------------------------------------END OF NON-BITCODE PART |
|
334 |
||
335 |
// bnullable function: tests whether the aregular |
|
336 |
// expression can recognise the empty string |
|
337 |
def bnullable (r: ARexp) : Boolean = r match { |
|
338 |
case AZERO => false |
|
339 |
case AONE(_) => true |
|
340 |
case ACHAR(_,_) => false |
|
341 |
case AALTS(_, rs) => rs.exists(bnullable) |
|
342 |
case ASEQ(_, r1, r2) => bnullable(r1) && bnullable(r2) |
|
343 |
case ASTAR(_, _) => true |
|
344 |
} |
|
345 |
||
346 |
def mkepsBC(r: ARexp) : Bits = r match { |
|
347 |
case AONE(bs) => bs |
|
348 |
case AALTS(bs, rs) => { |
|
349 |
val n = rs.indexWhere(bnullable) |
|
350 |
bs ++ mkepsBC(rs(n)) |
|
351 |
} |
|
352 |
case ASEQ(bs, r1, r2) => bs ++ mkepsBC(r1) ++ mkepsBC(r2) |
|
353 |
case ASTAR(bs, r) => bs ++ List(Z) |
|
354 |
} |
|
355 |
||
356 |
// derivative of a regular expression w.r.t. a character |
|
357 |
def bder(c: Char, r: ARexp) : ARexp = r match { |
|
358 |
case AZERO => AZERO |
|
359 |
case AONE(_) => AZERO |
|
12
768b833d6230
removed C(c) The retrieve and code in the previous version is still not correct and will crash. no prob now.
Chengsong
parents:
11
diff
changeset
|
360 |
case ACHAR(bs, f) => if (c == f) AONE(bs) else AZERO |
0 | 361 |
case AALTS(bs, rs) => AALTS(bs, rs.map(bder(c, _))) |
362 |
case ASEQ(bs, r1, r2) => |
|
363 |
if (bnullable(r1)) AALT(bs, ASEQ(Nil, bder(c, r1), r2), fuse(mkepsBC(r1), bder(c, r2))) |
|
364 |
else ASEQ(bs, bder(c, r1), r2) |
|
365 |
case ASTAR(bs, r) => ASEQ(bs, fuse(List(S), bder(c, r)), ASTAR(Nil, r)) |
|
366 |
} |
|
367 |
||
368 |
||
369 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
|
370 |
case Nil => r |
|
371 |
case c::s => ders(s, der(c, r)) |
|
372 |
} |
|
373 |
||
374 |
// derivative w.r.t. a string (iterates bder) |
|
375 |
@tailrec |
|
376 |
def bders (s: List[Char], r: ARexp) : ARexp = s match { |
|
377 |
case Nil => r |
|
378 |
case c::s => bders(s, bder(c, r)) |
|
379 |
} |
|
380 |
||
381 |
def flats(rs: List[ARexp]): List[ARexp] = rs match { |
|
382 |
case Nil => Nil |
|
383 |
case AZERO :: rs1 => flats(rs1) |
|
384 |
case AALTS(bs, rs1) :: rs2 => rs1.map(fuse(bs, _)) ::: flats(rs2) |
|
385 |
case r1 :: rs2 => r1 :: flats(rs2) |
|
15 | 386 |
} |
387 |
def remove(v: Val): Val = v match{ |
|
388 |
case Right(v1) => v1 |
|
389 |
case Left(v1) => v1 |
|
390 |
case _ => throw new Exception("Not removable") |
|
391 |
} |
|
392 |
def augment(v: Val, i: Int): Val = if(i > 1) augment(Right(v), i - 1) else Right(v) |
|
393 |
//an overly complex version |
|
394 |
/* |
|
395 |
if(rel_dist >0){//the regex we are dealing with is not what v points at |
|
396 |
rs match{ |
|
397 |
case Nil => throw new Exception("Trying to simplify a non-existent value") |
|
398 |
case AZERO :: rs1 => flats_vsimp(rs1, rel_dist - 1, remove(v)) |
|
399 |
case AALTS(bs, rs1) :: rs2 => flats_vsimp(rs2, rel_dist - 1, augment(v, rs1.length - 1))//rs1 is guaranteed to have a len geq 2 |
|
400 |
case r1 :: rs2 => flats_vsimp(rs2, rel_dist - 1, v) |
|
401 |
} |
|
0 | 402 |
} |
15 | 403 |
else if(rel_dist == 0){//we are dealing with regex v is pointing to -- "v->r itself" |
404 |
rs match{//r1 cannot be zero |
|
405 |
AALTS(bs, rs1) :: rs2 => flats_vsimp( ) |
|
406 |
AZERO::rs2 => throw new Exception("Value corresponds to zero") |
|
407 |
r1::rs2 => flats_vsimp(rs2, rel_dist - 1, v) |
|
408 |
} |
|
409 |
||
410 |
} |
|
411 |
else{ |
|
412 |
||
413 |
} |
|
414 |
*/ |
|
415 |
def flats_vsimp(rs: List[ARexp], position: Int): Int = (rs, position) match { |
|
416 |
case (_, 0) => 0 |
|
417 |
case (Nil, _) => 0 |
|
16 | 418 |
case (AZERO :: rs1, _) => flats_vsimp(rs1, position - 1) - 1 |
15 | 419 |
case (AALTS(bs, rs1) :: rs2, _) => rs1.length - 1 + flats_vsimp(rs2, position - 1) |
420 |
case (r1 :: rs2, _) => flats_vsimp(rs2, position - 1) |
|
421 |
} |
|
0 | 422 |
def rflats(rs: List[Rexp]): List[Rexp] = rs match { |
423 |
case Nil => Nil |
|
424 |
case ZERO :: rs1 => rflats(rs1) |
|
425 |
case ALTS(rs1) :: rs2 => rs1 ::: rflats(rs2) |
|
426 |
case r1 :: rs2 => r1 :: rflats(rs2) |
|
427 |
} |
|
428 |
var flats_time = 0L |
|
429 |
var dist_time = 0L |
|
430 |
||
431 |
def bsimp(r: ARexp): ARexp = r match { |
|
432 |
case ASEQ(bs1, r1, r2) => (bsimp(r1), bsimp(r2)) match { |
|
433 |
case (AZERO, _) => AZERO |
|
434 |
case (_, AZERO) => AZERO |
|
435 |
case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s) |
|
436 |
case (r1s, r2s) => ASEQ(bs1, r1s, r2s) |
|
437 |
} |
|
438 |
case AALTS(bs1, rs) => { |
|
439 |
val rs_simp = rs.map(bsimp) |
|
440 |
val flat_res = flats(rs_simp) |
|
441 |
val dist_res = distinctBy(flat_res, erase) |
|
442 |
dist_res match { |
|
443 |
case Nil => AZERO |
|
444 |
case s :: Nil => fuse(bs1, s) |
|
445 |
case rs => AALTS(bs1, rs) |
|
446 |
} |
|
447 |
} |
|
5 | 448 |
//case ASTAR(bs, r) => ASTAR(bs, bsimp(r)) |
0 | 449 |
case r => r |
450 |
} |
|
16 | 451 |
def find_pos(v: Val, rs: List[Rexp]): Int = (v, rs) match{ |
15 | 452 |
case (Right(v), r::Nil) => 1 |
453 |
case (Left(v), r::rs) => 0 |
|
454 |
case (Right(v), r::rs) => find_pos(v, rs) + 1 |
|
455 |
case (v, _) => 0 |
|
456 |
} |
|
16 | 457 |
def remove(v: Val, rs: List[Rexp]) : Val = (v,rs) match {//remove the outmost layer of ALTS's Left and Right |
15 | 458 |
case (Right(v), r::Nil) => v |
459 |
case (Left(v), r::rs) => v |
|
460 |
case (Right(v), r::rs) => remove(v, rs) |
|
461 |
} |
|
16 | 462 |
def simple_end(v: Val): Boolean = v match { |
15 | 463 |
case Left(v) => return false |
464 |
case Right(v) => return simple_end(v) |
|
465 |
case v => return true |
|
466 |
} |
|
16 | 467 |
def isend(v: Val, rs: List[ARexp], position: Int): Boolean = { |
15 | 468 |
val rsbh = rs.slice(position, rs.length) |
469 |
val out_end = if(flats(rsbh) == Nil) true else false |
|
470 |
val inner_end = simple_end(v) |
|
471 |
inner_end && out_end |
|
472 |
} |
|
16 | 473 |
def get_coat(v: Val, rs: List[Rexp], vs: Val): Val = (v, rs) match{//the dual operation of remove(so-called by myself) |
15 | 474 |
case (Right(v), r::Nil) => Right(vs) |
475 |
case (Left(v), r::rs) => Left(vs) |
|
476 |
case (Right(v), r::rs) => Right(get_coat(v, rs, vs)) |
|
477 |
} |
|
478 |
def coat(v: Val, i: Int) : Val = i match { |
|
479 |
case 0 => v |
|
480 |
case i => coat(Right(v), i - 1) |
|
481 |
} |
|
16 | 482 |
/* |
15 | 483 |
def bsimp2(r: ARexp, v: Val): (ARexp, Val => Val) = (r,v) match{ |
484 |
case (ASEQ(bs1, r1, r2), v) => (bsimp2(r1), bsimp2(r2)) match { |
|
485 |
case ((AZERO, _), (_, _) )=> (AZERO, undefined) |
|
486 |
case ((_, _), (AZERO, _)) => (AZERO, undefined) |
|
487 |
case ((AONE(bs2), f1) , (r2s, f2)) => (fuse(bs1 ++ bs2, r2s), lambda v => v match { case Sequ(_, v) => f2(v) } ) |
|
488 |
case ((r1s, f1), (r2s, f2)) => (ASEQ(bs1, r1s, r2s), lambda v => v match {case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))} |
|
489 |
} |
|
490 |
case AALTS(bs1, rs) => { |
|
491 |
val init_ind = find_pos(v, rs) |
|
492 |
val vs = bsimp2(rs[init_ind], remove(v, rs))//remove all the outer layers of left and right in v to match the regx rs[i] |
|
493 |
val rs_simp = rs.map(bsimp) |
|
494 |
val vs_kernel = rs_simp[init_ind] match { |
|
495 |
case AALTS(bs2, rs2) => remove(vs, rs_simp[init_ind])//remove the secondary layer of left and right |
|
496 |
case r => vs |
|
497 |
} |
|
498 |
val vs_for_coating = if(isend(vs, rs_simp, init_ind)) vs_kernel else Left(vs_kernel) |
|
499 |
||
500 |
val r_s = rs_simp[init_ind] |
|
501 |
val shift = flats_vsimp(vs, rs_simp, init_ind) + find_pos(vs, rs_simp[init_ind]) |
|
502 |
val vf = coat(vs_for_coating, shift + init_ind) |
|
503 |
||
504 |
val flat_res = flats(rs_simp)//flats2 returns a list of regex and a single v |
|
505 |
val dist_res = distinctBy(flat_res, erase) |
|
506 |
dist_res match { |
|
507 |
case Nil => AZERO |
|
508 |
case s :: Nil => fuse(bs1, s) |
|
509 |
case rs => AALTS(bs1, rs) |
|
510 |
} |
|
511 |
} |
|
512 |
//case ASTAR(bs, r) => ASTAR(bs, bsimp(r)) |
|
513 |
case r => r |
|
16 | 514 |
}*/ |
5 | 515 |
def super_bsimp(r: ARexp): ARexp = r match { |
516 |
case ASEQ(bs1, r1, r2) => (super_bsimp(r1), super_bsimp(r2)) match { |
|
0 | 517 |
case (AZERO, _) => AZERO |
518 |
case (_, AZERO) => AZERO |
|
11
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
519 |
case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)//万一是(r1, alts(rs))这种形式呢 |
9c1ca6d6e190
The C(Char) construct is incompatible with the code and retrieve in Fahad's thesis.
Chengsong
parents:
5
diff
changeset
|
520 |
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 | 521 |
case (r1s, r2s) => ASEQ(bs1, r1s, r2s) |
522 |
} |
|
523 |
case AALTS(bs1, rs) => { |
|
5 | 524 |
val rs_simp = rs.map(super_bsimp) |
0 | 525 |
val flat_res = flats(rs_simp) |
526 |
val dist_res = distinctBy(flat_res, erase) |
|
527 |
dist_res match { |
|
528 |
case Nil => AZERO |
|
529 |
case s :: Nil => fuse(bs1, s) |
|
530 |
case rs => AALTS(bs1, rs) |
|
531 |
} |
|
532 |
} |
|
5 | 533 |
//case ASTAR(bs, r) => ASTAR(bs, bsimp(r)) |
0 | 534 |
case r => r |
535 |
} |
|
536 |
||
5 | 537 |
|
0 | 538 |
def simp_weakened(r: Rexp): Rexp = r match { |
539 |
case SEQ(r1, r2) => (simp_weakened(r1), r2) match { |
|
540 |
case (ZERO, _) => ZERO |
|
541 |
case (_, ZERO) => ZERO |
|
542 |
case (ONE, r2s) => r2s |
|
543 |
case (r1s, r2s) => SEQ(r1s, r2s) |
|
544 |
} |
|
545 |
case ALTS(rs) => { |
|
546 |
val rs_simp = rs.map(simp_weakened) |
|
547 |
val flat_res = rflats(rs_simp) |
|
548 |
val dist_res = rs_simp.distinct |
|
549 |
dist_res match { |
|
550 |
case Nil => ZERO |
|
551 |
case s :: Nil => s |
|
552 |
case rs => ALTS(rs) |
|
553 |
} |
|
554 |
} |
|
555 |
case STAR(r) => STAR(simp_weakened(r)) |
|
556 |
case r => r |
|
557 |
} |
|
558 |
||
559 |
def bders_simp (s: List[Char], r: ARexp) : ARexp = s match { |
|
560 |
case Nil => r |
|
561 |
case c::s => bders_simp(s, bsimp(bder(c, r))) |
|
562 |
} |
|
14 | 563 |
//----------------------------------------------------------------------------experiment bsimp |
564 |
/* |
|
565 |
||
0 | 566 |
*/ |
567 |
/* |
|
568 |
def time[T](code: => T) = { |
|
569 |
val start = System.nanoTime() |
|
570 |
val result = code |
|
571 |
val end = System.nanoTime() |
|
572 |
println((end - start)/1.0e9) |
|
573 |
result |
|
574 |
} |
|
575 |
*/ |
|
576 |
// main unsimplified lexing function (produces a value) |
|
577 |
def blex(r: ARexp, s: List[Char]) : Bits = s match { |
|
578 |
case Nil => if (bnullable(r)) mkepsBC(r) else throw new Exception("Not matched") |
|
579 |
case c::cs => { |
|
580 |
val der_res = bder(c,r) |
|
581 |
blex(der_res, cs) |
|
582 |
} |
|
583 |
} |
|
584 |
||
585 |
def bpre_lexing(r: Rexp, s: String) = blex(internalise(r), s.toList) |
|
14 | 586 |
def blexing(r: Rexp, s: String) : Val = decode(r, blex(internalise(r), s.toList)) |
0 | 587 |
|
588 |
var bder_time = 0L |
|
589 |
var bsimp_time = 0L |
|
590 |
var mkepsBC_time = 0L |
|
591 |
var small_de = 2 |
|
592 |
var big_de = 5 |
|
593 |
var usual_de = 3 |
|
594 |
||
595 |
def blex_simp(r: ARexp, s: List[Char]) : Bits = s match { |
|
596 |
case Nil => { |
|
597 |
if (bnullable(r)) { |
|
598 |
//println(asize(r)) |
|
5 | 599 |
mkepsBC(r) |
0 | 600 |
} |
601 |
else throw new Exception("Not matched") |
|
602 |
} |
|
603 |
case c::cs => { |
|
604 |
val der_res = bder(c,r) |
|
605 |
val simp_res = bsimp(der_res) |
|
606 |
blex_simp(simp_res, cs) |
|
607 |
} |
|
608 |
} |
|
5 | 609 |
def super_blex_simp(r: ARexp, s: List[Char]): Bits = s match { |
610 |
case Nil => { |
|
611 |
if (bnullable(r)) { |
|
612 |
mkepsBC(r) |
|
613 |
} |
|
614 |
else throw new Exception("Not matched") |
|
615 |
} |
|
616 |
case c::cs => { |
|
617 |
super_blex_simp(super_bsimp(bder(c,r)), cs) |
|
618 |
} |
|
619 |
} |
|
0 | 620 |
def blex_real_simp(r: ARexp, s: List[Char]): ARexp = s match{ |
621 |
case Nil => r |
|
622 |
case c::cs => blex_real_simp(bsimp(bder(c, r)), cs) |
|
623 |
} |
|
624 |
||
625 |
||
626 |
//size: of a Aregx for testing purposes |
|
627 |
def size(r: Rexp) : Int = r match { |
|
628 |
case ZERO => 1 |
|
629 |
case ONE => 1 |
|
630 |
case CHAR(_) => 1 |
|
631 |
case SEQ(r1, r2) => 1 + size(r1) + size(r2) |
|
632 |
case ALTS(rs) => 1 + rs.map(size).sum |
|
633 |
case STAR(r) => 1 + size(r) |
|
634 |
} |
|
635 |
||
636 |
def asize(a: ARexp) = size(erase(a)) |
|
637 |
||
638 |
||
639 |
// decoding does not work yet |
|
640 |
def blexing_simp(r: Rexp, s: String) = { |
|
641 |
val bit_code = blex_simp(internalise(r), s.toList) |
|
5 | 642 |
decode(r, bit_code) |
643 |
} |
|
644 |
def super_blexing_simp(r: Rexp, s: String) = { |
|
645 |
decode(r, super_blex_simp(internalise(r), s.toList)) |
|
0 | 646 |
} |
647 |
||
648 |
||
649 |
||
650 |
||
651 |
||
652 |
// Lexing Rules for a Small While Language |
|
653 |
||
654 |
//symbols |
|
655 |
/* |
|
656 |
val SYM = PRED("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".contains(_)) |
|
657 |
||
658 |
//digits |
|
659 |
val DIGIT = PRED("0123456789".contains(_)) |
|
660 |
//identifiers |
|
661 |
val ID = SYM ~ (SYM | DIGIT).% |
|
662 |
//numbers |
|
663 |
val NUM = STAR(DIGIT) |
|
664 |
//keywords |
|
665 |
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false" |
|
666 |
val AKEYWORD: Rexp = ALTS(List("skip" , "while" , "do" , "if" , "then" , "else" , "read" , "write" , "true" , "false")) |
|
667 |
//semicolons |
|
668 |
val SEMI: Rexp = ";" |
|
669 |
//operators |
|
670 |
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/" |
|
671 |
val AOP: Rexp = ALTS(List(":=" , "==" , "-" , "+" , "*" , "!=" , "<" , ">" , "<=" , ">=" , "%" , "/")) |
|
672 |
//whitespaces |
|
673 |
val WHITESPACE = PLUS(" " | "\n" | "\t") |
|
674 |
//parentheses |
|
675 |
val RPAREN: Rexp = ")" |
|
676 |
val LPAREN: Rexp = "(" |
|
677 |
val BEGIN: Rexp = "{" |
|
678 |
val END: Rexp = "}" |
|
679 |
//strings...but probably needs not |
|
680 |
val STRING: Rexp = "\"" ~ SYM.% ~ "\"" |
|
681 |
||
682 |
||
683 |
||
684 |
val WHILE_REGS = (("k" $ KEYWORD) | |
|
685 |
("i" $ ID) | |
|
686 |
("o" $ OP) | |
|
687 |
("n" $ NUM) | |
|
688 |
("s" $ SEMI) | |
|
689 |
("str" $ STRING) | |
|
690 |
("p" $ (LPAREN | RPAREN)) | |
|
691 |
("b" $ (BEGIN | END)) | |
|
692 |
("w" $ WHITESPACE)).% |
|
693 |
||
694 |
val AWHILE_REGS = ( |
|
695 |
ALTS( |
|
696 |
List( |
|
697 |
("k" $ AKEYWORD), |
|
698 |
("i" $ ID), |
|
699 |
("o" $ AOP) , |
|
700 |
("n" $ NUM) , |
|
701 |
("s" $ SEMI) , |
|
702 |
("str" $ STRING), |
|
703 |
("p" $ (LPAREN | RPAREN)), |
|
704 |
("b" $ (BEGIN | END)), |
|
705 |
("w" $ WHITESPACE) |
|
706 |
) |
|
707 |
) |
|
708 |
).% |
|
709 |
||
710 |
*/ |
|
711 |
||
712 |
||
713 |
//--------------------------------------------------------------------------------------------------------START OF NON-BITCODE PART (TESTING) |
|
714 |
/* |
|
715 |
// Two Simple While programs |
|
716 |
//======================== |
|
717 |
println("prog0 test") |
|
718 |
||
719 |
val prog0 = """read n""" |
|
720 |
println(env(lexing_simp(WHILE_REGS, prog0))) |
|
721 |
println(tokenise(WHILE_REGS, prog0)) |
|
722 |
||
723 |
println("prog1 test") |
|
724 |
||
725 |
val prog1 = """read n; write (n)""" |
|
726 |
println(tokenise(WHILE_REGS, prog1)) |
|
727 |
||
728 |
*/ |
|
729 |
// Bigger Tests |
|
730 |
//============== |
|
731 |
||
732 |
def escape(raw: String): String = { |
|
733 |
import scala.reflect.runtime.universe._ |
|
734 |
Literal(Constant(raw)).toString |
|
735 |
} |
|
736 |
||
737 |
val prog2 = """ |
|
738 |
write "Fib"; |
|
739 |
read n; |
|
740 |
minus1 := 0; |
|
741 |
minus2 := 1; |
|
742 |
while n > 0 do { |
|
743 |
temp := minus2; |
|
744 |
minus2 := minus1 + minus2; |
|
745 |
minus1 := temp; |
|
746 |
n := n - 1 |
|
747 |
}; |
|
748 |
write "Result"; |
|
749 |
write minus2 |
|
750 |
""" |
|
751 |
||
752 |
val prog3 = """ |
|
753 |
start := 1000; |
|
754 |
x := start; |
|
755 |
y := start; |
|
756 |
z := start; |
|
757 |
while 0 < x do { |
|
758 |
while 0 < y do { |
|
759 |
while 0 < z do { |
|
760 |
z := z - 1 |
|
761 |
}; |
|
762 |
z := start; |
|
763 |
y := y - 1 |
|
764 |
}; |
|
765 |
y := start; |
|
766 |
x := x - 1 |
|
767 |
} |
|
768 |
""" |
|
769 |
/* |
|
770 |
for(i <- 400 to 400 by 1){ |
|
771 |
println(i+":") |
|
772 |
blexing_simp(WHILE_REGS, prog2 * i) |
|
773 |
} */ |
|
774 |
||
775 |
/* |
|
776 |
for (i <- 2 to 5){ |
|
777 |
for(j <- 1 to 3){ |
|
778 |
println(i,j) |
|
779 |
small_de = i |
|
780 |
usual_de = i + j |
|
781 |
big_de = i + 2*j |
|
782 |
blexing_simp(AWHILE_REGS, prog2 * 100) |
|
783 |
} |
|
784 |
}*/ |
|
785 |
||
786 |
/* |
|
787 |
println("Tokens of prog2") |
|
788 |
println(tokenise(WHILE_REGS, prog2).mkString("\n")) |
|
789 |
||
790 |
val fib_tokens = tokenise(WHILE_REGS, prog2) |
|
791 |
fib_tokens.map{case (s1, s2) => (escape(s1), escape(s2))}.mkString(",\n") |
|
792 |
||
793 |
||
794 |
val test_tokens = tokenise(WHILE_REGS, prog3) |
|
795 |
test_tokens.map{case (s1, s2) => (escape(s1), escape(s2))}.mkString(",\n") |
|
796 |
*/ |
|
797 |
||
798 |
/* |
|
799 |
println("time test for blexing_simp") |
|
800 |
for (i <- 1 to 1 by 1) { |
|
801 |
lexing_simp(WHILE_REGS, prog2 * i) |
|
802 |
blexing_simp(WHILE_REGS, prog2 * i) |
|
803 |
for( j <- 0 to each_simp_timeb.length - 1){ |
|
804 |
if( each_simp_timeb(j)/each_simp_time(j) >= 10.0 ) |
|
805 |
println(j, each_simp_timeb(j), each_simp_time(j)) |
|
806 |
} |
|
807 |
} |
|
808 |
*/ |
|
809 |
||
810 |
||
811 |
//--------------------------------------------------------------------------------------------------------END OF NON-BITCODE PART (TESTING) |
|
812 |
||
813 |
||
814 |
||
815 |
def clear() = { |
|
816 |
print("") |
|
817 |
//print("\33[H\33[2J") |
|
818 |
} |
|
819 |
||
820 |
//testing the two lexings produce the same value |
|
821 |
//enumerates strings of length n over alphabet cs |
|
822 |
def strs(n: Int, cs: String) : Set[String] = { |
|
823 |
if (n == 0) Set("") |
|
824 |
else { |
|
825 |
val ss = strs(n - 1, cs) |
|
826 |
ss ++ |
|
827 |
(for (s <- ss; c <- cs.toList) yield c + s) |
|
828 |
} |
|
829 |
} |
|
830 |
def enum(n: Int, s: String) : Stream[Rexp] = n match { |
|
831 |
case 0 => ZERO #:: ONE #:: s.toStream.map(CHAR) |
|
832 |
case n => { |
|
833 |
val rs = enum(n - 1, s) |
|
834 |
rs #::: |
|
835 |
(for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) #::: |
|
836 |
(for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) #::: |
|
837 |
(for (r1 <- rs) yield STAR(r1)) |
|
838 |
} |
|
839 |
} |
|
840 |
||
841 |
//tests blexing and lexing |
|
842 |
def tests_blexer_simp(ss: Set[String])(r: Rexp) = { |
|
843 |
clear() |
|
844 |
//println(s"Testing ${r}") |
|
845 |
for (s <- ss.par) yield { |
|
846 |
val res1 = Try(Some(lexing_simp(r, s))).getOrElse(None) |
|
5 | 847 |
val res2 = Try(Some(super_blexing_simp(r, s))).getOrElse(None) |
0 | 848 |
if (res1 != res2) println(s"Disagree on ${r} and ${s}") |
849 |
if (res1 != res2) println(s" ${res1} != ${res2}") |
|
850 |
if (res1 != res2) Some((r, s)) else None |
|
851 |
} |
|
852 |
} |
|
853 |
||
854 |
||
855 |
||
5 | 856 |
|
0 | 857 |
/* |
858 |
def single_expression_explorer(ar: ARexp, ss: Set[String]): Unit = { |
|
859 |
for (s <- ss){ |
|
860 |
||
861 |
val der_res = bder(c, ar) |
|
862 |
val simp_res = bsimp(der_res) |
|
863 |
println(asize(der_res)) |
|
864 |
println(asize(simp_res)) |
|
865 |
single_expression_explorer(simp_res, (sc - c)) |
|
866 |
} |
|
867 |
}*/ |
|
868 |
||
869 |
//single_expression_explorer(internalise(("c"~("a"+"b"))%) , Set('a','b','c')) |
|
870 |
||
871 |
||
872 |
} |
|
873 |
||
874 |
import Rexp.Bits |
|
875 |
abstract class ARexp |
|
876 |
case object AZERO extends ARexp |
|
877 |
case class AONE(bs: Bits) extends ARexp |
|
878 |
case class ACHAR(bs: Bits, f: Char) extends ARexp |
|
879 |
case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp |
|
880 |
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp |
|
881 |
case class ASTAR(bs: Bits, r: ARexp) extends ARexp |
|
882 |
||
883 |
||
884 |
||
885 |
abstract class Val |
|
886 |
case object Empty extends Val |
|
887 |
case class Chr(c: Char) extends Val |
|
888 |
case class Sequ(v1: Val, v2: Val) extends Val |
|
889 |
case class Left(v: Val) extends Val |
|
890 |
case class Right(v: Val) extends Val |
|
891 |
case class Stars(vs: List[Val]) extends Val |
|
892 |
case class Rec(x: String, v: Val) extends Val |
|
893 |
//case class Pos(i: Int, v: Val) extends Val |
|
894 |
case object Prd extends Val |