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