progs/re3a.scala
changeset 742 b5b5583a3a08
parent 741 e66bd5c563eb
child 743 6acabeecdf75
equal deleted inserted replaced
741:e66bd5c563eb 742:b5b5583a3a08
     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 // the 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 // the 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 // the 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 // the main matcher function
       
    69 def matches(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 val EVIL3 = SEQ(STAR(ALT(CHAR('a'), SEQ(CHAR('a'),CHAR('a')))), CHAR('b'))
       
    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   "%.5f".format((end - start) / (i * 1.0e9))
       
    84 }
       
    85 
       
    86 
       
    87 // test: (a?{n}) (a{n})
       
    88 for (i <- 0 to 8000 by 1000) {
       
    89   println(s"$i: ${time_needed(2, matches(EVIL1(i), "a" * i))}")
       
    90 }
       
    91 
       
    92 // test: (a*)* b
       
    93 for (i <- 0 to 6000000 by 500000) {
       
    94   println(s"$i: ${time_needed(2, matches(EVIL2, "a" * i))}")
       
    95 }
       
    96 
       
    97 
       
    98 val r0 = simp(der('a', EVIL3))
       
    99 val r1 = simp(der('a', r0))
       
   100 val r2 = simp(der('a', r1))
       
   101 val r3 = simp(der('a', r2))
       
   102 val r4 = simp(der('a', r3))
       
   103 val r5 = simp(der('a', r4))
       
   104 val r6 = simp(der('a', r5))
       
   105 
       
   106 //test: (a|aa)* b
       
   107 /*
       
   108 for (i <- 0 to 100 by 10) {
       
   109   println(s"$i: ${time_needed(2, matches(EVIL3, "a" * i ++ "c"))}")
       
   110 }
       
   111  */
       
   112