Attic/re0.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 25 Oct 2024 18:54:08 +0100
changeset 971 51e00f223792
parent 742 b5b5583a3a08
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
490
4fee50f38305 updated
Christian Urban <urbanc@in.tum.de>
parents: 93
diff changeset
     2
import scala.language.implicitConversions
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
     3
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
abstract class Rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
case object NULL extends Rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
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
     8
case object ALLCHAR extends Rexp
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
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
    10
case class STR(s: String) extends Rexp
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
case class STAR(r: Rexp) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
case class NOT(r: Rexp) extends Rexp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
case class REP(r: Rexp, n: Int) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
// 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
    18
implicit def string2rexp(s : String) : Rexp = STR(s)
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
499
dfd0f41f8668 updated
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
    20
implicit def RexpOps(r: Rexp) : Rexp = new {
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
  def | (s: Rexp) = ALT(r, s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
  def % = STAR(r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
  def %(n: Int) = REP(r, n) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
  def %%(n: Int) = SEQ(REP(r, n), STAR(r)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
  def ? = ALT(EMPTY, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  def unary_! = NOT(r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  def ~ (s: Rexp) = SEQ(r, s)
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
499
dfd0f41f8668 updated
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
    30
implicit def stringOps(s: String) : Rexp = new {
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  def | (r: Rexp) = ALT(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  def | (r: String) = ALT(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  def % = STAR(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  def %(n: Int) = REP(s, n)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  def %%(n: Int) = SEQ(REP(s, n), STAR(s)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  def ? = ALT(EMPTY, s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  def unary_! = NOT(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  def ~ (r: Rexp) = SEQ(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  def ~ (r: String) = SEQ(s, r)
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
// nullable function: tests whether the regular 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
// 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
    45
91
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
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
    49
  case ALLCHAR => false
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
  case CHAR(_) => false
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
    51
  case STR(s) => s.isEmpty
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  case STAR(_) => true
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  case NOT(r) => !(nullable(r))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
  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
    57
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
// derivative of a regular expression w.r.t. a character
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
def der (c: Char, r: Rexp) : Rexp = r match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
  case NULL => NULL
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
  case EMPTY => NULL
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
    63
  case ALLCHAR => EMPTY
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  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
    65
  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
    66
  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
    67
  case SEQ(r1, r2) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
    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
    69
    else SEQ(der(c, r1), r2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
  case STAR(r) => SEQ(der(c, r), STAR(r))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
  case NOT(r) => NOT(der (c, r))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
  case REP(r, i) => 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
    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
    74
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
490
4fee50f38305 updated
Christian Urban <urbanc@in.tum.de>
parents: 93
diff changeset
    76
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
// 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
    78
@tailrec
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
def ders (s: List[Char], r: Rexp) : Rexp = s match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
  case Nil => r
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
  case c::s => ders(s, der(c, r))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
// main matcher function
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
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
    86
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
//examples
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
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
    89
val int = ("+" | "-").? ~ digit.%%(1)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
val real = ("+" | "-").? ~ digit.%%(1) ~ ("." ~ digit.%%(1)).? ~ (("e" | "E") ~ ("+" | "-").? ~ digit.%%(1)).?
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
val ints = List("0", "-4534", "+049", "99")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
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
    94
val errs = List("", "-", "+", "+-1", "-+2", "2-")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
ints.map(s => matcher(int, s))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
reals.map(s => matcher(int, s))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
errs.map(s => matcher(int, s))
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
ints.map(s => matcher(real, s))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
reals.map(s => matcher(real, s))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
errs.map(s => matcher(real, s))
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
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 RTEST(n: Int) = ("a".? %(n)) ~ ("a" %(n))
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
def time_needed[T](i: Int, code: => T) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
  val start = System.nanoTime()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
  for (j <- 1 to i) code
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
  val end = System.nanoTime()
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
  (end - start)/(i * 1.0e9)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 91
diff changeset
   115
for (i <- 1 to 12000 by 500) {
91
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
  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
   117
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119