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