author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Sun, 02 Oct 2016 15:07:21 +0100 | |
changeset 434 | 8664ff87cd77 |
parent 424 | 1129024b26d5 |
child 440 | e14cd32ad497 |
permissions | -rw-r--r-- |
422 | 1 |
// version with explicit n-times regular expression |
2 |
||
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
3 |
abstract class Rexp |
424
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
4 |
case object ZERO extends Rexp |
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
5 |
case object ONE extends Rexp |
49 | 6 |
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
|
7 |
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
|
8 |
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
|
9 |
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
|
10 |
case class NTIMES(r: Rexp, n: Int) extends Rexp //explicit constructor |
49 | 11 |
|
12 |
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
|
13 |
case ZERO => false |
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
14 |
case ONE => true |
49 | 15 |
case CHAR(_) => false |
16 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
17 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
18 |
case STAR(_) => true |
|
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
19 |
case NTIMES(r, i) => if (i == 0) true else nullable(r) |
49 | 20 |
} |
21 |
||
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) |
|
30 |
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
|
31 |
case NTIMES(r, i) => |
424
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
32 |
if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1)) |
49 | 33 |
} |
34 |
||
35 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
|
36 |
case Nil => r |
|
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
37 |
case c::s => ders(s, der(c, r)) |
49 | 38 |
} |
39 |
||
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 |
|
42 |
||
258
1e4da6d2490c
updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
43 |
//optional: one or zero times |
424
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
44 |
def OPT(r: Rexp) = ALT(r, ONE) |
49 | 45 |
|
422 | 46 |
//evil regular expressions |
414 | 47 |
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
48 |
val EVIL2 = SEQ(STAR(CHAR('a')), CHAR('b')) |
|
49 | 49 |
|
50 |
def time_needed[T](i: Int, code: => T) = { |
|
51 |
val start = System.nanoTime() |
|
52 |
for (j <- 1 to i) code |
|
53 |
val end = System.nanoTime() |
|
54 |
(end - start)/(i * 1.0e9) |
|
55 |
} |
|
56 |
||
57 |
||
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
58 |
//test: (a?{n}) (a{n}) |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
59 |
for (i <- 1 to 1101 by 100) { |
415 | 60 |
println(i + " " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i)))) |
120
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 |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
63 |
for (i <- 1 to 1101 by 100) { |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
64 |
println(i + " " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i)))) |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
65 |
} |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
66 |
|
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
67 |
//test: (a*)* b |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
68 |
for (i <- 1 to 4501 by 500) { |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
69 |
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:
424
diff
changeset
|
70 |
} |
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
71 |
|
415 | 72 |
for (i <- 1 to 4501 by 500) { |
73 |
println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i)))) |
|
414 | 74 |
} |
75 |
||
76 |