author | Christian Urban <christian.urban@kcl.ac.uk> |
Wed, 21 Oct 2020 14:55:20 +0100 | |
changeset 786 | da2488db453e |
parent 742 | b5b5583a3a08 |
permissions | -rw-r--r-- |
564 | 1 |
// A version with simplification of derivatives; |
477 | 2 |
// this keeps the regular expressions small, which |
647 | 3 |
// is good for the 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 |
|
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
66 |
|
623 | 67 |
// the main matcher function |
647 | 68 |
def matcher(r: Rexp, s: String) : Boolean = |
69 |
nullable(ders(s.toList, r)) |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
|
623 | 72 |
// one or zero |
407
4b454a6d1814
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
121
diff
changeset
|
73 |
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
|
74 |
|
477 | 75 |
|
76 |
// Test Cases |
|
77 |
||
623 | 78 |
// evil regular expressions: (a?){n} a{n} and (a*)* b |
544 | 79 |
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
|
80 |
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
422 | 81 |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
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
|
84 |
val start = System.nanoTime() |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
for (j <- 1 to i) code |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
val end = System.nanoTime() |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
(end - start)/(i * 1.0e9) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
90 |
|
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
91 |
//test: (a?{n}) (a{n}) |
638 | 92 |
for (i <- 0 to 8000 by 1000) { |
93 |
println(f"$i: ${time_needed(3, matcher(EVIL1(i), "a" * i))}%.5f") |
|
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
|
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
96 |
//test: (a*)* b |
631 | 97 |
for (i <- 0 to 6000000 by 500000) { |
638 | 98 |
println(f"$i: ${time_needed(3, matcher(EVIL2, "a" * i))}%.5f") |
434
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
99 |
} |
8664ff87cd77
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
422
diff
changeset
|
100 |
|
477 | 101 |
|
102 |
// size of a regular expressions - for testing purposes |
|
103 |
def size(r: Rexp) : Int = r match { |
|
104 |
case ZERO => 1 |
|
105 |
case ONE => 1 |
|
106 |
case CHAR(_) => 1 |
|
107 |
case ALT(r1, r2) => 1 + size(r1) + size(r2) |
|
108 |
case SEQ(r1, r2) => 1 + size(r1) + size(r2) |
|
109 |
case STAR(r) => 1 + size(r) |
|
110 |
case NTIMES(r, _) => 1 + size(r) |
|
415 | 111 |
} |
112 |
||
477 | 113 |
|
114 |
// now the size of the derivatives grows |
|
115 |
// much, much slower |
|
116 |
||
117 |
size(ders("".toList, EVIL2)) // 5 |
|
118 |
size(ders("a".toList, EVIL2)) // 8 |
|
119 |
size(ders("aa".toList, EVIL2)) // 8 |
|
120 |
size(ders("aaa".toList, EVIL2)) // 8 |
|
121 |
size(ders("aaaa".toList, EVIL2)) // 8 |
|
122 |
size(ders("aaaaa".toList, EVIL2)) // 8 |
|
544 | 123 |
|
124 |
||
125 |
||
126 |
||
127 |