parser2.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 24 Nov 2012 14:58:27 +0000
changeset 77 49c0beef79a1
parent 71 7717f20f0504
permissions -rw-r--r--
added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
     1
// A naive version of parser combinators producing parse trees
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
     2
//
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
     3
// Needs
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
     4
//   :load matcher.scala
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
// some regular expressions
71
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
     7
val LETTER = RANGE("abcdefghijklmnopqrstuvwxyz")
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
val ID = PLUS(LETTER)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
71
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    10
val DIGIT = RANGE("0123456789")
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    11
val NONZERODIGIT = RANGE("123456789")
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
val NUMBER = ALT(SEQ(NONZERODIGIT, STAR(DIGIT)), "0")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
val LPAREN = CHAR('(')
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
val RPAREN = CHAR(')')
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
71
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    17
val WHITESPACE = PLUS(RANGE(" \n"))
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    18
val OPS = RANGE("+-*")
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
// for classifying the strings that have been recognised
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
abstract class Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
case object T_WHITESPACE extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
case class T_NUM(s: String) extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
case class T_ID(s: String) extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
case class T_OP(s: String) extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
case object T_LPAREN extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
case object T_RPAREN extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
case object T_IF extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
case object T_THEN extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
case object T_ELSE extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
// lexing rules for arithmetic expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
val lexing_rules: List[Rule[Token]]= 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
  List(("if", (s) => T_IF),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
       ("then", (s) => T_THEN),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
       ("else", (s) => T_ELSE),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
       (NUMBER, (s) => T_NUM(s.mkString)),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
       (ID, (s) => T_ID(s.mkString)),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
       (WHITESPACE, (s) => T_WHITESPACE),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
       (LPAREN, (s) => T_LPAREN),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
       (RPAREN, (s) => T_RPAREN),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
       (OPS, (s) => T_OP(s.mkString)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
71
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    45
val Tok = Tokenizer(lexing_rules, List(T_WHITESPACE))
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    46
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
// parse trees
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
abstract class ParseTree
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
case class Leaf(t: Token) extends ParseTree
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
case class Branch(pts: List[ParseTree]) extends ParseTree
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
def combine(pt1: ParseTree, pt2: ParseTree) = pt1 match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  case Leaf(t) => Branch(List(Leaf(t), pt2))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
  case Branch(pts) => Branch(pts ++ List(pt2))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
// parser combinators
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
abstract class Parser {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  def parse(ts: List[Token]): Set[(ParseTree, List[Token])]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  def parse_all(ts: List[Token]) : Set[ParseTree] =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
    for ((head, tail) <- parse(ts); if (tail == Nil)) yield head
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  def || (right : => Parser) : Parser = new AltParser(this, right)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  def ~ (right : => Parser) : Parser = new SeqParser(this, right)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
class AltParser(p: => Parser, q: => Parser) extends Parser {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  def parse (ts: List[Token]) = p.parse(ts) ++ q.parse(ts)   
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
class SeqParser(p: => Parser, q: => Parser) extends Parser {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  def parse(ts: List[Token]) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
    for ((head1, tail1) <- p.parse(ts); 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
         (head2, tail2) <- q.parse(tail1)) yield (combine(head1, head2), tail2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
class ListParser(ps: => List[Parser]) extends Parser {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  def parse(ts: List[Token]) = ps match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
    case Nil => Set()
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
    case p::Nil => p.parse(ts)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
    case p::ps =>
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
      for ((head1, tail1) <- p.parse(ts); 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
           (head2, tail2) <- new ListParser(ps).parse(tail1)) yield (Branch(List(head1, head2)), tail2)
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
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
case class TokParser(tok: Token) extends Parser {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
  def parse(ts: List[Token]) = ts match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
    case t::ts if (t == tok) => Set((Leaf(t), ts)) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
    case _ => Set ()
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
  }
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
implicit def token2tparser(t: Token) = TokParser(t)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
case object IdParser extends Parser {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  def parse(ts: List[Token]) = ts match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
    case T_ID(s)::ts => Set((Leaf(T_ID(s)), ts)) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
    case _ => Set ()
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
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
case object NumParser extends Parser {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
  def parse(ts: List[Token]) = ts match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
    case T_NUM(s)::ts => Set((Leaf(T_NUM(s)), ts)) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
    case _ => Set ()
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
  }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
lazy val E: Parser = (T ~ T_OP("+") ~ E) || T  // start symbol
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
lazy val T: Parser = (F ~ T_OP("*") ~ T) || F
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
lazy val F: Parser = (T_LPAREN ~ E ~ T_RPAREN) || NumParser
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
   
71
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
   116
println(Tok.fromString("1 + 2 + 3"))
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
   117
println(E.parse_all(Tok.fromString("1 + 2 + 3")))
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
def eval(t: ParseTree) : Int = t match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
  case Leaf(T_NUM(n)) => n.toInt
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
  case Branch(List(t1, Leaf(T_OP("+")), t2)) => eval(t1) + eval(t2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
  case Branch(List(t1, Leaf(T_OP("*")), t2)) => eval(t1) * eval(t2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
  case Branch(List(Leaf(T_LPAREN), t, Leaf(T_RPAREN))) => eval(t) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
71
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
   126
(E.parse_all(Tok.fromString("1 + 2 + 3"))).map(eval(_))
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
   127
(E.parse_all(Tok.fromString("1 + 2 * 3"))).map(eval(_))
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
lazy val EXPR: Parser = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
  new ListParser(List(T_IF, EXPR, T_THEN, EXPR)) || 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
  new ListParser(List(T_IF, EXPR, T_THEN, EXPR, T_ELSE, EXPR)) || 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
  IdParser
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
 
71
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
   134
println(EXPR.parse_all(Tok.fromString("if a then b else c")))
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
   135
println(EXPR.parse_all(Tok.fromString("if a then if x then y else c")))
62
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
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139