progs/matcher/re2.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 05 Jul 2023 16:12:40 +0100
changeset 913 eef6a56c185a
parent 879 ad9d4a01e072
child 919 53f08d873e09
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// A Version with an explicit n-times regular expression;
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
// this keeps the size of the regular expression in the
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
// EVIL1 test-case quite small
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
// call the test cases with X = {1,2}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
//   amm re2.sc testX
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
// or 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
//   amm re2.sc all
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
abstract class Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
case object ZERO extends Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
case object ONE extends Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
case class CHAR(c: Char) extends Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
case class STAR(r: Rexp) extends Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
case class NTIMES(r: Rexp, n: Int) extends Rexp   //explicit constructor for n-times
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
def nullable (r: Rexp) : Boolean = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
  case ZERO => false
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
  case ONE => true
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
  case CHAR(_) => false
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
  case STAR(_) => true
769
f9686b22db7e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 767
diff changeset
    31
  case NTIMES(r, n) => if (n == 0) true else nullable(r)
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
    35
def der(c: Char, r: Rexp) : Rexp = r match {
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
  case ZERO => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
  case ONE => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
  case CHAR(d) => if (c == d) ONE else ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
  case SEQ(r1, r2) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
    else SEQ(der(c, r1), r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
  case STAR(r1) => SEQ(der(c, r1), STAR(r1))
769
f9686b22db7e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 767
diff changeset
    44
  case NTIMES(r, n) => 
f9686b22db7e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 767
diff changeset
    45
    if (n == 0) ZERO else SEQ(der(c, r), NTIMES(r, n - 1))
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
    48
def ders(s: List[Char], r: Rexp) : Rexp = s match {
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
  case Nil => r
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
  case c::s => ders(s, der(c, r))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
def matcher(r: Rexp, s: String) : Boolean = 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
  nullable(ders(s.toList, r))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
// the optional regular expression: one or zero times
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
// this regular expression is still defined in terms of ALT
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
def OPT(r: Rexp) = ALT(r, ONE)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
// Test Cases
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
// evil regular expressions
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
def EVIL1(n: Int) = SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
def time_needed[T](i: Int, code: => T) = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
  val start = System.nanoTime()
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
  for (j <- 1 to i) code
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
  val end = System.nanoTime()
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
  (end - start) / (i * 1.0e9)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
    77
@arg(doc = "Test (a?{n}) (a{n})")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
@main
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
def test1() = {
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
    80
  println("Test (a?{n}) (a{n})")
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
    81
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
  for (i <- 0 to 1000 by 100) {
769
f9686b22db7e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 767
diff changeset
    83
    println(f"$i: ${time_needed(1, matcher(EVIL1(i), "a" * i))}%.5f")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
}  
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
    87
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
    88
@arg(doc = "Test (a*)* b")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
@main
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
def test2() = {
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
    91
  println("Test (a*)* b")
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
    92
767
bdd12391d345 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    93
  for (i <- 0 to 30 by 2) {
769
f9686b22db7e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 767
diff changeset
    94
    println(f"$i: ${time_needed(1, matcher(EVIL2, "a" * i))}%.5f")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
// the size of a regular expressions - for testing purposes 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
def size(r: Rexp) : Int = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
  case ZERO => 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
  case ONE => 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
  case CHAR(_) => 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
  case ALT(r1, r2) => 1 + size(r1) + size(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
  case STAR(r) => 1 + size(r)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
  case NTIMES(r, _) => 1 + size(r)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
// EVIL1(n) has now a constant size, no matter
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
// what n is; also the derivative only grows 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
// moderately 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
size(EVIL1(1))  // 7
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
size(EVIL1(3))  // 7
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
size(EVIL1(5))  // 7
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
size(EVIL1(7))  // 7
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
size(EVIL1(20)) // 7
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
size(ders("".toList, EVIL1(5)))       // 7
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
size(ders("a".toList, EVIL1(5)))      // 16
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
size(ders("aa".toList, EVIL1(5)))     // 35
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
size(ders("aaa".toList, EVIL1(5)))    // 59
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
size(ders("aaaa".toList, EVIL1(5)))   // 88
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
size(ders("aaaaa".toList, EVIL1(5)))  // 122
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
size(ders("aaaaaa".toList, EVIL1(5))) // 151
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
769
f9686b22db7e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 767
diff changeset
   127
size(ders(("a" * 20).toList, EVIL1(5))) 
f9686b22db7e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 767
diff changeset
   128
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
// but the size of the derivatives can still grow 
769
f9686b22db7e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 767
diff changeset
   130
// quite dramatically in case of EVIL2:  (a*)* b
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
size(ders("".toList, EVIL2))       // 5
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
size(ders("a".toList, EVIL2))      // 12
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
size(ders("aa".toList, EVIL2))     // 28
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
size(ders("aaa".toList, EVIL2))    // 58
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
size(ders("aaaa".toList, EVIL2))   // 116
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
size(ders("aaaaa".toList, EVIL2))  // 230
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
size(ders("aaaaaa".toList, EVIL2)) // 456
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
size(ders(("a" * 20).toList, EVIL2)) // 7340068
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   144
@arg(doc = "All tests.")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
@main
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   146
def all() = { test1(); test2() } 
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   147