progs/re2.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 19 Nov 2018 23:21:32 +0000
changeset 604 9e75249e96f2
parent 566 b153c04834eb
child 623 47a299e7010f
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
513
676e6484f29b updated
cu
parents: 478
diff changeset
     1
// Version with an explicit n-times regular expression;
676e6484f29b updated
cu
parents: 478
diff changeset
     2
// this keeps the size of the regular expression in the
676e6484f29b updated
cu
parents: 478
diff changeset
     3
// EVIL1 test-case quite small
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
     4
120
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     5
abstract class Rexp 
424
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
     6
case object ZERO extends Rexp
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
     7
case object ONE extends Rexp
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
case class CHAR(c: Char) extends Rexp
120
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     9
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    10
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    11
case class STAR(r: Rexp) extends Rexp 
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    12
case class NTIMES(r: Rexp, n: Int) extends Rexp   //explicit constructor for n-times
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    13
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
def nullable (r: Rexp) : Boolean = r match {
424
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    16
  case ZERO => false
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    17
  case ONE => true
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  case CHAR(_) => false
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  case STAR(_) => true
120
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    22
  case NTIMES(r, i) => if (i == 0) true else nullable(r)
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    25
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
def der (c: Char, r: Rexp) : Rexp = r match {
424
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    27
  case ZERO => ZERO
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    28
  case ONE => ZERO
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    29
  case CHAR(d) => if (c == d) ONE else ZERO
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  case SEQ(r1, r2) => 
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
    else SEQ(der(c, r1), r2)
440
e14cd32ad497 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
    34
  case STAR(r1) => SEQ(der(c, r1), STAR(r1))
e14cd32ad497 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 434
diff changeset
    35
  case NTIMES(r1, i) => 
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    36
    if (i == 0) ZERO else SEQ(der(c, r1), NTIMES(r1, i - 1))
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
def ders (s: List[Char], r: Rexp) : Rexp = s match {
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  case Nil => r
120
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    41
  case c::s => ders(s, der(c, r))
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
261
24531cfaa36a updated handouts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 258
diff changeset
    44
def matches(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    47
//optional regular expression: one or zero times
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
    48
//this regular expression is still defined in terms of ALT
424
1129024b26d5 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 422
diff changeset
    49
def OPT(r: Rexp) = ALT(r, ONE)
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    51
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    52
// Test Cases
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    53
422
5deefcc8cffa updated programs
Christian Urban <urbanc@in.tum.de>
parents: 415
diff changeset
    54
//evil regular expressions
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
    55
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
    56
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
def time_needed[T](i: Int, code: => T) = {
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
  val start = System.nanoTime()
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  for (j <- 1 to i) code
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  val end = System.nanoTime()
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  (end - start)/(i * 1.0e9)
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
}
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 424
diff changeset
    66
//test: (a?{n}) (a{n})
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    67
for (i <- 1 to 1201 by 100) {
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
    68
  println(i + " " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i))))
120
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    69
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    70
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    71
for (i <- 1 to 1201 by 100) {
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 424
diff changeset
    72
  println(i + " " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i))))
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 424
diff changeset
    73
}
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 424
diff changeset
    74
441
028816884f70 updated
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
    75
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 424
diff changeset
    76
//test: (a*)* b
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
    77
for (i <- 1 to 21) {
434
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 424
diff changeset
    78
  println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i))))
8664ff87cd77 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 424
diff changeset
    79
}
120
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    80
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
    81
for (i <- 1 to 21) {
415
4ae59fd3b174 updated
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
    82
  println(i + " " + "%.5f".format(time_needed(2, matches(EVIL2, "a" * i))))
414
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
    83
}
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
    84
065ca01b62ae updated
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
    85
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    86
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    87
// size of a regular expressions - for testing purposes 
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    88
def size(r: Rexp) : Int = r match {
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    89
  case ZERO => 1
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    90
  case ONE => 1
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    91
  case CHAR(_) => 1
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    92
  case ALT(r1, r2) => 1 + size(r1) + size(r2)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    93
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    94
  case STAR(r) => 1 + size(r)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    95
  case NTIMES(r, _) => 1 + size(r)
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    96
}
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    97
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    98
// EVIL1(n) has now a constant size, no matter
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
    99
// what n is; also the derivative only grows 
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   100
// moderately 
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   101
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   102
size(EVIL1(1))  // 7
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   103
size(EVIL1(3))  // 7
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   104
size(EVIL1(5))  // 7
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   105
size(EVIL1(7))  // 7
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   106
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   107
size(ders("".toList, EVIL1(5)))       // 7
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   108
size(ders("a".toList, EVIL1(5)))      // 16
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   109
size(ders("aa".toList, EVIL1(5)))     // 35
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   110
size(ders("aaa".toList, EVIL1(5)))    // 59
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   111
size(ders("aaaa".toList, EVIL1(5)))   // 88
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   112
size(ders("aaaaa".toList, EVIL1(5)))  // 122
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   113
size(ders("aaaaaa".toList, EVIL1(5))) // 151
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   114
478
48b842c997c7 updated
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   115
// but the size of the derivatives can still grow 
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   116
// quite dramatically in case of EVIL2
477
b78664a24f5d updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   117
564
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   118
size(ders("".toList, EVIL2))       // 5
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   119
size(ders("a".toList, EVIL2))      // 12
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   120
size(ders("aa".toList, EVIL2))     // 28
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   121
size(ders("aaa".toList, EVIL2))    // 58
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   122
size(ders("aaaa".toList, EVIL2))   // 116
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   123
size(ders("aaaaa".toList, EVIL2))  // 230
b5d57d7064bb updated
Christian Urban <urbanc@in.tum.de>
parents: 513
diff changeset
   124
size(ders("aaaaaa".toList, EVIL2)) // 456
566
b153c04834eb updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
   125
b153c04834eb updated
Christian Urban <urbanc@in.tum.de>
parents: 564
diff changeset
   126
size(ders(("a" * 20).toList, EVIL2)) // 7340068