progs/cw1.scala
author cu
Tue, 03 Oct 2017 23:35:16 +0100
changeset 513 676e6484f29b
parent 493 4e97783862ff
child 547 81eb43c41416
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
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
// nullable function: tests whether the regular 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
// expression can recognise the empty string
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
def nullable (r: Rexp) : Boolean = r match {
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    26
  case ZERO => false
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    27
  case ONE => true
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  case CHAR(_) => false
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
  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
    30
  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
    31
  case STAR(_) => true
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    32
  case RANGE(_) => false
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  case PLUS(r) => nullable(r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  case OPT(_) => true
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    35
  case NTIMES(r, n) => if (n == 0) true else nullable(r)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    36
  case UPNTIMES(_, _) => true
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    37
  case FROMNTIMES(r, n) => if (n == 0) true else nullable(r)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    38
  case NMTIMES(r, n, m) => if (n == 0) true else nullable(r)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    39
  case NOT(r) => !(nullable(r))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    40
  case CFUN(_) => false
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
    41
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
    42
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
// 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
    44
def der (c: Char, r: Rexp) : Rexp = r match {
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    45
  case ZERO => ZERO
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    46
  case ONE => ZERO
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    47
  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
    48
  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
    49
  case SEQ(r1, r2) => 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
    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
    51
    else SEQ(der(c, r1), r2)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  case STAR(r) => SEQ(der(c, r), STAR(r))
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    53
  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
    54
  case PLUS(r) => SEQ(der(c, r), STAR(r))
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    55
  case OPT(r) => der(c, r)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    56
  case NTIMES(r, n) => 
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    57
    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
    58
  case UPNTIMES(r, n) =>
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    59
    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
    60
  case FROMNTIMES(r, n) =>
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    61
    if (n == 0) ZERO else SEQ(der(c, r), FROMNTIMES(r, n - 1))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    62
  case NMTIMES(r, n, m) =>
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    63
    if (m < n) ZERO else
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    64
    if (n == 0 && m == 0) ZERO else 
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    65
    if (n == 0) SEQ(der(c, r), UPNTIMES(r, m - 1)) 
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    66
    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
    67
  case NOT(r) => NOT(der (c, r))
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    68
  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
    69
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    71
def simp(r: Rexp) : Rexp = r match {
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    72
  case ALT(r1, r2) => (simp(r1), simp(r2)) match {
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    73
    case (ZERO, r2s) => r2s
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    74
    case (r1s, ZERO) => r1s
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    75
    case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    76
  }
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    77
  case SEQ(r1, r2) =>  (simp(r1), simp(r2)) match {
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    78
    case (ZERO, _) => ZERO
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    79
    case (_, ZERO) => ZERO
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    80
    case (ONE, r2s) => r2s
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    81
    case (r1s, ONE) => r1s
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    82
    case (r1s, r2s) => SEQ(r1s, r2s)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    83
  }
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    84
  case r => r
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    85
}
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    86
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    87
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
// 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
    89
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
    90
  case Nil => r
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
  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
    92
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
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
    95
  case Nil => r
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    96
  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
    97
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
    99
// main matcher function including simplification
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   100
def matcher(r: Rexp, s: String) : Boolean = 
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   101
  nullable(ders_simp(s.toList, r))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   102
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   103
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
// some convenience for typing in regular expressions
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
def charlist2rexp(s : List[Char]) : Rexp = s match {
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   107
  case Nil => ONE
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
  case c::Nil => CHAR(c)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
  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
   110
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
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
   112
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
implicit def RexpOps (r: Rexp) = new {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
  def | (s: Rexp) = ALT(r, s)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
  def % = STAR(r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
  def ~ (s: Rexp) = SEQ(r, s)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
}
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 stringOps (s: String) = new {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
  def | (r: Rexp) = ALT(s, r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
  def | (r: String) = ALT(s, r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
  def % = STAR(s)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
  def ~ (r: Rexp) = SEQ(s, r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
  def ~ (r: String) = SEQ(s, r)
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
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   127
val r1 = NTIMES("a", 3)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   128
val r2 = NTIMES(OPT("a"), 3)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   129
val r3 = UPNTIMES("a", 3)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   130
val r4 = UPNTIMES(OPT("a"), 3)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   131
val r5 = NMTIMES("a", 3, 5)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   132
val r6 = NMTIMES(OPT("a"), 3, 5)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   133
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   134
val rs = List(r1, r2, r3, r4, r5, r6)
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   135
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   136
rs.map(matcher(_, ""))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   137
rs.map(matcher(_, "a"))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   138
rs.map(matcher(_, "aa"))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   139
rs.map(matcher(_, "aaa"))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   140
rs.map(matcher(_, "aaaa"))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   141
rs.map(matcher(_, "aaaaa"))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   142
rs.map(matcher(_, "aaaaaa"))
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   143
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
println("EMAIL:")
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   145
val LOWERCASE = ('a' to 'z').toSet
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   146
val DIGITS = ('0' to '9').toSet
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   147
val EMAIL = { PLUS(CFUN(LOWERCASE | DIGITS | ("_.-").toSet)) ~ "@" ~ 
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   148
              PLUS(CFUN(LOWERCASE | DIGITS | (".-").toSet)) ~ "." ~
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   149
              NMTIMES(CFUN(LOWERCASE | Set('.')), 2, 6) }
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
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
   152
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
println(EMAIL);
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
println(matcher(EMAIL, my_email))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
println(ders_simp(my_email.toList, EMAIL))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
println("COMMENTS:")
492
39b7ff2cf1bc updated
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
   158
val ALL = CFUN((c:Char) => true)
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
val COMMENT = "/*" ~ (NOT(ALL.% ~ "*/" ~ ALL.%)) ~ "*/"
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(matcher(COMMENT, "/**/"))
493
4e97783862ff updated
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
   162
println(matcher(COMMENT, "/*foobar*/"))
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
println(matcher(COMMENT, "/*test*/test*/"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
println(matcher(COMMENT, "/*test/*test*/"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
println("EVILS:")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
val EVIL1 = PLUS(PLUS("a" ~ "a" ~ "a"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
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
   169
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
//40 * aaa matches
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
//43 * aaa + aa does not
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
//45 * aaa + a
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
println("EVIL1:")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
println(matcher(EVIL1, "aaa" * 40))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
println(matcher(EVIL1, "aaa" * 43 + "aa"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
println(matcher(EVIL1, "aaa" * 45 + "a"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
println("EVIL2:")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
println(matcher(EVIL2, "aaa" * 40))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
println(matcher(EVIL2, "aaa" * 43 + "aa"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
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
   183
192
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 190
diff changeset
   184
println("TEST for bug pointed out by Filips Ivanovs")
493
4e97783862ff updated
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
   185
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
   186
  
192
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 190
diff changeset
   187
println(matcher(test,"a"))
190
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   188
println(matcher(test,"ab"))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   189
println(matcher(test,"abcdef"))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   190
println(matcher(test,"abc"))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   191
println(matcher(test,"...."))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   192
println(matcher(test,"asdfg"))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   193
println(matcher(test,"abcdefg"))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   194
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   195
println(test)
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   196
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
   197
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
   198
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
   199
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
   200
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
   201
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
   202
println(ders_simp("aaaaaaa".toList, test))
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   203
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   204
def TEST(s: String, r: Rexp) = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   205
  println("Rexp |" + s + "|")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   206
  println("Derivative:\n" + ders_simp(s.toList, r))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   207
  println("Is Nullable: " + nullable(ders_simp(s.toList, r)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   208
  Console.readLine
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   209
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   210
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
val ALL2 = "a" | "f"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   213
val COMMENT2 = ("/*" ~ NOT(ALL2.% ~ "*/" ~ ALL2.%) ~ "*/")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   214
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   215
println("1) TEST TEST")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   216
TEST("", COMMENT2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   217
TEST("/", COMMENT2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   218
TEST("/*", COMMENT2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   219
TEST("/*f", COMMENT2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   220
TEST("/*f*", COMMENT2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   221
TEST("/*f*/", COMMENT2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   222
TEST("/*f*/ ", COMMENT2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   223
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   224
val ALL3 = "a" | "f"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   225
val COMMENT3 = ("/" ~ NOT(ALL3.% ~ "&" ~ ALL3.%) ~ "&")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   226
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   227
println("2) TEST TEST")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   228
TEST("", COMMENT3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   229
TEST("/", COMMENT3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   230
TEST("/", COMMENT3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   231
TEST("/f", COMMENT3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   232
TEST("/f&", COMMENT3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   233
TEST("/f& ", COMMENT3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 192
diff changeset
   234