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