solutions/cw4a/parser.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 25 Oct 2024 18:54:08 +0100
changeset 971 51e00f223792
parent 910 b655ce68983f
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
910
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// CW3
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
import $file.lexer
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
import lexer._ 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
case class ~[+A, +B](_1: A, _2: B)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
type IsSeq[A] = A => Seq[_]
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
abstract class Parser[I : IsSeq, T] {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
  def parse(ts: I): Set[(T, I)]
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
  def parse_all(ts: I) : Set[T] =
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
    for ((head, tail) <- parse(ts); if tail.isEmpty) yield head
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
}
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, ~[T, S]] {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
  def parse(sb: I) = 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
    for ((head1, tail1) <- p.parse(sb); 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
         (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
}
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
class AltParser[I : IsSeq, T](p: => Parser[I, T], q: => Parser[I, T]) extends Parser[I, T] {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
}
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
class FunParser[I : IsSeq, T, S](p: => Parser[I, T], f: T => S) extends Parser[I, S] {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
  def parse(sb: I) = 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
}
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
// New parser that takes as input a list of tokens
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
case class TokenListParser(ts: List[Token]) extends Parser[List[Token], List[Token]] {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
    def parse(tsb: List[Token]) = {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
        val (prefix, suffix) = tsb.splitAt(ts.length)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
        if (prefix == ts) Set((prefix, suffix)) else Set()
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
    }
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
}
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
// Implicit definitions to go from a token 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
// or a list of tokens to a TokenListParser
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
implicit def token2parser(t: Token) = TokenListParser(List(t))
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
implicit def tokenList2parser(ts: List[Token]) = TokenListParser(ts)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
  def || (q : => Parser[I, T]) = new AltParser[I, T](p, q)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
}
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
implicit def TokenOps(t: Token) = new {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
    def || (q : => Parser[List[Token], List[Token]]) = new AltParser[List[Token], List[Token]](List(t), q)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
    def || (qs : List[Token]) = new AltParser[List[Token], List[Token]](List(t), qs)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
    def ==>[S] (f: => List[Token] => S) = new FunParser[List[Token], List[Token], S](List(t), f)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
    def ~[S](q : => Parser[List[Token], S]) =
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
        new SeqParser[List[Token], List[Token], S](List(t), q)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
    def ~ (qs : List[Token]) =
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
        new SeqParser[List[Token], List[Token], List[Token]](List(t), qs)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
}
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
implicit def TokenListOps(ts: List[Token]) = new {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
    def || (q : => Parser[List[Token], List[Token]]) = new AltParser[List[Token], List[Token]](ts, q)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
    def || (qs : List[Token]) = new AltParser[List[Token], List[Token]](ts, qs)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
    def ==>[S] (f: => List[Token] => S) = new FunParser[List[Token], List[Token], S](ts, f)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
    def ~[S](q : => Parser[List[Token], S]) =
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
        new SeqParser[List[Token], List[Token], S](ts, q)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
    def ~ (qs : List[Token]) =
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
        new SeqParser[List[Token], List[Token], List[Token]](ts, qs)
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
}
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
// Abstract Syntax Trees
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
abstract class Stmt
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
abstract class AExp
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
abstract class BExp
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
type Block = List[Stmt]
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
case object Skip extends Stmt
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
case object Break extends Stmt
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
case class While(b: BExp, bl: Block) extends Stmt
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
case class Assign(s: String, a: AExp) extends Stmt
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
case class Read(s: String) extends Stmt
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
case class WriteId(s: String) extends Stmt  // for printing values of variables
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
case class WriteString(s: String) extends Stmt  // for printing words
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
case class For(counter: String, lower: AExp, upper: AExp, code: Block) extends Stmt
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
case class Var(s: String) extends AExp
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
case class Num(i: Int) extends AExp
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
case object True extends BExp
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
case object False extends BExp
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
case class And(b1: BExp, b2: BExp) extends BExp
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
case class Or(b1: BExp, b2: BExp) extends BExp
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
case class IdParser() extends Parser[List[Token], String] {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
    def parse(tsb: List[Token]) = tsb match {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
        case T_ID(id) :: rest => Set((id, rest))
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
        case _ => Set()
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
    }
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
}
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
case class NumParser() extends Parser[List[Token], Int] {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
    def parse(tsb: List[Token]) = tsb match {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
        case T_NUM(n) :: rest => Set((n, rest))
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
        case _ => Set()
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
    }
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
}
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
case class StringParser() extends Parser[List[Token], String] {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
    def parse(tsb: List[Token]) = tsb match {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
        case T_STRING(s) :: rest => Set((s, rest))
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
        case _ => Set()
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
    }
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
}
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
// WHILE Language Parsing
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
lazy val AExp: Parser[List[Token], AExp] = 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
  (Te ~ T_OP("+") ~ AExp) ==> { case x ~ _ ~ z => Aop("+", x, z): AExp } ||
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
  (Te ~ T_OP("-") ~ AExp) ==> { case x ~ _ ~ z => Aop("-", x, z): AExp } || Te
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
lazy val Te: Parser[List[Token], AExp] = 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
  (Fa ~ T_OP("*") ~ Te) ==> { case x ~ _ ~ z => Aop("*", x, z): AExp } || 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
  (Fa ~ T_OP("/") ~ Te) ==> { case x ~ _ ~ z => Aop("/", x, z): AExp } || 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
  (Fa ~ T_OP("%") ~ Te) ==> { case x ~ _ ~ z => Aop("%", x, z): AExp } || Fa  
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
lazy val Fa: Parser[List[Token], AExp] = 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
   (T_PAREN("(") ~ AExp ~ T_PAREN(")")) ==> { case _ ~ y ~ _ => y } || 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
   IdParser() ==> Var  || 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
   NumParser() ==> Num
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
lazy val BExp: Parser[List[Token], BExp] = 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
   (AExp ~ T_OP("==") ~ AExp) ==> { case x ~ _ ~ z => Bop("==", x, z): BExp } || 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
   (AExp ~ T_OP("!=") ~ AExp) ==> { case x ~ _ ~ z => Bop("!=", x, z): BExp } || 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
   (AExp ~ T_OP("<") ~ AExp) ==> { case x ~ _ ~ z => Bop("<", x, z): BExp } || 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
   (AExp ~ T_OP(">") ~ AExp) ==> { case x ~ _ ~ z => Bop(">", x, z): BExp } ||
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
   (T_PAREN("(") ~ BExp ~ List(T_PAREN(")"), T_OP("&&")) ~ BExp) ==> { case _ ~ y ~ _ ~ v => And(y, v): BExp } ||
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
   (T_PAREN("(") ~ BExp ~ List(T_PAREN(")"), T_OP("||")) ~ BExp) ==> { case _ ~ y ~ _ ~ v => Or(y, v): BExp } ||
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
   (T_KEYWORD("true") ==> (_ => True: BExp )) || 
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
   (T_KEYWORD("false") ==> (_ => False: BExp )) ||
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
   (T_PAREN("(") ~ BExp ~ T_PAREN(")")) ==> { case _ ~ x ~ _ => x }
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
lazy val Stmt: Parser[List[Token], Stmt] =
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
    T_KEYWORD("skip") ==> (_ => Skip: Stmt) ||
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
    T_KEYWORD("break") ==> (_ => Break: Stmt) ||
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
    (IdParser() ~ T_OP(":=") ~ AExp) ==> { case id ~ _ ~ z => Assign(id, z): Stmt } ||
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
    (T_KEYWORD("if") ~ BExp ~ T_KEYWORD("then") ~ Block ~ T_KEYWORD("else") ~ Block) ==> { case _ ~ y ~ _ ~ u ~ _ ~ w => If(y, u, w): Stmt } ||
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
    (T_KEYWORD("while") ~ BExp ~ T_KEYWORD("do") ~ Block) ==> { case _ ~ y ~ _ ~ w => While(y, w) : Stmt } ||
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
    (T_KEYWORD("read") ~ IdParser()) ==> { case _ ~ id => Read(id): Stmt} ||
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
    (T_KEYWORD("write") ~ IdParser()) ==> { case _ ~ id => WriteId(id): Stmt} ||
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
    (T_KEYWORD("write") ~ StringParser()) ==> { case _ ~ s => WriteString(s): Stmt} ||
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
    (T_KEYWORD("for") ~ IdParser() ~ T_OP(":=") ~ AExp ~ T_KEYWORD("upto") ~ AExp ~ T_KEYWORD("do") ~ Block) ==> {
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
      case _ ~ id ~ _ ~ lower ~ _ ~ upper ~ _ ~ blck => For(id, lower, upper, blck): Stmt
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
    }
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
lazy val Stmts: Parser[List[Token], Block] =
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
    (Stmt ~ T_SEMI ~ Stmts) ==> { case x ~ _ ~ z => x :: z : Block } ||
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
    (Stmt ==> (s => List(s) : Block))
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
lazy val Block: Parser[List[Token], Block] =
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
    (T_PAREN("{") ~ Stmts ~ T_PAREN("}")) ==> { case x ~ y ~ z => y} ||
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
    (Stmt ==> (s => List(s)))
b655ce68983f added break
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164