420
|
1 |
// Main Part 3 about Regular Expression Matching
|
433
|
2 |
//==============================================
|
153
|
3 |
|
403
|
4 |
object M3 {
|
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
|
433
|
11 |
case class ALTs(rs: List[Rexp]) extends Rexp // alternatives
|
|
12 |
case class SEQs(rs: List[Rexp]) extends Rexp // sequences
|
|
13 |
case class STAR(r: Rexp) extends Rexp // star
|
153
|
14 |
|
|
15 |
|
433
|
16 |
//the usual binary choice and binary sequence can be defined
|
|
17 |
//in terms of ALTs and SEQs
|
403
|
18 |
def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
|
433
|
19 |
def SEQ(r1: Rexp, r2: Rexp) = SEQs(List(r1, r2))
|
403
|
20 |
|
424
|
21 |
// some convenience for typing in regular expressions
|
229
|
22 |
import scala.language.implicitConversions
|
|
23 |
import scala.language.reflectiveCalls
|
|
24 |
|
153
|
25 |
def charlist2rexp(s: List[Char]): Rexp = s match {
|
|
26 |
case Nil => ONE
|
|
27 |
case c::Nil => CHAR(c)
|
|
28 |
case c::s => SEQ(CHAR(c), charlist2rexp(s))
|
|
29 |
}
|
|
30 |
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
|
|
31 |
|
|
32 |
implicit def RexpOps (r: Rexp) = new {
|
|
33 |
def | (s: Rexp) = ALT(r, s)
|
|
34 |
def % = STAR(r)
|
|
35 |
def ~ (s: Rexp) = SEQ(r, s)
|
|
36 |
}
|
|
37 |
|
|
38 |
implicit def stringOps (s: String) = new {
|
|
39 |
def | (r: Rexp) = ALT(s, r)
|
|
40 |
def | (r: String) = ALT(s, r)
|
|
41 |
def % = STAR(s)
|
|
42 |
def ~ (r: Rexp) = SEQ(s, r)
|
|
43 |
def ~ (r: String) = SEQ(s, r)
|
|
44 |
}
|
|
45 |
|
433
|
46 |
// (1)
|
347
|
47 |
def nullable (r: Rexp) : Boolean = r match {
|
|
48 |
case ZERO => false
|
|
49 |
case ONE => true
|
424
|
50 |
case CHAR(_) => false
|
|
51 |
case ALTs(rs) => rs.exists(nullable)
|
433
|
52 |
case SEQs(rs) => rs.forall(nullable)
|
424
|
53 |
case STAR(_) => true
|
153
|
54 |
}
|
|
55 |
|
433
|
56 |
// (2)
|
|
57 |
def der(c: Char, r: Rexp) : Rexp = r match {
|
347
|
58 |
case ZERO => ZERO
|
|
59 |
case ONE => ZERO
|
424
|
60 |
case CHAR(d) => if (c == d) ONE else ZERO
|
|
61 |
case ALTs(rs) => ALTs(rs.map(der(c, _)))
|
433
|
62 |
case SEQs(Nil) => ZERO
|
|
63 |
case SEQs(r1::rs) =>
|
|
64 |
if (nullable(r1)) ALT(SEQs(der(c, r1)::rs), der(c, SEQs(rs)))
|
|
65 |
else SEQs(der(c, r1):: rs)
|
424
|
66 |
case STAR(r1) => SEQ(der(c, r1), STAR(r1))
|
420
|
67 |
}
|
|
68 |
|
|
69 |
|
433
|
70 |
// (3)
|
|
71 |
def denest(rs: List[Rexp]) : List[Rexp] = rs match {
|
|
72 |
case Nil => Nil
|
|
73 |
case ZERO::tl => denest(tl)
|
|
74 |
case ALTs(rs1)::rs2 => rs1 ::: denest(rs2)
|
|
75 |
case r::rs => r :: denest(rs)
|
|
76 |
}
|
420
|
77 |
|
433
|
78 |
// (4)
|
|
79 |
def flts(rs: List[Rexp], acc: List[Rexp] = Nil) : List[Rexp] = rs match {
|
|
80 |
case Nil => acc
|
|
81 |
case ZERO::rs => ZERO::Nil
|
|
82 |
case ONE::rs => flts(rs, acc)
|
|
83 |
case SEQs(rs1)::rs => flts(rs, acc ::: rs1)
|
|
84 |
case r::rs => flts(rs, acc :+ r)
|
153
|
85 |
}
|
|
86 |
|
433
|
87 |
// (5)
|
|
88 |
def ALTs_smart(rs: List[Rexp]) : Rexp = rs match {
|
|
89 |
case Nil => ZERO
|
|
90 |
case r::Nil => r
|
|
91 |
case rs => ALTs(rs)
|
|
92 |
}
|
403
|
93 |
|
433
|
94 |
def SEQs_smart(rs: List[Rexp]) : Rexp = rs match {
|
|
95 |
case Nil => ONE
|
|
96 |
case ZERO::nil => ZERO
|
|
97 |
case r::Nil => r
|
|
98 |
case rs => SEQs(rs)
|
|
99 |
}
|
424
|
100 |
|
433
|
101 |
// (6)
|
403
|
102 |
|
347
|
103 |
def simp(r: Rexp) : Rexp = r match {
|
433
|
104 |
case ALTs(rs) =>
|
|
105 |
ALTs_smart(denest(rs.map(simp)).distinct)
|
|
106 |
case SEQs(rs) =>
|
|
107 |
SEQs_smart(flts(rs.map(simp)))
|
424
|
108 |
case r => r
|
153
|
109 |
}
|
|
110 |
|
433
|
111 |
//println("Simp tests")
|
|
112 |
//println(simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE)))
|
|
113 |
//println(simp(((CHAR('a') | ZERO) ~ ONE) |
|
|
114 |
// (((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO))))
|
221
|
115 |
|
433
|
116 |
|
|
117 |
// (7)
|
153
|
118 |
|
347
|
119 |
def ders (s: List[Char], r: Rexp) : Rexp = s match {
|
|
120 |
case Nil => r
|
424
|
121 |
case c::s => ders(s, simp(der(c, r)))
|
153
|
122 |
}
|
|
123 |
|
424
|
124 |
// main matcher function
|
|
125 |
def matcher(r: Rexp, s: String) = nullable(ders(s.toList, r))
|
420
|
126 |
|
433
|
127 |
// (8)
|
424
|
128 |
|
347
|
129 |
def size(r: Rexp): Int = r match {
|
|
130 |
case ZERO => 1
|
|
131 |
case ONE => 1
|
424
|
132 |
case CHAR(_) => 1
|
|
133 |
case ALTs(rs) => 1 + rs.map(size).sum
|
433
|
134 |
case SEQs(rs) => 1 + rs.map(size).sum
|
424
|
135 |
case STAR(r1) => 1 + size(r1)
|
153
|
136 |
}
|
|
137 |
|
347
|
138 |
|
424
|
139 |
|
236
|
140 |
// some testing data
|
433
|
141 |
/*
|
|
142 |
println(matcher(("a" ~ "b") ~ "c", "abc")) // => true
|
|
143 |
println(matcher(("a" ~ "b") ~ "c", "ab")) // => false
|
229
|
144 |
|
|
145 |
// the supposedly 'evil' regular expression (a*)* b
|
433
|
146 |
val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
|
229
|
147 |
|
433
|
148 |
println(matcher(EVIL, "a" * 1000 ++ "b")) // => true
|
|
149 |
println(matcher(EVIL, "a" * 1000)) // => false
|
153
|
150 |
|
|
151 |
// size without simplifications
|
433
|
152 |
println(size(der('a', der('a', EVIL)))) // => 36
|
|
153 |
println(size(der('a', der('a', der('a', EVIL))))) // => 83
|
153
|
154 |
|
|
155 |
// size with simplification
|
433
|
156 |
println(simp(der('a', der('a', EVIL))))
|
|
157 |
println(simp(der('a', der('a', der('a', EVIL)))))
|
424
|
158 |
|
433
|
159 |
println(size(simp(der('a', der('a', EVIL))))) // => 7
|
|
160 |
println(size(simp(der('a', der('a', der('a', EVIL)))))) // => 7
|
228
|
161 |
|
229
|
162 |
// Python needs around 30 seconds for matching 28 a's with EVIL.
|
221
|
163 |
// Java 9 and later increase this to an "astonishing" 40000 a's in
|
424
|
164 |
// around 30 seconds.
|
153
|
165 |
//
|
424
|
166 |
// Lets see how long it takes to match strings with
|
|
167 |
// 5 Million a's...it should be in the range of a
|
433
|
168 |
// few seconds.
|
153
|
169 |
|
424
|
170 |
def time_needed[T](i: Int, code: => T) = {
|
|
171 |
val start = System.nanoTime()
|
|
172 |
for (j <- 1 to i) code
|
|
173 |
val end = System.nanoTime()
|
|
174 |
"%.5f".format((end - start)/(i * 1.0e9))
|
|
175 |
}
|
153
|
176 |
|
433
|
177 |
for (i <- 0 to 5000000 by 500000) {
|
|
178 |
println(s"$i ${time_needed(2, matcher(EVIL, "a" * i))} secs.")
|
|
179 |
}
|
221
|
180 |
|
229
|
181 |
// another "power" test case
|
433
|
182 |
println(simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(100).next()) == ONE)
|
221
|
183 |
|
|
184 |
// the Iterator produces the rexp
|
|
185 |
//
|
|
186 |
// SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
|
|
187 |
//
|
433
|
188 |
// where SEQ is nested 100 times.
|
|
189 |
*/
|
300
|
190 |
|
228
|
191 |
|
455
|
192 |
assert(simp(ZERO | ONE) == ONE)
|
|
193 |
assert(simp(STAR(ZERO | ONE)) == STAR(ZERO | ONE))
|
|
194 |
assert(simp(ONE ~ (ONE ~ (ONE ~ CHAR('a')))) == CHAR('a'))
|
|
195 |
assert(simp(((ONE ~ ONE) ~ ONE) ~ CHAR('a')) == CHAR('a'))
|
|
196 |
assert(simp(((ONE | ONE) ~ ONE) ~ CHAR('a')) == CHAR('a'))
|
|
197 |
assert(simp(ONE ~ (ONE ~ (ONE ~ ZERO))) == ZERO)
|
|
198 |
assert(simp(ALT(ONE ~ (ONE ~ (ONE ~ ZERO)), CHAR('a'))) == CHAR('a'))
|
|
199 |
assert(simp(CHAR('a') | CHAR('a')) == CHAR('a'))
|
|
200 |
assert(simp(CHAR('a') ~ CHAR('a')) == CHAR('a') ~ CHAR('a'))
|
|
201 |
assert(simp(ONE | CHAR('a')) == (ONE | CHAR('a')))
|
|
202 |
assert(simp(ALT((CHAR('a') | ZERO) ~ ONE,
|
|
203 |
((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO))) == CHAR('a'))
|
|
204 |
assert(simp((ZERO | ((ZERO | ZERO) | (ZERO | ZERO))) ~ ((ONE | ZERO) | ONE ) ~ (CHAR('a'))) == ZERO)
|
|
205 |
assert(simp(ALT(ONE | ONE, ONE | ONE)) == ONE)
|
|
206 |
assert(simp(ALT(ZERO | CHAR('a'), CHAR('a') | ZERO)) == CHAR('a'))
|
|
207 |
assert(simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE)) == ALT(ONE, CHAR('a')))
|
|
208 |
assert(simp(ALTs(Nil)) == ZERO)
|
|
209 |
assert(simp(SEQs(List(CHAR('a')))) == CHAR('a'))
|
|
210 |
|
|
211 |
|
300
|
212 |
}
|
433
|
213 |
|