progs/parser-combinators/comb2.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 17 Nov 2023 20:06:43 +0000
changeset 955 47acfd7f9096
parent 954 eda0ccf56c72
child 956 ae9782e62bdd
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
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
    12
// more convenience for the map parsers later on;
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
    13
// it allows writing nested patterns as
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
    14
// case x ~ y ~ z => ...
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
    15
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    16
948
6bb67c2dcfd3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    17
case class ~[+A, +B](x: A, y: B)
849
3d5ecb8f1f2f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 828
diff changeset
    18
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
    20
type IsSeq[I] = I => Seq[_]
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
    21
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
    22
abstract class Parser[I, T](using is: I => Seq[_])  {
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
    23
  def parse(in: I): Set[(T, I)]  
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
  def parse_all(in: I) : Set[T] =
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
    for ((hd, tl) <- parse(in); 
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
    27
        if is(tl).isEmpty) yield hd
732
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
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
// parser combinators
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
    32
// alternative parser
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
    33
class AltParser[I : IsSeq, T](p: => Parser[I, T], 
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
    34
                              q: => Parser[I, T]) extends Parser[I, T] {
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
    35
  def parse(in: I) = p.parse(in) ++ q.parse(in)   
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
    36
}
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
    37
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
// sequence parser
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
    39
class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], 
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
    40
                                 q: => Parser[I, S]) extends Parser[I, ~[T, S]] {
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
  def parse(in: I) = 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
    for ((hd1, tl1) <- p.parse(in); 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
         (hd2, tl2) <- q.parse(tl1)) yield (new ~(hd1, hd2), tl2)
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
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
// map parser
948
6bb67c2dcfd3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    47
class MapParser[I : IsSeq, T, S](p: => Parser[I, T], 
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
    48
                                f: T => S) extends Parser[I, S] {
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
  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
    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
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
// atomic parser for (particular) strings
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
case class StrParser(s: String) extends Parser[String, String] {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
  def parse(sb: String) = {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
    val (prefix, suffix) = sb.splitAt(s.length)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
    if (prefix == s) Set((prefix, suffix)) else Set()
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
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
// atomic parser for identifiers (variable names)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
case object IdParser extends Parser[String, String] {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
  val reg = "[a-z][a-z,0-9]*".r
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
  def parse(sb: String) = reg.findPrefixOf(sb) match {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
    case None => Set()
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
    case Some(s) => Set(sb.splitAt(s.length))
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
948
6bb67c2dcfd3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    72
// atomic parser for numbers (transformed into Ints)
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
case object NumParser extends Parser[String, Int] {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
  val reg = "[0-9]+".r
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
  def parse(sb: String) = reg.findPrefixOf(sb) match {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
    case None => Set()
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
    case Some(s) => {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
      val (hd, tl) = sb.splitAt(s.length)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
      Set((hd.toInt, tl)) 
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
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
// the following string interpolation allows us to write 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
// StrParser(_some_string_) more conveniently as 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
//
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
// p"<_some_string_>" 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
    89
extension (sc: StringContext) 
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
    90
  def p(args: Any*) = StrParser(sc.s(args:_*))
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 849
diff changeset
    91
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
// more convenient syntax for parser combinators
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 919
diff changeset
    94
extension [I : IsSeq, T](p: Parser[I, T]) {
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
  def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
948
6bb67c2dcfd3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    97
  def map[S](f: => T => S) = new MapParser[I, T, S](p, f)
732
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
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
// the abstract syntax trees for the WHILE language
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
abstract class Stmt
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
abstract class AExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
abstract class BExp 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
type Block = List[Stmt]
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
case object Skip extends Stmt
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
case class While(b: BExp, bl: Block) extends Stmt
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
case class Assign(s: String, a: AExp) extends Stmt
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
case class Write(s: String) extends Stmt
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
case class Var(s: String) extends AExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
case class Num(i: Int) extends AExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
case object True extends BExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
case object False extends BExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
case class And(b1: BExp, b2: BExp) extends BExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
case class Or(b1: BExp, b2: BExp) extends BExp
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
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
// arithmetic expressions
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
lazy val AExp: Parser[String, AExp] = 
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   127
  (Te ~ p"+" ~ AExp).map[AExp]{ case x ~ _ ~ z => Aop("+", x, z) } ||
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) } || Te
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
lazy val Te: Parser[String, AExp] = 
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   130
  (Fa ~ p"*" ~ Te).map[AExp]{ case x ~ _ ~ z => Aop("*", x, z) } || 
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) } || Fa  
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
lazy val Fa: Parser[String, AExp] = 
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   133
   (p"(" ~ AExp ~ p")").map{ case _ ~ y ~ _ => y } || 
948
6bb67c2dcfd3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   134
   IdParser.map(Var(_)) || 
6bb67c2dcfd3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   135
   NumParser.map(Num(_))
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
// boolean expressions with some simple nesting
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
lazy val BExp: Parser[String, BExp] = 
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   139
   (AExp ~ p"==" ~ AExp).map[BExp]{ case x ~ _ ~ z => Bop("==", x, z) } || 
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
   (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
   144
   (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
   145
   (p"true".map[BExp]{ _ => True }) || 
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   146
   (p"false".map[BExp]{ _ => False }) ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   147
   (p"(" ~ BExp ~ p")").map[BExp]{ case _ ~ x ~ _ => x }
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
   149
// a single statement 
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
lazy val Stmt: Parser[String, Stmt] =
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   151
  ((p"skip".map[Stmt]{_ => Skip }) ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   152
   (IdParser ~ p":=" ~ AExp).map[Stmt]{ case x ~ _ ~ z => Assign(x, z) } ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   153
   (p"write(" ~ IdParser ~ p")").map[Stmt]{ case _ ~ y ~ _ => Write(y) } ||
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
   (p"if" ~ BExp ~ p"then" ~ Block ~ p"else" ~ Block)
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   155
     .map[Stmt]{ case _ ~ y ~ _ ~ u ~ _ ~ w => If(y, u, w) } ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   156
   (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
   157
 
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
// statements
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
lazy val Stmts: Parser[String, Block] =
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   161
  (Stmt ~ p";" ~ Stmts).map[Block]{ case x ~ _ ~ z => x :: z } ||
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   162
  (Stmt.map[Block]{ s => List(s) })
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
// blocks (enclosed in curly braces)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
lazy val Block: Parser[String, Block] =
919
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   166
  ((p"{" ~ Stmts ~ p"}").map{ case _ ~ y ~ _ => y } || 
53f08d873e09 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
   167
   (Stmt.map(s => List(s))))
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
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
// Examples
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   171
println(BExp.parse_all("5+3"))
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   172
println(Stmt.parse_all("5==3"))
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   173
println(Stmt.parse_all("x2:=5+3"))
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   174
println(Block.parse_all("{x:=5;y:=8}"))
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   175
println(Block.parse_all("if(false)then{x:=5}else{x:=10}"))
732
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
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   189
println("fib testcase:")
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   190
println(Stmts.parse_all(fib))
732
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
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
// an interpreter for the WHILE language
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
type Env = Map[String, Int]
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
def eval_aexp(a: AExp, env: Env) : Int = a match {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
  case Num(i) => i
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
  case Var(s) => env(s)
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
  case Aop("/", a1, a2) => eval_aexp(a1, env) / eval_aexp(a2, env)
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
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
def eval_bexp(b: BExp, env: Env) : Boolean = b match {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
  case True => true
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
  case False => false
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 Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
  case And(b1, b2) => eval_bexp(b1, env) && eval_bexp(b2, env)
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
  case Or(b1, b2) => eval_bexp(b1, env) || eval_bexp(b2, env)
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
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
def eval_stmt(s: Stmt, env: Env) : Env = s match {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
  case Skip => env
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
  case Assign(x, a) => env + (x -> eval_aexp(a, env))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
  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
   220
  case While(b, bl) => 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
    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
   222
    else env
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
  case Write(x) => { println(env(x)) ; env }  
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
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
def eval_bl(bl: Block, env: Env) : Env = bl match {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
  case Nil => env
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
  case s::bl => eval_bl(bl, eval_stmt(s, env))
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
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
def eval(bl: Block) : Env = eval_bl(bl, Map())
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
// parse + evaluate fib program; then lookup what is
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
   234
// stored under the variable "result" 
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
println(eval(Stmts.parse_all(fib).head)("result"))
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
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
// more examles
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
// calculate and print all factors bigger 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
// than 1 and smaller than n
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
println("Factors")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
val factors =  
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
   """n := 12;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
      f := 2;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
      while (f < n / 2 + 1) do {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
        if ((n / f) * f == n) then  { write(f) } else { skip };
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
        f := f + 1
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
      }""".replaceAll("\\s+", "")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   252
println(eval(Stmts.parse_all(factors).head))
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
742
b5b5583a3a08 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 732
diff changeset
   254
732
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
// calculate all prime numbers up to a number 
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
println("Primes")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   257
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   258
val primes =  
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   259
   """end := 100;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   260
      n := 2;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
      while (n < end) do {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   262
        f := 2;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   263
        tmp := 0;
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   264
        while ((f < n / 2 + 1) && (tmp == 0)) do {
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   265
          if ((n / f) * f == n) then  { tmp := 1 } else { skip };
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   266
          f := f + 1
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   267
        };
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   268
        if (tmp == 0) then { write(n) } else { skip };
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   269
        n  := n + 1
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   270
      }""".replaceAll("\\s+", "")
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   271
c7bdd7eac4cb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   272
println(eval(Stmts.parse_all(primes).head))