progs/re1.scala
changeset 93 4794759139ea
parent 92 e85600529ca5
child 117 25999de692b2
equal deleted inserted replaced
92:e85600529ca5 93:4794759139ea
       
     1     
       
     2 abstract class Rexp
       
     3 
       
     4 case object NULL extends Rexp
       
     5 case object EMPTY extends Rexp
       
     6 case class CHAR(c: Char) extends Rexp
       
     7 case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
       
     8 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
       
     9 case class STAR(r: Rexp) extends Rexp 
       
    10 
       
    11 // some convenience for typing in regular expressions
       
    12 def charlist2rexp(s : List[Char]) : Rexp = s match {
       
    13   case Nil => EMPTY
       
    14   case c::Nil => CHAR(c)
       
    15   case c::s => SEQ(CHAR(c), charlist2rexp(s))
       
    16 }
       
    17 implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
       
    18 
       
    19 
       
    20 // nullable function: tests whether the regular 
       
    21 // expression can recognise the empty string
       
    22 def nullable (r: Rexp) : Boolean = r match {
       
    23   case NULL => false
       
    24   case EMPTY => true
       
    25   case CHAR(_) => false
       
    26   case ALT(r1, r2) => nullable(r1) || nullable(r2)
       
    27   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
       
    28   case STAR(_) => true
       
    29 }
       
    30 
       
    31 // derivative of a regular expression w.r.t. a character
       
    32 def der (c: Char, r: Rexp) : Rexp = r match {
       
    33   case NULL => NULL
       
    34   case EMPTY => NULL
       
    35   case CHAR(d) => if (c == d) EMPTY else NULL
       
    36   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
       
    37   case SEQ(r1, r2) => 
       
    38     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
       
    39     else SEQ(der(c, r1), r2)
       
    40   case STAR(r) => SEQ(der(c, r), STAR(r))
       
    41 }
       
    42 
       
    43 // derivative w.r.t. a string (iterates der)
       
    44 def ders (s: List[Char], r: Rexp) : Rexp = s match {
       
    45   case Nil => r
       
    46   case c::s => ders(s, der(c, r))
       
    47 }
       
    48 
       
    49 // main matcher function
       
    50 def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
       
    51 
       
    52 //example
       
    53 //val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b')))
       
    54 //der('b', r)
       
    55 //der('b', r)
       
    56 
       
    57 //one or zero
       
    58 def OPT(r: Rexp) = ALT(r, EMPTY)
       
    59 
       
    60 //n-times
       
    61 def NTIMES(r: Rexp, n: Int) : Rexp = n match {
       
    62   case 0 => EMPTY
       
    63   case 1 => r
       
    64   case n => SEQ(r, NTIMES(r, n - 1))
       
    65 }
       
    66 
       
    67 def RTEST(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n))
       
    68 
       
    69 def time_needed[T](i: Int, code: => T) = {
       
    70   val start = System.nanoTime()
       
    71   for (j <- 1 to i) code
       
    72   val end = System.nanoTime()
       
    73   (end - start)/(i * 1.0e9)
       
    74 }
       
    75 
       
    76 for (i <- 1 to 29) {
       
    77   println(i + ": " + "%.5f".format(time_needed(1, matcher(RTEST(i), "a" * i))))
       
    78 }
       
    79 
       
    80