progs/cw1.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 13 Nov 2013 20:07:26 +0000
changeset 189 04346d82fe01
child 190 0b63c0edfb09
permissions -rw-r--r--
added cw1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
import scala.language.implicitConversions    
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
import scala.language.reflectiveCalls 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
abstract class Rexp {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
  def simp : Rexp = this
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
case object NULL extends Rexp
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
case object EMPTY extends Rexp
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
case class CHAR(c: Char) extends Rexp
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
case class ALT(r1: Rexp, r2: Rexp) extends Rexp {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
  override def toString = r1.toString + " | " + r2.toString
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
  override def simp = (r1.simp, r2.simp) match {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
    case (NULL, r) => r
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
    case (r, NULL) => r
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
    case (r, EMPTY) => if (nullable(r)) r else ALT(r, EMPTY)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
    case (EMPTY, r) => if (nullable(r)) r else ALT(r, EMPTY)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
    case (r1, r2) => if (r1 == r2) r1 else ALT(r1, r2)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
  }
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
case class RANGE(cs: List[Char]) extends Rexp {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
  //override def toString = cs.mkString("[",",","]")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
  override def toString = "[" + cs.head + " .. " + cs.last + "]"
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
object RANGE {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
  def apply(s: String) : RANGE = RANGE(s.toList)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
  override def toString = r1.toString + " ~ " + r2.toString
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
  override def simp = (r1.simp, r2.simp) match {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
    case (NULL, _) => NULL
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
    case (_, NULL) => NULL
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
    case (EMPTY, r) => r
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
    case (r, EMPTY) => r
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
    case (r1, r2) => SEQ(r1, r2)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  }
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
case class PLUS(r: Rexp) extends Rexp {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  override def simp = r.simp match {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
    case NULL => EMPTY
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
    case EMPTY => EMPTY
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
    case r => PLUS(r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
  }
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
case class STAR(r: Rexp) extends Rexp {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  override def simp = r.simp match {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
    case NULL => EMPTY
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
    case EMPTY => EMPTY
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
    case r => STAR(r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
  }
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
case class NTIMES(r: Rexp, n: Int) extends Rexp {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
  override def simp = if (n == 0) EMPTY else 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
    r.simp match {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
      case NULL => NULL
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
      case EMPTY => EMPTY
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
      case r => NTIMES(r, n)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
    }
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
case class NMTIMES(r: Rexp, n: Int, m: Int) extends Rexp {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
  if(m < n) throw new IllegalArgumentException("the number m cannot be smaller than n.")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
  override def simp = if (m == 0) EMPTY else 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
    r.simp match {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
      case NULL => NULL
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
      case EMPTY => EMPTY
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
      case r => NMTIMES(r, n, m)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
    }
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
case class NOT(r: Rexp) extends Rexp {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
  override def simp = NOT(r.simp)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
case class OPT(r: Rexp) extends Rexp {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
  override def simp = OPT(r.simp)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
// nullable function: tests whether the regular 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
// expression can recognise the empty string
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
def nullable (r: Rexp) : Boolean = r match {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
  case NULL => false
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
  case EMPTY => true
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
  case CHAR(_) => false
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
  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
    84
  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
    85
  case STAR(_) => true
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
  case PLUS(r) => nullable(r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
  case NTIMES(r, i) => if (i == 0) true else nullable(r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
  case NMTIMES(r, i, j) => if (i == 0) true else nullable(r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
  case RANGE(_) => false
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
  case NOT(r) => !(nullable(r))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
  case OPT(_) => true
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
// 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
    95
def der (c: Char, r: Rexp) : Rexp = r match {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
  case NULL => NULL
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
  case EMPTY => NULL
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
  case CHAR(d) => if (c == d) EMPTY else NULL
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
  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
   100
  case SEQ(r1, r2) => 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
    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
   102
    else SEQ(der(c, r1), r2)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
  case STAR(r) => SEQ(der(c, r), STAR(r))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
  case PLUS(r) => der(c, SEQ(r, STAR(r)))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
  case NTIMES(r, i) => 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
    if (i == 0) NULL else der(c, SEQ(r, NTIMES(r, i - 1)))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
  case NMTIMES(r, i, j) => 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
    if (j == 0) NULL 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
    if (i == 0) der(c, NTIMES(r, j))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
    else der(c, SEQ(r, NMTIMES(r, i - 1, j - 1)))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
  case RANGE(cs) => if (cs contains c) EMPTY else NULL
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
  case NOT(r) => NOT(der (c, r))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
  case OPT(r) => der(c, r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
// 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
   117
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
   118
  case Nil => r
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
  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
   120
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
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
   123
  case Nil => r
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
  case c::s => ders_simp(s, der(c, r).simp)
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
// main matcher function
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
def matcher(r: Rexp, s: String) : Boolean = nullable(ders_simp(s.toList, r))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
// some convenience for typing in regular expressions
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
def charlist2rexp(s : List[Char]) : Rexp = s match {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
  case Nil => EMPTY
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
  case c::Nil => CHAR(c)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
  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
   135
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
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
   137
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
implicit def RexpOps (r: Rexp) = new {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
  def | (s: Rexp) = ALT(r, s)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
  def % = STAR(r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
  def ~ (s: Rexp) = SEQ(r, s)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
implicit def stringOps (s: String) = new {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
  def | (r: Rexp) = ALT(s, r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
  def | (r: String) = ALT(s, r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
  def % = STAR(s)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
  def ~ (r: Rexp) = SEQ(s, r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
  def ~ (r: String) = SEQ(s, r)
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
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
println("EMAIL:")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
val LOWERCASE = "abcdefghijklmnopqrstuvwxyz"
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
val DIGITS = "0123456789"
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
val EMAIL = PLUS(RANGE(LOWERCASE + DIGITS + "_" + "." + "-")) ~ "@" ~ 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
            PLUS(RANGE(LOWERCASE + DIGITS + "." + "-")) ~ "." ~
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
            NMTIMES(RANGE(LOWERCASE + "."), 2, 6)
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:")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
val ALL = RANGE(LOWERCASE + " ")
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, "/**/"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
println(matcher(COMMENT, "/*foobar_comment*/"))
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"))