author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Wed, 27 Nov 2013 21:36:30 +0000 | |
changeset 208 | bd5a8a6b3871 |
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:
120
diff
changeset
|
1 |
import scala.language.implicitConversions |
49 | 2 |
|
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
3 |
abstract class Rexp |
49 | 4 |
|
5 |
case object NULL extends Rexp |
|
6 |
case object EMPTY extends Rexp |
|
7 |
case class CHAR(c: Char) extends Rexp |
|
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
8 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
9 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
10 |
case class STAR(r: Rexp) extends Rexp |
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
11 |
case class NTIMES(r: Rexp, n: Int) extends Rexp |
49 | 12 |
|
13 |
// some convenience for typing in regular expressions |
|
14 |
def charlist2rexp(s : List[Char]) : Rexp = s match { |
|
15 |
case Nil => EMPTY |
|
16 |
case c::Nil => CHAR(c) |
|
17 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
|
18 |
} |
|
19 |
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList) |
|
20 |
||
21 |
||
22 |
// nullable function: tests whether the regular |
|
23 |
// expression can recognise the empty string |
|
24 |
def nullable (r: Rexp) : Boolean = r match { |
|
25 |
case NULL => false |
|
26 |
case EMPTY => true |
|
27 |
case CHAR(_) => false |
|
28 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
29 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
30 |
case STAR(_) => true |
|
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
31 |
case NTIMES(r, i) => if (i == 0) true else nullable(r) |
49 | 32 |
} |
33 |
||
34 |
// derivative of a regular expression w.r.t. a character |
|
35 |
def der (c: Char, r: Rexp) : Rexp = r match { |
|
36 |
case NULL => NULL |
|
37 |
case EMPTY => NULL |
|
38 |
case CHAR(d) => if (c == d) EMPTY else NULL |
|
39 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
40 |
case SEQ(r1, r2) => |
|
41 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
42 |
else SEQ(der(c, r1), r2) |
|
43 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
44 |
case NTIMES(r, i) => |
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
45 |
if (i == 0) NULL else SEQ(der(c, r), NTIMES(r, i - 1)) |
49 | 46 |
} |
47 |
||
48 |
// derivative w.r.t. a string (iterates der) |
|
49 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
|
50 |
case Nil => r |
|
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
51 |
case c::s => ders(s, der(c, r)) |
49 | 52 |
} |
53 |
||
54 |
// main matcher function |
|
55 |
def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) |
|
56 |
||
57 |
||
58 |
//one or zero |
|
59 |
def OPT(r: Rexp) = ALT(r, EMPTY) |
|
60 |
||
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
61 |
def EVIL(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n)) |
49 | 62 |
|
63 |
def time_needed[T](i: Int, code: => T) = { |
|
64 |
val start = System.nanoTime() |
|
65 |
for (j <- 1 to i) code |
|
66 |
val end = System.nanoTime() |
|
67 |
(end - start)/(i * 1.0e9) |
|
68 |
} |
|
69 |
||
70 |
for (i <- 1 to 100) { |
|
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
71 |
println(i + ": " + "%.5f".format(time_needed(1, matcher(EVIL(i), "a" * i)))) |
49 | 72 |
} |
73 |
||
74 |
||
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
75 |
for (i <- 1 to 1000 by 50) { |
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
76 |
println(i + " " + "%.5f".format(time_needed(1, matcher(EVIL(i), "a" * i)))) |
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
77 |
} |
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
78 |
|
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
79 |