360
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// uses bitcode sequences and annotated regular
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
// expressions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
// for basic regular expressions and RECD
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
// uses a non-standard extraction method for generating
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
// tokens (this is tail-recursive)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
// can match 60 copies of the fib-program (size 10500)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
// in about 20 secs
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
import scala.language.implicitConversions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
import scala.language.reflectiveCalls
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
import scala.annotation.tailrec
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
// standard regular expressions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
abstract class Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
case object ZERO extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
case object ONE extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
case class CHAR(c: Char) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
case class STAR(r: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
case class RECD(x: String, r: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
abstract class Bit
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
case object Z extends Bit
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
case object S extends Bit
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
type Bits = List[Bit]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
// annotated regular expressions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
abstract class ARexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
case object AZERO extends ARexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
case class AONE(bs: Bits) extends ARexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
case class ACHAR(bs: Bits, c: Char) extends ARexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
case class ASTAR(bs: Bits, r: ARexp) extends ARexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
// an abbreviation for binary alternatives
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
def AALT(bs: Bits, r1: ARexp, r2: ARexp) = AALTS(bs, List(r1, r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
abstract class Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
case object Empty extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
case class Chr(c: Char) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
case class Sequ(v1: Val, v2: Val) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
case class Left(v: Val) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
case class Right(v: Val) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
case class Stars(vs: List[Val]) extends Val
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
case class Recd(x: String, v: Val) extends Val
|
361
|
53 |
|
|
54 |
abstract class BTree
|
|
55 |
case object LZ extends BTree
|
|
56 |
case class L(bs: Bits) extends BTree
|
|
57 |
case class LALTS(bs: Bits, bts: List[BTree]) extends BTree
|
|
58 |
case class LSEQ(bs: Bits, bt1: BTree, bt2: BTree) extends BTree
|
|
59 |
case class LSTAR(bs: Bits, bt: BTree) extends BTree
|
|
60 |
|
|
61 |
def ext(r: ARexp): BTree = r match {
|
|
62 |
case AZERO => LZ
|
|
63 |
case AONE(bs) => L(bs)
|
|
64 |
case ACHAR(bs, _) => L(bs)
|
|
65 |
case AALTS(bs, rs) => LALTS(bs, rs.map(ext))
|
|
66 |
case ASEQ(bs, r1, r2) => LSEQ(bs, ext(r1), ext(r2))
|
|
67 |
case ASTAR(bs, r) => LSTAR(bs, ext(r))
|
|
68 |
}
|
|
69 |
|
|
70 |
// annotated regular expressions
|
|
71 |
abstract class BRexp
|
|
72 |
case object BZERO extends BRexp
|
|
73 |
case object BONE extends BRexp
|
|
74 |
case class BCHAR(c: Char) extends BRexp
|
|
75 |
case class BALTS(rs: List[BRexp]) extends BRexp
|
|
76 |
case class BSEQ(r1: BRexp, r2: BRexp) extends BRexp
|
|
77 |
case class BSTAR(r: BRexp) extends BRexp
|
|
78 |
|
|
79 |
def ex(r: ARexp): BRexp = r match {
|
|
80 |
case AZERO => BZERO
|
|
81 |
case AONE(_) => BONE
|
|
82 |
case ACHAR(_, c) => BCHAR(c)
|
|
83 |
case AALTS(_, rs) => BALTS(rs.map(ex))
|
|
84 |
case ASEQ(_, r1, r2) => BSEQ(ex(r1), ex(r2))
|
|
85 |
case ASTAR(_, r) => BSTAR(ex(r))
|
|
86 |
}
|
|
87 |
|
360
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
// some convenience for typing in regular expressions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
def charlist2rexp(s: List[Char]): Rexp = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
case Nil => ONE
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
case c::Nil => CHAR(c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
case c::s => SEQ(CHAR(c), charlist2rexp(s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
implicit def string2rexp(s: String) : Rexp = charlist2rexp(s.toList)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
implicit def RexpOps(r: Rexp) = new {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
def | (s: Rexp) = ALT(r, s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
def % = STAR(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
def ~ (s: Rexp) = SEQ(r, s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
101 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
102 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
103 |
implicit def stringOps(s: String) = new {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
104 |
def | (r: Rexp) = ALT(s, r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
105 |
def | (r: String) = ALT(s, r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
106 |
def % = STAR(s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
def ~ (r: Rexp) = SEQ(s, r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
108 |
def ~ (r: String) = SEQ(s, r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
def $ (r: Rexp) = RECD(s, r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
110 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
111 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
112 |
def size(r: Rexp) : Int = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
113 |
case ZERO => 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
114 |
case ONE => 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
115 |
case CHAR(_) => 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
116 |
case ALT(r1, r2) => 1 + size(r1) + size(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
117 |
case SEQ(r1, r2) => 1 + size(r1) + size(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
118 |
case STAR(r) => 1 + size(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
119 |
case RECD(_, r) => 1 + size(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
120 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
121 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
122 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
123 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
124 |
// Bitcoded + Annotation
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
125 |
//=======================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
126 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
127 |
//erase function: extracts the Rexp from ARexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
128 |
def erase(r:ARexp): Rexp = r match{
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
129 |
case AZERO => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
130 |
case AONE(_) => ONE
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
131 |
case ACHAR(bs, c) => CHAR(c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
132 |
case AALTS(bs, Nil) => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
133 |
case AALTS(bs, r::Nil) => erase(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
134 |
case AALTS(bs, r::rs) => ALT(erase(r), erase(AALTS(bs, rs)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
135 |
case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
136 |
case ASTAR(cs, r)=> STAR(erase(r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
137 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
138 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
139 |
def fuse(bs: Bits, r: ARexp) : ARexp = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
140 |
case AZERO => AZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
141 |
case AONE(cs) => AONE(bs ++ cs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
142 |
case ACHAR(cs, c) => ACHAR(bs ++ cs, c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
143 |
case AALTS(cs, rs) => AALTS(bs ++ cs, rs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
144 |
case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
145 |
case ASTAR(cs, r) => ASTAR(bs ++ cs, r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
146 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
147 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
148 |
def internalise(r: Rexp) : ARexp = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
149 |
case ZERO => AZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
150 |
case ONE => AONE(Nil)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
151 |
case CHAR(c) => ACHAR(Nil, c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
152 |
case ALT(r1, r2) => AALT(Nil, fuse(List(Z), internalise(r1)), fuse(List(S), internalise(r2)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
153 |
case SEQ(r1, r2) => ASEQ(Nil, internalise(r1), internalise(r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
154 |
case STAR(r) => ASTAR(Nil, internalise(r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
155 |
case RECD(_, r) => internalise(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
156 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
157 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
158 |
// example
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
159 |
// internalise(("a" | "ab") ~ ("b" | ""))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
160 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
161 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
162 |
// decoding of a value from a bitsequence
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
163 |
// (this is not tail-recursive and therefore a potential bottleneck)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
164 |
def vdecode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
165 |
case (ONE, bs) => (Empty, bs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
166 |
case (CHAR(c), bs) => (Chr(c), bs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
167 |
case (ALT(r1, r2), Z::bs) => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
168 |
val (v, bs1) = vdecode_aux(r1, bs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
169 |
(Left(v), bs1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
170 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
171 |
case (ALT(r1, r2), S::bs) => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
172 |
val (v, bs1) = vdecode_aux(r2, bs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
173 |
(Right(v), bs1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
174 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
175 |
case (SEQ(r1, r2), bs) => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
176 |
val (v1, bs1) = vdecode_aux(r1, bs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
177 |
val (v2, bs2) = vdecode_aux(r2, bs1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
178 |
(Sequ(v1, v2), bs2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
179 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
180 |
case (STAR(r1), Z::bs) => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
181 |
val (v, bs1) = vdecode_aux(r1, bs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
182 |
val (Stars(vs), bs2) = vdecode_aux(STAR(r1), bs1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
183 |
(Stars(v::vs), bs2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
184 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
185 |
case (STAR(_), S::bs) => (Stars(Nil), bs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
186 |
case (RECD(s, r1), bs) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
187 |
val (v, bs1) = vdecode_aux(r1, bs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
188 |
(Recd(s, v), bs1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
189 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
190 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
191 |
def vdecode(r: Rexp, bs: Bits) = vdecode_aux(r, bs) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
192 |
case (v, Nil) => v
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
193 |
case _ => throw new Exception("Not decodable")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
194 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
195 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
196 |
// decoding of sequence of string tokens from a bitsequence
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
197 |
// tail-recursive version using an accumulator (alternative for
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
198 |
// vdecode)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
199 |
@tailrec
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
200 |
def sdecode_aux(rs: List[Rexp], bs: Bits, acc: List[String]) : List[String] = (rs, bs) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
201 |
case (Nil, _) => acc
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
202 |
case (_, Nil) => acc
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
203 |
case (ONE::rest, bs) => sdecode_aux(rest, bs, acc)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
204 |
case (CHAR(c)::rest, bs) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
205 |
sdecode_aux(rest, bs, (acc.head ++ c.toString)::acc.tail)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
206 |
case (ALT(r1, r2)::rest, Z::bs) => sdecode_aux(r1::rest, bs, acc)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
207 |
case (ALT(r1, r2)::rest, S::bs) => sdecode_aux(r2::rest, bs, acc)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
208 |
case (SEQ(r1, r2)::rest, bs) => sdecode_aux(r1::r2::rest, bs, acc)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
209 |
case (STAR(r1)::rest, Z::bs) => sdecode_aux(r1::STAR(r1)::rest, bs, acc)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
210 |
case (STAR(_)::rest, S::bs) => sdecode_aux(rest, bs, acc)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
211 |
case (RECD(s, r1)::rest, bs) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
212 |
sdecode_aux(r1::rest, bs, s"$s:"::acc)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
213 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
214 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
215 |
def sdecode(r: Rexp, bs: Bits) : List[String] =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
216 |
sdecode_aux(List(r), bs, List("")).reverse.tail
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
217 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
218 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
219 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
220 |
// nullable function: tests whether the an (annotated)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
221 |
// regular expression can recognise the empty string
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
222 |
def bnullable (r: ARexp) : Boolean = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
223 |
case AZERO => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
224 |
case AONE(_) => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
225 |
case ACHAR(_,_) => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
226 |
case AALTS(_, rs) => rs.exists(bnullable)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
227 |
case ASEQ(_, r1, r2) => bnullable(r1) && bnullable(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
228 |
case ASTAR(_, _) => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
229 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
230 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
231 |
def bmkeps(r: ARexp) : Bits = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
232 |
case AONE(bs) => bs
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
233 |
case AALTS(bs, r::Nil) => bs ++ bmkeps(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
234 |
case AALTS(bs, r::rs) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
235 |
if (bnullable(r)) bs ++ bmkeps(r) else bmkeps(AALTS(bs, rs))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
236 |
case ASEQ(bs, r1, r2) => bs ++ bmkeps(r1) ++ bmkeps(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
237 |
case ASTAR(bs, r) => bs ++ List(S)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
238 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
239 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
240 |
// derivative of a regular expression w.r.t. a character
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
241 |
def bder(c: Char, r: ARexp) : ARexp = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
242 |
case AZERO => AZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
243 |
case AONE(_) => AZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
244 |
case ACHAR(bs, d) => if (c == d) AONE(bs) else AZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
245 |
case AALTS(bs, rs) => AALTS(bs, rs.map(bder(c, _)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
246 |
case ASEQ(bs, r1, r2) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
247 |
if (bnullable(r1)) AALT(bs, ASEQ(Nil, bder(c, r1), r2), fuse(bmkeps(r1), bder(c, r2)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
248 |
else ASEQ(bs, bder(c, r1), r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
249 |
case ASTAR(bs, r) => ASEQ(bs, fuse(List(Z), bder(c, r)), ASTAR(Nil, r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
250 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
251 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
252 |
// derivative w.r.t. a string (iterates bder)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
253 |
@tailrec
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
254 |
def bders (s: List[Char], r: ARexp) : ARexp = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
255 |
case Nil => r
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
256 |
case c::s => bders(s, bder(c, r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
257 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
258 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
259 |
// main unsimplified lexing function (produces a bitsequence)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
260 |
def blex(r: ARexp, s: List[Char]) : Bits = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
261 |
case Nil => if (bnullable(r)) bmkeps(r) else throw new Exception("Not matched")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
262 |
case c::cs => blex(bder(c, r), cs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
263 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
264 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
265 |
// calls blex and decodes the value
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
266 |
def blexing(r: Rexp, s: String) : Val =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
267 |
vdecode(r, blex(internalise(r), s.toList))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
268 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
269 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
270 |
// example by Tudor
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
271 |
//val reg = (STAR("a") ~ ("b" | "c")).%
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
272 |
//println(blexing(reg, "aab"))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
273 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
274 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
275 |
//=======================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
276 |
// simplification
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
277 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
278 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
279 |
def flts(rs: List[ARexp]) : List[ARexp] = rs match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
280 |
case Nil => Nil
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
281 |
case AZERO :: rs => flts(rs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
282 |
case AALTS(bs, rs1) :: rs => rs1.map(fuse(bs, _)) ++ flts(rs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
283 |
case r1 :: rs => r1 :: flts(rs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
284 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
285 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
286 |
def bsimp(r: ARexp): ARexp = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
287 |
case ASEQ(bs1, r1, r2) => (bsimp(r1), bsimp(r2)) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
288 |
case (AZERO, _) => AZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
289 |
case (_, AZERO) => AZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
290 |
case (AONE(bs2), r2s) => fuse(bs1 ++ bs2, r2s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
291 |
// needed in order to keep the size down
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
292 |
case (AALTS(bs, rs), r2) => AALTS(bs1 ++ bs, rs.map(ASEQ(Nil, _, r2)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
293 |
case (r1s, r2s) => ASEQ(bs1, r1s, r2s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
294 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
295 |
// distinctBy deletes copies of the same "erased" regex
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
296 |
case AALTS(bs1, rs) => (flts(rs.map(bsimp))).distinctBy(erase) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
297 |
case Nil => AZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
298 |
case r::Nil => fuse(bs1, r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
299 |
case rs => AALTS(bs1, rs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
300 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
301 |
case r => r
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
302 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
303 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
304 |
def blex_simp(r: ARexp, s: List[Char]) : Bits = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
305 |
case Nil => if (bnullable(r)) bmkeps(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
306 |
else throw new Exception("Not matched")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
307 |
case c::cs => blex_simp(bsimp(bder(c, r)), cs)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
308 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
309 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
310 |
// blexing_simp decodes a value from the bitsequence (potentially slow)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
311 |
// blexing2_simp decodes a string-list from the bitsequence
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
312 |
def blexing_simp(r: Rexp, s: String) : Val =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
313 |
vdecode(r, blex_simp(internalise(r), s.toList))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
314 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
315 |
def blexing2_simp(r: Rexp, s: String) : List[String] =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
316 |
sdecode(r, blex_simp(internalise(r), s.toList))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
317 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
318 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
319 |
//println(blexing_simp(reg, "aab"))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
320 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
321 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
322 |
// extracts a string from value
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
323 |
def flatten(v: Val) : String = v match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
324 |
case Empty => ""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
325 |
case Chr(c) => c.toString
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
326 |
case Left(v) => flatten(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
327 |
case Right(v) => flatten(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
328 |
case Sequ(v1, v2) => flatten(v1) + flatten(v2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
329 |
case Stars(vs) => vs.map(flatten).mkString
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
330 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
331 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
332 |
// extracts an environment from a value
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
333 |
def env(v: Val) : List[(String, String)] = v match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
334 |
case Empty => Nil
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
335 |
case Chr(c) => Nil
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
336 |
case Left(v) => env(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
337 |
case Right(v) => env(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
338 |
case Sequ(v1, v2) => env(v1) ::: env(v2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
339 |
case Stars(vs) => vs.flatMap(env)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
340 |
case Recd(x, v) => (x, flatten(v))::env(v)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
341 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
342 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
343 |
def bsize(a: ARexp) = size(erase(a))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
344 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
345 |
// Some Tests
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
346 |
//============
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
347 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
348 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
349 |
def time_needed[T](i: Int, code: => T) = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
350 |
val start = System.nanoTime()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
351 |
for (j <- 1 to i) code
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
352 |
val end = System.nanoTime()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
353 |
(end - start)/(i * 1.0e9)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
354 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
355 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
356 |
/*
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
357 |
val evil1 = STAR(STAR("a")) ~ "b"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
358 |
val evil2 = STAR(STAR(STAR("a"))) ~ "b"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
359 |
val evil3 = STAR("aa" | "a")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
360 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
361 |
for(i <- 0 to 10000 by 1000) {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
362 |
println(time_needed(1, blexing2_simp(evil1, "a"*i ++ "b")))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
363 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
364 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
365 |
for(i <- 0 to 10000 by 1000) {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
366 |
println(time_needed(1, blexing2_simp(evil2, "a"*i ++ "b")))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
367 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
368 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
369 |
for(i <- 0 to 10000 by 1000) {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
370 |
println(time_needed(1, blexing2_simp(evil3, "a"*i)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
371 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
372 |
*/
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
373 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
374 |
// WHILE LANGUAGE
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
375 |
//================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
376 |
def PLUS(r: Rexp) = r ~ r.%
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
377 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
378 |
def Range(s : List[Char]) : Rexp = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
379 |
case Nil => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
380 |
case c::Nil => CHAR(c)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
381 |
case c::s => ALT(CHAR(c), Range(s))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
382 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
383 |
def RANGE(s: String) = Range(s.toList)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
384 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
385 |
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
386 |
val DIGIT = RANGE("0123456789")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
387 |
val ID = SYM ~ (SYM | DIGIT).%
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
388 |
val NUM = PLUS(DIGIT)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
389 |
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
390 |
val SEMI: Rexp = ";"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
391 |
val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
392 |
val WHITESPACE = PLUS(" " | "\n" | "\t")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
393 |
val RPAREN: Rexp = "{"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
394 |
val LPAREN: Rexp = "}"
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
395 |
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
396 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
397 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
398 |
val WHILE_REGS = (("k" $ KEYWORD) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
399 |
("i" $ ID) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
400 |
("o" $ OP) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
401 |
("n" $ NUM) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
402 |
("s" $ SEMI) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
403 |
("str" $ STRING) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
404 |
("p" $ (LPAREN | RPAREN)) |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
405 |
("w" $ WHITESPACE)).%
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
406 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
407 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
408 |
// Some Simple While Tests
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
409 |
//========================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
410 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
411 |
val prog0 = """read n"""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
412 |
println(s"test: $prog0")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
413 |
println(env(blexing_simp(WHILE_REGS, prog0)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
414 |
println(blexing2_simp(WHILE_REGS, prog0))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
415 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
416 |
val prog1 = """read n; write n"""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
417 |
println(s"test: $prog1")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
418 |
println(env(blexing_simp(WHILE_REGS, prog1)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
419 |
println(blexing2_simp(WHILE_REGS, prog1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
420 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
421 |
val prog2 = """
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
422 |
write "Fib";
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
423 |
read n;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
424 |
minus1 := 0;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
425 |
minus2 := 1;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
426 |
while n > 0 do {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
427 |
temp := minus2;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
428 |
minus2 := minus1 + minus2;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
429 |
minus1 := temp;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
430 |
n := n - 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
431 |
};
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
432 |
write "Result";
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
433 |
write minus2
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
434 |
"""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
435 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
436 |
println("lexing fib program (once)")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
437 |
println(blexing2_simp(WHILE_REGS, prog2).filter(s => s == "" || !s.startsWith("w")))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
438 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
439 |
val n = 60
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
440 |
println(s"lexing fib program ($n times, size ${prog2.length * n})")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
441 |
println(time_needed(1, blexing2_simp(WHILE_REGS, prog2 * n)))
|