templates3/template3/re.scala
changeset 164 7ada03135b2e
parent 163 84917f2e16cd
equal deleted inserted replaced
163:84917f2e16cd 164:7ada03135b2e
       
     1 // Part 1 about Regular Expression Matching
       
     2 //==========================================
       
     3 
       
     4 
       
     5 object CW8a {
       
     6 
       
     7 abstract class Rexp
       
     8 case object ZERO extends Rexp
       
     9 case object ONE extends Rexp
       
    10 case class CHAR(c: Char) extends Rexp
       
    11 case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative 
       
    12 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence
       
    13 case class STAR(r: Rexp) extends Rexp             // star
       
    14 
       
    15 
       
    16 // some convenience for typing in regular expressions
       
    17 
       
    18 import scala.language.implicitConversions    
       
    19 import scala.language.reflectiveCalls 
       
    20 
       
    21 def charlist2rexp(s: List[Char]): Rexp = s match {
       
    22   case Nil => ONE
       
    23   case c::Nil => CHAR(c)
       
    24   case c::s => SEQ(CHAR(c), charlist2rexp(s))
       
    25 }
       
    26 implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
       
    27 
       
    28 implicit def RexpOps (r: Rexp) = new {
       
    29   def | (s: Rexp) = ALT(r, s)
       
    30   def % = STAR(r)
       
    31   def ~ (s: Rexp) = SEQ(r, s)
       
    32 }
       
    33 
       
    34 implicit def stringOps (s: String) = new {
       
    35   def | (r: Rexp) = ALT(s, r)
       
    36   def | (r: String) = ALT(s, r)
       
    37   def % = STAR(s)
       
    38   def ~ (r: Rexp) = SEQ(s, r)
       
    39   def ~ (r: String) = SEQ(s, r)
       
    40 }
       
    41 
       
    42 // (1a) Complete the function nullable according to
       
    43 // the definition given in the coursework; this 
       
    44 // function checks whether a regular expression
       
    45 // can match the empty string and Returns a boolean
       
    46 // accordingly.
       
    47 
       
    48 //def nullable (r: Rexp) : Boolean = ...
       
    49 
       
    50 
       
    51 // (1b) Complete the function der according to
       
    52 // the definition given in the coursework; this
       
    53 // function calculates the derivative of a 
       
    54 // regular expression w.r.t. a character.
       
    55 
       
    56 //def der (c: Char, r: Rexp) : Rexp = ...
       
    57 
       
    58 
       
    59 // (1c) Complete the simp function according to
       
    60 // the specification given in the coursework; this
       
    61 // function simplifies a regular expression from
       
    62 // the inside out, like you would simplify arithmetic 
       
    63 // expressions; however it does not simplify inside 
       
    64 // STAR-regular expressions.
       
    65 
       
    66 //def simp(r: Rexp) : Rexp = ... 
       
    67 
       
    68 
       
    69 // (1d) Complete the two functions below; the first 
       
    70 // calculates the derivative w.r.t. a string; the second
       
    71 // is the regular expression matcher taking a regular
       
    72 // expression and a string and checks whether the
       
    73 // string matches the regular expression
       
    74 
       
    75 //def ders (s: List[Char], r: Rexp) : Rexp = ... 
       
    76 
       
    77 //def matcher(r: Rexp, s: String): Boolean = ...
       
    78 
       
    79 
       
    80 // (1e) Complete the size function for regular
       
    81 // expressions according to the specification 
       
    82 // given in the coursework.
       
    83 
       
    84 //def size(r: Rexp): Int = ...
       
    85 
       
    86 
       
    87 // some testing data
       
    88 
       
    89 /*
       
    90 matcher(("a" ~ "b") ~ "c", "abc")  // => true
       
    91 matcher(("a" ~ "b") ~ "c", "ab")   // => false
       
    92 
       
    93 // the supposedly 'evil' regular expression (a*)* b
       
    94 val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
       
    95 
       
    96 matcher(EVIL, "a" * 1000 ++ "b")   // => true
       
    97 matcher(EVIL, "a" * 1000)          // => false
       
    98 
       
    99 // size without simplifications
       
   100 size(der('a', der('a', EVIL)))             // => 28
       
   101 size(der('a', der('a', der('a', EVIL))))   // => 58
       
   102 
       
   103 // size with simplification
       
   104 size(simp(der('a', der('a', EVIL))))           // => 8
       
   105 size(simp(der('a', der('a', der('a', EVIL))))) // => 8
       
   106 
       
   107 // Java needs around 30 seconds for matching 28 a's with EVIL. 
       
   108 //
       
   109 // Lets see how long it takes to match strings with 
       
   110 // 0.5 Million a's...it should be in the range of some
       
   111 // seconds.
       
   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 
       
   124 */
       
   125 
       
   126 
       
   127 }