author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Fri, 16 Oct 2015 08:42:21 +0100 | |
changeset 352 | 1e1b0fe66107 |
parent 341 | ac1187b2e5c9 |
child 363 | 0d6deecdb2eb |
permissions | -rw-r--r-- |
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
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 |
|
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
5 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
6 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
7 |
case class STAR(r: Rexp) extends Rexp |
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 |
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
9 |
|
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
10 |
def simp(r: Rexp): Rexp = r match { |
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
11 |
case ALT(r1, r2) => { |
341
ac1187b2e5c9
updated program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
12 |
(simp(r1), simp(r2)) match { |
ac1187b2e5c9
updated program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
13 |
case (NULL, r2s) => r2s |
ac1187b2e5c9
updated program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
14 |
case (r1s, NULL) => r1s |
ac1187b2e5c9
updated program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
15 |
case (r1s, r2s) => if (r1s == r2s) r1s else ALT(r1s, r2s) |
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
16 |
} |
49 | 17 |
} |
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
18 |
case SEQ(r1, r2) => { |
341
ac1187b2e5c9
updated program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
19 |
(simp(r1), simp(r2)) match { |
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
20 |
case (NULL, _) => NULL |
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
21 |
case (_, NULL) => NULL |
341
ac1187b2e5c9
updated program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
22 |
case (EMPTY, r2s) => r2s |
ac1187b2e5c9
updated program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
23 |
case (r1s, EMPTY) => r1s |
ac1187b2e5c9
updated program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
24 |
case (r1s, r2s) => SEQ(r1s, r2s) |
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
25 |
} |
49 | 26 |
} |
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
27 |
case NTIMES(r, n) => NTIMES(simp(r), n) |
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
28 |
case r => r |
49 | 29 |
} |
30 |
||
31 |
def nullable (r: Rexp) : Boolean = r match { |
|
32 |
case NULL => false |
|
33 |
case EMPTY => true |
|
34 |
case CHAR(_) => false |
|
35 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
36 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
37 |
case STAR(_) => true |
|
59 | 38 |
case NTIMES(r, i) => if (i == 0) true else nullable(r) |
49 | 39 |
} |
40 |
||
41 |
def der (c: Char, r: Rexp) : Rexp = r match { |
|
42 |
case NULL => NULL |
|
43 |
case EMPTY => NULL |
|
44 |
case CHAR(d) => if (c == d) EMPTY else NULL |
|
45 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
46 |
case SEQ(r1, r2) => |
|
47 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
48 |
else SEQ(der(c, r1), r2) |
|
49 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
50 |
case NTIMES(r, i) => |
|
51 |
if (i == 0) NULL else SEQ(der(c, r), NTIMES(r, i - 1)) |
|
52 |
} |
|
53 |
||
54 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
|
55 |
case Nil => r |
|
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
56 |
case c::s => ders(s, simp(der(c, r))) |
49 | 57 |
} |
58 |
||
261
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
59 |
def matches(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) |
49 | 60 |
|
61 |
||
62 |
//one or zero |
|
63 |
def OPT(r: Rexp) = ALT(r, EMPTY) |
|
64 |
||
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
65 |
def EVIL(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
49 | 66 |
|
67 |
def time_needed[T](i: Int, code: => T) = { |
|
68 |
val start = System.nanoTime() |
|
69 |
for (j <- 1 to i) code |
|
70 |
val end = System.nanoTime() |
|
71 |
(end - start)/(i * 1.0e9) |
|
72 |
} |
|
73 |
||
74 |
||
121
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
75 |
for (i <- 1 to 12001 by 500) { |
261
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
76 |
println(i + " " + "%.5f".format(time_needed(1, matches(EVIL(i), "a" * i)))) |
49 | 77 |
} |
78 |
||
79 |