progs/re5.scala
changeset 414 065ca01b62ae
parent 412 1cef3924f7a2
child 415 4ae59fd3b174
equal deleted inserted replaced
413:b7221df9662a 414:065ca01b62ae
       
     1 import scala.annotation.tailrec    
       
     2 import scala.language.implicitConversions
       
     3     
       
     4 abstract class Rexp {
       
     5   def simp : Rexp = this
       
     6 }
       
     7 
       
     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 {
       
    12   override def simp = (r1.simp, r2.simp) match {
       
    13     case (ZERO, r) => r
       
    14     case (r, ZERO) => r
       
    15     case (r, ONE) => if (nullable(r)) r else ALT(r, ONE)
       
    16     case (ONE, r) => if (nullable(r)) r else ALT(r, ONE)
       
    17     case (r1, r2) => if (r1 == r2) r1 else ALT(r1, r2)
       
    18   }
       
    19 }
       
    20 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp {
       
    21   override def simp = (r1.simp, r2.simp) match {
       
    22     case (ZERO, _) => ZERO
       
    23     case (_, ZERO) => ZERO
       
    24     case (ONE, r) => r
       
    25     case (r, ONE) => r
       
    26     case (r1, r2) => SEQ(r1, r2)
       
    27   }
       
    28 }
       
    29 case class STAR(r: Rexp) extends Rexp {
       
    30   override def simp = r.simp match {
       
    31     case ZERO => ONE
       
    32     case ONE => ONE
       
    33     case r => STAR(r)
       
    34   }
       
    35 }
       
    36 case class NTIMES(r: Rexp, n: Int) extends Rexp {
       
    37   override def simp = if (n == 0) ONE else 
       
    38     r.simp match {
       
    39       case ZERO => ZERO
       
    40       case ONE => ONE
       
    41       case r => NTIMES(r, n)
       
    42     }
       
    43 }
       
    44 
       
    45 // some convenience for typing in regular expressions
       
    46 def charlist2rexp(s : List[Char]) : Rexp = s match {
       
    47   case Nil => ONE
       
    48   case c::Nil => CHAR(c)
       
    49   case c::s => SEQ(CHAR(c), charlist2rexp(s))
       
    50 }
       
    51 implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
       
    52 
       
    53 
       
    54 // nullable function: tests whether the regular 
       
    55 // expression can recognise the empty string
       
    56 def nullable (r: Rexp) : Boolean = r match {
       
    57   case ZERO => false
       
    58   case ONE => true
       
    59   case CHAR(_) => false
       
    60   case ALT(r1, r2) => nullable(r1) || nullable(r2)
       
    61   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
       
    62   case STAR(_) => true
       
    63   case NTIMES(r, i) => if (i == 0) true else nullable(r)
       
    64 }
       
    65 
       
    66 // derivative of a regular expression w.r.t. a character
       
    67 def der (c: Char, r: Rexp) : Rexp = r match {
       
    68   case ZERO => ZERO
       
    69   case ONE => ZERO
       
    70   case CHAR(d) => if (c == d) ONE else ZERO
       
    71   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
       
    72   case SEQ(r1, r2) => 
       
    73     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
       
    74     else SEQ(der(c, r1), r2)
       
    75   case STAR(r) => SEQ(der(c, r), STAR(r))
       
    76   case NTIMES(r, i) => 
       
    77     if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1))
       
    78 }
       
    79 
       
    80 // derivative w.r.t. a string (iterates der)
       
    81 def ders2(s: List[Char], r: Rexp) : Rexp = (s, r) match {
       
    82   case (Nil, r) => r
       
    83   case (s, ZERO) => ZERO
       
    84   case (s, ONE) => if (s == Nil) ONE else ZERO
       
    85   case (s, CHAR(c)) => if (s == List(c)) ONE else 
       
    86                        if (s == Nil) CHAR(c) else ZERO
       
    87   case (s, ALT(r1, r2)) => ALT(ders2(s, r2), ders2(s, r2))
       
    88   case (c::s, r) => ders2(s, der(c, r).simp)
       
    89 }
       
    90 
       
    91 // main matcher function
       
    92 def matcher(r: Rexp, s: String) : Boolean = nullable(ders2(s.toList, r))
       
    93 
       
    94 
       
    95 //one or zero
       
    96 def OPT(r: Rexp) = ALT(r, ONE)
       
    97 
       
    98 def EVIL1(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n))
       
    99 val EVIL2 = SEQ(STAR("a"), "b")
       
   100 
       
   101 def time_needed[T](i: Int, code: => T) = {
       
   102   val start = System.nanoTime()
       
   103   for (j <- 1 to i) code
       
   104   val end = System.nanoTime()
       
   105   (end - start)/(i * 1.0e9)
       
   106 }
       
   107 
       
   108 val i = 5000
       
   109 println(i + " " + "%.5f".format(time_needed(10, matcher(EVIL1(i), "a" * i))))
       
   110 
       
   111 for (i <- 1 to 80000 by 10000) {
       
   112   println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i))))
       
   113 }
       
   114 
       
   115 for (i <- 1 to 10000001 by 1000000) {
       
   116   println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i))))
       
   117 }
       
   118