progs/re1.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 29 Nov 2013 09:01:08 +0000
changeset 211 deece8c6cf3a
parent 119 a6684e8961d0
child 258 1e4da6d2490c
permissions -rw-r--r--
added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
117
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     1
import scala.language.implicitConversions    
25999de692b2 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     2
54
485f38b530ab updated
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
     3
abstract class Rexp
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
case object NULL extends Rexp
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
case object EMPTY extends Rexp
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
case class CHAR(c: Char) extends Rexp
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
case class STAR(r: Rexp) extends Rexp 
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
// some convenience for typing in regular expressions
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
def charlist2rexp(s : List[Char]) : Rexp = s match {
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  case Nil => EMPTY
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  case c::Nil => CHAR(c)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
// nullable function: tests whether the regular 
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
// expression can recognise the empty string
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
def nullable (r: Rexp) : Boolean = r match {
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  case NULL => false
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  case EMPTY => true
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  case CHAR(_) => false
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  case STAR(_) => true
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
// derivative of a regular expression w.r.t. a character
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
def der (c: Char, r: Rexp) : Rexp = r match {
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  case NULL => NULL
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  case EMPTY => NULL
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  case CHAR(d) => if (c == d) EMPTY else NULL
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  case SEQ(r1, r2) => 
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
    else SEQ(der(c, r1), r2)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  case STAR(r) => SEQ(der(c, r), STAR(r))
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
// derivative w.r.t. a string (iterates der)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
def ders (s: List[Char], r: Rexp) : Rexp = s match {
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
  case Nil => r
54
485f38b530ab updated
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
    47
  case c::s => ders(s, der(c, r))
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
// main matcher function
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
85
1a4065f965fb added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 59
diff changeset
    53
//example
1a4065f965fb added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 59
diff changeset
    54
//val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b')))
119
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    55
//der('a', r)
85
1a4065f965fb added packages
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 59
diff changeset
    56
//der('b', r)
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
//one or zero
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
def OPT(r: Rexp) = ALT(r, EMPTY)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
//n-times
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
def NTIMES(r: Rexp, n: Int) : Rexp = n match {
59
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
    63
  case 0 => EMPTY
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  case 1 => r
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  case n => SEQ(r, NTIMES(r, n - 1))
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
119
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    68
def EVIL(n: Int) = SEQ(NTIMES(OPT("a"), n), NTIMES("a", n))
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
def time_needed[T](i: Int, code: => T) = {
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  val start = System.nanoTime()
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
  for (j <- 1 to i) code
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  val end = System.nanoTime()
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  (end - start)/(i * 1.0e9)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
54
485f38b530ab updated
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
    77
for (i <- 1 to 29) {
119
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 117
diff changeset
    78
  println(i + ": " + "%.5f".format(time_needed(1, matcher(EVIL(i), "a" * i))))
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81