regexp2.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 10 Oct 2012 14:08:49 +0100
changeset 28 f63ba92a7d78
parent 26 06be91bbb1cd
child 29 774007c4b1b3
permissions -rw-r--r--
zeroable by Chunhan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// regular expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
abstract class Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
case object NULL extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
case object EMPTY extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
case class CHAR(c: Char) extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
case class ALT(r1: Rexp, r2: Rexp) extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
case class STAR(r: Rexp) extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
case class NOT(r: Rexp) extends Rexp
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
// some convenience for typing in regular expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
def charlist2rexp(s : List[Char]) : Rexp = s match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  case Nil => EMPTY
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  case c::Nil => CHAR(c)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
// nullable function: tests whether the regular 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
// expression can recognise the empty string
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
def nullable (r: Rexp) : Boolean = r match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  case NULL => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  case EMPTY => true
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  case CHAR(_) => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  case STAR(_) => true
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  case NOT(r) => !(nullable(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
// tests whether a regular expression 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
// recognises nothing
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
def zeroable (r: Rexp) : Boolean = r match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  case NULL => true
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  case EMPTY => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  case CHAR(_) => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  case ALT(r1, r2) => zeroable(r1) && zeroable(r2)
28
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    41
  case SEQ(r1, r2) => if (nullable(r1)) (zeroable(r1) && zeroable(r2)) else zeroable(r1)
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  case STAR(_) => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  case NOT(r) => !(zeroable(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
// derivative of a regular expression w.r.t. a character
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
def der (c: Char, r: Rexp) : Rexp = r match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  case NULL => NULL
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  case EMPTY => NULL
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  case CHAR(d) => if (c == d) EMPTY else NULL
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  case SEQ(r1, r2) => 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
    else SEQ(der(c, r1), r2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  case STAR(r) => SEQ(der(c, r), STAR(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  case NOT(r) => NOT(der (c, r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
// derivative w.r.t. a string (iterates der)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
def ders (s: List[Char], r: Rexp) : Rexp = s match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  case Nil => r
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  case c::s => ders(s, der(c, r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
// main matcher function
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
// regular expression for specifying 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
// ranges of characters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
def RANGE(s : List[Char]) : Rexp = s match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  case Nil => NULL
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  case c::Nil => CHAR(c)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  case c::s => ALT(CHAR(c), RANGE(s))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
//one or more
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
def PLUS(r: Rexp) = SEQ(r, STAR(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
//some regular expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
val LOWERCASE = RANGE("abcdefghijklmnopqrstuvwxyz".toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
val UPPERCASE = RANGE("ABCDEFGHIJKLMNOPQRSTUVWXYZ".toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
val LETTER = ALT(LOWERCASE, UPPERCASE)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
val DIGITS = RANGE("0123456789".toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
val NONZERODIGITS = RANGE("123456789".toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
val IDENT = SEQ(LETTER, STAR(ALT(LETTER,DIGITS)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
val NUMBER = ALT(SEQ(NONZERODIGITS, STAR(DIGITS)), "0")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
val WHITESPACE = RANGE(" \n".toList)
28
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    92
val WHITESPACES = PLUS(WHITESPACE)
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
val ALL = ALT(ALT(LETTER, DIGITS), WHITESPACE)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
val COMMENT = SEQ(SEQ("/*", NOT(SEQ(SEQ(STAR(ALL), "*/"), STAR(ALL)))), "*/")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
println(matcher(NUMBER, "0"))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
println(matcher(NUMBER, "01"))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
println(matcher(NUMBER, "123450"))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
println(matcher(SEQ(STAR("a"), STAR("b")), "bbaaa"))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
println(matcher(ALT(STAR("a"), STAR("b")), ""))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
println(matcher("abc", ""))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
println(matcher(STAR(ALT(EMPTY, "a")), ""))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
println(matcher(STAR(EMPTY), "a"))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
println(matcher("cab","cab"))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
println(matcher(STAR("a"),"aaa"))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
println(matcher("cab" ,"cab"))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
println(matcher(STAR("a"),"aaa"))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
println(matcher(COMMENT, "/* */"))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
println(matcher(COMMENT, "/* foobar comment */"))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
println(matcher(COMMENT, "/* test */ test */"))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
// an example list of regular expressions
28
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   117
val regs: List[Rexp]=  List("if", "then", "else", "+", IDENT, NUMBER, WHITESPACES) 
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
def error (s: String) = throw new IllegalArgumentException ("Could not lex " + s)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
def munch(r: Rexp, s: List[Char], t: List[Char]) : Option[(List[Char], List[Char])] = s match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
  case Nil if (nullable(r)) => Some(Nil, t)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
  case Nil => None
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
  case c::s if (zeroable(der (c, r)) && nullable(r)) => Some(c::s, t)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
  case c::s if (zeroable(der (c, r))) => None
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
  case c::s => munch(der (c, r), s, t ::: List(c))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
def lex_one (regs: List[Rexp], s: List[Char]) : (List[Char], List[Char]) = {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
 val somes = regs.map { munch(_, s, Nil) } .flatten
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
 if (somes == Nil) error(s.mkString) else (somes sortBy (_._1.length) head)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
def lex_all (regs: List[Rexp], s: List[Char]) : List[String] = s match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
  case Nil => Nil
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
  case _ => lex_one(regs, s) match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
    case (rest, s) => s.mkString :: lex_all(regs, rest) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
  }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
28
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   141
val regs: List[Rexp]=  List("if", "then", "else", "+", IDENT, NUMBER, WHITESPACES) 
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
28
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   143
println(lex_all(regs, "if true then 42 else +".toList))
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   144
println(lex_all(regs, "ifff if     34 34".toList))
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   145
println(lex_all(regs, "ifff +if+     34 34".toList))
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   146
println(lex_all(regs, "1+x+3+4+foo".toList))
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   147
println(lex_all(regs, "ifff $ if 34".toList))
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   148