author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Tue, 04 Oct 2016 12:00:23 +0100 | |
changeset 440 | e14cd32ad497 |
parent 434 | 8664ff87cd77 |
child 441 | 028816884f70 |
permissions | -rw-r--r-- |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
|
422 | 2 |
abstract class Rexp |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
3 |
case object ZERO extends Rexp |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
4 |
case object ONE extends Rexp |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
case class CHAR(c: Char) extends Rexp |
422 | 6 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
7 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
|
8 |
case class STAR(r: Rexp) extends Rexp |
|
9 |
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
|
10 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
// nullable function: tests whether the regular |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
// expression can recognise the empty string |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
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
|
14 |
case ZERO => false |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
15 |
case ONE => true |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
case CHAR(_) => false |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
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
|
18 |
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
|
19 |
case STAR(_) => true |
121
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
20 |
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
|
21 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
// 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
|
24 |
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
|
25 |
case ZERO => ZERO |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
26 |
case ONE => ZERO |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
27 |
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
|
28 |
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
|
29 |
case SEQ(r1, r2) => |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
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
|
31 |
else SEQ(der(c, r1), r2) |
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
32 |
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
|
33 |
case NTIMES(r, i) => |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
34 |
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
|
35 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
|
422 | 37 |
def simp(r: Rexp) : Rexp = r match { |
38 |
case ALT(r1, r2) => (simp(r1), simp(r2)) match { |
|
39 |
case (ZERO, r2s) => r2s |
|
40 |
case (r1s, ZERO) => r1s |
|
41 |
case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s) |
|
42 |
} |
|
43 |
case SEQ(r1, r2) => (simp(r1), simp(r2)) match { |
|
44 |
case (ZERO, _) => ZERO |
|
45 |
case (_, ZERO) => ZERO |
|
46 |
case (ONE, r2s) => r2s |
|
47 |
case (r1s, ONE) => r1s |
|
48 |
case (r1s, r2s) => SEQ(r1s, r2s) |
|
49 |
} |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
50 |
case NTIMES(r1, n) => NTIMES(simp(r1), n) |
422 | 51 |
case r => r |
52 |
} |
|
53 |
||
54 |
||
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
// 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
|
56 |
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
|
57 |
case Nil => r |
422 | 58 |
case c::s => ders(s, simp(der(c, r))) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
|
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
61 |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
// main matcher function |
414 | 63 |
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
|
64 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
//one or zero |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
67 |
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
|
68 |
|
422 | 69 |
//evil regular expressions |
70 |
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
|
71 |
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
422 | 72 |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
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
|
75 |
val start = System.nanoTime() |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
for (j <- 1 to i) code |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
val end = System.nanoTime() |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
(end - start)/(i * 1.0e9) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
81 |
|
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
82 |
//test: (a?{n}) (a{n}) |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
83 |
for (i <- 1 to 10001 by 1000) { |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
84 |
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
|
85 |
} |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
87 |
for (i <- 1 to 10001 by 1000) { |
415 | 88 |
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
|
89 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
91 |
//test: (a*)* b |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
92 |
for (i <- 1 to 7500001 by 500000) { |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
93 |
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
|
94 |
} |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
95 |
|
415 | 96 |
for (i <- 1 to 7500001 by 500000) { |
97 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i)))) |
|
98 |
} |
|
99 |