progs/re3.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Mon, 20 Jul 2020 10:06:43 +0100
changeset 735 fc2e3609d5e5
parent 647 180600c04da2
permissions -rw-r--r--
updatd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
     1
// A version with simplification of derivatives;
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
     2
// this keeps the regular expressions small, which
647
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
     3
// is good for the 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
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
    17
// the nullable function: tests whether the regular 
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
// expression can recognise the empty string
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
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
    20
  case ZERO => false
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    21
  case ONE => true
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
  case CHAR(_) => false
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
  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
    24
  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
    25
  case STAR(_) => true
121
43c116860e47 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    26
  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
    27
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
    29
// the derivative of a regular expression w.r.t. a character
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
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
    31
  case ZERO => ZERO
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    32
  case ONE => ZERO
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    33
  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
    34
  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
    35
  case SEQ(r1, r2) => 
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
    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
    37
    else SEQ(der(c, r1), r2)
440
e14cd32ad497 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
    38
  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
    39
  case NTIMES(r, i) => 
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
    40
    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
    41
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
    43
def simp(r: Rexp) : Rexp = r match {
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
    44
  case ALT(r1, r2) => (simp(r1), simp(r2)) match {
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
    45
    case (ZERO, r2s) => r2s
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
    46
    case (r1s, ZERO) => r1s
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
    47
    case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    48
  }
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
    49
  case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
    50
    case (ZERO, _) => ZERO
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
    51
    case (_, ZERO) => ZERO
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
    52
    case (ONE, r2s) => r2s
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
    53
    case (r1s, ONE) => r1s
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
    54
    case (r1s, r2s) => SEQ(r1s, r2s)
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    55
  }
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
    56
  case r => r
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    57
}
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    58
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    59
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
    60
// the derivative w.r.t. a string (iterates der)
618
f4818c95a32e updated
Christian Urban <urbanc@in.tum.de>
parents: 566
diff changeset
    61
def ders(s: List[Char], r: Rexp) : Rexp = s match {
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
  case Nil => r
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 556
diff changeset
    63
  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
    64
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    66
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
    67
// the main matcher function
647
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
    68
def matcher(r: Rexp, s: String) : Boolean = 
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 638
diff changeset
    69
  nullable(ders(s.toList, r))
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
    72
// one or zero
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 121
diff changeset
    73
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
    74
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
    75
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
    76
// Test Cases
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
    77
623
47a299e7010f updated
Christian Urban <urbanc@in.tum.de>
parents: 618
diff changeset
    78
// evil regular expressions: (a?){n} a{n}  and (a*)* b
544
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
    79
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
    80
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    81
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
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
    84
  val start = System.nanoTime()
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
  for (j <- 1 to i) code
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
  val end = System.nanoTime()
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
  (end - start)/(i * 1.0e9)
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    90
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    91
//test: (a?{n}) (a{n})
638
0367aa7c764b updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
    92
for (i <- 0 to 8000 by 1000) {
0367aa7c764b updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
    93
  println(f"$i: ${time_needed(3, matcher(EVIL1(i), "a" * i))}%.5f")
92
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
}
e85600529ca5 moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    96
//test: (a*)* b
631
f618dd4de24a updated
Christian Urban <urbanc@in.tum.de>
parents: 623
diff changeset
    97
for (i <- 0 to 6000000 by 500000) {
638
0367aa7c764b updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
    98
  println(f"$i: ${time_needed(3, matcher(EVIL2, "a" * i))}%.5f")
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    99
}
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
   100
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   101
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   102
// size of a regular expressions - for testing purposes 
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   103
def size(r: Rexp) : Int = r match {
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   104
  case ZERO => 1
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   105
  case ONE => 1
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   106
  case CHAR(_) => 1
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   107
  case ALT(r1, r2) => 1 + size(r1) + size(r2)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   108
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   109
  case STAR(r) => 1 + size(r)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   110
  case NTIMES(r, _) => 1 + size(r)
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   111
}
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
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
// now the size of the derivatives grows 
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   115
// much, much slower
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   116
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   117
size(ders("".toList, EVIL2))      // 5
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   118
size(ders("a".toList, EVIL2))     // 8
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   119
size(ders("aa".toList, EVIL2))    // 8
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   120
size(ders("aaa".toList, EVIL2))   // 8
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   121
size(ders("aaaa".toList, EVIL2))  // 8
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
   122
size(ders("aaaaa".toList, EVIL2)) // 8
544
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   123
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   124
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   125
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   126
748207ad3ef0 updated
Christian Urban <urbanc@in.tum.de>
parents: 523
diff changeset
   127