4
|
1 |
|
|
2 |
abstract class Rexp
|
|
3 |
case object ZERO extends Rexp
|
|
4 |
case object ONE extends Rexp
|
|
5 |
case class CHAR(c: Char) extends Rexp
|
|
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 |
|
|
10 |
// nullable function: tests whether the regular
|
|
11 |
// expression can recognise the empty string
|
|
12 |
def nullable (r: Rexp) : Boolean = r match {
|
|
13 |
case ZERO => false
|
|
14 |
case ONE => true
|
|
15 |
case CHAR(_) => false
|
|
16 |
case ALT(r1, r2) => nullable(r1) || nullable(r2)
|
|
17 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2)
|
|
18 |
case STAR(_) => true
|
|
19 |
}
|
|
20 |
|
|
21 |
// derivative of a regular expression w.r.t. a character
|
|
22 |
def der (c: Char, r: Rexp) : Rexp = r match {
|
|
23 |
case ZERO => ZERO
|
|
24 |
case ONE => ZERO
|
|
25 |
case CHAR(d) => if (c == d) ONE else ZERO
|
|
26 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
|
|
27 |
case SEQ(r1, r2) =>
|
|
28 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
|
|
29 |
else SEQ(der(c, r1), r2)
|
|
30 |
case STAR(r1) => SEQ(der(c, r1), STAR(r1))
|
|
31 |
}
|
|
32 |
|
|
33 |
def simp(r: Rexp) : Rexp = r match {
|
|
34 |
case ALT(r1, r2) => (simp(r1), simp(r2)) match {
|
|
35 |
case (ZERO, r2s) => r2s
|
|
36 |
case (r1s, ZERO) => r1s
|
|
37 |
case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
|
|
38 |
}
|
|
39 |
case SEQ(r1, r2) => (simp(r1), simp(r2)) match {
|
|
40 |
case (ZERO, _) => ZERO
|
|
41 |
case (_, ZERO) => ZERO
|
|
42 |
case (ONE, r2s) => r2s
|
|
43 |
case (r1s, ONE) => r1s
|
|
44 |
case (r1s, r2s) => SEQ(r1s, r2s)
|
|
45 |
}
|
|
46 |
case r => r
|
|
47 |
}
|
|
48 |
|
|
49 |
|
|
50 |
// derivative w.r.t. a string (iterates der)
|
|
51 |
def ders (s: List[Char], r: Rexp) : Rexp = s match {
|
|
52 |
case Nil => r
|
|
53 |
case c::s => ders(s, simp(der(c, r)))
|
|
54 |
}
|
|
55 |
|
|
56 |
|
|
57 |
// main matcher function
|
|
58 |
def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
|
|
59 |
|
|
60 |
//one or zero
|
|
61 |
def OPT(r: Rexp) = ALT(r, ONE)
|
|
62 |
|
|
63 |
//evil regular expressions
|
|
64 |
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
|
|
65 |
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
|
|
66 |
|
|
67 |
|
|
68 |
def time_needed[T](i: Int, code: => T) = {
|
|
69 |
val start = System.nanoTime()
|
|
70 |
for (j <- 1 to i) code
|
|
71 |
val end = System.nanoTime()
|
|
72 |
(end - start)/(i * 1.0e9)
|
|
73 |
}
|
|
74 |
|
|
75 |
|
|
76 |
//test: (a?{n}) (a{n})
|
|
77 |
for (i <- 1 to 8001 by 1000) {
|
|
78 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i))))
|
|
79 |
}
|
|
80 |
|
|
81 |
for (i <- 1 to 8001 by 1000) {
|
|
82 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i))))
|
|
83 |
}
|
|
84 |
|
|
85 |
//test: (a*)* b
|
|
86 |
for (i <- 1 to 7000001 by 500000) {
|
|
87 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i))))
|
|
88 |
}
|
|
89 |
|
|
90 |
for (i <- 1 to 7000001 by 500000) {
|
|
91 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i))))
|
|
92 |
}
|
|
93 |
|