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