author | cu |
Tue, 03 Oct 2017 23:35:16 +0100 | |
changeset 513 | 676e6484f29b |
parent 478 | 48b842c997c7 |
child 564 | b5d57d7064bb |
permissions | -rw-r--r-- |
513 | 1 |
// Version with an explicit n-times regular expression; |
2 |
// this keeps the size of the regular expression in the |
|
3 |
// EVIL1 test-case quite small |
|
422 | 4 |
|
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
5 |
abstract class Rexp |
424
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
6 |
case object ZERO extends Rexp |
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
7 |
case object ONE extends Rexp |
49 | 8 |
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
|
9 |
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
|
10 |
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
|
11 |
case class STAR(r: Rexp) extends Rexp |
477 | 12 |
case class NTIMES(r: Rexp, n: Int) extends Rexp //explicit constructor for n-times |
13 |
||
49 | 14 |
|
15 |
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
|
16 |
case ZERO => false |
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
17 |
case ONE => true |
49 | 18 |
case CHAR(_) => false |
19 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
20 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
21 |
case STAR(_) => true |
|
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
22 |
case NTIMES(r, i) => if (i == 0) true else nullable(r) |
49 | 23 |
} |
24 |
||
477 | 25 |
|
49 | 26 |
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
|
27 |
case ZERO => ZERO |
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
28 |
case ONE => ZERO |
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
29 |
case CHAR(d) => if (c == d) ONE else ZERO |
49 | 30 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
31 |
case SEQ(r1, r2) => |
|
32 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
33 |
else SEQ(der(c, r1), r2) |
|
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
34 |
case STAR(r1) => SEQ(der(c, r1), STAR(r1)) |
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
35 |
case NTIMES(r1, i) => |
477 | 36 |
if (i == 0) ZERO else SEQ(der(c, r1), NTIMES(r1, i - 1)) |
49 | 37 |
} |
38 |
||
39 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
|
40 |
case Nil => r |
|
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
41 |
case c::s => ders(s, der(c, r)) |
49 | 42 |
} |
43 |
||
261
24531cfaa36a
updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
44 |
def matches(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) |
49 | 45 |
|
46 |
||
477 | 47 |
//optional regular expression: one or zero times |
424
1129024b26d5
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
48 |
def OPT(r: Rexp) = ALT(r, ONE) |
49 | 49 |
|
477 | 50 |
|
51 |
// Test Cases |
|
52 |
||
422 | 53 |
//evil regular expressions |
414 | 54 |
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
55 |
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
49 | 56 |
|
57 |
def time_needed[T](i: Int, code: => T) = { |
|
58 |
val start = System.nanoTime() |
|
59 |
for (j <- 1 to i) code |
|
60 |
val end = System.nanoTime() |
|
61 |
(end - start)/(i * 1.0e9) |
|
62 |
} |
|
63 |
||
64 |
||
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
65 |
//test: (a?{n}) (a{n}) |
477 | 66 |
for (i <- 1 to 1201 by 100) { |
415 | 67 |
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
|
68 |
} |
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
69 |
|
477 | 70 |
for (i <- 1 to 1201 by 100) { |
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
71 |
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
|
72 |
} |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
73 |
|
441 | 74 |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
75 |
//test: (a*)* b |
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
76 |
for (i <- 1 to 20) { |
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
424
diff
changeset
|
77 |
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
|
78 |
} |
120
3e71efb25ce9
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
79 |
|
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
80 |
for (i <- 1 to 20) { |
415 | 81 |
println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i)))) |
414 | 82 |
} |
83 |
||
84 |
||
477 | 85 |
|
86 |
// size of a regular expressions - for testing purposes |
|
87 |
def size(r: Rexp) : Int = r match { |
|
88 |
case ZERO => 1 |
|
89 |
case ONE => 1 |
|
90 |
case CHAR(_) => 1 |
|
91 |
case ALT(r1, r2) => 1 + size(r1) + size(r2) |
|
92 |
case SEQ(r1, r2) => 1 + size(r1) + size(r2) |
|
93 |
case STAR(r) => 1 + size(r) |
|
94 |
case NTIMES(r, _) => 1 + size(r) |
|
95 |
} |
|
96 |
||
97 |
// EVIL1(n) has now a constant size, no matter |
|
98 |
// what n is |
|
99 |
||
100 |
size(EVIL1(1)) // 7 |
|
101 |
size(EVIL1(3)) // 7 |
|
102 |
size(EVIL1(5)) // 7 |
|
103 |
size(EVIL1(7)) // 7 |
|
104 |
||
105 |
||
478 | 106 |
// but the size of the derivatives can still grow |
477 | 107 |
// quite dramatically |
108 |
||
109 |
size(ders("".toList, EVIL2)) // 5 |
|
110 |
size(ders("a".toList, EVIL2)) // 12 |
|
111 |
size(ders("aa".toList, EVIL2)) // 28 |
|
112 |
size(ders("aaa".toList, EVIL2)) // 58 |
|
113 |
size(ders("aaaa".toList, EVIL2)) // 116 |