progs/matcher/re1.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 22 Mar 2022 00:36:18 +0000
changeset 871 94b84d880c2b
parent 826 b0352633bf48
child 879 ad9d4a01e072
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 simple matcher for basic regular expressions
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
// Call the test cases with X = {1,2,3}
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
//   amm re1.sc testX
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
// or 
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 re1.sc all
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
    10
//
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
    11
745
7dc3643a0cc5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 725
diff changeset
    12
 
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
// regular expressions
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                    // matches nothing
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
case object ONE extends Rexp                     // matches an empty string
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
case class CHAR(c: Char) extends Rexp            // matches a character c
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
case class ALT(r1: Rexp, r2: Rexp) extends Rexp  // alternative
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp  // sequence
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
case class STAR(r: Rexp) extends Rexp            // star
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
759
d70dd0b57e35 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 745
diff changeset
    22
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
// nullable function: tests whether a regular 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
// expression can recognise the empty string
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
def nullable(r: Rexp) : Boolean = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
  case ZERO => false
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
  case ONE => true
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
  case CHAR(_) => false
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
  case STAR(_) => true
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
// the derivative of a regular expression w.r.t. a character
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))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
// the derivative w.r.t. a string (iterates der)
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
    47
def ders(s: List[Char], r: Rexp) : Rexp = s match {
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
  case Nil => r
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
  case c::s => ders(s, der(c, r))
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
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
// the main matcher function
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
871
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    57
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    58
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    59
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
// some examples from the homework
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
val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b')))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
der('a', r)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
der('b', r)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
der('c', r)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
val r2 = SEQ(SEQ(CHAR('x'), CHAR('y')), CHAR('z'))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
der('x', r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
der('y', der('x', r2))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
der('z', der('y', der('x', r2)))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
// the optional regular expression (one or zero times)
765
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 759
diff changeset
    74
def OPT(r: Rexp) = ALT(r, ONE)   // r + 1
725
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
// the n-times regular expression (explicitly expanded)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
def NTIMES(r: Rexp, n: Int) : Rexp = n match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
  case 0 => ONE
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
  case 1 => r
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
  case n => SEQ(r, NTIMES(r, n - 1))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
// Test Cases
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
765
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 759
diff changeset
    87
// the evil regular expression  (a?){n} a{n}
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 759
diff changeset
    88
def EVIL1(n: Int) = 
b294cfbb5c01 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 759
diff changeset
    89
  SEQ(NTIMES(OPT(CHAR('a')), n), NTIMES(CHAR('a'), n))
725
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
// the evil regular expression (a*)* b
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
// for measuring time
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
def time_needed[T](i: Int, code: => T) = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
  val start = System.nanoTime()
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
  for (j <- 1 to i) code
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
  val end = System.nanoTime()
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
  (end - start) / (i * 1.0e9)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
// test: (a?{n}) (a{n})
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   104
@arg(doc = "Test (a?{n}) (a{n})")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
@main
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
def test1() = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
  println("Test (a?{n}) (a{n})")
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
  for (i <- 0 to 20 by 2) {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
    println(f"$i: ${time_needed(2, matcher(EVIL1(i), "a" * i))}%.5f")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
  }
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
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
// test: (a*)* b
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   115
@arg(doc = "Test (a*)* b")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
@main
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
def test2() = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
  println("Test (a*)* b")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
  for (i <- 0 to 20 by 2) {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
    println(f"$i: ${time_needed(2, matcher(EVIL2, "a" * i))}%.5f")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
// the size of a regular expressions - for testing purposes 
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
def size(r: Rexp) : Int = r match {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
  case ZERO => 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
  case ONE => 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
  case CHAR(_) => 1
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
  case ALT(r1, r2) => 1 + size(r1) + size(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
  case SEQ(r1, r2) => 1 + size(r1) + size(r2)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
  case STAR(r) => 1 + size(r)
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
}
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
// the expicit expansion in EVIL1(n) increases
769
f9686b22db7e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 765
diff changeset
   136
// drastically its size - (a?){n} a{n}
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
size(EVIL1(1))  // 5
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
size(EVIL1(3))  // 17
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
size(EVIL1(5))  // 29
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
size(EVIL1(7))  // 41
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
size(EVIL1(20)) // 119
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
769
f9686b22db7e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 765
diff changeset
   144
size(ders(("a" * 20).toList, EVIL1(20))) 
f9686b22db7e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 765
diff changeset
   145
f9686b22db7e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 765
diff changeset
   146
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
// given a regular expression and building successive
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
// derivatives might result into bigger and bigger
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
// regular expressions...here is an example for this:
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
// (a+b)* o a o b o (a+b)*
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
val BIG_aux = STAR(ALT(CHAR('a'), CHAR('b')))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
val BIG = SEQ(BIG_aux, SEQ(CHAR('a'),SEQ(CHAR('b'), BIG_aux)))
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
size(ders("".toList, BIG))              // 13
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
size(ders("ab".toList, BIG))            // 51
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
size(ders("abab".toList, BIG))          // 112
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
size(ders("ababab".toList, BIG))        // 191
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
size(ders("abababab".toList, BIG))      // 288
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
size(ders("ababababab".toList, BIG))    // 403
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
size(ders("abababababab".toList, BIG))  // 536
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
size(ders(("ab" * 200).toList, BIG))    // 366808
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   166
@arg(doc = "Test (a + b)* o (a o b) o (a + b)*")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
@main
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
def test3() = {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
  println("Test (a + b)* o (a o b) o (a + b)*")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
  for (i <- 0 to 200 by 10) {
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
    println(f"$i: ${time_needed(2, matcher(BIG, "ab" * i))}%.5f")
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
  }
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
}
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   179
@arg(doc = "All tests.")
725
f345e89895f5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
@main
825
dca072e2bb7d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 769
diff changeset
   181
def all() = { test1(); test2() ; test3() } 
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   182
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   183
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   184
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 825
diff changeset
   185
// runs with amm2 and amm3
871
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   186
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   187
def pp(r: Rexp): String = r match {
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   188
  case SEQ(CHAR(a1), SEQ(r1, r2)) => s"${a1}${pp(r1)}${pp(r2)}"
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   189
  case SEQ(ONE, SEQ(r1, r2)) => s"1${pp(r1)}${pp(r2)}"
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   190
  case SEQ(ZERO, SEQ(r1, r2)) => s"0${pp(r1)}${pp(r2)}"
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   191
  case SEQ(CHAR(a1), CHAR(a2)) => s"${a1}${a2}"
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   192
  case SEQ(ONE, CHAR(a2)) => s"1${a2}"
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   193
  case SEQ(ZERO, CHAR(a2)) => s"0${a2}" 
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   194
  case ZERO => "0"
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   195
  case ONE => "1"
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   196
  case CHAR(a) => a.toString
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   197
  case ALT(r1, r2) => s"(${pp(r1)} + ${pp(r2)})"
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   198
  case SEQ(r1, r2) => s"(${pp(r1)} o ${pp(r2)})"
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   199
  case STAR(r1) => s"(${pp(r1)})*"
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   200
}
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   201
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   202
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   203
val REG = STAR(ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a'))))
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   204
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   205
print(pp(ders("".toList, REG)))
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   206
print(pp(ders("a".toList, REG)))
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   207
print(pp(ders("aa".toList, REG)))
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   208
print(pp(ders("aaa".toList, REG)))
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   209
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   210
size(ders("".toList, REG))        // 6
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   211
size(ders("a".toList, REG))       // 12
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   212
size(ders("aa".toList, REG))      // 27
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   213
size(ders("aaa".toList, REG))     // 55
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   214
size(ders("aaaa".toList, REG))    // 98
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   215
size(ders("aaaaa".toList, REG))   // 169
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   216
size(ders("aaaaaa".toList, REG))  // 283
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   217
size(ders(("a" * 7).toList, REG)) // 468
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   218
size(ders(("a" * 8).toList, REG)) // 767
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   219
size(ders(("a" * 9).toList, REG)) // 1251
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   220
size(ders(("a" * 10).toList, REG))// 2034
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   221
size(ders(("a" * 11).toList, REG))// 3301
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   222
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   223
for (i <- (0 to 40)) {
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   224
  println(s"$i:" + size(ders(("a" * i).toList, REG)))
94b84d880c2b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
   225
}