progs/cw1.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 14 Nov 2013 20:08:39 +0000
changeset 190 0b63c0edfb09
parent 189 04346d82fe01
child 192 9f0631804555
permissions -rw-r--r--
added new version
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
}
190
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
    60
case class NUPTOM(r: Rexp, n: Int, m: Int) extends Rexp {
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
  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
    62
    r.simp match {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
      case NULL => NULL
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
      case EMPTY => EMPTY
190
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
    65
      case r => NUPTOM(r, n, m)
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
    }
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
}
190
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
    68
def NMTIMES(r: Rexp, n: Int, m: Int) = {
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
    69
  if(m < n) throw new IllegalArgumentException("the number m cannot be smaller than n.")
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
    70
  else NUPTOM(r, n, m - n)
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
    71
}
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
    72
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
    73
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
case class NOT(r: Rexp) extends Rexp {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
  override def simp = NOT(r.simp)
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
case class OPT(r: Rexp) extends Rexp {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
  override def simp = OPT(r.simp)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
// nullable function: tests whether the regular 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
// expression can recognise the empty string
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
def nullable (r: Rexp) : Boolean = r match {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
  case NULL => false
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
  case EMPTY => true
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
  case CHAR(_) => false
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
  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
    88
  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
    89
  case STAR(_) => true
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
  case PLUS(r) => nullable(r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
  case NTIMES(r, i) => if (i == 0) true else nullable(r)
190
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
    92
  case NUPTOM(r, i, j) => if (i == 0) true else nullable(r)
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
  case RANGE(_) => false
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
  case NOT(r) => !(nullable(r))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
  case OPT(_) => true
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
}
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
// 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
    99
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
   100
  case NULL => NULL
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
  case EMPTY => NULL
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
  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
   103
  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
   104
  case SEQ(r1, r2) => 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
    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
   106
    else SEQ(der(c, r1), r2)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
  case STAR(r) => SEQ(der(c, r), STAR(r))
190
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   108
  case PLUS(r) => SEQ(der(c, r), STAR(r))
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
  case NTIMES(r, i) => 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
    if (i == 0) NULL else der(c, SEQ(r, NTIMES(r, i - 1)))
190
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   111
  case NUPTOM(r, i, j) =>
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   112
    if (i == 0 && j == 0) NULL else 
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   113
    if (i == 0) ALT(der(c, NTIMES(r, j)), der(c, NUPTOM(r, 0, j - 1)))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   114
    else der(c, SEQ(r, NUPTOM(r, i - 1, j)))
189
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
  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
   116
  case NOT(r) => NOT(der (c, r))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
  case OPT(r) => der(c, r)
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
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
// 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
   121
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
   122
  case Nil => r
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
  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
   124
}
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
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
   127
  case Nil => r
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
  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
   129
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
// main matcher function
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
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
   133
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
// some convenience for typing in regular expressions
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
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
   136
  case Nil => EMPTY
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
  case c::Nil => CHAR(c)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
  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
   139
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
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
   141
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
implicit def RexpOps (r: Rexp) = new {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
  def | (s: Rexp) = ALT(r, s)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
  def % = STAR(r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
  def ~ (s: Rexp) = SEQ(r, s)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
implicit def stringOps (s: String) = new {
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
  def | (r: Rexp) = ALT(s, r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
  def | (r: String) = ALT(s, r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
  def % = STAR(s)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
  def ~ (r: Rexp) = SEQ(s, r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
  def ~ (r: String) = SEQ(s, r)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
}
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
println("EMAIL:")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
val LOWERCASE = "abcdefghijklmnopqrstuvwxyz"
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
val DIGITS = "0123456789"
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
val EMAIL = PLUS(RANGE(LOWERCASE + DIGITS + "_" + "." + "-")) ~ "@" ~ 
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
            PLUS(RANGE(LOWERCASE + DIGITS + "." + "-")) ~ "." ~
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
            NMTIMES(RANGE(LOWERCASE + "."), 2, 6)
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
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
   164
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
println(EMAIL);
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
println(matcher(EMAIL, my_email))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
println(ders_simp(my_email.toList, EMAIL))
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("COMMENTS:")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
val ALL = RANGE(LOWERCASE + " ")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
val COMMENT = "/*" ~ (NOT(ALL.% ~ "*/" ~ ALL.%)) ~ "*/"
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
println(matcher(COMMENT, "/**/"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
println(matcher(COMMENT, "/*foobar_comment*/"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
println(matcher(COMMENT, "/*test*/test*/"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
println(matcher(COMMENT, "/*test/*test*/"))
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
println("EVILS:")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
val EVIL1 = PLUS(PLUS("a" ~ "a" ~ "a"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
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
   181
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
//40 * aaa matches
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
//43 * aaa + aa does not
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
//45 * aaa + a
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
println("EVIL1:")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
println(matcher(EVIL1, "aaa" * 40))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
println(matcher(EVIL1, "aaa" * 43 + "aa"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
println(matcher(EVIL1, "aaa" * 45 + "a"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
println("EVIL2:")
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
println(matcher(EVIL2, "aaa" * 40))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
println(matcher(EVIL2, "aaa" * 43 + "aa"))
04346d82fe01 added cw1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
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
   195
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   196
println("TEST")
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   197
val test = NMTIMES(RANGE(LOWERCASE + "."), 2, 6)
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   198
  
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   199
println(matcher(test,"ab"))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   200
println(matcher(test,"abcdef"))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   201
println(matcher(test,"abc"))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   202
println(matcher(test,"...."))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   203
println(matcher(test,"asdfg"))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   204
println(matcher(test,"abcdefg"))
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   205
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   206
println(test)
0b63c0edfb09 added new version
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 189
diff changeset
   207
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
   208
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
   209
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
   210
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
   211
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
   212
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
   213
println(ders_simp("aaaaaaa".toList, test))