parser2.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 21 Nov 2012 02:20:16 +0000
changeset 66 9215b9fb8852
parent 64 2d625418c011
child 71 7717f20f0504
permissions -rw-r--r--
tuned
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
// parse trees
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
abstract class ParseTree
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
case class Leaf(t: Token) extends ParseTree
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
case class Branch(pts: List[ParseTree]) extends ParseTree
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
def combine(pt1: ParseTree, pt2: ParseTree) = pt1 match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  case Leaf(t) => Branch(List(Leaf(t), pt2))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  case Branch(pts) => Branch(pts ++ List(pt2))
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
// parser combinators
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
abstract class Parser {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  def parse(ts: List[Token]): Set[(ParseTree, List[Token])]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  def parse_all(ts: List[Token]) : Set[ParseTree] =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
    for ((head, tail) <- parse(ts); if (tail == Nil)) yield head
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
  def || (right : => Parser) : Parser = new AltParser(this, right)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
  def ~ (right : => Parser) : Parser = new SeqParser(this, right)
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(p: => Parser, q: => Parser) extends Parser {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
  def parse (ts: List[Token]) = p.parse(ts) ++ q.parse(ts)   
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 SeqParser(p: => Parser, q: => Parser) extends Parser {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
  def parse(ts: List[Token]) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
    for ((head1, tail1) <- p.parse(ts); 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
         (head2, tail2) <- q.parse(tail1)) yield (combine(head1, head2), tail2)
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
class ListParser(ps: => List[Parser]) extends Parser {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
  def parse(ts: List[Token]) = ps match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
    case Nil => Set()
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
    case p::Nil => p.parse(ts)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
    case p::ps =>
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
      for ((head1, tail1) <- p.parse(ts); 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
           (head2, tail2) <- new ListParser(ps).parse(tail1)) yield (Branch(List(head1, head2)), tail2)
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
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
case class TokParser(tok: Token) extends Parser {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
  def parse(ts: List[Token]) = ts match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
    case t::ts if (t == tok) => Set((Leaf(t), ts)) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
    case _ => Set ()
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
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
implicit def token2tparser(t: Token) = TokParser(t)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
case object IdParser extends Parser {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
  def parse(ts: List[Token]) = ts match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
    case T_ID(s)::ts => Set((Leaf(T_ID(s)), ts)) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
    case _ => Set ()
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
  }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
case object NumParser extends Parser {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
  def parse(ts: List[Token]) = ts match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
    case T_NUM(s)::ts => Set((Leaf(T_NUM(s)), ts)) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
    case _ => Set ()
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
  }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
lazy val E: Parser = (T ~ T_OP("+") ~ E) || T  // start symbol
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
lazy val T: Parser = (F ~ T_OP("*") ~ T) || F
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
lazy val F: Parser = (T_LPAREN ~ E ~ T_RPAREN) || NumParser
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
   
64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
   118
tokenizer(lexing_rules, "1 + 2 + 3")
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
println(E.parse_all(tokenizer(lexing_rules, "1 + 2 + 3")))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
def eval(t: ParseTree) : Int = t match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
  case Leaf(T_NUM(n)) => n.toInt
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
  case Branch(List(t1, Leaf(T_OP("+")), t2)) => eval(t1) + eval(t2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
  case Branch(List(t1, Leaf(T_OP("*")), t2)) => eval(t1) * eval(t2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
  case Branch(List(Leaf(T_LPAREN), t, Leaf(T_RPAREN))) => eval(t) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
(E.parse_all(tokenizer(lexing_rules, "1 + 2 + 3"))).map(eval(_))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
(E.parse_all(tokenizer(lexing_rules, "1 + 2 * 3"))).map(eval(_))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
(E.parse_all(tokenizer(lexing_rules, "(1 + 2) * 3"))).map(eval(_))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
lazy val EXPR: Parser = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
  new ListParser(List(T_IF, EXPR, T_THEN, EXPR)) || 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
  new ListParser(List(T_IF, EXPR, T_THEN, EXPR, T_ELSE, EXPR)) || 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
  IdParser
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
println(EXPR.parse_all(tokenizer(lexing_rules, "if a then b else c")))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
println(EXPR.parse_all(tokenizer(lexing_rules, "if a then if x then y else c")))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142