parser.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 31 Oct 2012 21:46:27 +0000
changeset 54 485f38b530ab
parent 51 6fe4facb56a6
child 61 a80f0cf17f91
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50
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
// 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 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
// cannot recognise more
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
def no_more (r: Rexp) : Boolean = r match {
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
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  case ALT(r1, r2) => no_more(r1) && no_more(r2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  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
    43
  case STAR(_) => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
  case NOT(r) => !(no_more(r))
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  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
// 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
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
// one or more
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
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
// some regular expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
val DIGIT = RANGE("0123456789".toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
val NONZERODIGIT = RANGE("123456789".toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
val NUMBER = ALT(SEQ(NONZERODIGIT, STAR(DIGIT)), "0")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
val LPAREN = CHAR('(')
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
val RPAREN = CHAR(')')
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
val WHITESPACE = PLUS(RANGE(" \n".toList))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
val OPS = RANGE("+-*".toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
// for classifying the strings that have been recognised
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
abstract class Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
case object T_WHITESPACE extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
case object T_NUM extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
case class T_OP(s: String) extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
case object T_LPAREN extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
case object T_RPAREN extends Token
51
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    89
case class NT(s: String) extends Token
50
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
type Rule = (Rexp, List[Char] => Token)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
def error (s: String) = throw new IllegalArgumentException ("Cannot tokenize: " + s)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
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:
diff changeset
    96
  s match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
    case Nil if (nullable(r)) => Some(Nil, action(t))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
    case Nil => None
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
    case c::s if (no_more(der (c, r)) && nullable(r)) => Some(c::s, action(t))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
    case c::s if (no_more(der (c, r))) => None
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
    case c::s => munch(der (c, r), action, s, t ::: List(c))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
  }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
def one_token (rs: List[Rule], s: List[Char]) : (List[Char], Token) = {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
 val somes = rs.map { (r) => munch(r._1, r._2, s, Nil) } .flatten
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
 if (somes == Nil) error(s.mkString) else (somes sortBy (_._1.length) head)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
def tokenize (rs: List[Rule], s: List[Char]) : List[Token] = s match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
  case Nil => Nil
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
  case _ => one_token(rs, s) match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
    case (rest, token) => token :: tokenize(rs, rest) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
  }
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
def tokenizer(rs: List[Rule], s: String) : List[Token] = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
  tokenize(rs, s.toList).filterNot(_ match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
    case T_WHITESPACE => true
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
    case _ => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
  })
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
// lexing rules for arithmetic expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
val lexing_rules: List[Rule]= 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
  List((NUMBER, (s) => T_NUM),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
       (WHITESPACE, (s) => T_WHITESPACE),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
       (LPAREN, (s) => T_LPAREN),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
       (RPAREN, (s) => T_RPAREN),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
       (OPS, (s) => T_OP(s.mkString)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
// examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
println(tokenizer(lexing_rules, "2 + 3 * 4 + 1"))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
println(tokenizer(lexing_rules, "(2 + 3) * (4 + 1)"))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
type Grammar = List[(String, List[Token])]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
// grammar for arithmetic expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
val grammar = 
54
485f38b530ab updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   142
  List ("F" -> List(T_NUM),
485f38b530ab updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   143
        "E" -> List(T_NUM),
51
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   144
        "E" -> List(NT("E"), T_OP("+"), NT("E")),
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   145
        "E" -> List(NT("E"), T_OP("-"), NT("E")),
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   146
        "E" -> List(NT("E"), T_OP("*"), NT("E")),    
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   147
        "E" -> List(T_LPAREN, NT("E"), T_RPAREN))
50
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
def chop[A](ts1: List[A], prefix: List[A], ts2: List[A]) : Option[(List[A], List[A])] = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
  ts1 match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
    case Nil => None
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
    case t::ts => 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
      if (ts1.startsWith(prefix)) Some(ts2.reverse, ts1.drop(prefix.length))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
      else chop(ts, prefix, t::ts2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
  }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
// examples
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
chop(List(1,2,3,4,5,6,7,8,9), List(4,5), Nil)  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
chop(List(1,2,3,4,5,6,7,8,9), List(3,5), Nil)  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
def replace[A](ts: List[A], out: List[A], in: List [A]) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
  chop(ts, out, Nil) match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
    case None => None
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
    case Some((before, after)) => Some(before ::: in ::: after)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
  }  
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
51
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   168
def parse(g: Grammar, ts: List[Token]) : Boolean = {
54
485f38b530ab updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   169
  println(ts)
51
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   170
  if (ts == List(NT("E"))) true
50
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
  else {
51
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   172
    val tss = for ((lhs, rhs) <- g) yield replace(ts, rhs, List(NT(lhs)))
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   173
    tss.flatten.exists(parse(g, _))
50
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
  }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
 
51
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   177
def parser(g: Grammar, rs: List[Rule], s: String) = {
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   178
  println("\n")
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   179
  parse(g, tokenizer(rs, s))
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   180
}
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   181
  
50
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
51
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   183
54
485f38b530ab updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   184
parser(grammar, lexing_rules, "2 + 3 *    4 +       1")
51
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   185
parser(grammar, lexing_rules, "(2 + 3) * (4 + 1)")
6fe4facb56a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   186
parser(grammar, lexing_rules, "(2 + 3) * 4 (4 + 1)")
50
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189