progs/parser2a.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 29 Nov 2018 02:18:05 +0000
changeset 613 bfd511b7ecbf
parent 93 4794759139ea
permissions -rw-r--r--
updated
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
// Parser combinators including semantic actions
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
     2
// parses lists of tokens
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
     3
//
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
     4
// Needs
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
     5
//    :load matcher.scala
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
// some regular expressions
71
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
     8
val LETTER = RANGE("abcdefghijklmnopqrstuvwxyz")
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
val ID = PLUS(LETTER)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
71
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    11
val DIGIT = RANGE("0123456789")
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    12
val NONZERODIGIT = RANGE("123456789")
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
val NUMBER = ALT(SEQ(NONZERODIGIT, STAR(DIGIT)), "0")
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
val LPAREN = CHAR('(')
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
val RPAREN = CHAR(')')
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
71
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    18
val WHITESPACE = PLUS(RANGE(" \n"))
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    19
val OPS = RANGE("+-*")
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
// for classifying the strings that have been recognised
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
abstract class Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
case object T_WHITESPACE extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
case class T_NUM(s: String) extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
case class T_ID(s: String) extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
case class T_OP(s: String) extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
case object T_LPAREN extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
case object T_RPAREN extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
case object T_IF extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
case object T_THEN extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
case object T_ELSE extends Token
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
// lexing rules for arithmetic expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
val lexing_rules: List[Rule[Token]]= 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  List(("if", (s) => T_IF),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
       ("then", (s) => T_THEN),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
       ("else", (s) => T_ELSE),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
       (NUMBER, (s) => T_NUM(s.mkString)),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
       (ID, (s) => T_ID(s.mkString)),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
       (WHITESPACE, (s) => T_WHITESPACE),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
       (LPAREN, (s) => T_LPAREN),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
       (RPAREN, (s) => T_RPAREN),
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
       (OPS, (s) => T_OP(s.mkString)))
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
71
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    46
val Tok = Tokenizer(lexing_rules, List(T_WHITESPACE))
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
// parser combinators with return type T
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
abstract class Parser[T] {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  def parse(ts: List[Token]): Set[(T, List[Token])]
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  def parse_all(ts: List[Token]) : Set[T] =
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
    for ((head, tail) <- parse(ts); if (tail == Nil)) yield head
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
  def || (right : => Parser[T]) : Parser[T] = new AltParser(this, right)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
  def ==>[S] (f: => T => S) : Parser [S] = new FunParser(this, f)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
  def ~[S] (right : => Parser[S]) : Parser[(T, S)] = new SeqParser(this, right)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
  def ~>[S] (right : => Parser[S]) : Parser[S] = this ~ right ==> (x => x._2)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
  def <~[S] (right : => Parser[S]) : Parser[T] = this ~ right ==> (x => x._1)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
class SeqParser[T, S](p: => Parser[T], q: => Parser[S]) extends Parser[(T, S)] {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  def parse(sb: List[Token]) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
    for ((head1, tail1) <- p.parse(sb); 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
         (head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2)
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[T](p: => Parser[T], q: => Parser[T]) extends Parser[T] {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  def parse (sb: List[Token]) = p.parse(sb) ++ q.parse(sb)   
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 FunParser[T, S](p: => Parser[T], f: T => S) extends Parser[S] {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  def parse (sb: List[Token]) = 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
}
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
case class TokParser(tok: Token) extends Parser[Token] {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  def parse(ts: List[Token]) = ts match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
    case t::ts if (t == tok) => Set((t, ts)) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
    case _ => Set ()
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
  }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
implicit def token2tparser(t: Token) = TokParser(t)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
case object NumParser extends Parser[Int] {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  def parse(ts: List[Token]) = ts match {
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
    case T_NUM(s)::ts => Set((s.toInt, ts)) 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
    case _ => Set ()
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
  }
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
64
2d625418c011 added everything
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 62
diff changeset
    95
lazy val E: Parser[Int] = (T ~ T_OP("+") ~ E) ==> { case ((x, y), z) => x + z } || T  
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
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
    97
lazy val F: Parser[Int] = (T_LPAREN ~> E <~ T_RPAREN) || NumParser
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
   
71
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
    99
println(E.parse_all(Tok.fromString("1 + 2 + 3")))
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
   100
println(E.parse_all(Tok.fromString("1 + 2 * 3")))
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
   101
println(E.parse_all(Tok.fromString("(1 + 2) * 3")))
62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
71
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
   103
// Excercise: implement minus 
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
   104
println(E.parse_all(Tok.fromString("(1 - 2) * 3")))
7717f20f0504 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 64
diff changeset
   105
println(E.parse_all(Tok.fromString("(1 + 2) * - 3")))