| 219 |      1 | // Part 1 about Regular Expression Matching
 | 
|  |      2 | //==========================================
 | 
|  |      3 | 
 | 
| 220 |      4 | // Regular Expressions
 | 
| 219 |      5 | abstract class Rexp
 | 
|  |      6 | case object ZERO extends Rexp
 | 
|  |      7 | case object ONE extends Rexp
 | 
|  |      8 | case class CHAR(c: Char) extends Rexp
 | 
|  |      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
 | 
|  |     12 | 
 | 
|  |     13 | 
 | 
| 220 |     14 | // some convenience for typing regular expressions
 | 
| 219 |     15 | 
 | 
|  |     16 | import scala.language.implicitConversions    
 | 
|  |     17 | import scala.language.reflectiveCalls 
 | 
|  |     18 | 
 | 
|  |     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 | 
 | 
|  |     40 | // (1) Complete the function nullable according to
 | 
|  |     41 | // the definition given in the coursework; this 
 | 
|  |     42 | // function checks whether a regular expression
 | 
|  |     43 | // can match the empty string and Returns a boolean
 | 
|  |     44 | // accordingly.
 | 
|  |     45 | 
 | 
|  |     46 | //def nullable (r: Rexp) : Boolean = ...
 | 
|  |     47 | 
 | 
|  |     48 | 
 | 
|  |     49 | // (2) Complete the function der according to
 | 
|  |     50 | // the definition given in the coursework; this
 | 
|  |     51 | // function calculates the derivative of a 
 | 
|  |     52 | // regular expression w.r.t. a character.
 | 
|  |     53 | 
 | 
|  |     54 | //def der (c: Char, r: Rexp) : Rexp = ...
 | 
|  |     55 | 
 | 
|  |     56 | 
 | 
|  |     57 | // (3) Complete the simp function according to
 | 
|  |     58 | // the specification given in the coursework; this
 | 
|  |     59 | // function simplifies a regular expression from
 | 
|  |     60 | // the inside out, like you would simplify arithmetic 
 | 
|  |     61 | // expressions; however it does not simplify inside 
 | 
|  |     62 | // STAR-regular expressions.
 | 
|  |     63 | 
 | 
|  |     64 | //def simp(r: Rexp) : Rexp = ... 
 | 
|  |     65 | 
 | 
|  |     66 | 
 | 
|  |     67 | // (4) Complete the two functions below; the first 
 | 
|  |     68 | // calculates the derivative w.r.t. a string; the second
 | 
|  |     69 | // is the regular expression matcher taking a regular
 | 
|  |     70 | // expression and a string and checks whether the
 | 
|  |     71 | // string matches the regular expression
 | 
|  |     72 | 
 | 
|  |     73 | //def ders (s: List[Char], r: Rexp) : Rexp = ... 
 | 
|  |     74 | 
 | 
|  |     75 | //def matcher(r: Rexp, s: String): Boolean = ...
 | 
|  |     76 | 
 | 
|  |     77 | 
 | 
|  |     78 | // (5) Complete the size function for regular
 | 
|  |     79 | // expressions according to the specification 
 | 
|  |     80 | // given in the coursework.
 | 
|  |     81 | 
 | 
|  |     82 | //def size(r: Rexp): Int = ...
 | 
|  |     83 | 
 | 
|  |     84 | 
 | 
|  |     85 | // some testing data
 | 
|  |     86 | 
 | 
|  |     87 | /*
 | 
|  |     88 | matcher(("a" ~ "b") ~ "c", "abc")  // => true
 | 
|  |     89 | matcher(("a" ~ "b") ~ "c", "ab")   // => false
 | 
|  |     90 | 
 | 
|  |     91 | // the supposedly 'evil' regular expression (a*)* b
 | 
|  |     92 | val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
 | 
|  |     93 | 
 | 
|  |     94 | matcher(EVIL, "a" * 1000 ++ "b")   // => true
 | 
|  |     95 | matcher(EVIL, "a" * 1000)          // => false
 | 
|  |     96 | 
 | 
|  |     97 | // size without simplifications
 | 
|  |     98 | size(der('a', der('a', EVIL)))             // => 28
 | 
|  |     99 | size(der('a', der('a', der('a', EVIL))))   // => 58
 | 
|  |    100 | 
 | 
|  |    101 | // size with simplification
 | 
|  |    102 | size(simp(der('a', der('a', EVIL))))           // => 8
 | 
|  |    103 | size(simp(der('a', der('a', der('a', EVIL))))) // => 8
 | 
|  |    104 | 
 | 
| 220 |    105 | // Python needs around 30 seconds for matching 28 a's with EVIL. 
 | 
|  |    106 | // Java 9 and later increase this to an "astonishing" 40000 a's in
 | 
|  |    107 | // 30 seconds.
 | 
| 219 |    108 | //
 | 
|  |    109 | // Lets see how long it really takes to match strings with 
 | 
| 220 |    110 | // 5 Million a's...it should be in the range of a couple
 | 
|  |    111 | // of seconds.
 | 
| 219 |    112 | 
 | 
|  |    113 | def time_needed[T](i: Int, code: => T) = {
 | 
|  |    114 |   val start = System.nanoTime()
 | 
|  |    115 |   for (j <- 1 to i) code
 | 
|  |    116 |   val end = System.nanoTime()
 | 
|  |    117 |   (end - start)/(i * 1.0e9)
 | 
|  |    118 | }
 | 
|  |    119 | 
 | 
|  |    120 | for (i <- 0 to 5000000 by 500000) {
 | 
|  |    121 |   println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i))))
 | 
|  |    122 | }
 | 
|  |    123 | 
 | 
| 220 |    124 | // another "power" test case 
 | 
|  |    125 | simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(50).next) == ONE
 | 
|  |    126 | 
 | 
|  |    127 | // the Iterator produces the rexp
 | 
|  |    128 | //
 | 
|  |    129 | //      SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE)
 | 
|  |    130 | //
 | 
|  |    131 | //    where SEQ is nested 50 times.
 | 
|  |    132 | 
 | 
| 219 |    133 | */
 | 
|  |    134 | 
 |