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