Attic/while1.scala
changeset 742 b5b5583a3a08
parent 471 9476086849ad
equal deleted inserted replaced
741:e66bd5c563eb 742:b5b5583a3a08
       
     1 // A parser and evaluator for the WHILE language
       
     2 // 
       
     3 
       
     4 
       
     5 
       
     6 
       
     7 
       
     8 // some regular expressions
       
     9 val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_")
       
    10 val DIGIT = RANGE("0123456789")
       
    11 val ID = SEQ(SYM, STAR(ALT(SYM, DIGIT))) 
       
    12 val NUM = PLUS(DIGIT)
       
    13 val KEYWORD = ALTS("skip", "while", "do", "if", "then", "else", "true", "false", "write") 
       
    14 val SEMI: Rexp = ";"
       
    15 val OP: Rexp = ALTS(":=", "=", "-", "+", "*", "!=", "<", ">")
       
    16 val WHITESPACE = PLUS(RANGE(" \n"))
       
    17 val RPAREN: Rexp = ")"
       
    18 val LPAREN: Rexp = "("
       
    19 val BEGIN: Rexp = "{"
       
    20 val END: Rexp = "}"
       
    21 val COMMENT = SEQS("/*", NOT(SEQS(STAR(ALLC), "*/", STAR(ALLC))), "*/")
       
    22 
       
    23 // tokens for classifying the strings that have been recognised
       
    24 abstract class Token
       
    25 case object T_WHITESPACE extends Token
       
    26 case object T_COMMENT extends Token
       
    27 case object T_SEMI extends Token
       
    28 case object T_LPAREN extends Token
       
    29 case object T_RPAREN extends Token
       
    30 case object T_BEGIN extends Token
       
    31 case object T_END extends Token
       
    32 case class T_ID(s: String) extends Token
       
    33 case class T_OP(s: String) extends Token
       
    34 case class T_NUM(s: String) extends Token
       
    35 case class T_KWD(s: String) extends Token
       
    36 
       
    37 val lexing_rules: List[(Rexp, List[Char] => Token)] = 
       
    38   List((KEYWORD, (s) => T_KWD(s.mkString)),
       
    39        (ID, (s) => T_ID(s.mkString)),
       
    40        (OP, (s) => T_OP(s.mkString)),
       
    41        (NUM, (s) => T_NUM(s.mkString)),
       
    42        (SEMI, (s) => T_SEMI),
       
    43        (LPAREN, (s) => T_LPAREN),
       
    44        (RPAREN, (s) => T_RPAREN),
       
    45        (BEGIN, (s) => T_BEGIN),
       
    46        (END, (s) => T_END),
       
    47        (WHITESPACE, (s) => T_WHITESPACE),
       
    48        (COMMENT, (s) => T_COMMENT))
       
    49 
       
    50 // the tokenizer
       
    51 val Tok = Tokenizer(lexing_rules, List(T_WHITESPACE, T_COMMENT))
       
    52 
       
    53 // the abstract syntax trees
       
    54 abstract class Stmt
       
    55 abstract class AExp
       
    56 abstract class BExp 
       
    57 type Block = List[Stmt]
       
    58 case object Skip extends Stmt
       
    59 case class If(a: BExp, bl1: Block, bl2: Block) extends Stmt
       
    60 case class While(b: BExp, bl: Block) extends Stmt
       
    61 case class Assign(s: String, a: AExp) extends Stmt
       
    62 case class Write(s: String) extends Stmt
       
    63 
       
    64 case class Var(s: String) extends AExp
       
    65 case class Num(i: Int) extends AExp
       
    66 case class Aop(o: String, a1: AExp, a2: AExp) extends AExp
       
    67 
       
    68 case object True extends BExp
       
    69 case object False extends BExp
       
    70 case class Bop(o: String, a1: AExp, a2: AExp) extends BExp
       
    71 
       
    72 // atomic parsers
       
    73 case class TokParser(tok: Token) extends Parser[List[Token], Token] {
       
    74   def parse(ts: List[Token]) = ts match {
       
    75     case t::ts if (t == tok) => Set((t, ts)) 
       
    76     case _ => Set ()
       
    77   }
       
    78 }
       
    79 implicit def token2tparser(t: Token) = TokParser(t)
       
    80 
       
    81 case object NumParser extends Parser[List[Token], Int] {
       
    82   def parse(ts: List[Token]) = ts match {
       
    83     case T_NUM(s)::ts => Set((s.toInt, ts)) 
       
    84     case _ => Set ()
       
    85   }
       
    86 }
       
    87 
       
    88 case object IdParser extends Parser[List[Token], String] {
       
    89   def parse(ts: List[Token]) = ts match {
       
    90     case T_ID(s)::ts => Set((s, ts)) 
       
    91     case _ => Set ()
       
    92   }
       
    93 }
       
    94 
       
    95 
       
    96 // arithmetic expressions
       
    97 lazy val AExp: Parser[List[Token], AExp] = 
       
    98   (T ~ T_OP("+") ~ AExp) ==> { case ((x, y), z) => Aop("+", x, z): AExp } ||
       
    99   (T ~ T_OP("-") ~ AExp) ==> { case ((x, y), z) => Aop("-", x, z): AExp } || T  
       
   100 lazy val T: Parser[List[Token], AExp] = 
       
   101   (F ~ T_OP("*") ~ T) ==> { case ((x, y), z) => Aop("*", x, z): AExp } || F
       
   102 lazy val F: Parser[List[Token], AExp] = 
       
   103   (T_LPAREN ~> AExp <~ T_RPAREN) || 
       
   104   IdParser ==> Var || 
       
   105   NumParser ==> Num
       
   106 
       
   107 // boolean expressions
       
   108 lazy val BExp: Parser[List[Token], BExp] = 
       
   109   (T_KWD("true") ==> ((_) => True: BExp)) || 
       
   110   (T_KWD("false") ==> ((_) => False: BExp)) ||
       
   111   (T_LPAREN ~> BExp <~ T_RPAREN) ||
       
   112   (AExp ~ T_OP("=") ~ AExp) ==> { case ((x, y), z) => Bop("=", x, z): BExp } || 
       
   113   (AExp ~ T_OP("!=") ~ AExp) ==> { case ((x, y), z) => Bop("!=", x, z): BExp } || 
       
   114   (AExp ~ T_OP("<") ~ AExp) ==> { case ((x, y), z) => Bop("<", x, z): BExp } || 
       
   115   (AExp ~ T_OP(">") ~ AExp) ==> { case ((x, y), z) => Bop("<", z, x): BExp }
       
   116 
       
   117 lazy val Stmt: Parser[List[Token], Stmt] =
       
   118   (T_KWD("skip") ==> ((_) => Skip: Stmt)) ||
       
   119   (IdParser ~ T_OP(":=") ~ AExp) ==> { case ((x, y), z) => Assign(x, z): Stmt } ||
       
   120   (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Block ~ T_KWD("else") ~ Block) ==>
       
   121     { case (((((x,y),z),u),v),w) => If(y, u, w): Stmt } ||
       
   122   (T_KWD("while") ~ BExp ~ T_KWD("do") ~ Block) ==> { case (((x, y), z), w) => While(y, w) } || 
       
   123   (T_KWD("write") ~ IdParser) ==> { case (x, y) => Write(y) } 
       
   124 
       
   125 lazy val Stmts: Parser[List[Token], Block] =
       
   126   (Stmt ~ T_SEMI ~ Stmts) ==> { case ((x, y), z) => x :: z : Block } ||
       
   127   (Stmt ==> ((s) => List(s) : Block))
       
   128 
       
   129 lazy val Block: Parser[List[Token], Block] =
       
   130   (T_BEGIN ~> Stmts <~ T_END) || 
       
   131   (Stmt ==> ((s) => List(s)))
       
   132 
       
   133 // interpreter
       
   134 type Env = Map[String, Int]
       
   135 
       
   136 def eval_bexp(b: BExp, env: Env) : Boolean = b match {
       
   137   case True => true
       
   138   case False => false
       
   139   case Bop("=", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env)
       
   140   case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env))
       
   141   case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env)
       
   142 }
       
   143 
       
   144 def eval_aexp(a: AExp, env : Env) : Int = a match {
       
   145   case Num(i) => i
       
   146   case Var(s) => env(s)
       
   147   case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env)
       
   148   case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env)
       
   149   case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env)
       
   150 }
       
   151 
       
   152 def eval_stmt(s: Stmt, env: Env) : Env = s match {
       
   153   case Skip => env
       
   154   case Assign(x, a) => env + (x -> eval_aexp(a, env))
       
   155   case If(b, bl1, bl2) => if (eval_bexp(b, env)) eval_bl(bl1, env) else eval_bl(bl2, env) 
       
   156   case While(b, bl) => 
       
   157     if (eval_bexp(b, env)) eval_stmt(While(b, bl), eval_bl(bl, env))
       
   158     else env
       
   159   case Write(x) => { println(env(x)); env }
       
   160 }
       
   161 
       
   162 def eval_bl(bl: Block, env: Env) : Env = bl match {
       
   163   case Nil => env
       
   164   case s::bl => eval_bl(bl, eval_stmt(s, env))
       
   165 }
       
   166 
       
   167 def eval_prog(name: String) : Env = {
       
   168   val tks = Tok.fromFile(name)
       
   169   val ast = Stmts.parse_single(tks)
       
   170   eval_bl(ast, Map.empty)
       
   171 }
       
   172 
       
   173 
       
   174 //examples
       
   175 
       
   176 //eval_prog("loops.while")
       
   177 eval_prog("fib.while")
       
   178 
       
   179 
       
   180 def time_needed[T](i: Int, code: => T) = {
       
   181   val start = System.nanoTime()
       
   182   for (j <- 1 to i) code
       
   183   val end = System.nanoTime()
       
   184   (end - start)/(i * 1.0e9)
       
   185 }
       
   186 
       
   187 
       
   188 val test_prog = """
       
   189 start := XXX;
       
   190 x := start;
       
   191 y := start;
       
   192 z := start;
       
   193 while 0 < x do {
       
   194  while 0 < y do {
       
   195   while 0 < z do {
       
   196     z := z - 1
       
   197   };
       
   198   z := start;
       
   199   y := y - 1
       
   200  };     
       
   201  y := start;
       
   202  x := x - 1
       
   203 }
       
   204 """
       
   205 
       
   206 
       
   207 
       
   208 def eval_test(n: Int) : Unit = {
       
   209   val tks = Tok.fromString(test_prog.replaceAllLiterally("XXX", n.toString))
       
   210   val ast = Stmts.parse_single(tks)
       
   211   println(n + " " + time_needed(2, eval_bl(ast, Map.empty)))
       
   212 }
       
   213 
       
   214 List(1, 200, 400, 600, 800, 1000, 1200, 1400, 1600).map(eval_test(_))
       
   215 
       
   216 
       
   217 
       
   218 
       
   219 
       
   220 
       
   221