progs/regexp3.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 15 Jul 2016 10:54:09 +0100
changeset 406 0a42d73e795b
parent 93 4794759139ea
permissions -rw-r--r--
repl-slides
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)
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    30
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
26
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
39
Christian Urban <urbanc@in.tum.de>
parents: 29
diff changeset
    51
  case EMPTY => NULL  case CHAR(d) => if (c == d) EMPTY else NULL
26
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
// regular expression for specifying 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
// ranges of characters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
def RANGE(s : List[Char]) : Rexp = s match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  case Nil => NULL
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  case c::Nil => CHAR(c)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  case c::s => ALT(CHAR(c), RANGE(s))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    68
// one or more
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
def PLUS(r: Rexp) = SEQ(r, STAR(r))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    71
// some regular expressions
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
val LOWERCASE = RANGE("abcdefghijklmnopqrstuvwxyz".toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
val UPPERCASE = RANGE("ABCDEFGHIJKLMNOPQRSTUVWXYZ".toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
val LETTER = ALT(LOWERCASE, UPPERCASE)
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    75
val DIGIT = RANGE("0123456789".toList)
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    76
val NONZERODIGIT = RANGE("123456789".toList)
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    78
val IDENT = SEQ(LETTER, STAR(ALT(LETTER,DIGIT)))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    79
val NUMBER = ALT(SEQ(NONZERODIGIT, STAR(DIGIT)), "0")
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
val WHITESPACE = RANGE(" \n".toList)
28
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
    81
val WHITESPACES = PLUS(WHITESPACE)
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    83
val ALL = ALT(ALT(LETTER, DIGIT), WHITESPACE)
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
val COMMENT = SEQ(SEQ("/*", NOT(SEQ(SEQ(STAR(ALL), "*/"), STAR(ALL)))), "*/")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    86
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    87
// for classifying the strings that have been recognised
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
abstract class Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
case object T_WHITESPACE extends Token
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    91
case object T_COMMENT extends Token
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
case class T_IDENT(s: String) extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
case class T_OP(s: String) extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
case class T_NUM(n: Int) extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
case class T_KEYWORD(s: String) extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    98
// an example list of syntactic rules
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
    99
type Rule = (Rexp, List[Char] => Token)
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
28
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   101
val rules: List[Rule]= 
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   102
  List(("if", (s) => T_KEYWORD(s.mkString)),
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   103
       ("then", (s) => T_KEYWORD(s.mkString)),
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   104
       ("else", (s) => T_KEYWORD(s.mkString)),
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   105
       ("+", (s) => T_OP(s.mkString)),
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   106
       (IDENT, (s) => T_IDENT(s.mkString)),
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   107
       (NUMBER, (s) => T_NUM(s.mkString.toInt)),
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   108
       (WHITESPACES, (s) => T_WHITESPACE),
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   109
       (COMMENT, (s) => T_COMMENT))
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   110
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   112
def error (s: String) = throw new IllegalArgumentException ("Cannot tokenize: " + s)
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   114
def munch(r: Rexp, action: List[Char] => Token, s: List[Char], t: List[Char]) : Option[(List[Char], Token)] = 
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   115
  s match {
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   116
    case Nil if (nullable(r)) => Some(Nil, action(t))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   117
    case Nil => None
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   118
    case c::s if (no_more(der (c, r)) && nullable(r)) => Some(c::s, action(t))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   119
    case c::s if (no_more(der (c, r))) => None
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   120
    case c::s => munch(der (c, r), action, s, t ::: List(c))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   121
  }
28
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   122
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   123
def one_token (rs: List[Rule], s: List[Char]) : (List[Char], Token) = {
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   124
 val somes = rs.map { (r) => munch(r._1, r._2, s, Nil) } .flatten
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   125
 if (somes == Nil) error(s.mkString) else (somes sortBy (_._1.length) head)
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   126
}
28
f63ba92a7d78 zeroable by Chunhan
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   127
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   128
def tokenize (rs: List[Rule], s: List[Char]) : List[Token] = s match {
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   129
  case Nil => Nil
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   130
  case _ => one_token(rs, s) match {
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   131
    case (rest, token) => token :: tokenize(rs, rest) 
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   132
  }
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   133
}
26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
29
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   135
//examples
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   136
println(tokenize(rules, "if true then then 42 else +".toList))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   137
println(tokenize(rules, "if+true+then+then+42+else +".toList))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   138
println(tokenize(rules, "ifff if     34 34".toList))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   139
println(tokenize(rules, "/*ifff if */ hhjj /*34 */".toList))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   140
println(tokenize(rules, "/* if true then */ then 42 else +".toList))
Christian Urban <urbanc@in.tum.de>
parents: 28
diff changeset
   141
//println(tokenize(rules, "ifff $ if 34".toList)) // causes an error because of the symbol $