1 // A parser and evaluator for teh 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 |
|
91 // arithmetic expressions |
|
92 lazy val AExp: Parser[List[Token], AExp] = |
|
93 (T ~ T_OP("+") ~ AExp) ==> { case ((x, y), z) => Aop("+", x, z): AExp } || |
|
94 (T ~ T_OP("-") ~ AExp) ==> { case ((x, y), z) => Aop("-", x, z): AExp } || T |
|
95 lazy val T: Parser[List[Token], AExp] = |
|
96 (F ~ T_OP("*") ~ T) ==> { case ((x, y), z) => Aop("*", x, z): AExp } || F |
|
97 lazy val F: Parser[List[Token], AExp] = |
|
98 (T_LPAREN ~> AExp <~ T_RPAREN) || |
|
99 IdParser ==> Var || |
|
100 NumParser ==> Num |
|
101 |
|
102 // boolean expressions |
|
103 lazy val BExp: Parser[List[Token], BExp] = |
|
104 (AExp ~ T_OP("=") ~ AExp) ==> { case ((x, y), z) => Bop("=", x, z): BExp } || |
|
105 (AExp ~ T_OP("!=") ~ AExp) ==> { case ((x, y), z) => Bop("!=", x, z): BExp } || |
|
106 (AExp ~ T_OP("<") ~ AExp) ==> { case ((x, y), z) => Bop("<", x, z): BExp } || |
|
107 (AExp ~ T_OP(">") ~ AExp) ==> { case ((x, y), z) => Bop(">", x, z): BExp } || |
|
108 (T_KWD("true") ==> ((_) => True)) || |
|
109 (T_KWD("false") ==> ((_) => False: BExp)) |
|
110 |
|
111 lazy val Stmt: Parser[List[Token], Stmt] = |
|
112 (T_KWD("skip") ==> ((_) => Skip: Stmt)) || |
|
113 (IdParser ~ T_OP(":=") ~ AExp) ==> { case ((x, y), z) => Assign(x, z): Stmt } || |
|
114 (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Block ~ T_KWD("else") ~ Block) ==> |
|
115 { case (((((x,y),z),u),v),w) => If(y, u, w): Stmt } || |
|
116 (T_KWD("while") ~ BExp ~ T_KWD("do") ~ Block) ==> { case (((x, y), z), w) => While(y, w) } |
|
117 |
|
118 lazy val Stmts: Parser[List[Token], Block] = |
|
119 (Stmt ~ T_SEMI ~ Stmts) ==> { case ((x, y), z) => x :: z : Block } || |
|
120 (Stmt ==> ((s) => List(s) : Block)) |
|
121 |
|
122 lazy val Block: Parser[List[Token], Block] = |
|
123 (T_BEGIN ~> Stmts <~ T_END) || |
|
124 (Stmt ==> ((s) => List(s))) |
|
125 |
|
126 |
|
127 // examples |
|
128 val p1 = "x := 5" |
|
129 val p1_toks = Tok.fromString(p1) |
|
130 val p1_ast = Block.parse_all(p1_toks) |
|
131 println(p1_toks) |
|
132 println(p1_ast) |
|
133 |
|
134 val p1a = "{ x := 5; y := 8}" |
|
135 val p1a_toks = Tok.fromString(p1a) |
|
136 val p1a_ast = Block.parse_all(p1a_toks) |
|
137 println(p1a_ast) |
|
138 |
|
139 val p2 = "5 = 6" |
|
140 val p2_toks = Tok.fromString(p2) |
|
141 val p2_ast = BExp.parse_all(p2_toks) |
|
142 println(p2_ast) |
|
143 |
|
144 val p2a = "true" |
|
145 val p2a_toks = Tok.fromString(p2a) |
|
146 val p2a_ast = BExp.parse_all(p2a_toks) |
|
147 println(p2a_ast) |
|
148 |
|
149 val p3 = "if true then skip else skip" |
|
150 val p3_toks = Tok.fromString(p3) |
|
151 val p3_ast = Stmt.parse_all(p3_toks) |
|
152 println(p3_ast) |
|
153 |
|
154 val p3a = "if true then x := 5 else x := 10" |
|
155 val p3a_toks = Tok.fromString(p3a) |
|
156 val p3a_ast = Stmt.parse_all(p3a_toks) |
|
157 println(p3a_ast) |
|
158 |
|
159 val p3b = "if false then x := 5 else x := 10" |
|
160 val p3b_toks = Tok.fromString(p3b) |
|
161 val p3b_ast = Stmt.parse_all(p3b_toks) |
|
162 println(p3b_ast) |
|
163 |
|
164 // multiplication |
|
165 val p4 = """{ x := 5; |
|
166 y := 4; |
|
167 r := 0; |
|
168 while y > 0 do { |
|
169 r := r + x; |
|
170 y := y - 1 |
|
171 } |
|
172 }""" |
|
173 val p4_toks = Tok.fromString(p4) |
|
174 val p4_ast = Block.parse_all(p4_toks) |
|
175 println(p4_ast) |
|
176 |
|
177 val p5 = """ |
|
178 n := 9; |
|
179 minus1 := 0; |
|
180 minus2 := 1; |
|
181 temp := 0; |
|
182 while n > 0 do { |
|
183 temp := minus2; |
|
184 minus2 := minus1 + minus2; |
|
185 minus1 := temp; |
|
186 n := n - 1 |
|
187 }; |
|
188 fib_res := minus2 |
|
189 """ |
|
190 val p5_toks = Tok.fromString(p5) |
|
191 val p5_ast = Stmts.parse_all(p5_toks) |
|
192 |
|
193 // interpreter |
|
194 type Env = Map[String, Int] |
|
195 |
|
196 def eval_bexp(b: BExp, env: Env) : Boolean = b match { |
|
197 case True => true |
|
198 case False => false |
|
199 case Bop("=", a1, a2) => eval_aexp(a1, env) == eval_aexp(a2, env) |
|
200 case Bop("!=", a1, a2) => !(eval_aexp(a1, env) == eval_aexp(a2, env)) |
|
201 case Bop(">", a1, a2) => eval_aexp(a1, env) > eval_aexp(a2, env) |
|
202 case Bop("<", a1, a2) => eval_aexp(a1, env) < eval_aexp(a2, env) |
|
203 } |
|
204 |
|
205 def eval_aexp(a: AExp, env : Env) : Int = a match { |
|
206 case Num(i) => i |
|
207 case Var(s) => env(s) |
|
208 case Aop("+", a1, a2) => eval_aexp(a1, env) + eval_aexp(a2, env) |
|
209 case Aop("-", a1, a2) => eval_aexp(a1, env) - eval_aexp(a2, env) |
|
210 case Aop("*", a1, a2) => eval_aexp(a1, env) * eval_aexp(a2, env) |
|
211 } |
|
212 |
|
213 def eval_stmt(s: Stmt, env: Env) : Env = s match { |
|
214 case Skip => env |
|
215 case Assign(x, a) => env + (x -> eval_aexp(a, env)) |
|
216 case If(b, bl1, bl2) => if (eval_bexp(b, env)) eval_bl(bl1, env) else eval_bl(bl2, env) |
|
217 case While(b, bl) => |
|
218 if (eval_bexp(b, env)) eval_stmt(While(b, bl), eval_bl(bl, env)) |
|
219 else env |
|
220 } |
|
221 |
|
222 def eval_bl(bl: Block, env: Env) : Env = bl match { |
|
223 case Nil => env |
|
224 case s::bl => eval_bl(bl, eval_stmt(s, env)) |
|
225 } |
|
226 |
|
227 //examples |
|
228 println(eval_stmt(p3a_ast.head, Map.empty)) |
|
229 println(eval_stmt(p3b_ast.head, Map.empty)) |
|
230 println(eval_bl(p4_ast.head, Map.empty)) |
|
231 println(eval_bl(p5_ast.head, Map.empty)) |
|