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