progs/matcher/cw1.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 16 Oct 2024 13:14:13 +0100
changeset 968 d8d8911a3d6f
parent 967 ce5de01b9632
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
967
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Christian's Solution for CW 1
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
import scala.language.implicitConversions
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
// basic regular expressions
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
abstract class Rexp
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
case object ZERO extends Rexp
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
case object ONE extends Rexp
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
case class CHAR(c: Char) extends Rexp
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
case class STAR(r: Rexp) extends Rexp 
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
 
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
// extended regular expressions 
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
case class RANGE(cs: Set[Char]) extends Rexp             // set of characters
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
case class PLUS(r: Rexp) extends Rexp                    // plus
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
case class OPTIONAL(r: Rexp) extends Rexp                // optional
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
case class INTER(r1: Rexp, r2: Rexp) extends Rexp        // intersection
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
case class NTIMES(r: Rexp, n: Int) extends Rexp          // n-times
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
case class UPTO(r: Rexp, n: Int) extends Rexp            // up n-times
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
case class FROM(r: Rexp, n: Int) extends Rexp            // from n-times
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
case class BETWEEN(r: Rexp, n: Int, m: Int) extends Rexp // between nm-times
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
case class NOT(r: Rexp) extends Rexp                     // not
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
// general version of CHAR, RANGE and ALL
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
case class CFUN(f: Char => Boolean) extends Rexp         // subsuming CHAR and RANGE
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
def FCHAR(c: Char) = CFUN(c == _)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
val FALL = CFUN(_ => true)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
def FRANGE(cs: Set[Char]) = CFUN(cs.contains(_))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
// the nullable function: tests whether the regular 
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
// expression can recognise the empty string
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
def nullable (r: Rexp) : Boolean = r match {
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
  case ZERO => false
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
  case ONE => true
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
  case CHAR(_) => false
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
  case STAR(_) => true
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
  case RANGE(_) => false
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
  case PLUS(r) => nullable(r)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
  case OPTIONAL(_) => true
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
  case INTER(r1, r2) => nullable(r1) && nullable(r2)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
  case NTIMES(r, n) => if (n == 0) true else nullable(r)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
  case UPTO(_, _) => true
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
  case FROM(r, n) => if (n == 0) true else nullable(r)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
  case BETWEEN(r, n, m) => if (n == 0) true else nullable(r)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
  case NOT(r) => !nullable(r)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
  case CFUN(_) => false
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
}
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
// the derivative of a regular expression w.r.t. a character
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
def der(c: Char, r: Rexp) : Rexp = r match {
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
  case ZERO => ZERO
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
  case ONE => ZERO
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
  case CHAR(d) => if (c == d) ONE else ZERO
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
  case SEQ(r1, r2) => 
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
    else SEQ(der(c, r1), r2)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
  case STAR(r1) => SEQ(der(c, r1), STAR(r1))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
  case RANGE(cs) => if (cs contains c) ONE else ZERO
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
  case PLUS(r) => SEQ(der(c, r), STAR(r))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
  case OPTIONAL(r) => der(c, r)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  case INTER(r1, r2) => INTER(der(c, r1), der(c, r2))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
  case NTIMES(r, n) => 
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
    if (n == 0) ZERO else SEQ(der(c, r), NTIMES(r, n - 1))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
  case UPTO(r, n) =>
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
    if (n == 0) ZERO else SEQ(der(c, r), UPTO(r, n - 1))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
  case FROM(r, n) =>
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
    if (n == 0) SEQ(der(c, r), STAR(r)) else SEQ(der(c, r), FROM(r, n - 1))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
  case BETWEEN(r, n, m) =>
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
    if (m < n) ZERO else
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
    if (n == 0 && m == 0) ZERO else 
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
    if (n == 0) SEQ(der(c, r), UPTO(r, m - 1)) 
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
    else SEQ(der(c, r), BETWEEN(r, n - 1, m - 1)) 
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
  case NOT(r) => NOT(der (c, r))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
  case CFUN(f) => if (f(c)) ONE else ZERO
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
}
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
// simplification
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
def simp(r: Rexp) : Rexp = r match {
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
  case ALT(r1, r2) => (simp(r1), simp(r2)) match {
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
    case (ZERO, r2s) => r2s
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
    case (r1s, ZERO) => r1s
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
    case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
  }
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
  case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
    case (ZERO, _) => ZERO
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
    case (_, ZERO) => ZERO
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
    case (ONE, r2s) => r2s
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
    case (r1s, ONE) => r1s
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
    case (r1s, r2s) => SEQ(r1s, r2s)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
  }
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
  case r => r
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
}
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
// the derivative w.r.t. a string (iterates der)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
def ders(s: List[Char], r: Rexp) : Rexp = s match {
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
  case Nil => r
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
  case c::s => ders(s, simp(der(c, r)))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
}
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
// the main matcher function
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
def matcher(r: Rexp, s: String) : Boolean = 
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
  nullable(ders(s.toList, r))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
