progs/cw1.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 08 Oct 2019 21:12:52 +0100
changeset 649 e83afb44f276
parent 586 451a95e1bc25
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
     1
// CW 1
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
import scala.language.implicitConversions    
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
import scala.language.reflectiveCalls 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
     5
abstract class Rexp
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
     6
case object ZERO extends Rexp                            // matches nothing
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
     7
case object ONE extends Rexp                             // matches the empty string
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
     8
case class CHAR(c: Char) extends Rexp                    // matches a character c
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
     9
case class ALT(r1: Rexp, r2: Rexp) extends Rexp          // alternative
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    10
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp          // sequence
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    11
case class STAR(r: Rexp) extends Rexp                    // star
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    12
case class RANGE(cs: Set[Char]) extends Rexp             // set of characters
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    13
case class PLUS(r: Rexp) extends Rexp                    // plus
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    14
case class OPT(r: Rexp) extends Rexp                     // optional
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    15
case class NTIMES(r: Rexp, n: Int) extends Rexp          // n-times
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    16
case class UPNTIMES(r: Rexp, n: Int) extends Rexp        // up n-times
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    17
case class FROMNTIMES(r: Rexp, n: Int) extends Rexp      // from n-times
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    18
case class NMTIMES(r: Rexp, n: Int, m: Int) extends Rexp // between nm-times
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    19
case class NOT(r: Rexp) extends Rexp                     // not
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    20
case class CFUN(f: Char => Boolean) extends Rexp         // subsuming CHAR and RANGE
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
586
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
    22
def CHAR(c: Char) = CFUN(d => c == d)
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
    23
val ALL = CFUN(_ => true)
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
    24
def RANGE(cs: Set[Char]) = CFUN(d => cs.contains(d))
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
    25
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
    26
def CHAR(c: Char) = CFUN(c == _)
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
    27
val ALL = CFUN(_ => true)
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
    28
