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