// Test Cases
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
//============
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
// some syntactic convenience
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
def charlist2rexp(s: List[Char]) : Rexp = s match {
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
  case Nil => ONE
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
  case c::Nil => CHAR(c)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
}
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
given Conversion[String, Rexp] = (s => charlist2rexp(s.toList))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
extension (r: Rexp) {
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
  def ~ (s: Rexp) = SEQ(r, s)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
  def % = STAR(r)
968
d8d8911a3d6f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 967
diff changeset
   125
  def | (s: Rexp) = ALT(r, s)
967
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
}
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
println("EMAIL:")
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
val LOWERCASE = ('a' to 'z').toSet
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
val DIGITS = ('0' to '9').toSet
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
val SYMBOLS1 = ("_.-").toSet
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
val SYMBOLS2 = (".-").toSet
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
val EMAIL = { PLUS(CFUN(LOWERCASE | DIGITS | SYMBOLS1)) ~ "@" ~ 
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
              PLUS(CFUN(LOWERCASE | DIGITS | SYMBOLS2)) ~ "." ~
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
              BETWEEN(CFUN(LOWERCASE | Set('.')), 2, 6) }
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
val my_email = "christian.urban@kcl.ac.uk"
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
println(EMAIL);
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
println(matcher(EMAIL, my_email))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
println(ders(my_email.toList,EMAIL))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
val ALL = CFUN((c:Char) => true)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
val COMMENT = "/*" ~ (NOT(ALL.% ~ "*/" ~ ALL.%)) ~ " * /"
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
println(matcher(COMMENT, "/**/"))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
println(matcher(COMMENT, "/*foobar*/"))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
println(matcher(COMMENT, "/*test*/test*/"))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
println(matcher(COMMENT, "/*test/*test*/"))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
println("\n\nTEST TEST\n")
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
val r1 = PLUS(PLUS(SEQ(CHAR('a'), SEQ(CHAR('a'), CHAR('a')))))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
val r2 = PLUS(PLUS(SEQ(BETWEEN(CHAR('a'), 19, 19), OPTIONAL(CHAR('a')))))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
val s1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
val s2 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
val s3 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
for (s <- List(s1,s2,s3)) {
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
  println(matcher(r1, s))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
  println(matcher(r2, s))
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
}
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
 
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
// for measuring time
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
def time_needed[T](i: Int, code: => T) = {
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
  val start = System.nanoTime()
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
  for (j <- 1 to i) code
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
  val end = System.nanoTime()
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
  (end - start) / (i * 1.0e9)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
}
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
//@main
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
def test(file: String) = {
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
    println("Test a{n}")
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
    for (i <- 0 to 200000 by 5000) {
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
      val re = NTIMES(SEQ(SEQ(CHAR('a'), CHAR('b')), CHAR('c')), i)
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
      print(f"$i: ${time_needed(2, matcher(re, "abc" * i))}%.5f")
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
      println(s" ${matcher(re, "abcd" * i)}")
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
    }
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
}
ce5de01b9632 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186