def RANGE(cs: Set[Char]) = CFUN(cs.contains(_))
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
    29
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
// nullable function: tests whether the regular 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
// expression can recognise the empty string
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
def nullable (r: Rexp) : Boolean = r match {
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    34
  case ZERO => false
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    35
  case ONE => true
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  case CHAR(_) => false
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  case STAR(_) => true
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    40
  case RANGE(_) => false
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  case PLUS(r) => nullable(r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  case OPT(_) => true
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    43
  case NTIMES(r, n) => if (n == 0) true else nullable(r)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    44
  case UPNTIMES(_, _) => true
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    45
  case FROMNTIMES(r, n) => if (n == 0) true else nullable(r)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    46
  case NMTIMES(r, n, m) => if (n == 0) true else nullable(r)
547
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
    47
  case NOT(r) => !nullable(r)
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    48
  case CFUN(_) => false
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
    49
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
    50
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
// derivative of a regular expression w.r.t. a character
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
def der (c: Char, r: Rexp) : Rexp = r match {
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    53
  case ZERO => ZERO
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    54
  case ONE => ZERO
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    55
  case CHAR(d) => if (c == d) ONE else ZERO
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
  case SEQ(r1, r2) => 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
    else SEQ(der(c, r1), r2)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
  case STAR(r) => SEQ(der(c, r), STAR(r))
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    61
  case RANGE(cs) => if (cs contains c) ONE else ZERO
190
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
    62
  case PLUS(r) => SEQ(der(c, r), STAR(r))
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    63
  case OPT(r) => der(c, r)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    64
  case NTIMES(r, n) => 
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    65
    if (n == 0) ZERO else SEQ(der(c, r), NTIMES(r, n - 1))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    66
  case UPNTIMES(r, n) =>
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    67
    if (n == 0) ZERO else SEQ(der(c, r), UPNTIMES(r, n - 1))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    68
  case FROMNTIMES(r, n) =>
547
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
    69
    if (n == 0) SEQ(der(c, r), STAR(r)) else SEQ(der(c, r), FROMNTIMES(r, n - 1))
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    70
  case NMTIMES(r, n, m) =>
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    71
    if (m < n) ZERO else
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    72
    if (n == 0 && m == 0) ZERO else 
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    73
    if (n == 0) SEQ(der(c, r), UPNTIMES(r, m - 1)) 
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    74
    else SEQ(der(c, r), NMTIMES(r, n - 1, m - 1)) 
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
  case NOT(r) => NOT(der (c, r))
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    76
  case CFUN(f) => if (f(c)) ONE else ZERO
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    79
def simp(r: Rexp) : Rexp = r match {
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    80
  case ALT(r1, r2) => (simp(r1), simp(r2)) match {
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    81
    case (ZERO, r2s) => r2s
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    82
    case (r1s, ZERO) => r1s
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    83
    case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    84
  }
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    85
  case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    86
    case (ZERO, _) => ZERO
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    87
    case (_, ZERO) => ZERO
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    88
    case (ONE, r2s) => r2s
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    89
    case (r1s, ONE) => r1s
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    90
    case (r1s, r2s) => SEQ(r1s, r2s)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    91
  }
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    92
  case r => r
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    93
}
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    94
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    95
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
// derivative w.r.t. a string (iterates der)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
def ders (s: List[Char], r: Rexp) : Rexp = s match {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
  case Nil => r
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
  case c::s => ders(s, der(c, r))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
def ders_simp (s: List[Char], r: Rexp) : Rexp = s match {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
  case Nil => r
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   104
  case c::s => ders_simp(s, simp(der(c, r)))
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   107
// main matcher function including simplification
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   108
def matcher(r: Rexp, s: String) : Boolean = 
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   109
  nullable(ders_simp(s.toList, r))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   110
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   111
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
// some convenience for typing in regular expressions
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
def charlist2rexp(s : List[Char]) : Rexp = s match {
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   115
  case Nil => ONE
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
  case c::Nil => CHAR(c)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
implicit def RexpOps (r: Rexp) = new {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
  def | (s: Rexp) = ALT(r, s)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
  def % = STAR(r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
  def ~ (s: Rexp) = SEQ(r, s)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
implicit def stringOps (s: String) = new {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
  def | (r: Rexp) = ALT(s, r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
  def | (r: String) = ALT(s, r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
  def % = STAR(s)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
  def ~ (r: Rexp) = SEQ(s, r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
  def ~ (r: String) = SEQ(s, r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   135
val r1 = NTIMES("a", 3)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   136
val r2 = NTIMES(OPT("a"), 3)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   137
val r3 = UPNTIMES("a", 3)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   138
val r4 = UPNTIMES(OPT("a"), 3)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   139
val r5 = NMTIMES("a", 3, 5)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   140
val r6 = NMTIMES(OPT("a"), 3, 5)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   141
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   142
val rs = List(r1, r2, r3, r4, r5, r6)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   143
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   144
rs.map(matcher(_, ""))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   145
rs.map(matcher(_, "a"))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   146
rs.map(matcher(_, "aa"))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   147
rs.map(matcher(_, "aaa"))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   148
rs.map(matcher(_, "aaaa"))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   149
rs.map(matcher(_, "aaaaa"))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   150
rs.map(matcher(_, "aaaaaa"))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   151
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
println("EMAIL:")
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   153
val LOWERCASE = ('a' to 'z').toSet
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   154
val DIGITS = ('0' to '9').toSet
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   155
val EMAIL = { PLUS(CFUN(LOWERCASE | DIGITS | ("_.-").toSet)) ~ "@" ~ 
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   156
              PLUS(CFUN(LOWERCASE | DIGITS | (".-").toSet)) ~ "." ~
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   157
              NMTIMES(CFUN(LOWERCASE | Set('.')), 2, 6) }
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
val my_email = "christian.urban@kcl.ac.uk"
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
println(EMAIL);
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
println(matcher(EMAIL, my_email))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
println(ders_simp(my_email.toList, EMAIL))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
println("COMMENTS:")
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   166
val ALL = CFUN((c:Char) => true)
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
val COMMENT = "/*" ~ (NOT(ALL.% ~ "*/" ~ ALL.%)) ~ "*/"
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
println(matcher(COMMENT, "/**/"))
493
4e97783862ff updated
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
   170
println(matcher(COMMENT, "/*foobar*/"))
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
println(matcher(COMMENT, "/*test*/test*/"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
println(matcher(COMMENT, "/*test/*test*/"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
println("EVILS:")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
val EVIL1 = PLUS(PLUS("a" ~ "a" ~ "a"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
val EVIL2 = PLUS(PLUS("aaaaaaaaaaaaaaaaaaa" ~ OPT("a")))  // 19 as ~ a?
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
//40 * aaa matches
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
//43 * aaa + aa does not
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
//45 * aaa + a
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
println("EVIL1:")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
println(matcher(EVIL1, "aaa" * 40))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
println(matcher(EVIL1, "aaa" * 43 + "aa"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
println(matcher(EVIL1, "aaa" * 45 + "a"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
println("EVIL2:")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
println(matcher(EVIL2, "aaa" * 40))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
println(matcher(EVIL2, "aaa" * 43 + "aa"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
println(matcher(EVIL2, "aaa" * 45 + "a"))
190
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   191
192
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 190
diff changeset
   192
println("TEST for bug pointed out by Filips Ivanovs")
493
4e97783862ff updated
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
   193
val test = NMTIMES(CFUN(LOWERCASE | Set('.')), 2, 6)
190
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   194
  
192
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 190
diff changeset
   195
println(matcher(test,"a"))
190
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   196
println(matcher(test,"ab"))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   197
println(matcher(test,"abcdef"))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   198
println(matcher(test,"abc"))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   199
println(matcher(test,"...."))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   200
println(matcher(test,"asdfg"))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   201
println(matcher(test,"abcdefg"))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   202
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   203
println(test)
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   204
println(ders_simp("a".toList, test))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   205
println(ders_simp("aa".toList, test))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   206
println(ders_simp("aaa".toList, test))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   207
println(ders_simp("aaaa".toList, test))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   208
println(ders_simp("aaaaa".toList, test))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   209
println(ders_simp("aaaaaa".toList, test))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   210
println(ders_simp("aaaaaaa".toList, test))
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   211
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   212
def TEST(s: String, r: Rexp) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   213
  println("Rexp |" + s + "|")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   214
  println("Derivative:\n" + ders_simp(s.toList, r))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   215
  println("Is Nullable: " + nullable(ders_simp(s.toList, r)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   216
  Console.readLine
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   217
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   218
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   219
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   220
val ALL2 = "a" | "f"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   221
val COMMENT2 = ("/*" ~ NOT(ALL2.% ~ "*/" ~ ALL2.%) ~ "*/")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   222
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   223
println("1) TEST TEST")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   224
TEST("", COMMENT2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   225
TEST("/", COMMENT2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   226
TEST("/*", COMMENT2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   227
TEST("/*f", COMMENT2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   228
TEST("/*f*", COMMENT2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   229
TEST("/*f*/", COMMENT2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   230
TEST("/*f*/ ", COMMENT2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   231
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   232
val ALL3 = "a" | "f"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   233
val COMMENT3 = ("/" ~ NOT(ALL3.% ~ "&" ~ ALL3.%) ~ "&")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   234
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   235
println("2) TEST TEST")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   236
TEST("", COMMENT3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   237
TEST("/", COMMENT3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   238
TEST("/", COMMENT3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   239
TEST("/f", COMMENT3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   240
TEST("/f&", COMMENT3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   241
TEST("/f& ", COMMENT3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   242
547
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   243
//test: ("a" | "aa") ~ ("a" | "aa")*
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   244
val auxEVIL3 = ALT(CHAR('a'), SEQ(CHAR('a'), CHAR('a')))
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   245
val EVIL3 = SEQ(auxEVIL3, STAR(auxEVIL3))
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   246
val EVIL3p = FROMNTIMES(auxEVIL3, 1)
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   247
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 547
diff changeset
   248
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 547
diff changeset
   249
for (i <- 1 to 100 by 1) {
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 547
diff changeset
   250
  println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL3, "a" * i))) + " size: " + 
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 547
diff changeset
   251
	  size(ders(("a" * i).toList, EVIL3)))
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 547
diff changeset
   252
}
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 547
diff changeset
   253
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 547
diff changeset
   254
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 547
diff changeset
   255
547
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   256
val t1 = EVIL3
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   257
val t2 = simp(der('a', t1))
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   258
val t3 = simp(der('a', t2))
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   259
val t4 = simp(der('a', t3))
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   260
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   261
val t1 = EVIL3p
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   262
val t2 = simp(der('a', t1))
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   263
val t3 = simp(der('a', t2))
81eb43c41416 updated
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   264
val t4 = simp(der('a', t3))