progs/re4.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 26 Sep 2013 11:05:54 +0100
changeset 104 ffde837b1db1
parent 93 4794759139ea
child 121 43c116860e47
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:
diff changeset
     1
import scala.annotation.tailrec    
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
abstract class Rexp {
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
  def simp : Rexp = this
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
case object NULL extends Rexp
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
case object EMPTY extends Rexp
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
case class CHAR(c: Char) extends Rexp
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
case class ALT(r1: Rexp, r2: Rexp) extends Rexp {
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
  override def simp = (r1.simp, r2.simp) match {
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
    case (NULL, r) => r
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
    case (r, NULL) => r
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
    case (r, EMPTY) => if (nullable(r)) r else ALT(r, EMPTY)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
    case (EMPTY, r) => if (nullable(r)) r else ALT(r, EMPTY)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
    case (r1, r2) => ALT(r1, r2)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
  }
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp {
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
  override def simp = (r1.simp, r2.simp) match {
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
    case (NULL, _) => NULL
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
    case (_, NULL) => NULL
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
    case (EMPTY, r) => r
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
    case (r, EMPTY) => r
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
    case (r1, r2) => SEQ(r1, r2)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
  }
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
case class STAR(r: Rexp) extends Rexp 
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
case class NTIMES(r: Rexp, n: Int) extends Rexp 
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
// some convenience for typing in regular expressions
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
def charlist2rexp(s : List[Char]) : Rexp = s match {
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  case Nil => EMPTY
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  case c::Nil => CHAR(c)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
// nullable function: tests whether the regular 
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
// expression can recognise the empty string
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
def nullable (r: Rexp) : Boolean = r match {
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  case NULL => false
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
  case EMPTY => true
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  case CHAR(_) => false
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  case STAR(_) => true
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  case NTIMES(r, i) => if (i == 0) false else nullable(r)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
// derivative of a regular expression w.r.t. a character
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
def der (c: Char, r: Rexp) : Rexp = r match {
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
  case NULL => NULL
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  case EMPTY => NULL
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  case CHAR(d) => if (c == d) EMPTY else NULL
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
  case SEQ(r1, r2) => 
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
    else SEQ(der(c, r1), r2)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
  case STAR(r) => SEQ(der(c, r), STAR(r))
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
  case NTIMES(r, i) => 
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
    if (i == 0) NULL else SEQ(der(c, r), NTIMES(r, i - 1))
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
// derivative w.r.t. a string (iterates der)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
@tailrec
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
def ders (s: List[Char], r: Rexp) : Rexp = s match {
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
  case Nil => r
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
  case c::s => ders(s, der(c, r).simp)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
// main matcher function
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
//one or zero
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
def OPT(r: Rexp) = ALT(r, EMPTY)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
//n-times
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
/*def NTIMES(r: Rexp, n: Int) : Rexp = n match {
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
  case 0 => NULL
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
  case 1 => r
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
  case n => SEQ(r, NTIMES(r, n - 1))
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
}*/
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
def RTEST(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n))
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
def time_needed[T](i: Int, code: => T) = {
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
  val start = System.nanoTime()
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
  for (j <- 1 to i) code
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
  val end = System.nanoTime()
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
  (end - start)/(i * 1.0e9)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
for (i <- 1 to 13001 by 500) {
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
  println(i + " " + time_needed(1, matcher(RTEST(i), "a" * i)))
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101