progs/regexp2.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 26 Sep 2014 14:06:55 +0100
changeset 258 1e4da6d2490c
parent 93 4794759139ea
permissions -rw-r--r--
updated programs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
     1
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
     2
// regular expressions including NOT
26
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
// some convenience for typing in regular expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
def charlist2rexp(s : List[Char]) : Rexp = s match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  case Nil => EMPTY
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  case c::Nil => CHAR(c)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
// nullable function: tests whether the regular 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
// expression can recognise the empty string
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
def nullable (r: Rexp) : Boolean = r match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  case NULL => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  case EMPTY => true
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  case CHAR(_) => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  case STAR(_) => true
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  case NOT(r) => !(nullable(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
// tests whether a regular expression 
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    36
// cannot recognise more
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    37
def no_more (r: Rexp) : Boolean = r match {
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  case NULL => true
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  case EMPTY => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  case CHAR(_) => false
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    41
  case ALT(r1, r2) => no_more(r1) && no_more(r2)
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    42
  case SEQ(r1, r2) => if (nullable(r1)) (no_more(r1) && no_more(r2)) else no_more(r1)
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  case STAR(_) => false
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    44
  case NOT(r) => !(no_more(r))
26
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
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
// derivative of a regular expression w.r.t. a character
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
def der (c: Char, r: Rexp) : Rexp = r match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  case NULL => NULL
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  case EMPTY => NULL
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  case CHAR(d) => if (c == d) EMPTY else NULL
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  case SEQ(r1, r2) => 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
    else SEQ(der(c, r1), r2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  case STAR(r) => SEQ(der(c, r), STAR(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
  case NOT(r) => NOT(der (c, r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
// regular expression for specifying 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
// ranges of characters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
def RANGE(s : List[Char]) : Rexp = s match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  case Nil => NULL
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  case c::Nil => CHAR(c)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
  case c::s => ALT(CHAR(c), RANGE(s))
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
//one or more
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
def PLUS(r: Rexp) = SEQ(r, STAR(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
//some regular expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
val LOWERCASE = RANGE("abcdefghijklmnopqrstuvwxyz".toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
val UPPERCASE = RANGE("ABCDEFGHIJKLMNOPQRSTUVWXYZ".toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
val LETTER = ALT(LOWERCASE, UPPERCASE)
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    78
val DIGIT = RANGE("0123456789".toList)
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    79
val NONZERODIGIT = RANGE("123456789".toList)
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    81
val IDENT = SEQ(LETTER, STAR(ALT(LETTER,DIGIT)))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    82
val NUMBER = ALT(SEQ(NONZERODIGIT, STAR(DIGIT)), "0")
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
val WHITESPACE = RANGE(" \n".toList)
28
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    84
val WHITESPACES = PLUS(WHITESPACE)
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    86
val ALL = ALT(ALT(LETTER, DIGIT), WHITESPACE)
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
val COMMENT = SEQ(SEQ("/*", NOT(SEQ(SEQ(STAR(ALL), "*/"), STAR(ALL)))), "*/")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
// an example list of regular expressions
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    91
val regs: List[Rexp]=  List("if", "then", "else", "+", IDENT, NUMBER, WHITESPACES, COMMENT) 
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    92
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
def error (s: String) = throw new IllegalArgumentException ("Could not lex " + s)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    96
def munch(r: Rexp, s: List[Char], t: List[Char]) : Option[(List[Char], List[Char])] = 
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    97
  s match {
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    98
    case Nil if (nullable(r)) => Some(Nil, t)
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    99
    case Nil => None
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   100
    case c::s if (no_more(der (c, r)) && nullable(r)) => Some(c::s, t)
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   101
    case c::s if (no_more(der (c, r))) => None
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   102
    case c::s => munch(der (c, r), s, t ::: List(c))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   103
  }
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   105
def one_string (regs: List[Rexp], s: List[Char]) : (List[Char], List[Char]) = {
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
 val somes = regs.map { munch(_, s, Nil) } .flatten
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
 if (somes == Nil) error(s.mkString) else (somes sortBy (_._1.length) head)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   110
def tokenize (regs: List[Rexp], s: List[Char]) : List[String] = s match {
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
  case Nil => Nil
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   112
  case _ => one_string(regs, s) match {
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   113
    case (rest, s) => s.mkString :: tokenize(regs, rest) 
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
  }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   117
//examples
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   118
println(tokenize(regs, "if true then then 42 else +".toList))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   119
println(tokenize(regs, "if+true+then+then+42+else +".toList))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   120
println(tokenize(regs, "ifff if     34 34".toList))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   121
println(tokenize(regs, "/*ifff if */ hhjj /*34 */".toList))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   122
println(tokenize(regs, "/* if true then */ then 42 else +".toList))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   123
//println(tokenize(regs, "ifff $ if 34".toList)) // causes an error because of the symbol $