author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Thu, 14 Nov 2013 20:08:39 +0000 | |
changeset 190 | 0b63c0edfb09 |
parent 121 | 43c116860e47 |
child 258 | 1e4da6d2490c |
permissions | -rw-r--r-- |
121
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
1 |
import scala.language.implicitConversions |
49 | 2 |
|
3 |
abstract class Rexp { |
|
4 |
def simp : Rexp = this |
|
5 |
} |
|
6 |
||
7 |
case object NULL extends Rexp |
|
8 |
case object EMPTY extends Rexp |
|
9 |
case class CHAR(c: Char) extends Rexp |
|
10 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp { |
|
11 |
override def simp = (r1.simp, r2.simp) match { |
|
12 |
case (NULL, r) => r |
|
13 |
case (r, NULL) => r |
|
14 |
case (r, EMPTY) => if (nullable(r)) r else ALT(r, EMPTY) |
|
15 |
case (EMPTY, r) => if (nullable(r)) r else ALT(r, EMPTY) |
|
16 |
case (r1, r2) => if (r1 == r2) r1 else ALT(r1, r2) |
|
17 |
} |
|
18 |
} |
|
19 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp { |
|
20 |
override def simp = (r1.simp, r2.simp) match { |
|
21 |
case (NULL, _) => NULL |
|
22 |
case (_, NULL) => NULL |
|
23 |
case (EMPTY, r) => r |
|
24 |
case (r, EMPTY) => r |
|
25 |
case (r1, r2) => SEQ(r1, r2) |
|
26 |
} |
|
27 |
} |
|
28 |
case class STAR(r: Rexp) extends Rexp { |
|
29 |
override def simp = r.simp match { |
|
30 |
case NULL => EMPTY |
|
31 |
case EMPTY => EMPTY |
|
32 |
case r => STAR(r) |
|
33 |
} |
|
34 |
} |
|
35 |
case class NTIMES(r: Rexp, n: Int) extends Rexp { |
|
59 | 36 |
override def simp = if (n == 0) EMPTY else |
37 |
r.simp match { |
|
38 |
case NULL => NULL |
|
39 |
case EMPTY => EMPTY |
|
40 |
case r => NTIMES(r, n) |
|
41 |
} |
|
49 | 42 |
} |
43 |
||
44 |
// some convenience for typing in regular expressions |
|
45 |
def charlist2rexp(s : List[Char]) : Rexp = s match { |
|
46 |
case Nil => EMPTY |
|
47 |
case c::Nil => CHAR(c) |
|
48 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
|
49 |
} |
|
50 |
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList) |
|
51 |
||
52 |
||
53 |
// nullable function: tests whether the regular |
|
54 |
// expression can recognise the empty string |
|
55 |
def nullable (r: Rexp) : Boolean = r match { |
|
56 |
case NULL => false |
|
57 |
case EMPTY => true |
|
58 |
case CHAR(_) => false |
|
59 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
60 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
61 |
case STAR(_) => true |
|
59 | 62 |
case NTIMES(r, i) => if (i == 0) true else nullable(r) |
49 | 63 |
} |
64 |
||
65 |
// derivative of a regular expression w.r.t. a character |
|
66 |
def der (c: Char, r: Rexp) : Rexp = r match { |
|
67 |
case NULL => NULL |
|
68 |
case EMPTY => NULL |
|
69 |
case CHAR(d) => if (c == d) EMPTY else NULL |
|
70 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
71 |
case SEQ(r1, r2) => |
|
72 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
73 |
else SEQ(der(c, r1), r2) |
|
74 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
75 |
case NTIMES(r, i) => |
|
76 |
if (i == 0) NULL else SEQ(der(c, r), NTIMES(r, i - 1)) |
|
77 |
} |
|
78 |
||
79 |
// derivative w.r.t. a string (iterates der) |
|
80 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
|
81 |
case Nil => r |
|
82 |
case c::s => ders(s, der(c, r).simp) |
|
83 |
} |
|
84 |
||
85 |
// main matcher function |
|
86 |
def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) |
|
87 |
||
88 |
||
89 |
//one or zero |
|
90 |
def OPT(r: Rexp) = ALT(r, EMPTY) |
|
91 |
||
119
a6684e8961d0
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
92 |
def EVIL(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n)) |
49 | 93 |
|
94 |
def time_needed[T](i: Int, code: => T) = { |
|
95 |
val start = System.nanoTime() |
|
96 |
for (j <- 1 to i) code |
|
97 |
val end = System.nanoTime() |
|
98 |
(end - start)/(i * 1.0e9) |
|
99 |
} |
|
100 |
||
101 |
||
121
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
102 |
for (i <- 1 to 12001 by 500) { |
119
a6684e8961d0
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
103 |
println(i + " " + "%.5f".format(time_needed(1, matcher(EVIL(i), "a" * i)))) |
49 | 104 |
} |
105 |
||
106 |