author | cu |
Wed, 25 Oct 2017 00:05:59 +0100 | |
changeset 528 | 68fab15cd6fb |
parent 519 | 955d5b3b0619 |
child 623 | 47a299e7010f |
permissions | -rw-r--r-- |
455
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
|
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
abstract class Rexp |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
case object ZERO extends Rexp |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
case object ONE extends Rexp |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
case class CHAR(c: Char) extends Rexp |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
case class STAR(r: Rexp) extends Rexp |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
case class NTIMES(r: Rexp, n: Int) extends Rexp |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
case class UPNTIMES(r: Rexp, n: Int) extends Rexp |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
|
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
// nullable function: tests whether the regular |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
// expression can recognise the empty string |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
def nullable (r: Rexp) : Boolean = r match { |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
case ZERO => false |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
case ONE => true |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
case CHAR(_) => false |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
case STAR(_) => true |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
case NTIMES(r, i) => if (i == 0) true else nullable(r) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
case UPNTIMES(r: Rexp, n: Int) => true |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
} |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
|
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
// derivative of a regular expression w.r.t. a character |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
def der (c: Char, r: Rexp) : Rexp = r match { |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
case ZERO => ZERO |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
case ONE => ZERO |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
case CHAR(d) => if (c == d) ONE else ZERO |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
case SEQ(r1, r2) => |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
else SEQ(der(c, r1), r2) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
case STAR(r1) => SEQ(der(c, r1), STAR(r1)) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
case NTIMES(r1, i) => |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
if (i == 0) ZERO else |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
if (nullable(r1)) SEQ(der(c, r1), UPNTIMES(r1, i - 1)) |
457 | 38 |
else SEQ(der(c, r1), NTIMES(r1, i - 1)) |
455
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
case UPNTIMES(r1, i) => |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
if (i == 0) ZERO |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
else SEQ(der(c, r1), UPNTIMES(r1, i - 1)) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
} |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
|
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
def simp(r: Rexp) : Rexp = r match { |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
case ALT(r1, r2) => (simp(r1), simp(r2)) match { |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
case (ZERO, r2s) => r2s |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
case (r1s, ZERO) => r1s |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
} |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
case SEQ(r1, r2) => (simp(r1), simp(r2)) match { |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
case (ZERO, _) => ZERO |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
case (_, ZERO) => ZERO |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
case (ONE, r2s) => r2s |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
case (r1s, ONE) => r1s |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
case (r1s, r2s) => SEQ(r1s, r2s) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
} |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
case r => r |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
} |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
|
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
|
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
// derivative w.r.t. a string (iterates der) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
case Nil => r |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
case c::s => ders(s, simp(der(c, r))) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
} |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
|
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
|
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
// main matcher function |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
|
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
//one or zero |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
def OPT(r: Rexp) = ALT(r, ONE) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
|
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
//evil regular expressions |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n)) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
471 | 77 |
val EVIL3 = SEQ(STAR(ALT(CHAR('a'), SEQ(CHAR('a'),CHAR('a')))), CHAR('b')) |
455
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
|
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
def time_needed[T](i: Int, code: => T) = { |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
val start = System.nanoTime() |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
for (j <- 1 to i) code |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
val end = System.nanoTime() |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
(end - start)/(i * 1.0e9) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
} |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
|
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
|
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
//test: (a?{n}) (a{n}) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
for (i <- 1 to 8001 by 1000) { |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i)))) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
} |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
91 |
|
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
for (i <- 1 to 8001 by 1000) { |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i)))) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
} |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
|
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
//test: (a*)* b |
519 | 97 |
for (i <- 1 to 6000001 by 500000) { |
471 | 98 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i)))) |
99 |
} |
|
100 |
||
519 | 101 |
for (i <- 1 to 6000001 by 500000) { |
455
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i)))) |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
} |
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
104 |
|
471 | 105 |
|
106 |
val r0 = simp(der('a', EVIL3)) |
|
107 |
val r1 = simp(der('a', r0)) |
|
108 |
val r2 = simp(der('a', r1)) |
|
109 |
val r3 = simp(der('a', r2)) |
|
110 |
val r4 = simp(der('a', r3)) |
|
111 |
val r5 = simp(der('a', r4)) |
|
112 |
val r6 = simp(der('a', r5)) |
|
113 |
||
114 |
//test: (a|aa)* b |
|
519 | 115 |
/* |
471 | 116 |
for (i <- 1 to 7001 by 500) { |
117 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL3, "a" * i ++ "c")))) |
|
455
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
118 |
} |
519 | 119 |
*/ |
455
1dbf84ade62c
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
120 |