author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Tue, 18 Oct 2016 20:39:54 +0100 | |
changeset 456 | 2fddf8ab744f |
parent 454 | edb4ad356c56 |
child 467 | b5ec11e89768 |
permissions | -rw-r--r-- |
54 | 1 |
abstract class Rexp |
424
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
2 |
case object ZERO extends Rexp |
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
3 |
case object ONE extends Rexp |
49 | 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 { |
|
424
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
12 |
case ZERO => false |
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
13 |
case ONE => true |
49 | 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 |
|
453
36e5752fa191
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
440
diff
changeset
|
18 |
|
49 | 19 |
} |
20 |
||
21 |
// derivative of a regular expression w.r.t. a character |
|
22 |
def der (c: Char, r: Rexp) : Rexp = r match { |
|
424
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
23 |
case ZERO => ZERO |
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
24 |
case ONE => ZERO |
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
25 |
case CHAR(d) => if (c == d) ONE else ZERO |
49 | 26 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
27 |
case SEQ(r1, r2) => |
|
28 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
29 |
else SEQ(der(c, r1), r2) |
|
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
30 |
case STAR(r1) => SEQ(der(c, r1), STAR(r1)) |
49 | 31 |
} |
32 |
||
33 |
// derivative w.r.t. a string (iterates der) |
|
34 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
|
35 |
case Nil => r |
|
54 | 36 |
case c::s => ders(s, der(c, r)) |
49 | 37 |
} |
38 |
||
39 |
// main matcher function |
|
261
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
40 |
def matches(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) |
49 | 41 |
|
424
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
42 |
//examples from the homework |
343
539b2e88f5b9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
43 |
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
|
44 |
der('a', r) |
539b2e88f5b9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
45 |
der('b', r) |
539b2e88f5b9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
261
diff
changeset
|
46 |
der('c', r) |
49 | 47 |
|
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
48 |
//optional: one or zero times |
424
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
49 |
def OPT(r: Rexp) = ALT(r, ONE) |
49 | 50 |
|
51 |
//n-times |
|
52 |
def NTIMES(r: Rexp, n: Int) : Rexp = n match { |
|
424
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
53 |
case 0 => ONE |
49 | 54 |
case 1 => r |
55 |
case n => SEQ(r, NTIMES(r, n - 1)) |
|
56 |
} |
|
57 |
||
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
58 |
// the evil regular expression a?{n} a{n} |
415 | 59 |
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
422 | 60 |
|
61 |
// the evil regular expression (a*)*b |
|
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
62 |
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
49 | 63 |
|
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
119
diff
changeset
|
64 |
//for measuring time |
49 | 65 |
def time_needed[T](i: Int, code: => T) = { |
66 |
val start = System.nanoTime() |
|
67 |
for (j <- 1 to i) code |
|
68 |
val end = System.nanoTime() |
|
69 |
(end - start)/(i * 1.0e9) |
|
70 |
} |
|
71 |
||
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
433
diff
changeset
|
72 |
//test: (a?{n}) (a{n}) |
433
c08290ee4f1f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
73 |
for (i <- 1 to 20) { |
c08290ee4f1f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
74 |
println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i)))) |
c08290ee4f1f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
75 |
} |
c08290ee4f1f
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
76 |
|
415 | 77 |
for (i <- 1 to 20) { |
78 |
println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i)))) |
|
49 | 79 |
} |
363
0d6deecdb2eb
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
343
diff
changeset
|
80 |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
433
diff
changeset
|
81 |
//test: (a*)* b |
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
82 |
for (i <- 1 to 20) { |
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
433
diff
changeset
|
83 |
println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i)))) |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
433
diff
changeset
|
84 |
} |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
433
diff
changeset
|
85 |
|
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
86 |
for (i <- 1 to 20) { |
415 | 87 |
println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i)))) |
88 |
} |
|
363
0d6deecdb2eb
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
343
diff
changeset
|
89 |