progs/re4.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sun, 21 Sep 2014 23:23:43 +0100
changeset 254 dcd4688690ce
parent 121 43c116860e47
child 407 4b454a6d1814
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    
121
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     2
    
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
abstract class Rexp {
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
  def simp : Rexp = this
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
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
case object NULL extends Rexp
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
case object EMPTY extends Rexp
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
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
    10
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
    11
  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
    12
    case (NULL, r) => r
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
    case (r, NULL) => r
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
    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
    15
    case (EMPTY, r) => if (nullable(r)) r else ALT(r, EMPTY)
121
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    16
    case (r1, r2) => if (r1 == r2) r1 else ALT(r1, r2)
92
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
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
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
    20
  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
    21
    case (NULL, _) => NULL
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
    case (_, NULL) => NULL
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
    case (EMPTY, r) => r
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
    case (r, EMPTY) => r
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
    case (r1, r2) => SEQ(r1, r2)
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
}
121
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    28
case class STAR(r: Rexp) extends Rexp {
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    29
  override def simp = r.simp match {
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    30
    case NULL => EMPTY
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    31
    case EMPTY => EMPTY
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    32
    case r => STAR(r)
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    33
  }
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    34
}
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    35
case class NTIMES(r: Rexp, n: Int) extends Rexp {
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    36
  override def simp = if (n == 0) EMPTY else 
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    37
    r.simp match {
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    38
      case NULL => NULL
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    39
      case EMPTY => EMPTY
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    40
      case r => NTIMES(r, n)
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    41
    }
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    42
}
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
// 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
    45
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
    46
  case Nil => EMPTY
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  case c::Nil => CHAR(c)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  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
    49
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
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
    51
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
// nullable function: tests whether the regular 
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
// expression can recognise the empty string
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
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
    56
  case NULL => false
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
  case EMPTY => true
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
  case CHAR(_) => false
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
  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
    60
  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
    61
  case STAR(_) => true
121
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    62
  case NTIMES(r, i) => if (i == 0) true else nullable(r)
92
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 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
    66
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
    67
  case NULL => NULL
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
  case EMPTY => NULL
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
  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
    70
  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
    71
  case SEQ(r1, r2) => 
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
    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
    73
    else SEQ(der(c, r1), r2)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
  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
    75
  case NTIMES(r, i) => 
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
    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
    77
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
// 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
    80
@tailrec
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
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
    82
  case Nil => r
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
  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
    84
}
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
// main matcher function
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
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
    88
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
//one or zero
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
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
    92
121
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    93
def EVIL(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n))
92
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
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
    96
  val start = System.nanoTime()
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
  for (j <- 1 to i) code
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
  val end = System.nanoTime()
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
  (end - start)/(i * 1.0e9)
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
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
121
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
   103
for (i <- 1 to 12001 by 500) {
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
   104
  println(i + " " + "%.5f".format(time_needed(1, matcher(EVIL(i), "a" * i))))
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107