progs/re-alt.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 28 Apr 2017 11:01:25 +0100
changeset 487 a697421eaa04
parent 93 4794759139ea
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
90
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
trait RegExp {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
  def nullable: Boolean
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
  def derive(c: Char): RegExp
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
case object Empty extends RegExp {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
  def nullable = false
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
  def derive(c: Char) = Empty
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
case object Eps extends RegExp {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
  def nullable = true
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
  def derive(c: Char) = Empty
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
case class Str(s: String) extends RegExp {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
  def nullable = s.isEmpty
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
  def derive(c: Char) =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
    if (s.isEmpty || s.head != c) Empty
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
    else Str(s.tail)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
case class Cat(r: RegExp, s: RegExp) extends RegExp {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
  def nullable = r.nullable && s.nullable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
  def derive(c: Char) =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
    if (r.nullable) Or(Cat(r.derive(c), s), s.derive(c)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
    else Cat(r.derive(c), s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
case class Star(r: RegExp) extends RegExp {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  def nullable = true
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  def derive(c: Char) = Cat(r.derive(c), this)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
case class Or(r: RegExp, s: RegExp) extends RegExp {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  def nullable = r.nullable || s.nullable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  def derive(c: Char) = Or(r.derive(c), s.derive(c))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
case class And(r: RegExp, s: RegExp) extends RegExp {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  def nullable = r.nullable && s.nullable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  def derive(c: Char) = And(r.derive(c), s.derive(c))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
case class Not(r: RegExp) extends RegExp {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  def nullable = !r.nullable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  def derive(c: Char) = Not(r.derive(c))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
object Matcher {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  def matches(r: RegExp, s: String): Boolean = {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
    if (s.isEmpty) r.nullable
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
    else matches(r.derive(s.head), s.tail)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
object Pimps {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
  implicit def string2RegExp(s: String) = Str(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  implicit def regExpOps(r: RegExp) = new {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
    def | (s: RegExp) = Or(r, s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
    def & (s: RegExp) = And(r, s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
    def % = Star(r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
    def %(n: Int) = rep(r, n)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
    def ? = Or(Eps, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
    def ! = Not(r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
    def ++ (s: RegExp) = Cat(r, s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
    def ~ (s: String) = Matcher.matches(r, s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
  implicit def stringOps(s: String) = new {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
    def | (r: RegExp) = Or(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
    def | (r: String) = Or(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
    def & (r: RegExp) = And(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
    def & (r: String) = And(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
    def % = Star(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
    def % (n: Int) = rep(Str(s), n)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
    def ? = Or(Eps, s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
    def ! = Not(s)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
    def ++ (r: RegExp) = Cat(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
    def ++ (r: String) = Cat(s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
    def ~ (t: String) = Matcher.matches(s, t)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
  def rep(r: RegExp, n: Int): RegExp =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
    if (n <= 0) Star(r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
    else Cat(r, rep(r, n - 1))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
object Test {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
  import Pimps._
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
  val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
  val int = ("+" | "-").? ++ digit.%(1)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
  val real = ("+" | "-").? ++ digit.%(1) ++ ("." ++ digit.%(1)).? ++ (("e" | "E") ++ ("+" | "-").? ++ digit.%(1)).?
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
  def main(args: Array[String]) {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
    val ints = List("0", "-4534", "+049", "99")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
    val reals = List("0.9", "-12.8", "+91.0", "9e12", "+9.21E-12", "-512E+01")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
    val errs = List("", "-", "+", "+-1", "-+2", "2-")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
    ints.foreach(s => assert(int ~ s))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
    reals.foreach(s => assert(!(int ~ s)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
    errs.foreach(s => assert(!(int ~ s)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
    ints.foreach(s => assert(real ~ s))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
    reals.foreach(s => assert(real ~ s))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
    errs.foreach(s => assert(!(real ~ s)))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
}