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