author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Fri, 02 Oct 2015 07:48:56 +0100 | |
changeset 341 | ac1187b2e5c9 |
parent 261 | 24531cfaa36a |
child 343 | 539b2e88f5b9 |
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 |
85
1a4065f965fb
added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
59
diff
changeset
|
42 |
//val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b'))) |
119
a6684e8961d0
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
117
diff
changeset
|
43 |
//der('a', r) |
85
1a4065f965fb
added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
59
diff
changeset
|
44 |
//der('b', r) |
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
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} |
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
58 |
def EVIL(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
49 | 59 |
|
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
60 |
//for measuring time |
49 | 61 |
def time_needed[T](i: Int, code: => T) = { |
62 |
val start = System.nanoTime() |
|
63 |
for (j <- 1 to i) code |
|
64 |
val end = System.nanoTime() |
|
65 |
(end - start)/(i * 1.0e9) |
|
66 |
} |
|
67 |
||
54 | 68 |
for (i <- 1 to 29) { |
261
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
69 |
println(i + ": " + "%.5f".format(time_needed(1, matches(EVIL(i), "a" * i)))) |
49 | 70 |
} |