author | Christian Urban <christian.urban@kcl.ac.uk> |
Sat, 28 Nov 2020 15:37:30 +0000 | |
changeset 376 | 6cc36d0ef79e |
parent 98 | 8f03f0dc3065 |
permissions | -rw-r--r-- |
90
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
// Part 1 about Regular Expression Matching |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
//========================================== |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
abstract class Rexp |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
case object ZERO extends Rexp |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
case object ONE extends Rexp |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
case class CHAR(c: Char) extends Rexp |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
case class STAR(r: Rexp) extends Rexp |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
// some convenience for typing in regular expressions |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
import scala.language.implicitConversions |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
import scala.language.reflectiveCalls |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
def charlist2rexp(s: List[Char]): Rexp = s match { |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
case Nil => ONE |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
case c::Nil => CHAR(c) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
} |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
implicit def RexpOps (r: Rexp) = new { |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
def | (s: Rexp) = ALT(r, s) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
def % = STAR(r) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
def ~ (s: Rexp) = SEQ(r, s) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
} |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
implicit def stringOps (s: String) = new { |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
def | (r: Rexp) = ALT(s, r) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
def | (r: String) = ALT(s, r) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
def % = STAR(s) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
def ~ (r: Rexp) = SEQ(s, r) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
def ~ (r: String) = SEQ(s, r) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
} |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
// (1a) Complete the function nullable according to |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
// the definition given in the coursework; this |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
// function checks whether a regular expression |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
// can match the empty string |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
def nullable (r: Rexp) : Boolean = r match { |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
case ZERO => false |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
case ONE => true |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
case CHAR(_) => false |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
case STAR(_) => true |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
} |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
// (1b) Complete the function der according to |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
// the definition given in the coursework; this |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
// function calculates the derivative of a |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
// regular expression w.r.t. a character |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
def der (c: Char, r: Rexp) : Rexp = r match { |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
case ZERO => ZERO |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
case ONE => ZERO |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
case CHAR(d) => if (c == d) ONE else ZERO |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
case SEQ(r1, r2) => |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
else SEQ(der(c, r1), r2) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
case STAR(r1) => SEQ(der(c, r1), STAR(r1)) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
} |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
// (1c) Complete the function der according to |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
// the specification given in the coursework; this |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
// function simplifies a regular expression; |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
// however it does not simplify inside STAR-regular |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
// expressions |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
def simp(r: Rexp) : Rexp = r match { |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
case ALT(r1, r2) => (simp(r1), simp(r2)) match { |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
case (ZERO, r2s) => r2s |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
case (r1s, ZERO) => r1s |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
} |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
case SEQ(r1, r2) => (simp(r1), simp(r2)) match { |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
case (ZERO, _) => ZERO |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
case (_, ZERO) => ZERO |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
case (ONE, r2s) => r2s |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
case (r1s, ONE) => r1s |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
case (r1s, r2s) => SEQ(r1s, r2s) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
} |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
case r => r |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
} |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
// (1d) Complete the two functions below; the first |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
91 |
// calculates the derivative w.r.t. a string; the second |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
// is the regular expression matcher taking a regular |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
// expression and a string and checks whether the |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
// string matches the regular expression |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
case Nil => r |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
98 |
case c::s => ders(s, simp(der(c, r))) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
99 |
} |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
// main matcher function |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
def matcher(r: Rexp, s: String): Boolean = nullable(ders(s.toList, r)) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
104 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
// (1e) Complete the function below: it searches (from the left to |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
106 |
// right) in string s1 all the non-empty substrings that match the |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
107 |
// regular expression -- these substrings are assumed to be |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
// the longest substrings matched by the regular expression and |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
109 |
// assumed to be non-overlapping. All these substrings in s1 are replaced |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
110 |
// by s2. |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
111 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
112 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
113 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
114 |
def splits(s: String): List[(String, String)] = |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
115 |
(for (i <- (1 to s.length).toList) yield s.splitAt(i)).reverse |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
116 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
117 |
splits("abcde") |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
118 |
splits("") |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
119 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
120 |
def first(r: Rexp, lst: List[(String, String)]): Option[String] = lst match { |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
121 |
case Nil => None |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
122 |
case (s1, s2)::xs => if (matcher(r, s1)) Some(s2) else first(r, xs) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
123 |
} |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
124 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
125 |
"abcd".head |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
126 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
127 |
def replace(r: Rexp, s1: String, s2: String): String = first(r, splits(s1)) match { |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
128 |
case None if (s1 == "") => "" |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
129 |
case None => s1.head.toString ++ replace(r, s1.tail, s2) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
130 |
case Some(s) => s2 ++ replace(r, s, s2) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
131 |
} |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
132 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
133 |
val s1 = "aabbbaaaaaaabaaaaabbaaaabb" |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
134 |
val r: Rexp = "aa".% | "bb" |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
135 |
splits(s1) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
136 |
first(r, splits(s1)) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
137 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
138 |
replace(r, s1, "c") |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
139 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
140 |
splits("bb") |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
141 |
first(r, splits("bb")) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
142 |
replace(r, "abb", "c") |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
143 |
|
96 | 144 |
val rl = SEQ("a",SEQ("b", "c")) |
145 |
val rk = SEQ(SEQ("a", "b"), "c") |
|
90
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
146 |
|
96 | 147 |
val rm = der('a', rk) |
148 |
der('b', rk) |
|
149 |
der('c', rk) |
|
90
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
150 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
151 |
der('a', rm) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
152 |
val rn = der('b', rm) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
153 |
der('c', rm) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
154 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
155 |
der('a', rn) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
156 |
der('b', rn) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
157 |
der('c', rn) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
158 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
159 |
// some testing data |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
160 |
// the supposedly 'evil' regular expression (a*)* b |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
161 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
162 |
val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
163 |
println(matcher(EVIL, "a" * 1000 ++ "b")) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
164 |
println(matcher(EVIL, "a" * 1000)) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
165 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
166 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
167 |
def time_needed[T](i: Int, code: => T) = { |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
168 |
val start = System.nanoTime() |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
169 |
for (j <- 1 to i) code |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
170 |
val end = System.nanoTime() |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
171 |
(end - start)/(i * 1.0e9) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
172 |
} |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
173 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
174 |
for (i <- 1 to 5000001 by 500000) { |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
175 |
println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i)))) |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
176 |
} |
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
177 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
178 |
|
d77af4aca939
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
179 |