progs/matcher/re3.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 29 Nov 2024 18:59:32 +0000
changeset 976 e9eac62928f5
parent 967 ce5de01b9632
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
929
9541e073f2ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
     1
// A version of the simple matcher with simplification 
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 882
diff changeset
     2
// of derivatives
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 882
diff changeset
     3
//
929
9541e073f2ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
     4
// this keeps the regular expressions (often) small, which
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
// is good for the run-time
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
// 
929
9541e073f2ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
     7
// call the test cases with X = {1,2,3,4}
725
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
//   amm re3.sc testX
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
// or 
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
//   amm re3.sc all
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
abstract class Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
case object ZERO extends Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
case object ONE extends Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
case class CHAR(c: Char) extends Rexp
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
case class STAR(r: Rexp) extends Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
case class NTIMES(r: Rexp, n: Int) extends Rexp 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
// the nullable function: tests whether the regular 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
// expression can recognise the empty string
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
def nullable (r: Rexp) : Boolean = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
  case ZERO => false
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
  case ONE => true
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
  case CHAR(_) => false
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
  case STAR(_) => true
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
  case NTIMES(r, i) => if (i == 0) true else nullable(r)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
// the derivative of a regular expression w.r.t. a character
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 782
diff changeset
    39
def der(c: Char, r: Rexp) : Rexp = r match {
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
  case ZERO => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
  case ONE => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
  case CHAR(d) => if (c == d) ONE else ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
  case SEQ(r1, r2) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
    else SEQ(der(c, r1), r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
  case STAR(r1) => SEQ(der(c, r1), STAR(r1))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
  case NTIMES(r, i) => 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
    if (i == 0) ZERO else SEQ(der(c, r), NTIMES(r, i - 1))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 882
diff changeset
    52
// simplification
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
def simp(r: Rexp) : Rexp = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
  case ALT(r1, r2) => (simp(r1), simp(r2)) match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
    case (ZERO, r2s) => r2s
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
    case (r1s, ZERO) => r1s
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
    case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
  case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
    case (ZERO, _) => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
    case (_, ZERO) => ZERO
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
    case (ONE, r2s) => r2s
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
    case (r1s, ONE) => r1s
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
    case (r1s, r2s) => SEQ(r1s, r2s)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
  case r => r
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
929
9541e073f2ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
    69
// the derivative w.r.t. a string (iterates der and simp)
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
def ders(s: List[Char], r: Rexp) : Rexp = s match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
  case Nil => r
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
  case c::s => ders(s, simp(der(c, r)))
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
// the main matcher function
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
def matcher(r: Rexp, s: String) : Boolean = 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
  nullable(ders(s.toList, r))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 882
diff changeset
    80
// Test Cases
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 882
diff changeset
    81
//============
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 882
diff changeset
    82
929
9541e073f2ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
    83
// optional is still just defined
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
def OPT(r: Rexp) = ALT(r, ONE)
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
// evil regular expressions: (a?){n} a{n}  and (a*)* b
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
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
    88
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
def time_needed[T](i: Int, code: => T) = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
  val start = System.nanoTime()
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
  for (j <- 1 to i) code
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
  val end = System.nanoTime()
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
  (end - start)/(i * 1.0e9)
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
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
    99
@arg(doc = "Test (a?{n}) (a{n})")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
@main
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
def test1() = {
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
   102
  println("Test (a?{n}) (a{n})")
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
   103
769
f9686b22db7e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
   104
  for (i <- 0 to 9000 by 1000) {
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
    println(f"$i: ${time_needed(3, matcher(EVIL1(i), "a" * i))}%.5f")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
  }
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
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   109
@arg(doc = "Test (a*)* b")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
@main
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
def test2() = {
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
   112
  println("Test (a*)* b")
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
   113
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
  for (i <- 0 to 6000000 by 500000) {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
    println(f"$i: ${time_needed(3, matcher(EVIL2, "a" * i))}%.5f")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
}
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 of a regular expressions - for testing purposes 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
def size(r: Rexp) : Int = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
  case ZERO => 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
  case ONE => 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
  case CHAR(_) => 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
  case ALT(r1, r2) => 1 + size(r1) + size(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
  case STAR(r) => 1 + size(r)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
  case NTIMES(r, _) => 1 + size(r)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
// now the size of the derivatives grows 
929
9541e073f2ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
   132
// much, much slower and actually maxes out
9541e073f2ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
   133
// at size 8
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
size(ders("".toList, EVIL2))      // 5
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
size(ders("a".toList, EVIL2))     // 8
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
size(ders("aa".toList, EVIL2))    // 8
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
size(ders("aaa".toList, EVIL2))   // 8
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
size(ders("aaaa".toList, EVIL2))  // 8
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
size(ders("aaaaa".toList, EVIL2)) // 8
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
929
9541e073f2ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
   142
size(ders(("a" * 20000).toList, EVIL2)) // 8
9541e073f2ed updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
   143
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   145
@arg(doc = "All tests.")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
@main
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
def all() = { test1(); test2() } 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
782
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   150
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   151
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   152
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   153
// PS:
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   154
//
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   155
// If you want to dig deeper into the topic, you can have 
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   156
// a look at these examples which still explode. They
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   157
// need an even more aggressive simplification.
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   158
782
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   159
// test: (a + aa)*
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   160
val EVIL3 = STAR(ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a'))))
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   161
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   162
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   163
@arg(doc = "Test EVIL3")
782
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   164
@main
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   165
def test3() = {
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   166
  println("Test (a + aa)*")
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   167
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   168
  for (i <- 0 to 30 by 5) {
782
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   169
    println(f"$i: ${time_needed(1, matcher(EVIL3, "a" * i))}%.5f")
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   170
  }
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   171
}
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   172
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   173
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   174
// test: (1 + a + aa)*
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   175
val EVIL4 = STAR(ALT(ONE, ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a')))))
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   176
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   177
@arg(doc = "Test EVIL4")
782
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   178
@main
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   179
def test4() = {
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   180
  println("Test (1 + a + aa)*")
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   181
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   182
  for (i <- 0 to 30 by 5) {
782
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   183
    println(f"$i: ${time_needed(1, matcher(EVIL4, "a" * i))}%.5f")
a26a20acd1c2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   184
  }
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 782
diff changeset
   185
}
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   186
967
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
   187
@main
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
   188
def test5() = {
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
   189
  println("Test (abcdef){n}")
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
   190
  val re = SEQ(CHAR('a'), SEQ(CHAR('b'), SEQ(CHAR('c'), SEQ(CHAR('d'), SEQ(CHAR('e'), CHAR('f'))))))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
   191
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
   192
  for (i <- 0 to 40000 by 5000) {
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
   193
    println(f"$i: ${time_needed(1, matcher(NTIMES(re, i), "abcdef" * i))}%.5f")
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
   194
  }
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
   195
}
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
   196
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
   197
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   198
@arg(doc = "Tests that show not all is hunky-dory, but a solution leads too far afield.")
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   199
@main
967
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 929
diff changeset
   200
def fail() = { test3(); test4(); test5() } 
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   201
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   202