progs/re3.scala
author cu
Wed, 18 Oct 2017 12:33:00 +0100
changeset 523 25e74e6fe2f7
parent 519 955d5b3b0619
child 544 748207ad3ef0
permissions -rw-r--r--
update
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 
523
cu
parents: 519
diff changeset
    14
case class CSET(cs: Set[Char]) extends Rexp
cu
parents: 519
diff changeset
    15
case class CFUN(f: Char => Bool) extends Rexp
cu
parents: 519
diff changeset
    16
cu
parents: 519
diff changeset
    17
CSET(('a' to 'z').toSet)
cu
parents: 519
diff changeset
    18
cu
parents: 519
diff changeset
    19
val CSET2(cs: Set[Char]) = CFUN((c:Char) => cs.contains(c))
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
467
b5ec11e89768 fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 458
diff changeset
    21
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
// nullable function: tests whether the regular 
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
// expression can recognise the empty string
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
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
    25
  case ZERO => false
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    26
  case ONE => true
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  case CHAR(_) => false
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  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
    29
  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
    30
  case STAR(_) => true
121
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    31
  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
    32
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
// 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
    35
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
    36
  case ZERO => ZERO
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    37
  case ONE => ZERO
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    38
  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
    39
  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
    40
  case SEQ(r1, r2) => 
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
    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
    42
    else SEQ(der(c, r1), r2)
440
e14cd32ad497 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
    43
  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
    44
  case NTIMES(r, i) => 
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
    45
    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
    46
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    48
def simp(r: Rexp) : Rexp = r match {
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    49
  case ALT(r1, r2) => (simp(r1), simp(r2)) match {
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    50
    case (ZERO, r2s) => r2s
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    51
    case (r1s, ZERO) => r1s
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    52
    case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    53
  }
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    54
  case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    55
    case (ZERO, _) => ZERO
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    56
    case (_, ZERO) => ZERO
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    57
    case (ONE, r2s) => r2s
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    58
    case (r1s, ONE) => r1s
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    59
    case (r1s, r2s) => SEQ(r1s, r2s)
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    60
  }
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    61
  case r => r
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    62
}
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    63
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    64
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
// 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
    66
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
    67
  case Nil => r
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    68
  case c::s => ders(s, simp(der(c, r)))
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    71
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
// main matcher function
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
    73
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
    74
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
//one or zero
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    77
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
    78
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
    79
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
    80
// Test Cases
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
    81
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    82
//evil regular expressions
519
955d5b3b0619 updated
cu
parents: 517
diff changeset
    83
def EVIL1(n: Int) = SEQ(NTIMEemacs re3S(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
    84
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    85
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
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
    88
  val start = System.nanoTime()
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
  for (j <- 1 to i) code
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
  val end = System.nanoTime()
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
  (end - start)/(i * 1.0e9)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    94
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    95
//test: (a?{n}) (a{n})
519
955d5b3b0619 updated
cu
parents: 517
diff changeset
    96
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
    97
  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
    98
}
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
519
955d5b3b0619 updated
cu
parents: 517
diff changeset
   100
for (i <- 1 to 8001 by 1000) {
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   101
  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
   102
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
   104
//test: (a*)* b
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   105
for (i <- 1 to 6000001 by 500000) {
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   106
  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL2, "a" * i))))
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   107
}
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   108
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   109
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
   110
  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
   111
}
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
   112
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   113
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   114
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   115
// size of a regular expressions - for testing purposes 
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   116
def size(r: Rexp) : Int = r match {
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   117
  case ZERO => 1
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   118
  case ONE => 1
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   119
  case CHAR(_) => 1
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   120
  case ALT(r1, r2) => 1 + size(r1) + size(r2)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   121
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   122
  case STAR(r) => 1 + size(r)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   123
  case NTIMES(r, _) => 1 + size(r)
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   124
}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   125
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   126
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   127
// now the size of the derivatives grows 
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   128
// much, much slower
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   129
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   130
size(ders("".toList, EVIL2))      // 5
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   131
size(ders("a".toList, EVIL2))     // 8
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   132
size(ders("aa".toList, EVIL2))    // 8
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   133
size(ders("aaa".toList, EVIL2))   // 8
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   134
size(ders("aaaa".toList, EVIL2))  // 8
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   135
size(ders("aaaaa".toList, EVIL2)) // 8