author | Chengsong |
Thu, 21 Jul 2022 20:21:52 +0100 | |
changeset 573 | 454ced557605 |
parent 298 | db329a4d2bc0 |
permissions | -rw-r--r-- |
296
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
1 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
2 |
import scala.language.implicitConversions |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
3 |
import scala.language.reflectiveCalls |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
4 |
import scala.annotation.tailrec |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
5 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
6 |
def distinctBy[B, C](xs: List[B], f: B => C, acc: List[C] = Nil): List[B] = xs match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
7 |
case Nil => Nil |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
8 |
case (x::xs) => { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
9 |
val res = f(x) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
10 |
if (acc.contains(res)) distinctBy(xs, f, acc) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
11 |
else x::distinctBy(xs, f, res::acc) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
12 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
13 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
14 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
15 |
abstract class Bit |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
16 |
case object Z extends Bit |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
17 |
case object S extends Bit |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
18 |
case class C(c: Char) extends Bit |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
19 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
20 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
21 |
type Bits = List[Bit] |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
22 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
23 |
abstract class Rexp |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
24 |
case object ZERO extends Rexp |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
25 |
case object ONE extends Rexp |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
26 |
case class PRED(f: Char => Boolean) extends Rexp |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
27 |
case class ALTS(rs: List[Rexp]) extends Rexp |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
28 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
29 |
case class STAR(r: Rexp) extends Rexp |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
30 |
case class RECD(x: String, r: Rexp) extends Rexp |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
31 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
32 |
// abbreviations |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
33 |
def CHAR(c: Char) = PRED(_ == c) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
34 |
def ALT(r1: Rexp, r2: Rexp) = ALTS(List(r1, r2)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
35 |
def PLUS(r: Rexp) = SEQ(r, STAR(r)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
36 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
37 |
abstract class ARexp |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
38 |
case object AZERO extends ARexp |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
39 |
case class AONE(bs: Bits) extends ARexp |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
40 |
case class APRED(bs: Bits, f: Char => Boolean) extends ARexp |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
41 |
case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
42 |
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
43 |
case class ASTAR(bs: Bits, r: ARexp) extends ARexp |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
44 |
|
298 | 45 |
// abbreviations |
296
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
46 |
def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
47 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
48 |
abstract class Val |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
49 |
case object Empty extends Val |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
50 |
case class Chr(c: Char) extends Val |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
51 |
case class Sequ(v1: Val, v2: Val) extends Val |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
52 |
case class Left(v: Val) extends Val |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
53 |
case class Right(v: Val) extends Val |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
54 |
case class Stars(vs: List[Val]) extends Val |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
55 |
case class Rec(x: String, v: Val) extends Val |
298 | 56 |
|
57 |
||
296
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
58 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
59 |
// some convenience for typing in regular expressions |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
60 |
def charlist2rexp(s : List[Char]): Rexp = s match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
61 |
case Nil => ONE |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
62 |
case c::Nil => CHAR(c) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
63 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
64 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
65 |
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
66 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
67 |
implicit def RexpOps(r: Rexp) = new { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
68 |
def | (s: Rexp) = ALT(r, s) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
69 |
def % = STAR(r) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
70 |
def ~ (s: Rexp) = SEQ(r, s) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
71 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
72 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
73 |
implicit def stringOps(s: String) = new { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
74 |
def | (r: Rexp) = ALT(s, r) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
75 |
def | (r: String) = ALT(s, r) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
76 |
def % = STAR(s) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
77 |
def ~ (r: Rexp) = SEQ(s, r) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
78 |
def ~ (r: String) = SEQ(s, r) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
79 |
def $ (r: Rexp) = RECD(s, r) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
80 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
81 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
82 |
// translation into ARexps |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
83 |
def fuse(bs: Bits, r: ARexp) : ARexp = r match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
84 |
case AZERO => AZERO |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
85 |
case AONE(cs) => AONE(bs ++ cs) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
86 |
case APRED(cs, f) => APRED(bs ++ cs, f) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
87 |
case AALTS(cs, rs) => AALTS(bs ++ cs, rs) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
88 |
case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
89 |
case ASTAR(cs, r) => ASTAR(bs ++ cs, r) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
90 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
91 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
92 |
def internalise(r: Rexp) : ARexp = r match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
93 |
case ZERO => AZERO |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
94 |
case ONE => AONE(Nil) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
95 |
case PRED(f) => APRED(Nil, f) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
96 |
case ALTS(List(r1, r2)) => |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
97 |
AALTS(Nil, List(fuse(List(Z), internalise(r1)), fuse(List(S), internalise(r2)))) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
98 |
case ALTS(r1::rs) => { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
99 |
val AALTS(Nil, rs2) = internalise(ALTS(rs)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
100 |
AALTS(Nil, fuse(List(Z), internalise(r1)) :: rs2.map(fuse(List(S), _))) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
101 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
102 |
case SEQ(r1, r2) => ASEQ(Nil, internalise(r1), internalise(r2)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
103 |
case STAR(r) => ASTAR(Nil, internalise(r)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
104 |
case RECD(x, r) => internalise(r) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
105 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
106 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
107 |
internalise(("a" | "ab") ~ ("b" | "")) |
298 | 108 |
|
109 |
def decode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match { |
|
110 |
case (ONE, bs) => (Empty, bs) |
|
111 |
case (PRED(f), C(c)::bs) => (Chr(c), bs) |
|
112 |
case (ALTS(r::Nil), bs) => decode_aux(r, bs) |
|
113 |
case (ALTS(rs), bs) => bs match { |
|
114 |
case Z::bs1 => { |
|
115 |
val (v, bs2) = decode_aux(rs.head, bs1) |
|
116 |
(Left(v), bs2) |
|
296
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
117 |
} |
298 | 118 |
case S::bs1 => { |
119 |
val (v, bs2) = decode_aux(ALTS(rs.tail), bs1) |
|
120 |
(Right(v), bs2) |
|
296
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
121 |
} |
298 | 122 |
} |
123 |
case (SEQ(r1, r2), bs) => { |
|
124 |
val (v1, bs1) = decode_aux(r1, bs) |
|
125 |
val (v2, bs2) = decode_aux(r2, bs1) |
|
126 |
(Sequ(v1, v2), bs2) |
|
127 |
} |
|
128 |
case (STAR(r1), S::bs) => { |
|
129 |
val (v, bs1) = decode_aux(r1, bs) |
|
130 |
val (Stars(vs), bs2) = decode_aux(STAR(r1), bs1) |
|
131 |
(Stars(v::vs), bs2) |
|
132 |
} |
|
133 |
case (STAR(_), Z::bs) => (Stars(Nil), bs) |
|
134 |
case (RECD(x, r1), bs) => { |
|
135 |
val (v, bs1) = decode_aux(r1, bs) |
|
136 |
(Rec(x, v), bs1) |
|
296
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
137 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
138 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
139 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
140 |
def decode(r: Rexp, bs: Bits) = decode_aux(r, bs) match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
141 |
case (v, Nil) => v |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
142 |
case _ => throw new Exception("Not decodable") |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
143 |
} |
298 | 144 |
|
296
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
145 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
146 |
//erase function: extracts the regx from Aregex |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
147 |
def erase(r:ARexp): Rexp = r match{ |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
148 |
case AZERO => ZERO |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
149 |
case AONE(_) => ONE |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
150 |
case APRED(bs, f) => PRED(f) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
151 |
case AALTS(bs, rs) => ALTS(rs.map(erase(_))) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
152 |
case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
153 |
case ASTAR(cs, r)=> STAR(erase(r)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
154 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
155 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
156 |
// nullable function: tests whether the aregular |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
157 |
// expression can recognise the empty string |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
158 |
def nullable (r: ARexp) : Boolean = r match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
159 |
case AZERO => false |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
160 |
case AONE(_) => true |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
161 |
case APRED(_,_) => false |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
162 |
case AALTS(_, rs) => rs.exists(nullable) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
163 |
case ASEQ(_, r1, r2) => nullable(r1) && nullable(r2) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
164 |
case ASTAR(_, _) => true |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
165 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
166 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
167 |
def mkepsBC(r: ARexp) : Bits = r match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
168 |
case AONE(bs) => bs |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
169 |
case AALTS(bs, rs) => { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
170 |
val n = rs.indexWhere(nullable) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
171 |
bs ++ mkepsBC(rs(n)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
172 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
173 |
case ASEQ(bs, r1, r2) => bs ++ mkepsBC(r1) ++ mkepsBC(r2) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
174 |
case ASTAR(bs, r) => bs ++ List(Z) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
175 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
176 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
177 |
// derivative of a regular expression w.r.t. a character |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
178 |
def der(c: Char, r: ARexp) : ARexp = r match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
179 |
case AZERO => AZERO |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
180 |
case AONE(_) => AZERO |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
181 |
case APRED(bs, f) => if (f(c)) AONE(bs:::List(C(c))) else AZERO |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
182 |
case AALTS(bs, rs) => AALTS(bs, rs.map(der(c, _))) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
183 |
case ASEQ(bs, r1, r2) => |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
184 |
if (nullable(r1)) AALT(bs, ASEQ(Nil, der(c, r1), r2), fuse(mkepsBC(r1), der(c, r2))) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
185 |
else ASEQ(bs, der(c, r1), r2) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
186 |
case ASTAR(bs, r) => ASEQ(bs, fuse(List(S), der(c, r)), ASTAR(Nil, r)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
187 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
188 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
189 |
// derivative w.r.t. a string (iterates der) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
190 |
@tailrec |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
191 |
def ders (s: List[Char], r: ARexp) : ARexp = s match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
192 |
case Nil => r |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
193 |
case c::s => ders(s, der(c, r)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
194 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
195 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
196 |
// main unsimplified lexing function (produces a value) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
197 |
def lex(r: ARexp, s: List[Char]) : Bits = s match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
198 |
case Nil => if (nullable(r)) mkepsBC(r) else throw new Exception("Not matched") |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
199 |
case c::cs => lex(der(c, r), cs) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
200 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
201 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
202 |
def pre_lexing(r: Rexp, s: String) = lex(internalise(r), s.toList) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
203 |
//def lexing(r: Rexp, s: String) : Val = decode(r, lex(internalise(r), s.toList)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
204 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
205 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
206 |
def flats(rs: List[ARexp]): List[ARexp] = rs match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
207 |
case Nil => Nil |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
208 |
case AZERO :: rs1 => flats(rs1) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
209 |
case AALTS(bs, rs1) :: rs2 => rs1.map(fuse(bs, _)) ::: flats(rs2) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
210 |
case r1 :: rs2 => r1 :: flats(rs2) |
298 | 211 |
} |
296
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
212 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
213 |
def simp(r: ARexp): ARexp = r match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
214 |
case ASEQ(bs1, r1, r2) => (simp(r1), simp(r2)) match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
215 |
case (AZERO, _) => AZERO |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
216 |
case (_, AZERO) => AZERO |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
217 |
case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
218 |
case (r1s, r2s) => ASEQ(bs1, r1s, r2s) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
219 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
220 |
case AALTS(bs1, rs) => distinctBy(flats(rs.map(simp)), erase) match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
221 |
case Nil => AZERO |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
222 |
case s :: Nil => fuse(bs1, s) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
223 |
case rs => AALTS(bs1, rs) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
224 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
225 |
case r => r |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
226 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
227 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
228 |
def ders_simp (s: List[Char], r: ARexp) : ARexp = s match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
229 |
case Nil => r |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
230 |
case c::s => ders_simp(s, simp(der(c, r))) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
231 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
232 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
233 |
def lex_simp(r: ARexp, s: List[Char]) : Bits = s match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
234 |
case Nil => { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
235 |
if (nullable(r)) { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
236 |
//println(asize(r)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
237 |
mkepsBC(r) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
238 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
239 |
else throw new Exception("Not matched") |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
240 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
241 |
case c::cs => lex_simp(simp(der(c, r)), cs) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
242 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
243 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
244 |
//size: of a Aregx for testing purposes |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
245 |
def size(r: Rexp) : Int = r match { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
246 |
case ZERO => 1 |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
247 |
case ONE => 1 |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
248 |
case PRED(_) => 1 |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
249 |
case SEQ(r1, r2) => 1 + size(r1) + size(r2) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
250 |
case ALTS(rs) => 1 + rs.map(size).sum |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
251 |
case STAR(r) => 1 + size(r) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
252 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
253 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
254 |
def asize(a: ARexp) = size(erase(a)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
255 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
256 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
257 |
// decoding does not work yet |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
258 |
def lexing_simp(r: Rexp, s: String) = { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
259 |
val final_derivative = lex_simp(internalise(r), s.toList) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
260 |
println("The length of bit sequence:") |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
261 |
println((final_derivative.length)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
262 |
//println(final_derivative) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
263 |
decode(r, final_derivative) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
264 |
//println(vsize(value)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
265 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
266 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
267 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
268 |
// Lexing Rules for a Small While Language |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
269 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
270 |
//symbols |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
271 |
val SYM = PRED("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".contains(_)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
272 |
//digits |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
273 |
val DIGIT = PRED("0123456789".contains(_)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
274 |
//identifiers |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
275 |
val ID = SYM ~ (SYM | DIGIT).% |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
276 |
//numbers |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
277 |
val NUM = PLUS(DIGIT) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
278 |
//keywords |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
279 |
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false" |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
280 |
//semicolons |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
281 |
val SEMI: Rexp = ";" |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
282 |
//operators |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
283 |
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/" |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
284 |
//whitespaces |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
285 |
val WHITESPACE = PLUS(" " | "\n" | "\t") |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
286 |
//parentheses |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
287 |
val RPAREN: Rexp = ")" |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
288 |
val LPAREN: Rexp = "(" |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
289 |
val BEGIN: Rexp = "{" |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
290 |
val END: Rexp = "}" |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
291 |
//strings...but probably needs not |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
292 |
val STRING: Rexp = "\"" ~ SYM.% ~ "\"" |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
293 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
294 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
295 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
296 |
val WHILE_REGS = (("k" $ KEYWORD) | |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
297 |
("i" $ ID) | |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
298 |
("o" $ OP) | |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
299 |
("n" $ NUM) | |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
300 |
("s" $ SEMI) | |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
301 |
("str" $ STRING) | |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
302 |
("p" $ (LPAREN | RPAREN)) | |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
303 |
("b" $ (BEGIN | END)) | |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
304 |
("w" $ WHITESPACE)).% |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
305 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
306 |
// filters out all white spaces |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
307 |
//def tokenise(r: Rexp, s: String) = |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
308 |
// env(lexing_simp(r, s)).filterNot { _._1 == "w"} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
309 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
310 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
311 |
//reads the string from a file |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
312 |
//def fromFile(name: String) : String = |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
313 |
// io.Source.fromFile(name).mkString |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
314 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
315 |
//def tokenise_file(r: Rexp, name: String) = |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
316 |
// tokenise(r, fromFile(name)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
317 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
318 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
319 |
// Some Tests |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
320 |
//============ |
298 | 321 |
|
296
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
322 |
println("simple tests:") |
298 | 323 |
|
296
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
324 |
println(lexing_simp((SYM.%), "abcd")) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
325 |
println(lexing_simp(((SYM.%) | NUM), "12345")) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
326 |
println(lexing_simp((WHILE_REGS), "abcd")) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
327 |
println(lexing_simp((WHILE_REGS), "12345")) |
298 | 328 |
println(lexing_simp((WHILE_REGS), """write "Fib";""")) |
329 |
||
330 |
||
296
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
331 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
332 |
def time[T](code: => T) = { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
333 |
val start = System.nanoTime() |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
334 |
val result = code |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
335 |
val end = System.nanoTime() |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
336 |
println((end - start)/1.0e9) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
337 |
result |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
338 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
339 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
340 |
val prog2 = """ |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
341 |
write "Fib"; |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
342 |
read n; |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
343 |
minus1 := 0; |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
344 |
minus2 := 1; |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
345 |
while n > 0 do { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
346 |
temp := minus2; |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
347 |
minus2 := minus1 + minus2; |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
348 |
minus1 := temp; |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
349 |
n := n -x 1 |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
350 |
}; |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
351 |
write "Result"; |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
352 |
write minus2 |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
353 |
""" |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
354 |
/* |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
355 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
356 |
val prog2 = """ |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
357 |
write "Fib"; |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
358 |
""" |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
359 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
360 |
*/ |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
361 |
|
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
362 |
println("Iteration test with fib") |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
363 |
for (i <- 900 to 1000 by 50) { |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
364 |
print(i.toString + ": ") |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
365 |
time(lexing_simp((WHILE_REGS), (prog2 * i))) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
366 |
//time(lex_simp(internalise(WHILE_REGS), (prog2 * i).toList)) |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
367 |
} |
9aebc106549b
added Chengsong's experiment
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
368 |