author | Christian Urban <urbanc@in.tum.de> |
Sun, 28 Jul 2019 01:00:41 +0100 | |
changeset 623 | 47a299e7010f |
parent 618 | f4818c95a32e |
child 631 | f618dd4de24a |
permissions | -rw-r--r-- |
564 | 1 |
// A version with simplification of derivatives; |
477 | 2 |
// this keeps the regular expressions small, which |
3 |
// is good for run-time |
|
517 | 4 |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
|
422 | 6 |
abstract class Rexp |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
7 |
case object ZERO extends Rexp |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
8 |
case object ONE extends Rexp |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
case class CHAR(c: Char) extends Rexp |
422 | 10 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
11 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
|
12 |
case class STAR(r: Rexp) extends Rexp |
|
13 |
case class NTIMES(r: Rexp, n: Int) extends Rexp |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
|
467
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
458
diff
changeset
|
15 |
|
550 | 16 |
|
623 | 17 |
// the nullable function: tests whether the regular |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
// expression can recognise the empty string |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
def nullable (r: Rexp) : Boolean = r match { |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
20 |
case ZERO => false |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
21 |
case ONE => true |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
case CHAR(_) => false |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
case STAR(_) => true |
121
43c116860e47
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
93
diff
changeset
|
26 |
case NTIMES(r, i) => if (i == 0) true else nullable(r) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
|
623 | 29 |
// the derivative of a regular expression w.r.t. a character |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
def der (c: Char, r: Rexp) : Rexp = r match { |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
31 |
case ZERO => ZERO |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
32 |
case ONE => ZERO |
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
33 |
case CHAR(d) => if (c == d) ONE else ZERO |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
case SEQ(r1, r2) => |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
else SEQ(der(c, r1), r2) |
440
e14cd32ad497
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
434
diff
changeset
|
38 |
case STAR(r1) => SEQ(der(c, r1), STAR(r1)) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
case NTIMES(r, i) => |
477 | 40 |
if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1)) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
|
564 | 43 |
def simp(r: Rexp) : Rexp = r match { |
44 |
case ALT(r1, r2) => (simp(r1), simp(r2)) match { |
|
45 |
case (ZERO, r2s) => r2s |
|
46 |
case (r1s, ZERO) => r1s |
|
47 |
case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s) |
|
422 | 48 |
} |
564 | 49 |
case SEQ(r1, r2) => (simp(r1), simp(r2)) match { |
50 |
case (ZERO, _) => ZERO |
|
51 |
case (_, ZERO) => ZERO |
|
52 |
case (ONE, r2s) => r2s |
|
53 |
case (r1s, ONE) => r1s |
|
54 |
case (r1s, r2s) => SEQ(r1s, r2s) |
|
422 | 55 |
} |
564 | 56 |
case r => r |
422 | 57 |
} |
58 |
||
59 |
||
623 | 60 |
// the derivative w.r.t. a string (iterates der) |
618 | 61 |
def ders(s: List[Char], r: Rexp) : Rexp = s match { |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
case Nil => r |
564 | 63 |
case c::s => ders(s, simp(der(c, r))) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
|
623 | 66 |
// the derivative w.r.t. a string (iterates der) |
618 | 67 |
def dersp(s: List[Char], r: Rexp) : Rexp = s match { |
68 |
case Nil => r |
|
69 |
case c::s => dersp(s, der(c, r)) |
|
70 |
} |
|
71 |
||
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
72 |
|
623 | 73 |
// the main matcher function |
414 | 74 |
def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
|
623 | 76 |
// tests |
618 | 77 |
val q = SEQ(SEQ(CHAR('x'), CHAR('y')), CHAR('z')) |
78 |
dersp("x".toList, q) |
|
79 |
dersp("xy".toList, q) |
|
80 |
dersp("xyz".toList, q) |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
|
623 | 82 |
// one or zero |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
83 |
def OPT(r: Rexp) = ALT(r, ONE) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
|
477 | 85 |
|
86 |
// Test Cases |
|
87 |
||
623 | 88 |
// evil regular expressions: (a?){n} a{n} and (a*)* b |
544 | 89 |
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
|
90 |
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
422 | 91 |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
def time_needed[T](i: Int, code: => T) = { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
val start = System.nanoTime() |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
for (j <- 1 to i) code |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
val end = System.nanoTime() |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
(end - start)/(i * 1.0e9) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
98 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
99 |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
100 |
|
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
101 |
//test: (a?{n}) (a{n}) |
564 | 102 |
for (i <- 1 to 7001 by 1000) { |
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
103 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i)))) |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
104 |
} |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
|
564 | 106 |
for (i <- 1 to 7001 by 1000) { |
415 | 107 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i)))) |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
109 |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
110 |
//test: (a*)* b |
477 | 111 |
for (i <- 1 to 6000001 by 500000) { |
112 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i)))) |
|
113 |
} |
|
114 |
||
115 |
for (i <- 1 to 6000001 by 500000) { |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
116 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i)))) |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
117 |
} |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
118 |
|
477 | 119 |
|
120 |
// size of a regular expressions - for testing purposes |
|
121 |
def size(r: Rexp) : Int = r match { |
|
122 |
case ZERO => 1 |
|
123 |
case ONE => 1 |
|
124 |
case CHAR(_) => 1 |
|
125 |
case ALT(r1, r2) => 1 + size(r1) + size(r2) |
|
126 |
case SEQ(r1, r2) => 1 + size(r1) + size(r2) |
|
127 |
case STAR(r) => 1 + size(r) |
|
128 |
case NTIMES(r, _) => 1 + size(r) |
|
415 | 129 |
} |
130 |
||
477 | 131 |
|
132 |
// now the size of the derivatives grows |
|
133 |
// much, much slower |
|
134 |
||
135 |
size(ders("".toList, EVIL2)) // 5 |
|
136 |
size(ders("a".toList, EVIL2)) // 8 |
|
137 |
size(ders("aa".toList, EVIL2)) // 8 |
|
138 |
size(ders("aaa".toList, EVIL2)) // 8 |
|
139 |
size(ders("aaaa".toList, EVIL2)) // 8 |
|
140 |
size(ders("aaaaa".toList, EVIL2)) // 8 |
|
544 | 141 |
|
142 |
||
143 |
||
144 |
||
145 |