progs/re3.scala
author Christian Urban <urbanc@in.tum.de>
Sun, 23 Sep 2018 09:02:52 +0100
changeset 556 40e22ad45744
parent 550 71fc4a7a7039
child 564 b5d57d7064bb
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
     1
// Version with simplification during derivatives;
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
     2
// this keeps the regular expressions small, which
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
     3
// is good for run-time
517
edab48a5b37e updated
cu
parents: 477
diff changeset
     4
 
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
     6
abstract class Rexp
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
     7
case object ZERO extends Rexp
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
     8
case object ONE extends Rexp
92
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
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    10
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    11
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    12
case class STAR(r: Rexp) extends Rexp 
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    13
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
    14
467
b5ec11e89768 fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 458
diff changeset
    15
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    16
// string of a regular expressions - for testing purposes 
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    17
def string(r: Rexp) : String = r match {
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    18
  case ZERO => "0"
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    19
  case ONE => "1"
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    20
  case CHAR(c) => c.toString 
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    21
  case ALT(r1, r2) => s"(${string(r1)} + ${string(r2)})"
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    22
  case SEQ(CHAR(c), CHAR(d)) => s"${c}${d}"
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    23
  case SEQ(ONE, CHAR(c)) => s"1${c}"
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    24
  case SEQ(r1, r2) => s"(${string(r1)} ~ ${string(r2)})"
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    25
  case STAR(r) => s"(${string(r)})*"
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    26
  case NTIMES(r, n) =>  s"(${string(r)}){${n}}"
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    27
}
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    28
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    29
// size of a regular expressions - for testing purposes 
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    30
def size(r: Rexp) : Int = r match {
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    31
  case ZERO => 1
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    32
  case ONE => 1
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    33
  case CHAR(_) => 1
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    34
  case ALT(r1, r2) => 1 + size(r1) + size(r2)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    35
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    36
  case STAR(r) => 1 + size(r)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    37
  case NTIMES(r, _) => 1 + size(r)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    38
}
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    39
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    40
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    41
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
// nullable function: tests whether the regular 
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
// expression can recognise the empty string
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
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
    45
  case ZERO => false
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    46
  case ONE => true
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  case CHAR(_) => false
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  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
    49
  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
    50
  case STAR(_) => true
121
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    51
  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
    52
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
// 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
    55
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
    56
  case ZERO => ZERO
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    57
  case ONE => ZERO
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    58
  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
    59
  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
    60
  case SEQ(r1, r2) => 
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
    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
    62
    else SEQ(der(c, r1), r2)
440
e14cd32ad497 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
    63
  case STAR(r1) => SEQ(der(c, r1), STAR(r1))
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  case NTIMES(r, i) => 
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
    65
    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
    66
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    68
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    69
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    70
def simp(r: Rexp, seen: Set[Rexp]) : (Rexp, Set[Rexp]) = r match {
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    71
  case ALT(r1, r2) => {
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    72
    val (r1s, seen1) = simp(r1, seen)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    73
    val (r2s, seen2) = simp(r2, seen1)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    74
    (r1s, r2s) match {
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    75
      case (ZERO, r2s) => (r2s, seen2)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    76
      case (r1s, ZERO) => (r1s, seen2)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    77
      case (r1s, r2s) => (ALT(r1s, r2s), seen2)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    78
    }
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    79
  }
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    80
  case SEQ(r1, r2) =>  {
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    81
    val (r1s, _) = simp(r1, Set())
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    82
    val (r2s, _) = simp(r2, Set())
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    83
    if (seen.contains(SEQ(r1s, r2s))) (ZERO, seen)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    84
    else (r1s, r2s) match {
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    85
      case (ZERO, _) => (ZERO, seen)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    86
      case (_, ZERO) => (ZERO, seen)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    87
      case (ONE, r2s) => (r2s, seen + r2s)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    88
      case (r1s, ONE) => (r1s, seen + r1s)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    89
      case (r1s, r2s) => (SEQ(r1s, r2s), seen + SEQ(r1s, r2s))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    90
    }
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    91
  }
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    92
  case r => if (seen.contains(r)) (ZERO, seen) else (r, seen + r)
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    93
}
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    94
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    95
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
// 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
    97
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
    98
  case Nil => r
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
    99
  case c::s => ders(s, simp(der(c, r), Set())._1)
