author | Christian Urban <urbanc@in.tum.de> |
Sun, 23 Sep 2018 09:02:52 +0100 | |
changeset 556 | 40e22ad45744 |
parent 550 | 71fc4a7a7039 |
child 564 | b5d57d7064bb |
permissions | -rw-r--r-- |
477 | 1 |
// Version with simplification during derivatives; |
2 |
// this keeps the regular expressions small, which |
|
3 |
// is good for run-time |
|
517 | 4 |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
|
422 | 6 |
abstract class Rexp |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
7 |
case object ZERO extends Rexp |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
8 |
case object ONE extends Rexp |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
case class CHAR(c: Char) extends Rexp |
422 | 10 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
11 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
|
12 |
case class STAR(r: Rexp) extends Rexp |
|
13 |
case class NTIMES(r: Rexp, n: Int) extends Rexp |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
|
467
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
458
diff
changeset
|
15 |
|
550 | 16 |
// string of a regular expressions - for testing purposes |
17 |
def string(r: Rexp) : String = r match { |
|
18 |
case ZERO => "0" |
|
19 |
case ONE => "1" |
|
20 |
case CHAR(c) => c.toString |
|
21 |
case ALT(r1, r2) => s"(${string(r1)} + ${string(r2)})" |
|
22 |
case SEQ(CHAR(c), CHAR(d)) => s"${c}${d}" |
|
23 |
case SEQ(ONE, CHAR(c)) => s"1${c}" |
|
24 |
case SEQ(r1, r2) => s"(${string(r1)} ~ ${string(r2)})" |
|
25 |
case STAR(r) => s"(${string(r)})*" |
|
26 |
case NTIMES(r, n) => s"(${string(r)}){${n}}" |
|
27 |
} |
|
28 |
||
29 |
// size of a regular expressions - for testing purposes |
|
30 |
def size(r: Rexp) : Int = r match { |
|
31 |
case ZERO => 1 |
|
32 |
case ONE => 1 |
|
33 |
case CHAR(_) => 1 |
|
34 |
case ALT(r1, r2) => 1 + size(r1) + size(r2) |
|
35 |
case SEQ(r1, r2) => 1 + size(r1) + size(r2) |
|
36 |
case STAR(r) => 1 + size(r) |
|
37 |
case NTIMES(r, _) => 1 + size(r) |
|
38 |
} |
|
39 |
||
40 |
||
41 |
||
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
// nullable function: tests whether the regular |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
// expression can recognise the empty string |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
def nullable (r: Rexp) : Boolean = r match { |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
45 |
case ZERO => false |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
46 |
case ONE => true |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
case CHAR(_) => false |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
case STAR(_) => true |
121
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
51 |
case NTIMES(r, i) => if (i == 0) true else nullable(r) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
// derivative of a regular expression w.r.t. a character |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
def der (c: Char, r: Rexp) : Rexp = r match { |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
56 |
case ZERO => ZERO |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
57 |
case ONE => ZERO |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
58 |
case CHAR(d) => if (c == d) ONE else ZERO |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
case SEQ(r1, r2) => |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
else SEQ(der(c, r1), r2) |
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
63 |
case STAR(r1) => SEQ(der(c, r1), STAR(r1)) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
case NTIMES(r, i) => |
477 | 65 |
if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1)) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
|
550 | 68 |
|
69 |
||
70 |
def simp(r: Rexp, seen: Set[Rexp]) : (Rexp, Set[Rexp]) = r match { |
|
71 |
case ALT(r1, r2) => { |
|
72 |
val (r1s, seen1) = simp(r1, seen) |
|
73 |
val (r2s, seen2) = simp(r2, seen1) |
|
74 |
(r1s, r2s) match { |
|
75 |
case (ZERO, r2s) => (r2s, seen2) |
|
76 |
case (r1s, ZERO) => (r1s, seen2) |
|
77 |
case (r1s, r2s) => (ALT(r1s, r2s), seen2) |
|
78 |
} |
|
422 | 79 |
} |
550 | 80 |
case SEQ(r1, r2) => { |
81 |
val (r1s, _) = simp(r1, Set()) |
|
82 |
val (r2s, _) = simp(r2, Set()) |
|
83 |
if (seen.contains(SEQ(r1s, r2s))) (ZERO, seen) |
|
84 |
else (r1s, r2s) match { |
|
85 |
case (ZERO, _) => (ZERO, seen) |
|
86 |
case (_, ZERO) => (ZERO, seen) |
|
87 |
case (ONE, r2s) => (r2s, seen + r2s) |
|
88 |
case (r1s, ONE) => (r1s, seen + r1s) |
|
89 |
case (r1s, r2s) => (SEQ(r1s, r2s), seen + SEQ(r1s, r2s)) |
|
90 |
} |
|
422 | 91 |
} |
550 | 92 |
case r => if (seen.contains(r)) (ZERO, seen) else (r, seen + r) |
422 | 93 |
} |
94 |
||
95 |
||
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
// derivative w.r.t. a string (iterates der) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
98 |
case Nil => r |
550 | 99 |
case c::s => ders(s, simp(der(c, r), Set())._1) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
|
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
102 |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
// main matcher function |
414 | 104 |
def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
106 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
107 |
//one or zero |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
108 |
def OPT(r: Rexp) = ALT(r, ONE) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
109 |
|
477 | 110 |
|
550 | 111 |
|
112 |
||
113 |
||
114 |
||
115 |
||
116 |
||
117 |
||
118 |
def time_needed[T](i: Int, code: => T) = { |
|
119 |
val start = System.nanoTime() |
|
120 |
for (j <- 1 to i) code |
|
121 |
val end = System.nanoTime() |
|
122 |
(end - start)/(i * 1.0e9) |
|
123 |
} |
|
124 |
||
125 |
||
126 |
// star example |
|
127 |
val Tstar = STAR(STAR(STAR(CHAR('a')))) |
|
128 |
||
129 |
string(ders("".toList, Tstar)) |
|
130 |
size(ders("".toList, Tstar)) // 4 |
|
131 |
string(ders("a".toList, Tstar)) |
|
132 |
size(ders("a".toList, Tstar)) // 11 |
|
133 |
string(ders("aa".toList, Tstar)) |
|
134 |
size(ders("aa".toList, Tstar)) // 11 |
|
135 |
string(ders("aaa".toList, Tstar)) |
|
136 |
size(ders("aaa".toList, Tstar)) // 11 |
|
137 |
string(ders("aaaa".toList, Tstar)) |
|
138 |
size(ders("aaaa".toList, Tstar)) // 11 |
|
139 |
string(ders("aaaa".toList, Tstar)) |
|
140 |
size(ders("aaaaa".toList, Tstar)) // 11 |
|
141 |
string(ders("aaaab".toList, Tstar)) |
|
142 |
size(ders("aaaaab".toList, Tstar)) // 1 |
|
143 |
||
144 |
// test: ("a" | "aa")* |
|
145 |
val EVIL3 = STAR(ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a')))) |
|
146 |
||
147 |
for (i <- 1 to 100 by 1) { |
|
148 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL3, "a" * i))) + |
|
149 |
" size: " + size(ders(("a" * i).toList, EVIL3))) |
|
150 |
} |
|
151 |
||
152 |
||
153 |
println("start " + string(EVIL3) + " " + size(EVIL3)) |
|
154 |
val t1 = der('a', EVIL3) |
|
155 |
println(string(t1) + " " + size(t1)) |
|
156 |
val t1s = simp(t1, Set())._1 |
|
157 |
println("simplified " + string(t1s) + " " + size(t1s)) |
|
158 |
val t2 = der('a', t1s) |
|
159 |
println(string(t2) + " " + size(t2)) |
|
160 |
val t2s = simp(t2, Set())._1 |
|
161 |
println("simplified " + string(t2s) + " " + size(t2s)) |
|
162 |
val t3 = der('a', t2s) |
|
163 |
println(string(t3) + " " + size(t3)) |
|
164 |
val t3s = simp(t3, Set())._1 |
|
165 |
println("simplified " + string(t3s) + " " + size(t3s)) |
|
166 |
val t4 = der('a', t3s) |
|
167 |
val t4s = simp(t4, Set())._1 |
|
168 |
||
169 |
||
170 |
||
171 |
||
172 |
||
173 |
||
174 |
||
175 |
||
176 |
println(string(t4) + " " + size(t4)) |
|
177 |
println("simplified " + string(t4s) + " " + size(t4s)) |
|
178 |
||
179 |
||
477 | 180 |
// Test Cases |
181 |
||
544 | 182 |
//evil regular expressions: (a?){n} a{n} and (a*)* b |
183 |
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
|
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
184 |
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
422 | 185 |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
186 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
187 |
def time_needed[T](i: Int, code: => T) = { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
188 |
val start = System.nanoTime() |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
189 |
for (j <- 1 to i) code |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
190 |
val end = System.nanoTime() |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
191 |
(end - start)/(i * 1.0e9) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
192 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
193 |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
194 |
|
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
195 |
//test: (a?{n}) (a{n}) |
519 | 196 |
for (i <- 1 to 8001 by 1000) { |
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
197 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i)))) |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
198 |
} |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
199 |
|
519 | 200 |
for (i <- 1 to 8001 by 1000) { |
415 | 201 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i)))) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
202 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
203 |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
204 |
//test: (a*)* b |
477 | 205 |
for (i <- 1 to 6000001 by 500000) { |
206 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i)))) |
|
207 |
} |
|
208 |
||
209 |
for (i <- 1 to 6000001 by 500000) { |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
210 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i)))) |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
211 |
} |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
212 |
|
477 | 213 |
|
214 |
||
215 |
// size of a regular expressions - for testing purposes |
|
216 |
def size(r: Rexp) : Int = r match { |
|
217 |
case ZERO => 1 |
|
218 |
case ONE => 1 |
|
219 |
case CHAR(_) => 1 |
|
220 |
case ALT(r1, r2) => 1 + size(r1) + size(r2) |
|
221 |
case SEQ(r1, r2) => 1 + size(r1) + size(r2) |
|
222 |
case STAR(r) => 1 + size(r) |
|
223 |
case NTIMES(r, _) => 1 + size(r) |
|
415 | 224 |
} |
225 |
||
477 | 226 |
|
227 |
// now the size of the derivatives grows |
|
228 |
// much, much slower |
|
229 |
||
230 |
size(ders("".toList, EVIL2)) // 5 |
|
231 |
size(ders("a".toList, EVIL2)) // 8 |
|
232 |
size(ders("aa".toList, EVIL2)) // 8 |
|
233 |
size(ders("aaa".toList, EVIL2)) // 8 |
|
234 |
size(ders("aaaa".toList, EVIL2)) // 8 |
|
235 |
size(ders("aaaaa".toList, EVIL2)) // 8 |
|
544 | 236 |
|
237 |
||
238 |
||
239 |
||
240 |
||
241 |
// Examples from the Sulzmann paper |
|
242 |
val sulzmann = STAR(ALT(ALT(CHAR('a'), CHAR('b')), SEQ(CHAR('a'), CHAR('b')))) |
|
243 |
||
244 |
||
245 |
for (i <- 1 to 4501 by 500) { |
|
246 |
println(i + ": " + "%.5f".format(time_needed(1, matcher(sulzmann, "a" * i)))) |
|
247 |
} |
|
248 |
||
249 |
for (i <- 1 to 4501 by 500) { |
|
250 |
println(i + ": " + "%.5f".format(time_needed(1, matcher(sulzmann, "ab" * i)))) |
|
251 |
} |
|
550 | 252 |
|
556 | 253 |
size(ders("".toList, EVIL2)) // 5 |
254 |
size(ders("a".toList, EVIL2)) // 8 |
|
255 |
size(ders("aa".toList, EVIL2)) // 8 |
|
256 |
size(ders("aaa".toList, EVIL2)) // 8 |
|
257 |
size(ders("aaaa".toList, EVIL2)) // 8 |
|
258 |
size(ders("aaaaa".toList, EVIL2)) // 8 |
|
550 | 259 |
|
260 |
||
261 |
||
262 |
(((1 + 1a) ~ ((a + aa))*) + (((0 + 1) ~ ((a + aa))*) + ((1 + 1a) ~ ((a + aa))*))) |