progs/parser-combinators/comb2-simple.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 09 Nov 2024 06:31:45 +0000
changeset 973 f25d338d16c9
parent 972 ebb4a40d9bae
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
972
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Parser Combinators:
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
// Simple Version for WHILE-language
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
//====================================
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
//
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
//
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
// call with
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
//
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
//    amm comb2-simple.sc
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
973
f25d338d16c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 972
diff changeset
    11
// to make sure the input has an empty-method
972
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
trait IsSeq[I] {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
  extension (i: I) def isEmpty: Boolean
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
}
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
given IsSeq[String] = _.isEmpty
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
given [I]: IsSeq[Seq[I]] = _.isEmpty
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
// parser class
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
//==============
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
abstract class Parser[I : IsSeq, T]  {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
  def parse(in: I): Set[(T, I)]
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
  def parse_all(in: I) : Set[T] =
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
    for ((hd, tl) <- parse(in);
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
        if tl.isEmpty) yield hd
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
}
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
// parser combinators
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
//====================
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
// alternative parser
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
class AltParser[I : IsSeq, T](p: => Parser[I, T],
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
                              q: => Parser[I, T]) extends Parser[I, T] {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
  def parse(in: I) = p.parse(in) ++ q.parse(in)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
}
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
// sequence parser
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
class SeqParser[I: IsSeq, T, S](p: => Parser[I, T],
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
                                q: => Parser[I, S]) extends Parser[I, (T, S)] {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
  def parse(in: I) =
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
    for ((hd1, tl1) <- p.parse(in);
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
         (hd2, tl2) <- q.parse(tl1)) yield ((hd1, hd2), tl2)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
}
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
// map parser
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
class MapParser[I : IsSeq, T, S](p: => Parser[I, T],
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
                                 f: T => S) extends Parser[I, S] {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
  def parse(in: I) = for ((hd, tl) <- p.parse(in)) yield (f(hd), tl)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
}
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
// more convenient syntax for parser combinators
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
extension [I: IsSeq, T](p: Parser[I, T]) {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
  def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
  def map[S](f: => T => S) = new MapParser[I, T, S](p, f)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
}
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
// atomic parser for (particular) strings
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
case class StrParser(s: String) extends Parser[String, String] {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
  def parse(sb: String) = {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
    val (prefix, suffix) = sb.splitAt(s.length)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
    if (prefix == s) Set((prefix, suffix)) else Set()
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  }
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
}
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
// atomic parser for identifiers (variable names)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
case object IdParser extends Parser[String, String] {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
  val reg = "[a-z][a-z,0-9]*".r
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
  def parse(sb: String) = reg.findPrefixOf(sb) match {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
    case None => Set()
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
    case Some(s) => Set(sb.splitAt(s.length))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
  }
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
}
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
// atomic parser for numbers (transformed into Ints)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
case object NumParser extends Parser[String, Int] {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
  val reg = "[0-9]+".r
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
  def parse(sb: String) = reg.findPrefixOf(sb) match {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
    case None => Set()
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
    case Some(s) => {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
      val (hd, tl) = sb.splitAt(s.length)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
      Set((hd.toInt, tl)) 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
    }
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
  }
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
}
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
// the following string interpolation allows us to write 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
// StrParser(_some_string_) more conveniently as 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
//
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
// p"<_some_string_>" 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
extension (sc: StringContext) 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
  def p(args: Any*) = StrParser(sc.s(args*))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
// the abstract syntax trees for the WHILE language
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
abstract class Stmt
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
abstract class AExp
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
abstract class BExp 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
type Block = List[Stmt]
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
case object Skip extends Stmt
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
case class While(b: BExp, bl: Block) extends Stmt
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
case class Assign(s: String, a: AExp) extends Stmt
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
case class Write(s: String) extends Stmt
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
case class Var(s: String) extends AExp
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
case class Num(i: Int) extends AExp
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
case object True extends BExp
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
case object False extends BExp
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
case class And(b1: BExp, b2: BExp) extends BExp
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
case class Or(b1: BExp, b2: BExp) extends BExp
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
// arithmetic expressions
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
lazy val AExp: Parser[String, AExp] = 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
  (Te ~ p"+" ~ AExp).map[AExp]{ case ((x, _), z) => Aop("+", x, z) } ||
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
  (Te ~ p"-" ~ AExp).map[AExp]{ case ((x, _), z) => Aop("-", x, z) } || Te
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
lazy val Te: Parser[String, AExp] = 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
  (Fa ~ p"*" ~ Te).map[AExp]{ case ((x, _), z) => Aop("*", x, z) } || 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
  (Fa ~ p"/" ~ Te).map[AExp]{ case ((x, _), z) => Aop("/", x, z) } || Fa  
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
lazy val Fa: Parser[String, AExp] = 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
   (p"(" ~ AExp ~ p")").map{ case ((_, y), _) => y } || 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
   IdParser.map(Var(_)) || 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
   NumParser.map(Num(_))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