92
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
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
   102
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
// main matcher function
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   104
def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
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
//one or zero
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
   108
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
   109
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   110
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   111
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   112
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   113
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   114
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   115
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   116
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   117
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   118
def time_needed[T](i: Int, code: => T) = {
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   119
  val start = System.nanoTime()
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   120
  for (j <- 1 to i) code
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   121
  val end = System.nanoTime()
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   122
  (end - start)/(i * 1.0e9)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   123
}
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   124
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   125
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   126
// star example
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   127
val Tstar = STAR(STAR(STAR(CHAR('a'))))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   128
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   129
string(ders("".toList, Tstar))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   130
size(ders("".toList, Tstar))      // 4
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   131
string(ders("a".toList, Tstar))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   132
size(ders("a".toList, Tstar))     // 11
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   133
string(ders("aa".toList, Tstar))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   134
size(ders("aa".toList, Tstar))    // 11
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   135
string(ders("aaa".toList, Tstar))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   136
size(ders("aaa".toList, Tstar))   // 11
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   137
string(ders("aaaa".toList, Tstar))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   138
size(ders("aaaa".toList, Tstar))  // 11
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   139
string(ders("aaaa".toList, Tstar))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   140
size(ders("aaaaa".toList, Tstar)) // 11
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   141
string(ders("aaaab".toList, Tstar))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   142
size(ders("aaaaab".toList, Tstar)) // 1
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   143
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   144
// test: ("a" | "aa")*
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   145
val EVIL3 = STAR(ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a'))))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   146
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   147
for (i <- 1 to 100 by 1) {
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   148
  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL3, "a" * i))) + 
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   149
	  " size: " + size(ders(("a" * i).toList, EVIL3)))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   150
}
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   151
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   152
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   153
println("start " + string(EVIL3) + "    " + size(EVIL3))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   154
val t1  = der('a', EVIL3)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   155
println(string(t1) + "    " + size(t1))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   156
val t1s = simp(t1, Set())._1 
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   157
println("simplified " + string(t1s) + "    " + size(t1s))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   158
val t2  = der('a', t1s)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   159
println(string(t2) + "    " + size(t2))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   160
val t2s = simp(t2, Set())._1 
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   161
println("simplified " + string(t2s) + "    " + size(t2s))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   162
val t3  = der('a', t2s)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   163
println(string(t3) + "    " + size(t3))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   164
val t3s = simp(t3, Set())._1 
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   165
println("simplified " + string(t3s) + "    " + size(t3s))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   166
val t4  = der('a', t3s)
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   167
val t4s = simp(t4, Set())._1 
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   168
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   169
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   170
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   171
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   172
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   173
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   174
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   175
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   176
println(string(t4) + "    " + size(t4))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   177
println("simplified " + string(t4s) + "    " + size(t4s))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   178
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   179
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   180
// Test Cases
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   181
544
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   182
//evil regular expressions: (a?){n} a{n}  and (a*)* b
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   183
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
440
e14cd32ad497 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
   184
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
   185
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
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
   188
  val start = System.nanoTime()
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
  for (j <- 1 to i) code
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
  val end = System.nanoTime()
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
  (end - start)/(i * 1.0e9)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
   194
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
   195
//test: (a?{n}) (a{n})
519
955d5b3b0619 updated
cu
parents: 517
diff changeset
   196
for (i <- 1 to 8001 by 1000) {
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
   197
  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL1(i), "a" * i))))
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
   198
}
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
519
955d5b3b0619 updated
cu
parents: 517
diff changeset
   200
for (i <- 1 to 8001 by 1000) {
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   201
  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
   202
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
   204
//test: (a*)* b
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   205
for (i <- 1 to 6000001 by 500000) {
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   206
  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i))))
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   207
}
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   208
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   209
for (i <- 1 to 6000001 by 500000) {
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
   210
  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i))))
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
   211
}
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
   212
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   213
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   214
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   215
// size of a regular expressions - for testing purposes 
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   216
def size(r: Rexp) : Int = r match {
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   217
  case ZERO => 1
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   218
  case ONE => 1
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   219
  case CHAR(_) => 1
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   220
  case ALT(r1, r2) => 1 + size(r1) + size(r2)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   221
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   222
  case STAR(r) => 1 + size(r)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   223
  case NTIMES(r, _) => 1 + size(r)
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   224
}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   225
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   226
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   227
// now the size of the derivatives grows 
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   228
// much, much slower
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   229
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   230
size(ders("".toList, EVIL2))      // 5
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   231
size(ders("a".toList, EVIL2))     // 8
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   232
size(ders("aa".toList, EVIL2))    // 8
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   233
size(ders("aaa".toList, EVIL2))   // 8
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   234
size(ders("aaaa".toList, EVIL2))  // 8
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   235
size(ders("aaaaa".toList, EVIL2)) // 8
544
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   236
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   237
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   238
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   239
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   240
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   241
// Examples from the Sulzmann paper
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   242
val sulzmann = STAR(ALT(ALT(CHAR('a'), CHAR('b')), SEQ(CHAR('a'), CHAR('b'))))
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   243
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   244
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   245
for (i <- 1 to 4501 by 500) {
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   246
  println(i + ": " + "%.5f".format(time_needed(1, matcher(sulzmann, "a" * i))))
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   247
}
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   248
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   249
for (i <- 1 to 4501 by 500) {
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   250
  println(i + ": " + "%.5f".format(time_needed(1, matcher(sulzmann, "ab" * i))))
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   251
}
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   252
556
40e22ad45744 updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
   253
size(ders("".toList, EVIL2))      // 5
40e22ad45744 updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
   254
size(ders("a".toList, EVIL2))     // 8
40e22ad45744 updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
   255
size(ders("aa".toList, EVIL2))    // 8
40e22ad45744 updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
   256
size(ders("aaa".toList, EVIL2))   // 8
40e22ad45744 updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
   257
size(ders("aaaa".toList, EVIL2))  // 8
40e22ad45744 updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
   258
size(ders("aaaaa".toList, EVIL2)) // 8
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   259
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   260
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   261
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 544
diff changeset
   262
(((1 + 1a) ~ ((a + aa))*) + (((0 + 1) ~ ((a + aa))*) + ((1 + 1a) ~ ((a + aa))*)))