author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Mon, 17 Nov 2014 08:38:52 +0000 | |
changeset 308 | 3703ade9b17c |
parent 261 | 24531cfaa36a |
child 343 | 539b2e88f5b9 |
permissions | -rw-r--r-- |
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
1 |
abstract class Rexp |
49 | 2 |
case object NULL extends Rexp |
3 |
case object EMPTY extends Rexp |
|
4 |
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
|
5 |
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
|
6 |
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
|
7 |
case class STAR(r: Rexp) extends Rexp |
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
8 |
case class NTIMES(r: Rexp, n: Int) extends Rexp //explicit constructor |
49 | 9 |
|
10 |
def nullable (r: Rexp) : Boolean = r match { |
|
11 |
case NULL => false |
|
12 |
case EMPTY => true |
|
13 |
case CHAR(_) => false |
|
14 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
15 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
16 |
case STAR(_) => true |
|
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
17 |
case NTIMES(r, i) => if (i == 0) true else nullable(r) |
49 | 18 |
} |
19 |
||
20 |
def der (c: Char, r: Rexp) : Rexp = r match { |
|
21 |
case NULL => NULL |
|
22 |
case EMPTY => NULL |
|
23 |
case CHAR(d) => if (c == d) EMPTY else NULL |
|
24 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
25 |
case SEQ(r1, r2) => |
|
26 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
27 |
else SEQ(der(c, r1), r2) |
|
28 |
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
|
29 |
case NTIMES(r, i) => |
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
30 |
if (i == 0) NULL else SEQ(der(c, r), NTIMES(r, i - 1)) |
49 | 31 |
} |
32 |
||
33 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
|
34 |
case Nil => r |
|
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
35 |
case c::s => ders(s, der(c, r)) |
49 | 36 |
} |
37 |
||
261
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
38 |
def matches(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) |
49 | 39 |
|
40 |
||
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
41 |
//optional: one or zero times |
49 | 42 |
def OPT(r: Rexp) = ALT(r, EMPTY) |
43 |
||
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
44 |
def EVIL(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
49 | 45 |
|
46 |
def time_needed[T](i: Int, code: => T) = { |
|
47 |
val start = System.nanoTime() |
|
48 |
for (j <- 1 to i) code |
|
49 |
val end = System.nanoTime() |
|
50 |
(end - start)/(i * 1.0e9) |
|
51 |
} |
|
52 |
||
53 |
for (i <- 1 to 100) { |
|
261
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
54 |
println(i + ": " + "%.5f".format(time_needed(1, matches(EVIL(i), "a" * i)))) |
49 | 55 |
} |
56 |
||
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
57 |
//a bit bolder test |
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
58 |
for (i <- 1 to 1000 by 50) { |
261
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
59 |
println(i + " " + "%.5f".format(time_needed(1, matches(EVIL(i), "a" * i)))) |
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
60 |
} |
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
61 |
|
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
62 |