progs/while.scala
changeset 742 b5b5583a3a08
parent 741 e66bd5c563eb
child 743 6acabeecdf75
equal deleted inserted replaced
741:e66bd5c563eb 742:b5b5583a3a08
     1 // A parser and evaluator for the while language
       
     2 // 
       
     3 import matcher._
       
     4 import parser._
       
     5 
       
     6 
       
     7 // some regular expressions
       
     8 val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_")
       
     9 val DIGIT = RANGE("0123456789")
       
    10 val ID = SEQ(SYM, STAR(ALT(SYM, DIGIT))) 
       
    11 val NUM = PLUS(DIGIT)
       
    12 val KEYWORD = ALTS("skip", "while", "do", "if", "then", "else", "true", "false") 
       
    13 val SEMI: Rexp = ";"
       
    14 val OP: Rexp = ALTS(":=", "=", "-", "+", "*", "!=", "<", ">")
       
    15 val WHITESPACE = PLUS(RANGE(" \n"))
       
    16 val RPAREN: Rexp = ")"
       
    17 val LPAREN: Rexp = "("
       
    18 val BEGIN: Rexp = "{"
       
    19 val END: Rexp = "}"
       
    20 
       
    21 // tokens for classifying the strings that have been recognised
       
    22 abstract class Token
       
    23 case object T_WHITESPACE extends Token
       
    24 case object T_SEMI extends Token
       
    25 case object T_LPAREN extends Token
       
    26 case object T_RPAREN extends Token
       
    27 case object T_BEGIN extends Token
       
    28 case object T_END extends Token
       
    29 case class T_ID(s: String) extends Token
       
    30 case class T_OP(s: String) extends Token
       
    31 case class T_NUM(s: String) extends Token
       
    32 case class T_KWD(s: String) extends Token
       
    33 
       
    34 val lexing_rules: List[(Rexp, List[Char] => Token)] = 
       
    35   List((KEYWORD, (s) => T_KWD(s.mkString)),
       
    36        (ID, (s) => T_ID(s.mkString)),
       
    37        (OP, (s) => T_OP(s.mkString)),
       
    38        (NUM, (s) => T_NUM(s.mkString)),
       
    39        (SEMI, (s) => T_SEMI),
       
    40        (LPAREN, (s) => T_LPAREN),
       
    41        (RPAREN, (s) => T_RPAREN),
       
    42        (BEGIN, (s) => T_BEGIN),
       
    43        (END, (s) => T_END),
       
    44        (WHITESPACE, (s) => T_WHITESPACE))
       
    45 
       
    46 // the tokenizer
       
    47 val Tok = Tokenizer(lexing_rules, List(T_WHITESPACE))
       
    48 
       
    49 // the abstract syntax trees
       
    50 abstract class Stmt
       
    51 abstract class AExp
       
    52 abstract class BExp 
       
    53 type Block = List[Stmt]
       
    54 case object Skip extends Stmt
       
    55 case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
       
    56 case class While(b: BExp, bl: Block) extends Stmt
       
    57 case class Assign(s: String, a: AExp) extends Stmt
       
    58 
       
    59 case class Var(s: String) extends AExp
       
    60 case class Num(i: Int) extends AExp
       
    61 case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
       
    62 
       
    63 case object True extends BExp
       
    64 case object False extends BExp
       
    65 case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
       
    66 
       
    67 // atomic parsers
       
    68 case class TokParser(tok: Token) extends Parser[List[Token], Token] {
       
    69   def parse(ts: List[Token]) = ts match {
       
    70     case t::ts if (t == tok) => Set((t, ts)) 
       
    71     case _ => Set ()
       
    72   }
       
    73 }
       
    74 implicit def token2tparser(t: Token) = TokParser(t)
       
    75 
       
    76 case object NumParser extends Parser[List[Token], Int] {
       
    77   def parse(ts: List[Token]) = ts match {
       
    78     case T_NUM(s)::ts => Set((s.toInt, ts)) 
       
    79     case _ => Set ()
       
    80   }
       
    81 }
       
    82 
       
    83 case object IdParser extends Parser[List[Token], String] {
       
    84   def parse(ts: List[Token]) = ts match {
       
    85     case T_ID(s)::ts => Set((s, ts)) 
       
    86     case _ => Set ()
       
    87   }
       
    88 }
       
    89 
       
    90 def len(xs: List[(Int, Int)]): Int = xs match {
       
    91   case Nil => 0
       
    92   case (1, x)::xs => len(xs) + 1
       
    93   case (_, x)::xs => len(xs) 
       
    94 }
       
    95 
       
    96 def fst(p: (Int, Int)): Int = p match {
       
    97   case Nil => 0
       
    98   case (1, x)::xs => len(xs) + 1
       
    99   case (_, x)::xs => len(xs) 
       
   100 }
       
   101 
       
   102 
       
   103 
       
   104 // arithmetic expressions
       
   105 lazy val AExp: Parser[List[Token], AExp] = 
       
   106   (T ~ T_OP("+") ~ AExp) ==> { case ((x, y), z) => Aop("+", x, z): AExp } ||
       
   107   (T ~ T_OP("-") ~ AExp) ==> { case ((x, y), z) => Aop("-", x, z): AExp } || T  
       
   108 lazy val T: Parser[List[Token], AExp] = 
       
   109   (F ~ T_OP("*") ~ T) ==> { case ((x, y), z) => Aop("*", x, z): AExp } || F
       
   110 lazy val F: Parser[List[Token], AExp] = 
       
   111   (T_LPAREN ~> AExp <~ T_RPAREN) || 
       
   112   IdParser ==> Var || 
       
   113   NumParser ==> Num
       
   114 
       
   115 // boolean expressions
       
   116 lazy val BExp: Parser[List[Token], BExp] = 
       
   117   (AExp ~ T_OP("=") ~ AExp) ==> { case ((x, y), z) => Bop("=", x, z): BExp } || 
       
   118   (AExp ~ T_OP("!=") ~ AExp) ==> { case ((x, y), z) => Bop("!=", x, z): BExp } || 
       
   119   (AExp ~ T_OP("<") ~ AExp) ==> { case ((x, y), z) => Bop("<", x, z): BExp } || 
       
   120   (AExp ~ T_OP(">") ~ AExp) ==> { case ((x, y), z) => Bop(">", x, z): BExp } || 
       
   121   (T_KWD("true") ==> ((_) => True)) || 
       
   122   (T_KWD("false") ==> ((_) => False: BExp))
       
   123 
       
   124 lazy val Stmt: Parser[List[Token], Stmt] =
       
   125   (T_KWD("skip") ==> ((_) => Skip: Stmt)) ||
       
   126   (IdParser ~ T_OP(":=") ~ AExp) ==> { case ((x, y), z) => Assign(x, z): Stmt } ||
       
   127   (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Block ~ T_KWD("else") ~ Block) ==>
       
   128     { case (((((x,y),z),u),v),w) => If(y, u, w): Stmt } ||
       
   129   (T_KWD("while") ~ BExp ~ T_KWD("do") ~ Block) ==> { case (((x, y), z), w) => While(y, w) } 
       
   130  
       
   131 lazy val Stmts: Parser[List[Token], Block] =
       
   132   (Stmt ~ T_SEMI ~ Stmts) ==> { case ((x, y), z) => x :: z : Block } ||
       
   133   (Stmt ==> ((s) => List(s) : Block))
       
   134 
       
   135 lazy val Block: Parser[List[Token], Block] =
       
   136   (T_BEGIN ~> Stmts <~ T_END) || 
       
   137   (Stmt ==> ((s) => List(s)))
       
   138 
       
   139 
       
   140 // examples
       
   141 val p1 = "x := 5"
       
   142 val p1_toks = Tok.fromString(p1) 
       
   143 val p1_ast = Block.parse_all(p1_toks)
       
   144 println(p1_toks)
       
   145 println(p1_ast)
       
   146 
       
   147 val p1a = "{ x := 5; y := 8}"
       
   148 val p1a_toks = Tok.fromString(p1a) 
       
   149 val p1a_ast = Block.parse_all(p1a_toks)
       
   150 println(p1a_ast)
       
   151 
       
   152 val p2 = "5 = 6"
       
   153 val p2_toks = Tok.fromString(p2) 
       
   154 val p2_ast = BExp.parse_all(p2_toks)
       
   155 println(p2_ast)
       
   156 
       
   157 val p2a = "true"
       
   158 val p2a_toks = Tok.fromString(p2a) 
       
   159 val p2a_ast = BExp.parse_all(p2a_toks)
       
   160 println(p2a_ast)
       
   161 
       
   162 val p3 = "if true then skip else skip"
       
   163 val p3_toks = Tok.fromString(p3) 
       
   164 val p3_ast = Stmt.parse_all(p3_toks)
       
   165 println(p3_ast)
       
   166 
       
   167 val p3a = "if true then x := 5 else x := 10"
       
   168 val p3a_toks = Tok.fromString(p3a) 
       
   169 val p3a_ast = Stmt.parse_all(p3a_toks)
       
   170 println(p3a_ast)
       
   171 
       
   172 val p3b = "if false then x := 5 else x := 10"
       
   173 val p3b_toks = Tok.fromString(p3b) 
       
   174 val p3b_ast = Stmt.parse_all(p3b_toks)
       
   175 println(p3b_ast)
       
   176 
       
   177 // multiplication
       
   178 val p4 = """{ x := 5;
       
   179               y := 4;
       
   180               r := 0;
       
   181               while y > 0 do {
       
   182                  r := r + x;
       
   183                  y := y - 1
       
   184               } 
       
   185             }"""
       
   186 val p4_toks = Tok.fromString(p4) 
       
   187 val p4_ast = Block.parse_all(p4_toks)
       
   188 println(p4_ast)
       
   189 
       
   190 val p5 = """
       
   191  n := 9;
       
   192  minus1 := 0;
       
   193  minus2 := 1;
       
   194  temp := 0;
       
   195  while n > 0 do  {
       
   196    temp := minus2;
       
   197    minus2 := minus1 + minus2;
       
   198    minus1 := temp;
       
   199    n := n - 1
       
   200  };
       
   201  fib_res := minus2
       
   202 """
       
   203 val p5_toks = Tok.fromString(p5) 
       
   204 val p5_ast = Stmts.parse_all(p5_toks)
       
   205 
       
   206 // interpreter
       
   207 type Env = Map[String, Int]
       
   208 
       
   209 def eval_bexp(b: BExp, env: Env) : Boolean = b match {
       
   210   case True => true
       
   211   case False => false
       
   212   case Bop("=", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env)
       
   213   case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env))
       
   214   case Bop(">", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env)
       
   215   case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
       
   216 }
       
   217 
       
   218 def eval_aexp(a: AExp, env : Env) : Int = a match {
       
   219   case Num(i) => i
       
   220   case Var(s) => env(s)
       
   221   case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env)
       
   222   case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env)
       
   223   case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env)
       
   224 }
       
   225 
       
   226 def eval_stmt(s: Stmt, env: Env) : Env = s match {
       
   227   case Skip => env
       
   228   case Assign(x, a) => env + (x -> eval_aexp(a, env))
       
   229   case If(b, bl1, bl2) => if (eval_bexp(b, env)) eval_bl(bl1, env) else eval_bl(bl2, env) 
       
   230   case While(b, bl) => 
       
   231     if (eval_bexp(b, env)) eval_stmt(While(b, bl), eval_bl(bl, env))
       
   232     else env
       
   233 }
       
   234 
       
   235 def eval_bl(bl: Block, env: Env) : Env = bl match {
       
   236   case Nil => env
       
   237   case s::bl => eval_bl(bl, eval_stmt(s, env))
       
   238 }
       
   239 
       
   240 //examples
       
   241 println(eval_stmt(p3a_ast.head, Map.empty))
       
   242 println(eval_stmt(p3b_ast.head, Map.empty))
       
   243 println(eval_bl(p4_ast.head, Map.empty))
       
   244 println(eval_bl(p5_ast.head, Map.empty))