progs/re0.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 20 Nov 2015 02:23:24 +0000
changeset 383 a6a6bf32fade
parent 93 4794759139ea
child 490 4fee50f38305
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
     1
import scala.annotation.tailrec
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
     2
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
abstract class Rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
case object NULL extends Rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
case object EMPTY extends Rexp
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
     7
case object ALLCHAR extends Rexp
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
case class CHAR(c: Char) extends Rexp
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
     9
case class STR(s: String) extends Rexp
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
case class STAR(r: Rexp) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
case class NOT(r: Rexp) extends Rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
case class REP(r: Rexp, n: Int) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
// some convenience for typing in regular expressions
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
    17
implicit def string2rexp(s : String) : Rexp = STR(s)
91
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 RexpOps(r: Rexp) = new {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
  def | (s: Rexp) = ALT(r, s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
  def % = STAR(r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
  def %(n: Int) = REP(r, n) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
  def %%(n: Int) = SEQ(REP(r, n), STAR(r)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
  def ? = ALT(EMPTY, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
  def unary_! = NOT(r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  def ~ (s: Rexp) = SEQ(r, s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
implicit def stringOps(s: String) = new {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
  def | (r: Rexp) = ALT(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  def | (r: String) = ALT(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  def % = STAR(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  def %(n: Int) = REP(s, n)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  def %%(n: Int) = SEQ(REP(s, n), STAR(s)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  def ? = ALT(EMPTY, s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  def unary_! = NOT(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  def ~ (r: Rexp) = SEQ(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  def ~ (r: String) = SEQ(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
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
// nullable function: tests whether the regular 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
// expression can recognise the empty string
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
    44
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
def nullable (r: Rexp) : Boolean = r match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  case NULL => false
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  case EMPTY => true
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
    48
  case ALLCHAR => false
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
  case CHAR(_) => false
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
    50
  case STR(s) => s.isEmpty
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
  case STAR(_) => true
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  case NOT(r) => !(nullable(r))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  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
    56
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
// derivative of a regular expression w.r.t. a character
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
def der (c: Char, r: Rexp) : Rexp = r match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
  case NULL => NULL
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
  case EMPTY => NULL
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
    62
  case ALLCHAR => EMPTY
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  case CHAR(d) => if (c == d) EMPTY else NULL
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
    64
  case STR(s) => if (s.isEmpty || s.head != c) NULL else STR(s.tail)
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
  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
    66
  case SEQ(r1, r2) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
    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
    68
    else SEQ(der(c, r1), r2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
  case STAR(r) => SEQ(der(c, r), STAR(r))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
  case NOT(r) => NOT(der (c, r))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
  case REP(r, i) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
    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
    73
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
// derivative w.r.t. a string (iterates der)
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
    76
@tailrec
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
def ders (s: List[Char], r: Rexp) : Rexp = s match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
  case Nil => r
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
  case c::s => ders(s, der(c, 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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
// main matcher function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
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
    84
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
//examples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
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
    87
val int = ("+" | "-").? ~ digit.%%(1)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
val real = ("+" | "-").? ~ digit.%%(1) ~ ("." ~ digit.%%(1)).? ~ (("e" | "E") ~ ("+" | "-").? ~ digit.%%(1)).?
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
val ints = List("0", "-4534", "+049", "99")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
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
    92
val errs = List("", "-", "+", "+-1", "-+2", "2-")
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(int, s))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
reals.map(s => matcher(int, s))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
errs.map(s => matcher(int, 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
ints.map(s => matcher(real, s))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
reals.map(s => matcher(real, s))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
errs.map(s => matcher(real, s))
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
def RTEST(n: Int) = ("a".? %(n)) ~ ("a" %(n))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
def time_needed[T](i: Int, code: => T) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
  val start = System.nanoTime()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
  for (j <- 1 to i) code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
  val end = System.nanoTime()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
  (end - start)/(i * 1.0e9)
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
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
   113
for (i <- 1 to 12000 by 500) {
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
  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
   115
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117