progs/re.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 18 Nov 2016 18:47:02 +0000
changeset 62 2151c77e1e24
parent 4 f31c22f4f104
child 68 8da9e0c16194
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
abstract class Rexp
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
case object ZERO extends Rexp
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
case object ONE extends Rexp
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
case class CHAR(c: Char) extends Rexp
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
case class STAR(r: Rexp) extends Rexp 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
// nullable function: tests whether the regular 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
// expression can recognise the empty string
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
def nullable (r: Rexp) : Boolean = r match {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  case ZERO => false
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  case ONE => true
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  case CHAR(_) => false
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  case STAR(_) => true
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
// derivative of a regular expression w.r.t. a character
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
def der (c: Char, r: Rexp) : Rexp = r match {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  case ZERO => ZERO
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  case ONE => ZERO
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  case CHAR(d) => if (c == d) ONE else ZERO
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  case SEQ(r1, r2) => 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
    else SEQ(der(c, r1), r2)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  case STAR(r1) => SEQ(der(c, r1), STAR(r1))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
def simp(r: Rexp) : Rexp = r match {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  case ALT(r1, r2) => (simp(r1), simp(r2)) match {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
    case (ZERO, r2s) => r2s
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
    case (r1s, ZERO) => r1s
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
    case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  }
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
    case (ZERO, _) => ZERO
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
    case (_, ZERO) => ZERO
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
    case (ONE, r2s) => r2s
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
    case (r1s, ONE) => r1s
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
    case (r1s, r2s) => SEQ(r1s, r2s)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
  }
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
  case r => r
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
// derivative w.r.t. a string (iterates der)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
def ders (s: List[Char], r: Rexp) : Rexp = s match {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  case Nil => r
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  case c::s => ders(s, simp(der(c, r)))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
// main matcher function
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
//one or zero
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
def OPT(r: Rexp) = ALT(r, ONE)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
//evil regular expressions
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
def time_needed[T](i: Int, code: => T) = {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
  val start = System.nanoTime()
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  for (j <- 1 to i) code
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  val end = System.nanoTime()
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
  (end - start)/(i * 1.0e9)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
//test: (a?{n}) (a{n})
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
for (i <- 1 to 8001 by 1000) {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i))))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
for (i <- 1 to 8001 by 1000) {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i))))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
//test: (a*)* b
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
for (i <- 1 to 7000001 by 500000) {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i))))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
for (i <- 1 to 7000001 by 500000) {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i))))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93