re0.scala
changeset 92 e85600529ca5
parent 91 47f86885d481
child 93 4794759139ea
equal deleted inserted replaced
91:47f86885d481 92:e85600529ca5
     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 case class NOT(r: Rexp) extends Rexp
       
    11 case class REP(r: Rexp, n: Int) extends Rexp 
       
    12 
       
    13 // some convenience for typing in regular expressions
       
    14 def charlist2rexp(s : List[Char]) : Rexp = s match {
       
    15   case Nil => EMPTY
       
    16   case c::Nil => CHAR(c)
       
    17   case c::s => SEQ(CHAR(c), charlist2rexp(s))
       
    18 }
       
    19 implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
       
    20 
       
    21 implicit def RexpOps(r: Rexp) = new {
       
    22   def | (s: Rexp) = ALT(r, s)
       
    23   def % = STAR(r)
       
    24   def %(n: Int) = REP(r, n) 
       
    25   def %%(n: Int) = SEQ(REP(r, n), STAR(r)) 
       
    26   def ? = ALT(EMPTY, r)
       
    27   def unary_! = NOT(r)
       
    28   def ~ (s: Rexp) = SEQ(r, s)
       
    29 }
       
    30 
       
    31 implicit def stringOps(s: String) = new {
       
    32   def | (r: Rexp) = ALT(s, r)
       
    33   def | (r: String) = ALT(s, r)
       
    34   def % = STAR(s)
       
    35   def %(n: Int) = REP(s, n)
       
    36   def %%(n: Int) = SEQ(REP(s, n), STAR(s)) 
       
    37   def ? = ALT(EMPTY, s)
       
    38   def unary_! = NOT(s)
       
    39   def ~ (r: Rexp) = SEQ(s, r)
       
    40   def ~ (r: String) = SEQ(s, r)
       
    41 }
       
    42 
       
    43 
       
    44 // nullable function: tests whether the regular 
       
    45 // expression can recognise the empty string
       
    46 def nullable (r: Rexp) : Boolean = r match {
       
    47   case NULL => false
       
    48   case EMPTY => true
       
    49   case CHAR(_) => false
       
    50   case ALT(r1, r2) => nullable(r1) || nullable(r2)
       
    51   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
       
    52   case STAR(_) => true
       
    53   case NOT(r) => !(nullable(r))
       
    54   case REP(r, i) => if (i == 0) true else nullable(r)
       
    55 }
       
    56 
       
    57 // derivative of a regular expression w.r.t. a character
       
    58 def der (c: Char, r: Rexp) : Rexp = r match {
       
    59   case NULL => NULL
       
    60   case EMPTY => NULL
       
    61   case CHAR(d) => if (c == d) EMPTY else NULL
       
    62   case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
       
    63   case SEQ(r1, r2) => 
       
    64     if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
       
    65     else SEQ(der(c, r1), r2)
       
    66   case STAR(r) => SEQ(der(c, r), STAR(r))
       
    67   case NOT(r) => NOT(der (c, r))
       
    68   case REP(r, i) => 
       
    69     if (i == 0) NULL else SEQ(der(c, r), REP(r, i - 1))
       
    70 }
       
    71 
       
    72 // derivative w.r.t. a string (iterates der)
       
    73 def ders (s: List[Char], r: Rexp) : Rexp = s match {
       
    74   case Nil => r
       
    75   case c::s => ders(s, der(c, r))
       
    76 }
       
    77 
       
    78 // main matcher function
       
    79 def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
       
    80 
       
    81 //examples
       
    82 val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
       
    83 val int = ("+" | "-").? ~ digit.%%(1)
       
    84 val real = ("+" | "-").? ~ digit.%%(1) ~ ("." ~ digit.%%(1)).? ~ (("e" | "E") ~ ("+" | "-").? ~ digit.%%(1)).?
       
    85 
       
    86 val ints = List("0", "-4534", "+049", "99")
       
    87 val reals = List("0.9", "-12.8", "+91.0", "9e12", "+9.21E-12", "-512E+01")
       
    88 val errs = List("", "-", "+", "+-1", "-+2", "2-")
       
    89 
       
    90 ints.map(s => matcher(int, s))
       
    91 reals.map(s => matcher(int, s))
       
    92 errs.map(s => matcher(int, s))
       
    93 
       
    94 ints.map(s => matcher(real, s))
       
    95 reals.map(s => matcher(real, s))
       
    96 errs.map(s => matcher(real, s))
       
    97 
       
    98 
       
    99 
       
   100 def RTEST(n: Int) = ("a".? %(n)) ~ ("a" %(n))
       
   101 
       
   102 def time_needed[T](i: Int, code: => T) = {
       
   103   val start = System.nanoTime()
       
   104   for (j <- 1 to i) code
       
   105   val end = System.nanoTime()
       
   106   (end - start)/(i * 1.0e9)
       
   107 }
       
   108 
       
   109 for (i <- 1 to 29) {
       
   110   println(i + ": " + "%.5f".format(time_needed(1, matcher(RTEST(i), "a" * i))))
       
   111 }
       
   112 
       
   113