solutions/cw4/parser.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 19 Sep 2024 15:47:33 +0100
changeset 961 c0600f8b6427
parent 959 64ec1884d860
child 965 94f5cce73a4f
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// CW3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
     3
import scala.language.implicitConversions
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
     4
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
     5
import $file.lexer, lexer._ 
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
     7
case class ~[+A, +B](x: A, y: B)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
     9
// parser combinators
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    10
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    11
abstract class Parser[I, T](using is: I => Seq[?])  {
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    12
  def parse(in: I): Set[(T, I)]  
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    13
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    14
  def parse_all(in: I) : Set[T] =
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    15
    for ((hd, tl) <- parse(in); 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    16
        if is(tl).isEmpty) yield hd
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    17
}
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    19
// alternative parser
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    20
class AltParser[I, T](p: => Parser[I, T], 
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    21
                      q: => Parser[I, T])(using I => Seq[?]) extends Parser[I, T] {
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    22
  def parse(in: I) = p.parse(in) ++ q.parse(in)   
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    23
}
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    25
// sequence parser
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    26
class SeqParser[I, T, S](p: => Parser[I, T], 
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    27
                         q: => Parser[I, S])(using I => Seq[?]) extends Parser[I, ~[T, S]] {
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    28
  def parse(in: I) = 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    29
    for ((hd1, tl1) <- p.parse(in); 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    30
         (hd2, tl2) <- q.parse(tl1)) yield (new ~(hd1, hd2), tl2)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    33
// map parser
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    34
class MapParser[I, T, S](p: => Parser[I, T], 
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    35
                         f: T => S)(using I => Seq[?]) extends Parser[I, S] {
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    36
  def parse(in: I) = for ((hd, tl) <- p.parse(in)) yield (f(hd), tl)
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    37
}
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    38
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    39
// more convenient syntax for parser combinators
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    40
extension [I, T](p: Parser[I, T])(using I => Seq[?]) {
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    41
  def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    42
  def ~[S] (q : => Parser[I, S])(using S => Seq[?]) = new SeqParser[I, T, S](p, q)
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    43
  def map[S](f: => T => S) = new MapParser[I, T, S](p, f)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    46
/*
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    47
// atomic parser for (particular) strings
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    48
case class StrParser(s: String) extends Parser[String, String] {
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    49
  def parse(sb: String) = {
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    50
    val (prefix, suffix) = sb.splitAt(s.length)
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    51
    if (prefix == s) Set((prefix, suffix)) else Set()
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    52
  }
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    55
extension (sc: StringContext) 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    56
  def p(args: Any*) = StrParser(sc.s(args:_*))
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    57
*/
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    59
case class TokenParser(t: Token) extends Parser[List[Token], Token] {
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    60
    def parse(in: List[Token]) = {
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    61
      // an example of an atomic parser for characters
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    62
      if (!in.isEmpty && in.head == t) Set((t, in.tail)) else Set()
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    63
    }
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    64
}   
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    65
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
case class TokenListParser(ts: List[Token]) extends Parser[List[Token], List[Token]] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
    def parse(tsb: List[Token]) = {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
        val (prefix, suffix) = tsb.splitAt(ts.length)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
        if (prefix == ts) Set((prefix, suffix)) else Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
// Implicit definitions to go from a token 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
// or a list of tokens to a TokenListParser
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    75
given Conversion[Token, Parser[List[Token], Token]] = (t => TokenParser(t))
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    76
//implicit def token2parser(t: Token) : Parser[List[Token], Token] = 
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    77
//  TokenParser(t)
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    79
/*
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    80
extension (t: Token) {
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    81
    def || (q : => Parser[List[Token], Token]) = 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    82
      new AltParser[List[Token], Token](t, q)
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    83
    def ~[S](q : => Parser[List[Token], S]) = 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
    84
      new SeqParser[List[Token], Token, S](t, q)  
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
}
961
c0600f8b6427 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 959
diff changeset
    86
*/
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
// Abstract Syntax Trees
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
abstract class Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
abstract class AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
abstract class BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
type Block = List[Stmt]
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
case object Skip extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
case class While(b: BExp, bl: Block) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
case class Assign(s: String, a: AExp) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
case class Read(s: String) extends Stmt
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
case class WriteId(s: String) extends Stmt  // for printing values of variables
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
case class WriteString(s: String) extends Stmt  // for printing words
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
case class For(counter: String, lower: AExp, upper: AExp, code: Block) extends Stmt
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   104
case object Break extends Stmt
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
case class Var(s: String) extends AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
case class Num(i: Int) extends AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
case object True extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
case object False extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
case class And(b1: BExp, b2: BExp) extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
case class Or(b1: BExp, b2: BExp) extends BExp
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
case class IdParser() extends Parser[List[Token], String] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
    def parse(tsb: List[Token]) = tsb match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
        case T_ID(id) :: rest => Set((id, rest))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
        case _ => Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
case class NumParser() extends Parser[List[Token], Int] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
    def parse(tsb: List[Token]) = tsb match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
        case T_NUM(n) :: rest => Set((n, rest))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
        case _ => Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
case class StringParser() extends Parser[List[Token], String] {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
    def parse(tsb: List[Token]) = tsb match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
        case T_STRING(s) :: rest => Set((s, rest))
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
        case _ => Set()
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
    }
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
// WHILE Language Parsing
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   139
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   140
// WHILE Language Parsing
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
lazy val AExp: Parser[List[Token], AExp] = 
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   142
  (Te ~ T_OP("+") ~ AExp).map{ case x ~ _ ~ z => Aop("+", x, z): AExp } ||
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   143
  (Te ~ T_OP("-") ~ AExp).map{ case x ~ _ ~ z => Aop("-", x, z): AExp } || Te
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
lazy val Te: Parser[List[Token], AExp] = 
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   145
  (Fa ~ T_OP("*") ~ Te).map{ case x ~ _ ~ z => Aop("*", x, z): AExp } || 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   146
  (Fa ~ T_OP("/") ~ Te).map{ case x ~ _ ~ z => Aop("/", x, z): AExp } || 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   147
  (Fa ~ T_OP("%") ~ Te).map{ case x ~ _ ~ z => Aop("%", x, z): AExp } || Fa  
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
lazy val Fa: Parser[List[Token], AExp] = 
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   149
   (T_PAREN("(") ~ AExp ~ T_PAREN(")")).map{ case _ ~ y ~ _ => y } || 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   150
   IdParser().map{Var(_)}  || 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   151
   NumParser().map{Num(_)}
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
lazy val BExp: Parser[List[Token], BExp] = 
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   154
   (AExp ~ T_OP("==") ~ AExp).map{ case x ~ _ ~ z => Bop("==", x, z): BExp } || 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   155
   (AExp ~ T_OP("!=") ~ AExp).map{ case x ~ _ ~ z => Bop("!=", x, z): BExp } || 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   156
   (AExp ~ T_OP("<") ~ AExp).map{ case x ~ _ ~ z => Bop("<", x, z): BExp } || 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   157
   (AExp ~ T_OP(">") ~ AExp).map{ case x ~ _ ~ z => Bop(">", x, z): BExp } ||
959
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 920
diff changeset
   158
   (AExp ~ T_OP("<=") ~ AExp).map{ case x ~ _ ~ z => Bop("<=", x, z): BExp } ||
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents: 920
diff changeset
   159
   (AExp ~ T_OP("=>") ~ AExp).map{ case x ~ _ ~ z => Bop("=>", x, z): BExp } ||
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   160
   (T_PAREN("(") ~ BExp ~ T_PAREN(")") ~ T_OP("&&") ~ BExp).map{ case _ ~ y ~ _ ~ _ ~ v => And(y, v): BExp } ||
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   161
   (T_PAREN("(") ~ BExp ~ T_PAREN(")") ~ T_OP("||") ~ BExp).map{ case _ ~ y ~ _ ~ _ ~ v => Or(y, v): BExp } ||
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   162
   (T_KEYWORD("true").map(_ => True: BExp )) || 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   163
   (T_KEYWORD("false").map(_ => False: BExp )) ||
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   164
   (T_PAREN("(") ~ BExp ~ T_PAREN(")")).map{ case _ ~ x ~ _ => x }
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
lazy val Stmt: Parser[List[Token], Stmt] =
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   167
    T_KEYWORD("skip").map(_ => Skip: Stmt) ||
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   168
    T_KEYWORD("break").map(_ => Break: Stmt) ||
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   169
    (IdParser() ~ T_OP(":=") ~ AExp).map{ case id ~ _ ~ z => Assign(id, z): Stmt } ||
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   170
    (T_KEYWORD("if") ~ BExp ~ T_KEYWORD("then") ~ Block ~ T_KEYWORD("else") ~ Block).map{ case _ ~ y ~ _ ~ u ~ _ ~ w => If(y, u, w): Stmt } ||
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   171
    (T_KEYWORD("while") ~ BExp ~ T_KEYWORD("do") ~ Block).map{ case _ ~ y ~ _ ~ w => While(y, w) : Stmt } ||
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   172
    (T_KEYWORD("for") ~ IdParser() ~ T_OP(":=") ~ AExp ~T_KEYWORD("upto") ~ AExp ~ T_KEYWORD("do") ~ Block).map{ 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   173
        case _ ~ id ~ _ ~ low ~ _ ~ high ~ _ ~ bl => For(id, low, high, bl) : Stmt } ||
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   174
    (T_KEYWORD("read") ~ IdParser()).map{ case _ ~ id => Read(id): Stmt} ||
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   175
    (T_KEYWORD("write") ~ IdParser()).map{ case _ ~ id => WriteId(id): Stmt} ||
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   176
    (T_KEYWORD("write") ~ StringParser()).map{ case _ ~ s => WriteString(s): Stmt} || 
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   177
    (T_KEYWORD("write") ~ T_PAREN("(") ~ IdParser() ~ T_PAREN(")")).map{ case _ ~ _ ~ id ~ _ => WriteId(id): Stmt} ||
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   178
    (T_KEYWORD("write") ~  T_PAREN("(") ~ StringParser() ~ T_PAREN(")")).map{ case _ ~ _ ~ s ~ _ => WriteString(s): Stmt}
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
lazy val Stmts: Parser[List[Token], Block] =
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   181
    (Stmt ~ T_SEMI ~ Stmts).map{ case x ~ _ ~ z => x :: z : Block } ||
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   182
    (Stmt.map(s => List(s) : Block))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
lazy val Block: Parser[List[Token], Block] =
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   185
    (T_PAREN("{") ~ Stmts ~ T_PAREN("}")).map{ case x ~ y ~ z => y} ||
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   186
    (Stmt.map(s => List(s)))
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
920
7af2eea19646 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   188