| 417 |      1 | // Main Part 3 about Regular Expression Matching
 | 
| 430 |      2 | //==============================================
 | 
| 153 |      3 | 
 | 
| 403 |      4 | object M3 {
 | 
| 249 |      5 | 
 | 
| 153 |      6 | abstract class Rexp
 | 
|  |      7 | case object ZERO extends Rexp
 | 
|  |      8 | case object ONE extends Rexp
 | 
|  |      9 | case class CHAR(c: Char) extends Rexp
 | 
| 430 |     10 | case class ALTs(rs: List[Rexp]) extends Rexp  // alternatives 
 | 
|  |     11 | case class SEQs(rs: List[Rexp]) extends Rexp  // sequences
 | 
|  |     12 | case class STAR(r: Rexp) extends Rexp         // star
 | 
| 153 |     13 | 
 | 
|  |     14 | 
 | 
| 430 |     15 | //the usual binary choice and binary sequence can be defined 
 | 
|  |     16 | //in terms of ALTs and SEQs
 | 
| 403 |     17 | def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
 | 
| 430 |     18 | def SEQ(r1: Rexp, r2: Rexp) = SEQs(List(r1, r2))
 | 
| 403 |     19 | 
 | 
| 460 |     20 | 
 | 
|  |     21 | // some convenience for typing 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 | 
 | 
| 460 |     46 | // examples for the implicits:
 | 
|  |     47 | // ALT(CHAR('a'), CHAR('b'))
 | 
|  |     48 | // val areg : Rexp = "a" | "b"
 | 
|  |     49 | 
 | 
|  |     50 | // SEQ(CHAR('a'), CHAR('b')) 
 | 
|  |     51 | // val sreg : Rexp = "a" ~ "b"
 | 
|  |     52 | 
 | 
|  |     53 | 
 | 
|  |     54 | // ADD YOUR CODE BELOW
 | 
|  |     55 | //======================
 | 
|  |     56 | 
 | 
|  |     57 | // (1)
 | 
| 347 |     58 | def nullable (r: Rexp) : Boolean = r match {
 | 
| 460 |     59 |   case ZERO     => false
 | 
|  |     60 |   case ONE      => true
 | 
|  |     61 |   case CHAR(_)  => false
 | 
|  |     62 |   case ALTs(rs) => (for(reg <- rs) yield nullable(reg)).exists(_ == true)
 | 
|  |     63 |   case SEQs(rs) => (for(reg <- rs) yield nullable(reg)).forall(_ == true)
 | 
|  |     64 |   case STAR(_)  => true
 | 
| 153 |     65 | }
 | 
|  |     66 | 
 | 
| 460 |     67 | /*
 | 
|  |     68 | nullable(ZERO) == false
 | 
|  |     69 | nullable(ONE) == true
 | 
|  |     70 | nullable(CHAR('a')) == false
 | 
|  |     71 | nullable(ZERO | ONE) == true
 | 
|  |     72 | nullable(ZERO | CHAR('a')) == false
 | 
|  |     73 | nullable(ONE ~ ONE) == true
 | 
|  |     74 | nullable(ONE ~ CHAR('a')) == false
 | 
|  |     75 | nullable(STAR(ZERO)) == true
 | 
|  |     76 | nullable(ALTs(List(ONE, CHAR('a'), ZERO))) == true
 | 
|  |     77 | nullable(SEQs(List(ONE, ALTs(List(ONE, CHAR('a'), ZERO)), STAR(ZERO)))) == true
 | 
|  |     78 | */
 | 
|  |     79 | 
 | 
| 430 |     80 | // (2) 
 | 
| 460 |     81 | def der (c: Char, r: Rexp) : Rexp = r match {
 | 
|  |     82 |   case ZERO           => ZERO
 | 
|  |     83 |   case ONE            => ZERO
 | 
|  |     84 |   case CHAR(d)        => if(c == d) ONE else ZERO
 | 
|  |     85 |   case ALTs(rs)       => ALTs(for(reg <- rs) yield der(c, reg))
 | 
|  |     86 |   case SEQs(Nil)      => ZERO
 | 
|  |     87 |   case SEQs(r :: rs)  => if(nullable(r)) ALT(SEQs(der(c, r) :: rs), der(c, SEQs(rs))) else SEQs(der(c, r) :: rs)
 | 
|  |     88 |   case STAR(r)        => SEQ(der(c,r), STAR(r))
 | 
| 417 |     89 | }
 | 
|  |     90 | 
 | 
| 460 |     91 | /*
 | 
|  |     92 | der('a', ZERO | ONE) == (ZERO | ZERO)
 | 
|  |     93 | der('a', (CHAR('a') | ONE) ~ CHAR('a')) == ALT((ONE | ZERO) ~ CHAR('a'), SEQs(List(ONE)))
 | 
|  |     94 | der('a', (CHAR('a') | CHAR('a')) ~ CHAR('a')) == (ONE | ONE) ~ CHAR('a')
 | 
|  |     95 | der('a', STAR(CHAR('a'))) == (ONE ~ STAR(CHAR('a')))
 | 
|  |     96 | der('b', STAR(CHAR('a'))) == (ZERO ~ STAR(CHAR('a')))
 | 
|  |     97 | */
 | 
| 417 |     98 | 
 | 
| 430 |     99 | // (3) 
 | 
|  |    100 | def denest(rs: List[Rexp]) : List[Rexp] = rs match {
 | 
| 460 |    101 |   case Nil                => Nil
 | 
|  |    102 |   case ZERO :: rest       => denest(rest)
 | 
|  |    103 |   case ALTs(rgs) :: rest  => rgs ::: denest(rest)
 | 
|  |    104 |   case r :: rest          => r :: denest(rest)
 | 
| 430 |    105 | }
 | 
| 417 |    106 | 
 | 
| 460 |    107 | /*
 | 
|  |    108 | denest(List(ONE, ZERO, ALTs(List(ONE, CHAR('a'))))) == List(ONE, ONE, CHAR('a'))
 | 
|  |    109 | denest(List(ONE ~ ONE, ZERO, ZERO | ONE)) == List(ONE ~ ONE, ZERO, ONE)
 | 
|  |    110 | */
 | 
|  |    111 | 
 | 
| 430 |    112 | // (4)
 | 
|  |    113 | def flts(rs: List[Rexp], acc: List[Rexp] = Nil) : List[Rexp] = rs match {
 | 
| 460 |    114 |   case Nil                => acc
 | 
|  |    115 |   case ZERO :: rest       => List(ZERO)
 | 
|  |    116 |   case ONE :: rest        => flts(rest, acc)
 | 
|  |    117 |   case SEQs(rgs) :: rest  => flts(rest, acc ::: rgs)
 | 
|  |    118 |   case r :: rest          => flts(rest, acc ::: List(r)) 
 | 
| 153 |    119 | }
 | 
|  |    120 | 
 | 
| 460 |    121 | /*
 | 
|  |    122 | flts(List(CHAR('a'), ZERO, ONE), Nil) == List(ZERO)
 | 
|  |    123 | flts(List(CHAR('a'), ONE, ONE, CHAR('b')), Nil) == List(CHAR('a'), CHAR('b'))
 | 
|  |    124 | flts(List(ONE ~ CHAR('a'), CHAR('b') ~ ONE), Nil) == List(ONE, CHAR('a'), CHAR('b'), ONE)
 | 
|  |    125 | */
 | 
|  |    126 | 
 | 
| 430 |    127 | // (5)
 | 
|  |    128 | def ALTs_smart(rs: List[Rexp]) : Rexp = rs match {
 | 
| 460 |    129 |   case Nil      => ZERO
 | 
|  |    130 |   case List(r)  => r
 | 
|  |    131 |   case _        => ALTs(rs)
 | 
| 430 |    132 | }
 | 
| 403 |    133 | 
 | 
| 430 |    134 | def SEQs_smart(rs: List[Rexp]) : Rexp = rs match {
 | 
| 460 |    135 |   case Nil      => ONE
 | 
|  |    136 |   case List(r)  => r
 | 
|  |    137 |   case _        => SEQs(rs)
 | 
|  |    138 | }
 | 
|  |    139 | 
 | 
|  |    140 | /*
 | 
|  |    141 | SEQs_smart(Nil) == ONE
 | 
|  |    142 | SEQs_smart(List(ZERO)) == ZERO
 | 
|  |    143 | SEQs_smart(List(CHAR('a'))) == CHAR('a')
 | 
|  |    144 | SEQs_smart(List(ONE ~ ONE)) == ONE ~ ONE
 | 
|  |    145 | SEQs_smart(List(ONE, ONE)) == SEQs(List(ONE, ONE))
 | 
|  |    146 | ALTs_smart(Nil) == ZERO
 | 
|  |    147 | ALTs_smart(List(ONE ~ ONE)) == ONE ~ ONE
 | 
|  |    148 | ALTs_smart(List(ZERO, ZERO)) == ALTs(List(ZERO, ZERO))
 | 
|  |    149 | */
 | 
|  |    150 | 
 | 
|  |    151 | // (6)
 | 
|  |    152 | def simp(r: Rexp) : Rexp = r match {
 | 
|  |    153 |   case ALTs(rs) => ALTs_smart(denest(for(reg <- rs) yield simp(reg)).distinct)
 | 
|  |    154 |   case SEQs(rs) => SEQs_smart(flts(for(reg <- rs) yield simp(reg)))
 | 
|  |    155 |   case _        => r
 | 
| 430 |    156 | }
 | 
| 421 |    157 | 
 | 
| 460 |    158 | /*
 | 
|  |    159 | simp(ZERO | ONE) == ONE
 | 
|  |    160 | simp(STAR(ZERO | ONE)) == STAR(ZERO | ONE)
 | 
|  |    161 | simp(ONE ~ (ONE ~ (ONE ~ CHAR('a')))) == CHAR('a')
 | 
|  |    162 | simp(((ONE ~ ONE) ~ ONE) ~ CHAR('a')) == CHAR('a')
 | 
|  |    163 | simp(((ONE | ONE) ~ ONE) ~ CHAR('a')) == CHAR('a')
 | 
|  |    164 | simp(ONE ~ (ONE ~ (ONE ~ ZERO))) == ZERO
 | 
|  |    165 | simp(ALT(ONE ~ (ONE ~ (ONE ~ ZERO)), CHAR('a'))) == CHAR('a')
 | 
|  |    166 | simp(CHAR('a') | CHAR('a')) == CHAR('a')
 | 
|  |    167 | simp(CHAR('a') ~ CHAR('a')) == CHAR('a') ~ CHAR('a')
 | 
|  |    168 | simp(ONE | CHAR('a')) == (ONE | CHAR('a'))
 | 
|  |    169 | simp(ALT((CHAR('a') | ZERO) ~ ONE,((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO))) == CHAR('a')
 | 
|  |    170 | simp((ZERO | ((ZERO | ZERO) | (ZERO | ZERO))) ~ ((ONE | ZERO) | ONE ) ~ (CHAR('a'))) == ZERO
 | 
|  |    171 | simp(ALT(ONE | ONE, ONE | ONE)) == ONE
 | 
|  |    172 | simp(ALT(ZERO | CHAR('a'), CHAR('a') | ZERO)) == CHAR('a')
 | 
|  |    173 | simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE)) == ALT(ONE, CHAR('a'))
 | 
|  |    174 | simp(ALTs(Nil)) == ZERO
 | 
|  |    175 | simp(SEQs(List(CHAR('a')))) == CHAR('a')
 | 
|  |    176 | */
 | 
| 403 |    177 | 
 | 
| 460 |    178 | // (7)
 | 
|  |    179 | def ders (s: List[Char], r: Rexp) : Rexp = s match {
 | 
|  |    180 |   case Nil      => r
 | 
|  |    181 |   case c :: cs  => ders(cs, simp(der(c, r)))
 | 
|  |    182 | }
 | 
|  |    183 | def matcher(r: Rexp, s: String): Boolean = {
 | 
|  |    184 |   val derivatives = ders(s.toList, r)
 | 
|  |    185 |   nullable(derivatives)
 | 
| 153 |    186 | }
 | 
|  |    187 | 
 | 
| 460 |    188 | /*
 | 
|  |    189 | val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
 | 
|  |    190 | ders("aaaaa".toList, EVIL) == SEQs(List(STAR(CHAR('a')), STAR(STAR(CHAR('a'))), CHAR('b')))
 | 
|  |    191 | ders(List('b'), EVIL) == ONE
 | 
|  |    192 | ders("bb".toList, EVIL) == ZERO
 | 
|  |    193 | matcher(EVIL, "a" * 5 ++ "b") == true
 | 
|  |    194 | matcher(EVIL, "b") == true
 | 
|  |    195 | matcher(EVIL, "bb") == false
 | 
|  |    196 | matcher("abc", "abc") == true
 | 
|  |    197 | matcher(("ab" | "a") ~ (ONE | "bc"), "abc") == true
 | 
|  |    198 | matcher(ONE, "") == true
 | 
|  |    199 | matcher(ZERO, "") == false
 | 
|  |    200 | matcher(ONE | CHAR('a'), "") == true
 | 
|  |    201 | matcher(ONE | CHAR('a'), "a") == true
 | 
|  |    202 | */
 | 
|  |    203 | 
 | 
|  |    204 | // (8) 
 | 
|  |    205 | def size(r: Rexp): Int = r match {
 | 
|  |    206 |   case ZERO     => 1
 | 
|  |    207 |   case ONE      => 1
 | 
|  |    208 |   case CHAR(_)  => 1
 | 
|  |    209 |   case ALTs(rs) => 1 + (for(reg <- rs) yield size(reg)).sum
 | 
|  |    210 |   case SEQs(rs) => 1 + (for(reg <- rs) yield size(reg)).sum
 | 
|  |    211 |   case STAR(r)  => 1 + size(r)
 | 
|  |    212 | }
 | 
|  |    213 | 
 | 
|  |    214 | /*
 | 
|  |    215 | val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
 | 
|  |    216 | size(der('a', der('a', EVIL))) == 36
 | 
|  |    217 | size(der('a', der('a', der('a', EVIL)))) == 83
 | 
|  |    218 | size(ders("aaaaaa".toList, EVIL)) == 7
 | 
|  |    219 | size(ders(("a" * 50).toList, EVIL)) == 7
 | 
|  |    220 | */
 | 
| 221 |    221 | 
 | 
| 430 |    222 | 
 | 
| 460 |    223 | // Some testing data
 | 
|  |    224 | //===================
 | 
|  |    225 | /*
 | 
| 421 |    226 | 
 | 
| 460 |    227 | simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE))   // => ALTs(List(ONE, CHAR(a)))
 | 
|  |    228 | simp(((CHAR('a') | ZERO) ~ ONE) | (((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO)))   // => CHAR(a)
 | 
| 153 |    229 | 
 | 
| 460 |    230 | matcher(("a" ~ "b") ~ "c", "ab")   // => false
 | 
|  |    231 | matcher(("a" ~ "b") ~ "c", "abc")  // => true
 | 
| 421 |    232 | 
 | 
| 229 |    233 | 
 | 
|  |    234 | // the supposedly 'evil' regular expression (a*)* b
 | 
| 430 |    235 | val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
 | 
| 229 |    236 | 
 | 
| 460 |    237 | matcher(EVIL, "a" * 1000)          // => false
 | 
|  |    238 | matcher(EVIL, "a" * 1000 ++ "b")   // => true
 | 
|  |    239 | 
 | 
| 153 |    240 | 
 | 
|  |    241 | // size without simplifications
 | 
| 460 |    242 | size(der('a', der('a', EVIL)))             // => 36
 | 
|  |    243 | size(der('a', der('a', der('a', EVIL))))   // => 83
 | 
| 153 |    244 | 
 | 
|  |    245 | // size with simplification
 | 
| 460 |    246 | size(simp(der('a', der('a', EVIL))))           // => 7
 | 
|  |    247 | size(simp(der('a', der('a', der('a', EVIL))))) // => 7
 | 
| 228 |    248 | 
 | 
| 229 |    249 | // Python needs around 30 seconds for matching 28 a's with EVIL. 
 | 
| 221 |    250 | // Java 9 and later increase this to an "astonishing" 40000 a's in
 | 
| 460 |    251 | // 30 seconds.
 | 
| 153 |    252 | //
 | 
| 460 |    253 | // Lets see how long it really takes to match strings with 
 | 
|  |    254 | // 5 Million a's...it should be in the range of a few
 | 
|  |    255 | // of seconds.
 | 
| 153 |    256 | 
 | 
| 421 |    257 | def time_needed[T](i: Int, code: => T) = {
 | 
|  |    258 |   val start = System.nanoTime()
 | 
|  |    259 |   for (j <- 1 to i) code
 | 
|  |    260 |   val end = System.nanoTime()
 | 
|  |    261 |   "%.5f".format((end - start)/(i * 1.0e9))
 | 
|  |    262 | }
 | 
| 153 |    263 | 
 | 
| 430 |    264 | for (i <- 0 to 5000000 by 500000) {
 | 
|  |    265 |   println(s"$i ${time_needed(2, matcher(EVIL, "a" * i))} secs.") 
 | 
|  |    266 | }
 | 
| 221 |    267 | 
 | 
| 229 |    268 | // another "power" test case 
 | 
| 460 |    269 | simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(50).next()) == ONE
 | 
| 221 |    270 | 
 | 
|  |    271 | // the Iterator produces the rexp
 | 
|  |    272 | //
 | 
|  |    273 | //      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
 | 
|  |    274 | //
 | 
| 460 |    275 | //    where SEQ is nested 50 times.
 | 
| 228 |    276 | 
 | 
| 460 |    277 | */
 | 
| 452 |    278 | 
 | 
| 300 |    279 | }
 | 
| 430 |    280 | 
 | 
| 460 |    281 | 
 | 
|  |    282 | 
 | 
|  |    283 | 
 | 
|  |    284 | 
 | 
|  |    285 | 
 | 
|  |    286 | // This template code is subject to copyright 
 | 
|  |    287 | // by King's College London, 2022. Do not 
 | 
|  |    288 | // make the template code public in any shape 
 | 
|  |    289 | // or form, and do not exchange it with other 
 | 
|  |    290 | // students under any circumstance.
 |