re3.scala
changeset 49 d2c6852ca8da
child 59 b64e876832cc
equal deleted inserted replaced
48:ddd357703b6c 49:d2c6852ca8da
       
     1     
       
     2 abstract class Rexp {
       
     3   def simp : Rexp = this
       
     4 }
       
     5 
       
     6 case object NULL extends Rexp
       
     7 case object EMPTY extends Rexp
       
     8 case class CHAR(c: Char) extends Rexp
       
     9 case class ALT(r1: Rexp, r2: Rexp) extends Rexp {
       
    10   override def simp = (r1.simp, r2.simp) match {
       
    11     case (NULL, r) => r
       
    12     case (r, NULL) => r
       
    13     case (r, EMPTY) => if (nullable(r)) r else ALT(r, EMPTY)
       
    14     case (EMPTY, r) => if (nullable(r)) r else ALT(r, EMPTY)
       
    15     case (r1, r2) => if (r1 == r2) r1 else ALT(r1, r2)
       
    16   }
       
    17 }
       
    18 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp {
       
    19   override def simp = (r1.simp, r2.simp) match {
       
    20     case (NULL, _) => NULL
       
    21     case (_, NULL) => NULL
       
    22     case (EMPTY, r) => r
       
    23     case (r, EMPTY) => r
       
    24     case (r1, r2) => SEQ(r1, r2)
       
    25   }
       
    26 }
       
    27 case class STAR(r: Rexp) extends Rexp {
       
    28   override def simp = r.simp match {
       
    29     case NULL => EMPTY
       
    30     case EMPTY => EMPTY
       
    31     case r => STAR(r)
       
    32   }
       
    33 }
       
    34 case class NTIMES(r: Rexp, n: Int) extends Rexp {
       
    35   override def simp = r.simp match {
       
    36     case NULL => NULL
       
    37     case EMPTY => EMPTY
       
    38     case r => NTIMES(r, n)
       
    39   }
       
    40 }
       
    41 
       
    42 // some convenience for typing in regular expressions
       
    43 def charlist2rexp(s : List[Char]) : Rexp = s match {
       
    44   case Nil => EMPTY
       
    45   case c::Nil => CHAR(c)
       
    46   case c::s => SEQ(CHAR(c), charlist2rexp(s))
       
    47 }
       
    48 implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
       
    49 
       
    50 
       
    51 // nullable function: tests whether the regular 
       
    52 // expression can recognise the empty string
       
    53 def nullable (r: Rexp) : Boolean = r match {
       
    54   case NULL => false
       
    55   case EMPTY => true
       
    56   case CHAR(_) => false
       
    57   case ALT(r1, r2) => nullable(r1) || nullable(r2)
       
    58   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
       
    59   case STAR(_) => true
       
    60   case NTIMES(r, i) => if (i == 0) false else nullable(r)
       
    61 }
       
    62 
       
    63 // derivative of a regular expression w.r.t. a character
       
    64 def der (c: Char, r: Rexp) : Rexp = r match {
       
    65   case NULL => NULL
       
    66   case EMPTY => NULL
       
    67   case CHAR(d) => if (c == d) EMPTY else NULL
       
    68   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
       
    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(r) => SEQ(der(c, r), STAR(r))
       
    73   case NTIMES(r, i) => 
       
    74     if (i == 0) NULL else SEQ(der(c, r), NTIMES(r, i - 1))
       
    75 }
       
    76 
       
    77 // derivative w.r.t. a string (iterates der)
       
    78 def ders (s: List[Char], r: Rexp) : Rexp = s match {
       
    79   case Nil => r
       
    80   case c::s => ders(s, der(c, r).simp)
       
    81 }
       
    82 
       
    83 // main matcher function
       
    84 def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
       
    85 
       
    86 
       
    87 
       
    88 //one or zero
       
    89 def OPT(r: Rexp) = ALT(r, EMPTY)
       
    90 
       
    91 //n-times
       
    92 /*def NTIMES(r: Rexp, n: Int) : Rexp = n match {
       
    93   case 0 => NULL
       
    94   case 1 => r
       
    95   case n => SEQ(r, NTIMES(r, n - 1))
       
    96 }*/
       
    97 
       
    98 def RTEST(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n))
       
    99 
       
   100 def time_needed[T](i: Int, code: => T) = {
       
   101   val start = System.nanoTime()
       
   102   for (j <- 1 to i) code
       
   103   val end = System.nanoTime()
       
   104   (end - start)/(i * 1.0e9)
       
   105 }
       
   106 
       
   107 
       
   108 for (i <- 1 to 10001 by 500) {
       
   109   println(i + " " + time_needed(1, matcher(RTEST(i), "a" * i)))
       
   110 }
       
   111 
       
   112