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