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