725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// A version with simplification of derivatives;
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
// this keeps the regular expressions small, which
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
// is good for the run-time
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
// call the test cases with X = {1,2}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
// amm re3.sc testX
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
// or
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
// amm re3.sc all
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
abstract class Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
case object ZERO extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
case object ONE extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
case class CHAR(c: Char) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
case class STAR(r: Rexp) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
case class NTIMES(r: Rexp, n: Int) extends Rexp
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
// the nullable function: tests whether the regular
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
// expression can recognise the empty string
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
def nullable (r: Rexp) : Boolean = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
case ZERO => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
case ONE => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
case CHAR(_) => false
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
case ALT(r1, r2) => nullable(r1) || nullable(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
case STAR(_) => true
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
case NTIMES(r, i) => if (i == 0) true else nullable(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
// the derivative of a regular expression w.r.t. a character
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
def der (c: Char, r: Rexp) : Rexp = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
case ZERO => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
case ONE => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
case CHAR(d) => if (c == d) ONE else ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
case SEQ(r1, r2) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
else SEQ(der(c, r1), r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
case STAR(r1) => SEQ(der(c, r1), STAR(r1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
case NTIMES(r, i) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
def simp(r: Rexp) : Rexp = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
case ALT(r1, r2) => (simp(r1), simp(r2)) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
case (ZERO, r2s) => r2s
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
case (r1s, ZERO) => r1s
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
case SEQ(r1, r2) => (simp(r1), simp(r2)) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
case (ZERO, _) => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
case (_, ZERO) => ZERO
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
case (ONE, r2s) => r2s
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
case (r1s, ONE) => r1s
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
case (r1s, r2s) => SEQ(r1s, r2s)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
64 |
case r => r
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
// the derivative w.r.t. a string (iterates der)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
def ders(s: List[Char], r: Rexp) : Rexp = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
case Nil => r
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
71 |
case c::s => ders(s, simp(der(c, r)))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
72 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
73 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
74 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
// the main matcher function
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
def matcher(r: Rexp, s: String) : Boolean =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
77 |
nullable(ders(s.toList, r))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
78 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
79 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
80 |
// one or zero
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
81 |
def OPT(r: Rexp) = ALT(r, ONE)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
83 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
84 |
// Test Cases
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
86 |
// evil regular expressions: (a?){n} a{n} and (a*)* b
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
def time_needed[T](i: Int, code: => T) = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
val start = System.nanoTime()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
for (j <- 1 to i) code
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
val end = System.nanoTime()
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
(end - start)/(i * 1.0e9)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
|
742
|
99 |
//
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
@doc("Test (a?{n}) (a{n})")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
101 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
102 |
def test1() = {
|
742
|
103 |
println("Test (a?{n}) (a{n})")
|
|
104 |
|
769
|
105 |
for (i <- 0 to 9000 by 1000) {
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
106 |
println(f"$i: ${time_needed(3, matcher(EVIL1(i), "a" * i))}%.5f")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
108 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
|
742
|
110 |
//
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
111 |
@doc("Test (a*)* b")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
112 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
113 |
def test2() = {
|
742
|
114 |
println("Test (a*)* b")
|
|
115 |
|
725
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
116 |
for (i <- 0 to 6000000 by 500000) {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
117 |
println(f"$i: ${time_needed(3, matcher(EVIL2, "a" * i))}%.5f")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
118 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
119 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
120 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
121 |
// size of a regular expressions - for testing purposes
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
122 |
def size(r: Rexp) : Int = r match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
123 |
case ZERO => 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
124 |
case ONE => 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
125 |
case CHAR(_) => 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
126 |
case ALT(r1, r2) => 1 + size(r1) + size(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
127 |
case SEQ(r1, r2) => 1 + size(r1) + size(r2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
128 |
case STAR(r) => 1 + size(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
129 |
case NTIMES(r, _) => 1 + size(r)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
130 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
131 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
132 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
133 |
// now the size of the derivatives grows
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
134 |
// much, much slower
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
135 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
136 |
size(ders("".toList, EVIL2)) // 5
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
137 |
size(ders("a".toList, EVIL2)) // 8
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
138 |
size(ders("aa".toList, EVIL2)) // 8
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
139 |
size(ders("aaa".toList, EVIL2)) // 8
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
140 |
size(ders("aaaa".toList, EVIL2)) // 8
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
141 |
size(ders("aaaaa".toList, EVIL2)) // 8
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
142 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
143 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
144 |
@doc("All tests.")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
145 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
146 |
def all() = { test1(); test2() }
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
147 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
148 |
|
782
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
// test: (a + aa)*
|
|
153 |
val EVIL3 = STAR(ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a'))))
|
|
154 |
|
|
155 |
// test: (1 + a + aa)*
|
|
156 |
val EVIL4 = STAR(ALT(ONE, ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a')))))
|
|
157 |
|
|
158 |
@doc("Test Evil3")
|
|
159 |
@main
|
|
160 |
def test3() = {
|
|
161 |
println("Test (a + aa)*")
|
|
162 |
|
|
163 |
for (i <- 0 to 35 by 5) {
|
|
164 |
println(f"$i: ${time_needed(1, matcher(EVIL3, "a" * i))}%.5f")
|
|
165 |
}
|
|
166 |
}
|
|
167 |
|
|
168 |
@doc("Test Evil4")
|
|
169 |
@main
|
|
170 |
def test4() = {
|
|
171 |
println("Test (1 + a + aa)*")
|
|
172 |
|
|
173 |
for (i <- 0 to 35 by 5) {
|
|
174 |
println(f"$i: ${time_needed(1, matcher(EVIL4, "a" * i))}%.5f")
|
|
175 |
}
|
|
176 |
} |