author | Christian Urban <urbanc@in.tum.de> |
Tue, 23 Aug 2016 12:26:13 +0200 | |
changeset 415 | 4ae59fd3b174 |
parent 414 | 065ca01b62ae |
permissions | -rw-r--r-- |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
import scala.annotation.tailrec |
412 | 2 |
import scala.language.implicitConversions |
121
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
3 |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
abstract class Rexp { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
def simp : Rexp = this |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
|
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
8 |
case object ZERO extends Rexp |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
9 |
case object ONE extends Rexp |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
case class CHAR(c: Char) extends Rexp |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
override def simp = (r1.simp, r2.simp) match { |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
13 |
case (ZERO, r) => r |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
14 |
case (r, ZERO) => r |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
15 |
case (r, ONE) => if (nullable(r)) r else ALT(r, ONE) |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
16 |
case (ONE, r) => if (nullable(r)) r else ALT(r, ONE) |
121
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
17 |
case (r1, r2) => if (r1 == r2) r1 else ALT(r1, r2) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
override def simp = (r1.simp, r2.simp) match { |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
22 |
case (ZERO, _) => ZERO |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
23 |
case (_, ZERO) => ZERO |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
24 |
case (ONE, r) => r |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
25 |
case (r, ONE) => r |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
case (r1, r2) => SEQ(r1, r2) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
} |
121
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
29 |
case class STAR(r: Rexp) extends Rexp { |
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
30 |
override def simp = r.simp match { |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
31 |
case ZERO => ONE |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
32 |
case ONE => ONE |
121
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
33 |
case r => STAR(r) |
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
34 |
} |
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
35 |
} |
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
36 |
case class NTIMES(r: Rexp, n: Int) extends Rexp { |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
37 |
override def simp = if (n == 0) ONE else |
121
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
38 |
r.simp match { |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
39 |
case ZERO => ZERO |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
40 |
case ONE => ONE |
121
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
41 |
case r => NTIMES(r, n) |
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
42 |
} |
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
43 |
} |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
// some convenience for typing in regular expressions |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
def charlist2rexp(s : List[Char]) : Rexp = s match { |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
47 |
case Nil => ONE |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
case c::Nil => CHAR(c) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList) |
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 |
// nullable function: tests whether the regular |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
// expression can recognise the empty string |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
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
|
57 |
case ZERO => false |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
58 |
case ONE => true |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
case CHAR(_) => false |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
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
|
61 |
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
|
62 |
case STAR(_) => true |
121
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
63 |
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
|
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 |
// 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
|
67 |
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
|
68 |
case ZERO => ZERO |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
69 |
case ONE => ZERO |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
70 |
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
|
71 |
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
|
72 |
case SEQ(r1, r2) => |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
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
|
74 |
else SEQ(der(c, r1), r2) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
case NTIMES(r, i) => |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
77 |
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
|
78 |
} |
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 |
// 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
|
81 |
@tailrec |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
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
|
83 |
case Nil => r |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
case c::s => ders(s, der(c, r).simp) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
|
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
87 |
|
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
88 |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
// main matcher function |
414 | 90 |
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
|
91 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
//one or zero |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
94 |
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
|
95 |
|
412 | 96 |
def EVIL1(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n)) |
97 |
val EVIL2 = SEQ(STAR("a"), "b") |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
98 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
99 |
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
|
100 |
val start = System.nanoTime() |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
for (j <- 1 to i) code |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
val end = System.nanoTime() |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
(end - start)/(i * 1.0e9) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
104 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
|
412 | 106 |
val i = 5000 |
107 |
println(i + " " + "%.5f".format(time_needed(10, matcher(EVIL1(i), "a" * i)))) |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
|
415 | 109 |
for (i <- 1 to 9001 by 1000) { |
110 |
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
|
111 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
112 |
|
415 | 113 |
for (i <- 1 to 7500001 by 500000) { |
114 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i)))) |
|
115 |
} |
|
116 |