300
|
1 |
// Core Part about Regular Expression Matching
|
|
2 |
//=============================================
|
153
|
3 |
|
300
|
4 |
object CW9c {
|
249
|
5 |
|
221
|
6 |
// Regular Expressions
|
153
|
7 |
abstract class Rexp
|
|
8 |
case object ZERO extends Rexp
|
|
9 |
case object ONE extends Rexp
|
|
10 |
case class CHAR(c: Char) extends Rexp
|
249
|
11 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp
|
|
12 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
|
|
13 |
case class STAR(r: Rexp) extends Rexp
|
153
|
14 |
|
249
|
15 |
// some convenience for typing in regular expressions
|
153
|
16 |
|
229
|
17 |
import scala.language.implicitConversions
|
|
18 |
import scala.language.reflectiveCalls
|
|
19 |
|
249
|
20 |
|
153
|
21 |
def charlist2rexp(s: List[Char]): Rexp = s match {
|
|
22 |
case Nil => ONE
|
|
23 |
case c::Nil => CHAR(c)
|
|
24 |
case c::s => SEQ(CHAR(c), charlist2rexp(s))
|
|
25 |
}
|
|
26 |
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
|
|
27 |
|
|
28 |
implicit def RexpOps (r: Rexp) = new {
|
|
29 |
def | (s: Rexp) = ALT(r, s)
|
|
30 |
def % = STAR(r)
|
|
31 |
def ~ (s: Rexp) = SEQ(r, s)
|
|
32 |
}
|
|
33 |
|
|
34 |
implicit def stringOps (s: String) = new {
|
|
35 |
def | (r: Rexp) = ALT(s, r)
|
|
36 |
def | (r: String) = ALT(s, r)
|
|
37 |
def % = STAR(s)
|
|
38 |
def ~ (r: Rexp) = SEQ(s, r)
|
|
39 |
def ~ (r: String) = SEQ(s, r)
|
|
40 |
}
|
|
41 |
|
221
|
42 |
// (1) Complete the function nullable according to
|
229
|
43 |
// the definition given in the coursework; this
|
153
|
44 |
// function checks whether a regular expression
|
221
|
45 |
// can match the empty string and Returns a boolean
|
|
46 |
// accordingly.
|
153
|
47 |
|
229
|
48 |
def nullable (r: Rexp) : Boolean = r match {
|
249
|
49 |
case ZERO => false
|
|
50 |
case ONE => true
|
|
51 |
case CHAR(_) => false
|
|
52 |
case ALT(r1, r2) => nullable(r1) || nullable(r2)
|
|
53 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2)
|
|
54 |
case STAR(_) => true
|
153
|
55 |
}
|
|
56 |
|
221
|
57 |
// (2) Complete the function der according to
|
153
|
58 |
// the definition given in the coursework; this
|
229
|
59 |
// function calculates the derivative of a
|
221
|
60 |
// regular expression w.r.t. a character.
|
153
|
61 |
|
229
|
62 |
def der (c: Char, r: Rexp) : Rexp = r match {
|
249
|
63 |
case ZERO => ZERO
|
|
64 |
case ONE => ZERO
|
|
65 |
case CHAR(d) => if (c == d) ONE else ZERO
|
|
66 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
|
|
67 |
case SEQ(r1, r2) =>
|
|
68 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
|
|
69 |
else SEQ(der(c, r1), r2)
|
|
70 |
case STAR(r1) => SEQ(der(c, r1), STAR(r1))
|
153
|
71 |
}
|
|
72 |
|
221
|
73 |
// (3) Complete the simp function according to
|
153
|
74 |
// the specification given in the coursework; this
|
221
|
75 |
// function simplifies a regular expression from
|
229
|
76 |
// the inside out, like you would simplify arithmetic
|
|
77 |
// expressions; however it does not simplify inside
|
221
|
78 |
// STAR-regular expressions.
|
153
|
79 |
|
229
|
80 |
def simp(r: Rexp) : Rexp = r match {
|
249
|
81 |
case ALT(r1, r2) => (simp(r1), simp(r2)) match {
|
|
82 |
case (ZERO, r2s) => r2s
|
|
83 |
case (r1s, ZERO) => r1s
|
|
84 |
case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
|
|
85 |
}
|
|
86 |
case SEQ(r1, r2) => (simp(r1), simp(r2)) match {
|
|
87 |
case (ZERO, _) => ZERO
|
|
88 |
case (_, ZERO) => ZERO
|
|
89 |
case (ONE, r2s) => r2s
|
|
90 |
case (r1s, ONE) => r1s
|
|
91 |
case (r1s, r2s) => SEQ(r1s, r2s)
|
|
92 |
}
|
|
93 |
case r => r
|
153
|
94 |
}
|
|
95 |
|
221
|
96 |
|
229
|
97 |
// (4) Complete the two functions below; the first
|
153
|
98 |
// calculates the derivative w.r.t. a string; the second
|
|
99 |
// is the regular expression matcher taking a regular
|
|
100 |
// expression and a string and checks whether the
|
249
|
101 |
// string matches the regular expression.
|
153
|
102 |
|
229
|
103 |
def ders (s: List[Char], r: Rexp) : Rexp = s match {
|
249
|
104 |
case Nil => r
|
|
105 |
case c::s => ders(s, simp(der(c, r)))
|
153
|
106 |
}
|
|
107 |
|
249
|
108 |
// main matcher function
|
|
109 |
def matcher(r: Rexp, s: String) = nullable(ders(s.toList, r))
|
153
|
110 |
|
221
|
111 |
// (5) Complete the size function for regular
|
229
|
112 |
// expressions according to the specification
|
153
|
113 |
// given in the coursework.
|
|
114 |
|
249
|
115 |
|
229
|
116 |
def size(r: Rexp): Int = r match {
|
249
|
117 |
case ZERO => 1
|
|
118 |
case ONE => 1
|
|
119 |
case CHAR(_) => 1
|
|
120 |
case ALT(r1, r2) => 1 + size(r1) + size (r2)
|
|
121 |
case SEQ(r1, r2) => 1 + size(r1) + size (r2)
|
|
122 |
case STAR(r1) => 1 + size(r1)
|
153
|
123 |
}
|
|
124 |
|
228
|
125 |
|
249
|
126 |
|
236
|
127 |
// some testing data
|
300
|
128 |
|
|
129 |
//matcher(("a" ~ "b") ~ "c", "abc") // => true
|
|
130 |
//matcher(("a" ~ "b") ~ "c", "ab") // => false
|
229
|
131 |
|
|
132 |
// the supposedly 'evil' regular expression (a*)* b
|
|
133 |
val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
|
|
134 |
|
300
|
135 |
//matcher(EVIL, "a" * 1000 ++ "b") // => true
|
|
136 |
//matcher(EVIL, "a" * 1000) // => false
|
153
|
137 |
|
|
138 |
// size without simplifications
|
300
|
139 |
//size(der('a', der('a', EVIL))) // => 28
|
|
140 |
//size(der('a', der('a', der('a', EVIL)))) // => 58
|
153
|
141 |
|
|
142 |
// size with simplification
|
300
|
143 |
//size(simp(der('a', der('a', EVIL)))) // => 8
|
|
144 |
//size(simp(der('a', der('a', der('a', EVIL))))) // => 8
|
228
|
145 |
|
229
|
146 |
// Python needs around 30 seconds for matching 28 a's with EVIL.
|
221
|
147 |
// Java 9 and later increase this to an "astonishing" 40000 a's in
|
249
|
148 |
// around 30 seconds.
|
153
|
149 |
//
|
249
|
150 |
// Lets see how long it takes to match strings with
|
|
151 |
// 5 Million a's...it should be in the range of a
|
|
152 |
// couple of seconds.
|
153
|
153 |
|
|
154 |
def time_needed[T](i: Int, code: => T) = {
|
|
155 |
val start = System.nanoTime()
|
|
156 |
for (j <- 1 to i) code
|
|
157 |
val end = System.nanoTime()
|
|
158 |
(end - start)/(i * 1.0e9)
|
|
159 |
}
|
|
160 |
|
300
|
161 |
//for (i <- 0 to 5000000 by 500000) {
|
|
162 |
// println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i))) + " secs.")
|
|
163 |
//}
|
221
|
164 |
|
229
|
165 |
// another "power" test case
|
300
|
166 |
//simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(100).next) == ONE
|
221
|
167 |
|
|
168 |
// the Iterator produces the rexp
|
|
169 |
//
|
|
170 |
// SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
|
|
171 |
//
|
249
|
172 |
// where SEQ is nested 100 times.
|
|
173 |
|
300
|
174 |
|
228
|
175 |
|
300
|
176 |
}
|