progs/re.scala
changeset 4 f31c22f4f104
child 62 2151c77e1e24
equal deleted inserted replaced
3:575a5c07c7fe 4:f31c22f4f104
       
     1 
       
     2 abstract class Rexp
       
     3 case object ZERO extends Rexp
       
     4 case object ONE extends Rexp
       
     5 case class CHAR(c: Char) extends Rexp
       
     6 case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
       
     7 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
       
     8 case class STAR(r: Rexp) extends Rexp 
       
     9 case class NTIMES(r: Rexp, n: Int) extends Rexp 
       
    10 case class UPNTIMES(r: Rexp, n: Int) extends Rexp 
       
    11 
       
    12 // nullable function: tests whether the regular 
       
    13 // expression can recognise the empty string
       
    14 def nullable (r: Rexp) : Boolean = r match {
       
    15   case ZERO => false
       
    16   case ONE => true
       
    17   case CHAR(_) => false
       
    18   case ALT(r1, r2) => nullable(r1) || nullable(r2)
       
    19   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
       
    20   case STAR(_) => true
       
    21   case NTIMES(r, i) => if (i == 0) true else nullable(r)
       
    22   case UPNTIMES(r: Rexp, n: Int) => true
       
    23 }
       
    24 
       
    25 // derivative of a regular expression w.r.t. a character
       
    26 def der (c: Char, r: Rexp) : Rexp = r match {
       
    27   case ZERO => ZERO
       
    28   case ONE => ZERO
       
    29   case CHAR(d) => if (c == d) ONE else ZERO
       
    30   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
       
    31   case SEQ(r1, r2) => 
       
    32     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
       
    33     else SEQ(der(c, r1), r2)
       
    34   case STAR(r1) => SEQ(der(c, r1), STAR(r1))
       
    35   case NTIMES(r1, i) => 
       
    36     if (i == 0) ZERO else
       
    37     if (nullable(r1)) SEQ(der(c, r1), UPNTIMES(r1, i - 1))
       
    38     else SEQ(der(c, r1), NTIMES(r1, i - 1))
       
    39   case UPNTIMES(r1, i) => 
       
    40     if (i == 0) ZERO
       
    41     else SEQ(der(c, r1), UPNTIMES(r1, i - 1)) 
       
    42 }
       
    43 
       
    44 def simp(r: Rexp) : Rexp = r match {
       
    45   case ALT(r1, r2) => (simp(r1), simp(r2)) match {
       
    46     case (ZERO, r2s) => r2s
       
    47     case (r1s, ZERO) => r1s
       
    48     case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
       
    49   }
       
    50   case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
       
    51     case (ZERO, _) => ZERO
       
    52     case (_, ZERO) => ZERO
       
    53     case (ONE, r2s) => r2s
       
    54     case (r1s, ONE) => r1s
       
    55     case (r1s, r2s) => SEQ(r1s, r2s)
       
    56   }
       
    57   case r => r
       
    58 }
       
    59 
       
    60 
       
    61 // derivative w.r.t. a string (iterates der)
       
    62 def ders (s: List[Char], r: Rexp) : Rexp = s match {
       
    63   case Nil => r
       
    64   case c::s => ders(s, simp(der(c, r)))
       
    65 }
       
    66 
       
    67 
       
    68 // main matcher function
       
    69 def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
       
    70 
       
    71 //one or zero
       
    72 def OPT(r: Rexp) = ALT(r, ONE)
       
    73 
       
    74 //evil regular expressions
       
    75 def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
       
    76 val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
       
    77 
       
    78 
       
    79 def time_needed[T](i: Int, code: => T) = {
       
    80   val start = System.nanoTime()
       
    81   for (j <- 1 to i) code
       
    82   val end = System.nanoTime()
       
    83   (end - start)/(i * 1.0e9)
       
    84 }
       
    85 
       
    86 
       
    87 //test: (a?{n}) (a{n})
       
    88 for (i <- 1 to 8001 by 1000) {
       
    89   println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i))))
       
    90 }
       
    91 
       
    92 for (i <- 1 to 8001 by 1000) {
       
    93   println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i))))
       
    94 }
       
    95 
       
    96 //test: (a*)* b
       
    97 for (i <- 1 to 7000001 by 500000) {
       
    98   println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i))))
       
    99 }
       
   100 
       
   101 for (i <- 1 to 7000001 by 500000) {
       
   102   println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i))))
       
   103 }
       
   104