parser2a.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 14 Nov 2012 08:45:30 +0000
changeset 62 5988e44ea048
child 64 2d625418c011
permissions -rw-r--r--
added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
:load matcher.scala
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
// some regular expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
val LETTER = RANGE("abcdefghijklmnopqrstuvwxyz".toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
val ID = PLUS(LETTER)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
val DIGIT = RANGE("0123456789".toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
val NONZERODIGIT = RANGE("123456789".toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
val NUMBER = ALT(SEQ(NONZERODIGIT, STAR(DIGIT)), "0")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
val LPAREN = CHAR('(')
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
val RPAREN = CHAR(')')
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
val WHITESPACE = PLUS(RANGE(" \n".toList))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
val OPS = RANGE("+-*".toList)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
// for classifying the strings that have been recognised
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
abstract class Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
case object T_WHITESPACE extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
case class T_NUM(s: String) extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
case class T_ID(s: String) extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
case class T_OP(s: String) extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
case object T_LPAREN extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
case object T_RPAREN extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
case object T_IF extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
case object T_THEN extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
case object T_ELSE extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
def tokenizer(rs: List[Rule[Token]], s: String) : List[Token] = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  tokenize(rs, s.toList).filterNot(_ match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
    case T_WHITESPACE => true
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
    case _ => false
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  })
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
// lexing rules for arithmetic expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
val lexing_rules: List[Rule[Token]]= 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  List(("if", (s) => T_IF),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
       ("then", (s) => T_THEN),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
       ("else", (s) => T_ELSE),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
       (NUMBER, (s) => T_NUM(s.mkString)),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
       (ID, (s) => T_ID(s.mkString)),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
       (WHITESPACE, (s) => T_WHITESPACE),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
       (LPAREN, (s) => T_LPAREN),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
       (RPAREN, (s) => T_RPAREN),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
       (OPS, (s) => T_OP(s.mkString)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
// parser combinators with return type T
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
abstract class Parser[T] {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  def parse(ts: List[Token]): Set[(T, List[Token])]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  def parse_all(ts: List[Token]) : Set[T] =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
    for ((head, tail) <- parse(ts); if (tail == Nil)) yield head
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  def || (right : => Parser[T]) : Parser[T] = new AltParser(this, right)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
  def ==>[S] (f: => T => S) : Parser [S] = new FunParser(this, f)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
  def ~[S] (right : => Parser[S]) : Parser[(T, S)] = new SeqParser(this, right)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  def ~>[S] (right : => Parser[S]) : Parser[S] = this ~ right ==> (x => x._2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  def <~[S] (right : => Parser[S]) : Parser[T] = this ~ right ==> (x => x._1)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
class SeqParser[T, S](p: => Parser[T], q: => Parser[S]) extends Parser[(T, S)] {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  def parse(sb: List[Token]) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
    for ((head1, tail1) <- p.parse(sb); 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
         (head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
class AltParser[T](p: => Parser[T], q: => Parser[T]) extends Parser[T] {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
  def parse (sb: List[Token]) = p.parse(sb) ++ q.parse(sb)   
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
class FunParser[T, S](p: => Parser[T], f: T => S) extends Parser[S] {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
  def parse (sb: List[Token]) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
case class TokParser(tok: Token) extends Parser[Token] {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
  def parse(ts: List[Token]) = ts match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
    case t::ts if (t == tok) => Set((t, ts)) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
    case _ => Set ()
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
implicit def token2tparser(t: Token) = TokParser(t)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
case object NumParser extends Parser[Int] {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
  def parse(ts: List[Token]) = ts match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
    case T_NUM(s)::ts => Set((s.toInt, ts)) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
    case _ => Set ()
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
  }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
lazy val E: Parser[Int] = (T ~ T_OP("+") ~ E) ==> { case ((x, y), z) => x + z } || T  // start symbol
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
lazy val T: Parser[Int] = (F ~ T_OP("*") ~ T) ==> { case ((x, y), z) => x * z } || F
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
lazy val F: Parser[Int] = (T_LPAREN ~> E <~ T_RPAREN) || NumParser
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
   
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
println(E.parse_all(tokenizer(lexing_rules, "1 + 2 + 3")))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
println(E.parse_all(tokenizer(lexing_rules, "1 + 2 * 3")))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
println(E.parse_all(tokenizer(lexing_rules, "(1 + 2) * 3")))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
println(E.parse_all(tokenizer(lexing_rules, "(1 - 2) * 3")))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
println(E.parse_all(tokenizer(lexing_rules, "(1 + 2) * - 3")))