// boolean expressions with some simple nesting
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
lazy val BExp: Parser[String, BExp] = 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
  (AExp ~ p"==" ~ AExp).map[BExp]{ case ((x, _), z) => Bop("==", x, z) } || 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
  (AExp ~ p"!=" ~ AExp).map[BExp]{ case ((x, _), z) => Bop("!=", x, z) } || 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
  (AExp ~ p"<" ~ AExp).map[BExp]{ case ((x, _), z) => Bop("<", x, z) } || 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
  (AExp ~ p">" ~ AExp).map[BExp]{ case ((x, _), z) => Bop(">", x, z) } ||
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
  (p"(" ~ BExp ~ p")" ~ p"&&" ~ BExp).map[BExp]{ case ((((_, y), _), _), v) => And(y, v) } ||
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
  (p"(" ~ BExp ~ p")" ~ p"||" ~ BExp).map[BExp]{ case ((((_, y), _), _), v) => Or(y, v) } ||
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
  (p"true".map[BExp]{ _ => True }) || 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
  (p"false".map[BExp]{ _ => False }) ||
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
  (p"(" ~ BExp ~ p")").map[BExp]{ case ((_, x), _) => x }
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
// Stmt: a single statement
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
// Stmts: multiple statements
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
// Block: blocks (enclosed in curly braces)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
lazy val Stmt: Parser[String, Stmt] =
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
  ((p"skip".map[Stmt]{_ => Skip }) ||
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
  (IdParser ~ p":=" ~ AExp).map[Stmt]{ case ((x, _), z) => Assign(x, z) } ||
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
  (p"write(" ~ IdParser ~ p")").map[Stmt]{ case ((_, y), _) => Write(y) } ||
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
  (p"if" ~ BExp ~ p"then" ~ Block ~ p"else" ~ Block)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
    .map[Stmt]{ case (((((_, y), _), u), _), w) => If(y, u, w) } ||
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
  (p"while" ~ BExp ~ p"do" ~ Block).map[Stmt]{ case (((_, y), _), w) => While(y, w) })  
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
lazy val Stmts: Parser[String, Block] =
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
  (Stmt ~ p";" ~ Stmts).map[Block]{ case ((x, _), z) => x :: z } ||
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
  (Stmt.map[Block]{ s => List(s) })
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
lazy val Block: Parser[String, Block] =
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
  ((p"{" ~ Stmts ~ p"}").map{ case ((_, y), _) => y } || 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
  (Stmt.map(s => List(s))))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
// Examples
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
println(AExp.parse_all("2*2*2"))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
println(BExp.parse_all("5+3"))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
println(Stmt.parse_all("5==3"))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
println(Stmt.parse_all("x2:=5+3"))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
println(Block.parse_all("{x:=5;y:=8}"))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
println(Block.parse_all("if(false)then{x:=5}else{x:=10}"))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
val fib = """n := 10;
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
             minus1 := 0;
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
             minus2 := 1;
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
             temp := 0;
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
             while (n > 0) do {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
                 temp := minus2;
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
                 minus2 := minus1 + minus2;
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
                 minus1 := temp;
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
                 n := n - 1
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
             };
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
             result := minus2""".replaceAll("\\s+", "")
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
println("fib testcase:")
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
println(Stmts.parse_all(fib))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
// an interpreter for the WHILE language
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
type Env = Map[String, Int]
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
973
f25d338d16c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 972
diff changeset
   190
def eval_aexp(a: AExp, env: Env) : Int = a match {
972
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
    case Num(i) => i
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
    case Var(s) => env(s)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
    case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
    case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
    case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
    case Aop("/", a1, a2) => eval_aexp(a1, env) / eval_aexp(a2, env)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
  }
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
973
f25d338d16c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 972
diff changeset
   199
def eval_bexp(b: BExp, env: Env) : Boolean = b match {
972
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
    case True => true
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
    case False => false
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
    case Bop("==", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
    case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
    case Bop(">", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
    case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
    case And(b1, b2) => eval_bexp(b1, env) && eval_bexp(b2, env)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
    case Or(b1, b2) => eval_bexp(b1, env) || eval_bexp(b2, env)
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
  }
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
973
f25d338d16c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 972
diff changeset
   210
def eval_stmt(s: Stmt, env: Env) : Env = s match {
972
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
    case Skip => env
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
    case Assign(x, a) => env + (x -> eval_aexp(a, env))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
    case If(b, bl1, bl2) => if (eval_bexp(b, env)) eval_bl(bl1, env) else eval_bl(bl2, env) 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
    case While(b, bl) => 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
      if (eval_bexp(b, env)) eval_stmt(While(b, bl), eval_bl(bl, env))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
      else env
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
    case Write(x) => { println(env(x)) ; env }  
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
  }
973
f25d338d16c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 972
diff changeset
   219
def eval_bl(bl: Block, env: Env) : Env = bl match {
972
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
    case Nil => env
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
    case s::bl => eval_bl(bl, eval_stmt(s, env))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
  }
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
def eval(bl: Block) : Env = eval_bl(bl, Map())
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
// parse + evaluate fib program; then lookup what is
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
// stored under the variable "result" 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
println(eval(Stmts.parse_all(fib).head)("result"))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
// more examples
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
// calculate and print all factors bigger 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
// than 1 and smaller than n
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
println("Factors")
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
val factors =  
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
   """n := 12;
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
      f := 2;
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
      while (f < n / 2 + 1) do {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
        if ((n / f) * f == n) then  { write(f) } else { skip };
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
        f := f + 1
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
      }""".replaceAll("\\s+", "")
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
println(eval(Stmts.parse_all(factors).head))
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
// calculate all prime numbers up to a number 
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
println("Primes")
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
val primes =  
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   252
   """end := 100;
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
      n := 2;
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   254
      while (n < end) do {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
        f := 2;
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
        tmp := 0;
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   257
        while ((f < n / 2 + 1) && (tmp == 0)) do {
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   258
          if ((n / f) * f == n) then  { tmp := 1 } else { skip };
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   259
          f := f + 1
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   260
        };
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
        if (tmp == 0) then { write(n) } else { skip };
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   262
        n  := n + 1
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   263
      }""".replaceAll("\\s+", "")
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   264
ebb4a40d9bae updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   265
println(eval(Stmts.parse_all(primes).head))