matcher.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 21 Nov 2012 02:34:37 +0000
changeset 68 a09ecb1e7384
parent 64 2d625418c011
child 75 898c25a4e399
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
// regular expressions including NOT
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
abstract class Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
case object NULL extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
case object EMPTY extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
case class CHAR(c: Char) extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
case class ALT(r1: Rexp, r2: Rexp) extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
case class STAR(r: Rexp) extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
case class NOT(r: Rexp) extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
// nullable function: tests whether the regular 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
// expression can recognise the empty string
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
def nullable (r: Rexp) : Boolean = r match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  case NULL => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  case EMPTY => true
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  case CHAR(_) => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  case STAR(_) => true
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  case NOT(r) => !(nullable(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
// tests whether a regular expression 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
// cannot recognise more
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
def no_more (r: Rexp) : Boolean = r match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  case NULL => true
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  case EMPTY => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  case CHAR(_) => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  case ALT(r1, r2) => no_more(r1) && no_more(r2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  case SEQ(r1, r2) => if (nullable(r1)) (no_more(r1) && no_more(r2)) else no_more(r1)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  case STAR(_) => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  case NOT(r) => !(no_more(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
// derivative of a regular expression w.r.t. a character
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
def der (c: Char, r: Rexp) : Rexp = r match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  case NULL => NULL
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  case EMPTY => NULL  case CHAR(d) => if (c == d) EMPTY else NULL
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
  case SEQ(r1, r2) => 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
    else SEQ(der(c, r1), r2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  case STAR(r) => SEQ(der(c, r), STAR(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  case NOT(r) => NOT(der (c, r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
// regular expression for specifying 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
// ranges of characters
64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    53
def Range(s : List[Char]) : Rexp = s match {
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  case Nil => NULL
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
  case c::Nil => CHAR(c)
64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    56
  case c::s => ALT(CHAR(c), Range(s))
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
}
64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    58
def RANGE(s: String) = Range(s.toList)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    59
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
// one or more
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
def PLUS(r: Rexp) = SEQ(r, STAR(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    64
// many alternatives
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    65
def Alts(rs: List[Rexp]) : Rexp = rs match {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    66
  case Nil => NULL
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    67
  case r::Nil => r
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    68
  case r::rs => ALT(r, Alts(rs))
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    69
}
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    70
def ALTS(rs: Rexp*) = Alts(rs.toList)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    71
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    72
// repetitions
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    73
def Seqs(rs: List[Rexp]) : Rexp = rs match {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    74
  case Nil => NULL
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    75
  case r::Nil => r
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    76
  case r::rs => SEQ(r, Seqs(rs))
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    77
}
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    78
def SEQS(rs: Rexp*) = Seqs(rs.toList)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    79
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    80
// some convenience for typing in regular expressions
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    81
def charlist2rexp(s : List[Char]) : Rexp = s match {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    82
  case Nil => EMPTY
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    83
  case c::Nil => CHAR(c)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    84
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    85
}
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    86
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    87
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
type Rule[T] = (Rexp, List[Char] => T)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    91
case class Tokenizer[T](rules: List[Rule[T]], excl: List[T] = Nil) {
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    93
  def munch(r: Rexp, action: List[Char] => T, s: List[Char], t: List[Char]) : Option[(List[Char], T)] = 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    94
    s match {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    95
      case Nil if (nullable(r)) => Some(Nil, action(t))
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    96
      case Nil => None
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    97
      case c::s if (no_more(der (c, r)) && nullable(r)) => Some(c::s, action(t))
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    98
      case c::s if (no_more(der (c, r))) => None
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    99
      case c::s => munch(der (c, r), action, s, t ::: List(c))
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   100
    }
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   101
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   102
  def one_token(s: List[Char]) : Either[(List[Char], T), String] = {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   103
    val somes = rules.map { (r) => munch(r._1, r._2, s, Nil) }.flatten
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   104
    if (somes == Nil) Right(s.mkString) 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   105
    else Left(somes sortBy (_._1.length) head)
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
  }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   108
  def tokenize(cs: List[Char]) : List[T] = cs match {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   109
    case Nil => Nil
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   110
    case _ => one_token(cs) match {
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   111
      case Left((rest, token)) => token :: tokenize(rest)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   112
      case Right(s) => { println("Cannot tokenize: \"" + s + "\""); Nil } 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   113
    }
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   114
  }
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   115
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   116
  def fromString(s: String) : List[T] = 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   117
    tokenize(s.toList).filterNot(excl.contains(_))
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   118
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   119
  def fromFile(name: String) : List[T] = 
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   120
    fromString(io.Source.fromFile(name).mkString)
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   121
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123