author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Tue, 20 Sep 2016 12:17:01 +0100 | |
changeset 421 | 7a04f2c532c1 |
parent 415 | 4ae59fd3b174 |
child 422 | 5deefcc8cffa |
permissions | -rw-r--r-- |
54 | 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 |
|
5 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
|
6 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
|
7 |
case class STAR(r: Rexp) extends Rexp |
|
8 |
||
9 |
// nullable function: tests whether the regular |
|
10 |
// expression can recognise the empty string |
|
11 |
def nullable (r: Rexp) : Boolean = r match { |
|
12 |
case NULL => false |
|
13 |
case EMPTY => true |
|
14 |
case CHAR(_) => false |
|
15 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
16 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
17 |
case STAR(_) => true |
|
18 |
} |
|
19 |
||
20 |
// derivative of a regular expression w.r.t. a character |
|
21 |
def der (c: Char, r: Rexp) : Rexp = r match { |
|
22 |
case NULL => NULL |
|
23 |
case EMPTY => NULL |
|
24 |
case CHAR(d) => if (c == d) EMPTY else NULL |
|
25 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
26 |
case SEQ(r1, r2) => |
|
27 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
28 |
else SEQ(der(c, r1), r2) |
|
29 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
30 |
} |
|
31 |
||
32 |
// derivative w.r.t. a string (iterates der) |
|
33 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
|
34 |
case Nil => r |
|
54 | 35 |
case c::s => ders(s, der(c, r)) |
49 | 36 |
} |
37 |
||
38 |
// main matcher function |
|
261
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
39 |
def matches(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) |
49 | 40 |
|
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
41 |
//example from the homework |
343
539b2e88f5b9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
42 |
val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b'))) |
539b2e88f5b9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
43 |
der('a', r) |
539b2e88f5b9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
44 |
der('b', r) |
539b2e88f5b9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
45 |
der('c', r) |
49 | 46 |
|
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
47 |
//optional: one or zero times |
49 | 48 |
def OPT(r: Rexp) = ALT(r, EMPTY) |
49 |
||
50 |
//n-times |
|
51 |
def NTIMES(r: Rexp, n: Int) : Rexp = n match { |
|
59 | 52 |
case 0 => EMPTY |
49 | 53 |
case 1 => r |
54 |
case n => SEQ(r, NTIMES(r, n - 1)) |
|
55 |
} |
|
56 |
||
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
57 |
// the evil regular expression a?{n} a{n} |
415 | 58 |
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
59 |
val EVIL2 = SEQ(STAR(CHAR('a')), CHAR('b')) |
|
49 | 60 |
|
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
61 |
//for measuring time |
49 | 62 |
def time_needed[T](i: Int, code: => T) = { |
63 |
val start = System.nanoTime() |
|
64 |
for (j <- 1 to i) code |
|
65 |
val end = System.nanoTime() |
|
66 |
(end - start)/(i * 1.0e9) |
|
67 |
} |
|
68 |
||
363
0d6deecdb2eb
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
343
diff
changeset
|
69 |
|
415 | 70 |
for (i <- 1 to 20) { |
71 |
println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i)))) |
|
49 | 72 |
} |
363
0d6deecdb2eb
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
343
diff
changeset
|
73 |
|
415 | 74 |
for (i <- 1 to 6502 by 500) { |
75 |
println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i)))) |
|
76 |
} |
|
363
0d6deecdb2eb
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
343
diff
changeset
|
77 |