progs/re3.scala
changeset 93 4794759139ea
parent 92 e85600529ca5
child 119 a6684e8961d0
equal deleted inserted replaced
92:e85600529ca5 93:4794759139ea
       
     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 = if (n == 0) EMPTY else 
       
    36     r.simp match {
       
    37       case NULL => NULL
       
    38       case EMPTY => EMPTY
       
    39       case r => NTIMES(r, n)
       
    40     }
       
    41 }
       
    42 
       
    43 // some convenience for typing in regular expressions
       
    44 def charlist2rexp(s : List[Char]) : Rexp = s match {
       
    45   case Nil => EMPTY
       
    46   case c::Nil => CHAR(c)
       
    47   case c::s => SEQ(CHAR(c), charlist2rexp(s))
       
    48 }
       
    49 implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
       
    50 
       
    51 
       
    52 // nullable function: tests whether the regular 
       
    53 // expression can recognise the empty string
       
    54 def nullable (r: Rexp) : Boolean = r match {
       
    55   case NULL => false
       
    56   case EMPTY => true
       
    57   case CHAR(_) => false
       
    58   case ALT(r1, r2) => nullable(r1) || nullable(r2)
       
    59   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
       
    60   case STAR(_) => true
       
    61   case NTIMES(r, i) => if (i == 0) true else nullable(r)
       
    62 }
       
    63 
       
    64 // derivative of a regular expression w.r.t. a character
       
    65 def der (c: Char, r: Rexp) : Rexp = r match {
       
    66   case NULL => NULL
       
    67   case EMPTY => NULL
       
    68   case CHAR(d) => if (c == d) EMPTY else NULL
       
    69   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
       
    70   case SEQ(r1, r2) => 
       
    71     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
       
    72     else SEQ(der(c, r1), r2)
       
    73   case STAR(r) => SEQ(der(c, r), STAR(r))
       
    74   case NTIMES(r, i) => 
       
    75     if (i == 0) NULL else SEQ(der(c, r), NTIMES(r, i - 1))
       
    76 }
       
    77 
       
    78 // derivative w.r.t. a string (iterates der)
       
    79 def ders (s: List[Char], r: Rexp) : Rexp = s match {
       
    80   case Nil => r
       
    81   case c::s => ders(s, der(c, r).simp)
       
    82 }
       
    83 
       
    84 // main matcher function
       
    85 def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
       
    86 
       
    87 
       
    88 
       
    89 //one or zero
       
    90 def OPT(r: Rexp) = ALT(r, EMPTY)
       
    91 
       
    92 def RTEST(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n))
       
    93 
       
    94 def time_needed[T](i: Int, code: => T) = {
       
    95   val start = System.nanoTime()
       
    96   for (j <- 1 to i) code
       
    97   val end = System.nanoTime()
       
    98   (end - start)/(i * 1.0e9)
       
    99 }
       
   100 
       
   101 
       
   102 for (i <- 1 to 11001 by 500) {
       
   103   println(i + " " + + " " + time_needed(1, matcher(RTEST(i), "a" * i)))
       
   104 }
       
   105 
       
   106