progs/re4.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Tue, 20 Sep 2016 12:40:57 +0100
changeset 425 d14a9bdac26f
parent 422 5deefcc8cffa
child 441 028816884f70
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
121
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     1
    
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
     2
abstract class Rexp
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
     3
case object ZERO extends Rexp
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
     4
case object ONE extends Rexp
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
case class CHAR(c: Char) extends Rexp
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
     6
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
     7
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
     8
case class STAR(r: Rexp) extends Rexp 
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
     9
case class NTIMES(r: Rexp, n: Int) extends Rexp 
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
// nullable function: tests whether the regular 
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
// expression can recognise the empty string
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
def nullable (r: Rexp) : Boolean = r match {
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    14
  case ZERO => false
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    15
  case ONE => true
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
  case CHAR(_) => false
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
  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
    18
  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
    19
  case STAR(_) => true
121
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    20
  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
    21
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
// 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
    24
def der (c: Char, r: Rexp) : Rexp = r match {
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    25
  case ZERO => ZERO
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    26
  case ONE => ZERO
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    27
  case CHAR(d) => if (c == d) ONE else ZERO
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  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
    29
  case SEQ(r1, r2) => 
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
    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
    31
    else SEQ(der(c, r1), r2)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  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
    33
  case NTIMES(r, i) => 
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    34
    if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1))
92
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
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    37
def simp(r: Rexp) : Rexp = r match {
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    38
  case ALT(r1, r2) => (simp(r1), simp(r2)) match {
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    39
    case (ZERO, r2s) => r2s
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    40
    case (r1s, ZERO) => r1s
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    41
    case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    42
  }
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    43
  case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    44
    case (ZERO, _) => ZERO
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    45
    case (_, ZERO) => ZERO
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    46
    case (ONE, r2s) => r2s
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    47
    case (r1s, ONE) => r1s
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    48
    case (r1s, r2s) => SEQ(r1s, r2s)
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    49
  }
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    50
  case NTIMES(r1, n) => simp(r1) match {
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    51
    case ZERO => ZERO
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    52
    case ONE => ONE
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    53
    case r1s => NTIMES(r1s, n)
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    54
  }
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    55
  case r => r
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    56
}
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    57
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
// derivative w.r.t. a string (iterates der)
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
    59
def ders2(s: List[Char], r: Rexp) : Rexp = (s, r) match {
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    60
  case (Nil, r) => r
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    61
  case (s, ZERO) => ZERO
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    62
  case (s, ONE) => if (s == Nil) ONE else ZERO
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    63
  case (s, CHAR(c)) => if (s == List(c)) ONE else 
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    64
                       if (s == Nil) CHAR(c) else ZERO
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    65
  case (s, ALT(r1, r2)) => ALT(ders2(s, r2), ders2(s, r2))
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    66
  case (c::s, r) => ders2(s, simp(der(c, r)))
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    67
}
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    68
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
// main matcher function
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    70
def matcher(r: Rexp, s: String) : Boolean = nullable(ders2(s.toList, r))
92
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
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
//one or zero
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    74
def OPT(r: Rexp) = ALT(r, ONE)
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    76
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    77
val EVIL2 = SEQ(STAR(CHAR('a')), CHAR('b'))
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    78
92
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
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
    81
  val start = System.nanoTime()
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
  for (j <- 1 to i) code
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
  val end = System.nanoTime()
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
  (end - start)/(i * 1.0e9)
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
425
d14a9bdac26f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    87
// warmup
412
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
    88
val i = 5000
1cef3924f7a2 updated
Christian Urban <urbanc@in.tum.de>
parents: 407
diff changeset
    89
println(i + " " + "%.5f".format(time_needed(10, matcher(EVIL1(i), "a" * i))))
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
    91
for (i <- 1 to 7000001 by 1000000) {
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
    92
  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i))))
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
    95
for (i <- 1 to 7500001 by 500000) {
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
    96
  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i))))
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
    97
}
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
    98