1 // A Small Compiler for a Simple Functional Language |
1 // A Small Compiler for a Simple Functional Language |
2 // (includes a lexer and a parser) |
2 // (includes an external lexer and parser) |
3 |
3 |
4 import scala.language.implicitConversions |
4 import java.io._ |
5 import scala.language.reflectiveCalls |
5 |
6 |
6 object Compiler { |
7 abstract class Rexp |
7 |
8 case object ZERO extends Rexp |
8 // Abstract syntax trees for the Fun language |
9 case object ONE extends Rexp |
9 abstract class Exp extends Serializable |
10 case class CHAR(c: Char) extends Rexp |
10 abstract class BExp extends Serializable |
11 case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
11 abstract class Decl extends Serializable |
12 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
|
13 case class STAR(r: Rexp) extends Rexp |
|
14 case class RECD(x: String, r: Rexp) extends Rexp |
|
15 |
|
16 abstract class Val |
|
17 case object Empty extends Val |
|
18 case class Chr(c: Char) extends Val |
|
19 case class Sequ(v1: Val, v2: Val) extends Val |
|
20 case class Left(v: Val) extends Val |
|
21 case class Right(v: Val) extends Val |
|
22 case class Stars(vs: List[Val]) extends Val |
|
23 case class Rec(x: String, v: Val) extends Val |
|
24 |
|
25 // some convenience for typing in regular expressions |
|
26 def charlist2rexp(s : List[Char]): Rexp = s match { |
|
27 case Nil => ONE |
|
28 case c::Nil => CHAR(c) |
|
29 case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
|
30 } |
|
31 implicit def string2rexp(s : String) : Rexp = |
|
32 charlist2rexp(s.toList) |
|
33 |
|
34 implicit def RexpOps(r: Rexp) = new { |
|
35 def | (s: Rexp) = ALT(r, s) |
|
36 def % = STAR(r) |
|
37 def ~ (s: Rexp) = SEQ(r, s) |
|
38 } |
|
39 |
|
40 implicit def stringOps(s: String) = new { |
|
41 def | (r: Rexp) = ALT(s, r) |
|
42 def | (r: String) = ALT(s, r) |
|
43 def % = STAR(s) |
|
44 def ~ (r: Rexp) = SEQ(s, r) |
|
45 def ~ (r: String) = SEQ(s, r) |
|
46 def $ (r: Rexp) = RECD(s, r) |
|
47 } |
|
48 |
|
49 def nullable (r: Rexp) : Boolean = r match { |
|
50 case ZERO => false |
|
51 case ONE => true |
|
52 case CHAR(_) => false |
|
53 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
54 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
55 case STAR(_) => true |
|
56 case RECD(_, r1) => nullable(r1) |
|
57 } |
|
58 |
|
59 def der (c: Char, r: Rexp) : Rexp = r match { |
|
60 case ZERO => ZERO |
|
61 case ONE => ZERO |
|
62 case CHAR(d) => if (c == d) ONE else ZERO |
|
63 case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
64 case SEQ(r1, r2) => |
|
65 if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
66 else SEQ(der(c, r1), r2) |
|
67 case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
68 case RECD(_, r1) => der(c, r1) |
|
69 } |
|
70 |
|
71 |
|
72 // extracts a string from value |
|
73 def flatten(v: Val) : String = v match { |
|
74 case Empty => "" |
|
75 case Chr(c) => c.toString |
|
76 case Left(v) => flatten(v) |
|
77 case Right(v) => flatten(v) |
|
78 case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
|
79 case Stars(vs) => vs.map(flatten).mkString |
|
80 case Rec(_, v) => flatten(v) |
|
81 } |
|
82 |
|
83 // extracts an environment from a value; |
|
84 // used for tokenise a string |
|
85 def env(v: Val) : List[(String, String)] = v match { |
|
86 case Empty => Nil |
|
87 case Chr(c) => Nil |
|
88 case Left(v) => env(v) |
|
89 case Right(v) => env(v) |
|
90 case Sequ(v1, v2) => env(v1) ::: env(v2) |
|
91 case Stars(vs) => vs.flatMap(env) |
|
92 case Rec(x, v) => (x, flatten(v))::env(v) |
|
93 } |
|
94 |
|
95 // The Injection Part of the lexer |
|
96 |
|
97 def mkeps(r: Rexp) : Val = r match { |
|
98 case ONE => Empty |
|
99 case ALT(r1, r2) => |
|
100 if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
|
101 case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
|
102 case STAR(r) => Stars(Nil) |
|
103 case RECD(x, r) => Rec(x, mkeps(r)) |
|
104 } |
|
105 |
|
106 def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
|
107 case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
|
108 case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
|
109 case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
|
110 case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
|
111 case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1)) |
|
112 case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
|
113 case (CHAR(d), Empty) => Chr(c) |
|
114 case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
|
115 case _ => { println ("Injection error") ; sys.exit(-1) } |
|
116 } |
|
117 |
|
118 // some "rectification" functions for simplification |
|
119 def F_ID(v: Val): Val = v |
|
120 def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v)) |
|
121 def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v)) |
|
122 def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
|
123 case Right(v) => Right(f2(v)) |
|
124 case Left(v) => Left(f1(v)) |
|
125 } |
|
126 def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
|
127 case Sequ(v1, v2) => Sequ(f1(v1), f2(v2)) |
|
128 } |
|
129 def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = |
|
130 (v:Val) => Sequ(f1(Empty), f2(v)) |
|
131 def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = |
|
132 (v:Val) => Sequ(f1(v), f2(Empty)) |
|
133 def F_RECD(f: Val => Val) = (v:Val) => v match { |
|
134 case Rec(x, v) => Rec(x, f(v)) |
|
135 } |
|
136 def F_ERROR(v: Val): Val = throw new Exception("error") |
|
137 |
|
138 def simp(r: Rexp): (Rexp, Val => Val) = r match { |
|
139 case ALT(r1, r2) => { |
|
140 val (r1s, f1s) = simp(r1) |
|
141 val (r2s, f2s) = simp(r2) |
|
142 (r1s, r2s) match { |
|
143 case (ZERO, _) => (r2s, F_RIGHT(f2s)) |
|
144 case (_, ZERO) => (r1s, F_LEFT(f1s)) |
|
145 case _ => if (r1s == r2s) (r1s, F_LEFT(f1s)) |
|
146 else (ALT (r1s, r2s), F_ALT(f1s, f2s)) |
|
147 } |
|
148 } |
|
149 case SEQ(r1, r2) => { |
|
150 val (r1s, f1s) = simp(r1) |
|
151 val (r2s, f2s) = simp(r2) |
|
152 (r1s, r2s) match { |
|
153 case (ZERO, _) => (ZERO, F_ERROR) |
|
154 case (_, ZERO) => (ZERO, F_ERROR) |
|
155 case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s)) |
|
156 case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s)) |
|
157 case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s)) |
|
158 } |
|
159 } |
|
160 case RECD(x, r1) => { |
|
161 val (r1s, f1s) = simp(r1) |
|
162 (RECD(x, r1s), F_RECD(f1s)) |
|
163 } |
|
164 case r => (r, F_ID) |
|
165 } |
|
166 |
|
167 // lexing functions including simplification |
|
168 def lex_simp(r: Rexp, s: List[Char]) : Val = s match { |
|
169 case Nil => if (nullable(r)) mkeps(r) else { println ("Lexing Error") ; sys.exit(-1) } |
|
170 case c::cs => { |
|
171 val (r_simp, f_simp) = simp(der(c, r)) |
|
172 inj(r, c, f_simp(lex_simp(r_simp, cs))) |
|
173 } |
|
174 } |
|
175 |
|
176 def lexing_simp(r: Rexp, s: String) = env(lex_simp(r, s.toList)) |
|
177 |
|
178 |
|
179 // The Lexing Rules for the Fun Language |
|
180 |
|
181 def PLUS(r: Rexp) = r ~ r.% |
|
182 |
|
183 val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | |
|
184 "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | |
|
185 "w" | "x" | "y" | "z" | "T" | "_" |
|
186 val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
|
187 val ID = SYM ~ (SYM | DIGIT).% |
|
188 val NUM = PLUS(DIGIT) |
|
189 val KEYWORD : Rexp = "if" | "then" | "else" | "write" | "def" |
|
190 val SEMI: Rexp = ";" |
|
191 val OP: Rexp = "=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/" |
|
192 val WHITESPACE = PLUS(" " | "\n" | "\t") |
|
193 val RPAREN: Rexp = ")" |
|
194 val LPAREN: Rexp = "(" |
|
195 val COMMA: Rexp = "," |
|
196 val ALL = SYM | DIGIT | OP | " " | ":" | ";" | "\"" | "=" | "," | "(" | ")" |
|
197 val ALL2 = ALL | "\n" |
|
198 val COMMENT = ("/*" ~ ALL2.% ~ "*/") | ("//" ~ ALL.% ~ "\n") |
|
199 |
|
200 |
|
201 val WHILE_REGS = (("k" $ KEYWORD) | |
|
202 ("i" $ ID) | |
|
203 ("o" $ OP) | |
|
204 ("n" $ NUM) | |
|
205 ("s" $ SEMI) | |
|
206 ("c" $ COMMA) | |
|
207 ("pl" $ LPAREN) | |
|
208 ("pr" $ RPAREN) | |
|
209 ("w" $ (WHITESPACE | COMMENT))).% |
|
210 |
|
211 |
|
212 |
|
213 // The tokens for the Fun language |
|
214 |
|
215 abstract class Token |
|
216 case object T_SEMI extends Token |
|
217 case object T_COMMA extends Token |
|
218 case object T_LPAREN extends Token |
|
219 case object T_RPAREN extends Token |
|
220 case class T_ID(s: String) extends Token |
|
221 case class T_OP(s: String) extends Token |
|
222 case class T_NUM(n: Int) extends Token |
|
223 case class T_KWD(s: String) extends Token |
|
224 |
|
225 val token : PartialFunction[(String, String), Token] = { |
|
226 case ("k", s) => T_KWD(s) |
|
227 case ("i", s) => T_ID(s) |
|
228 case ("o", s) => T_OP(s) |
|
229 case ("n", s) => T_NUM(s.toInt) |
|
230 case ("s", _) => T_SEMI |
|
231 case ("c", _) => T_COMMA |
|
232 case ("pl", _) => T_LPAREN |
|
233 case ("pr", _) => T_RPAREN |
|
234 } |
|
235 |
|
236 |
|
237 def tokenise(s: String) : List[Token] = |
|
238 lexing_simp(WHILE_REGS, s).collect(token) |
|
239 |
|
240 |
|
241 |
|
242 // Parser combinators |
|
243 abstract class Parser[I, T](implicit ev: I => Seq[_]) { |
|
244 def parse(ts: I): Set[(T, I)] |
|
245 |
|
246 def parse_all(ts: I) : Set[T] = |
|
247 for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head |
|
248 |
|
249 def parse_single(ts: I) : T = parse_all(ts).toList match { |
|
250 case List(t) => t |
|
251 case _ => { println ("Parse Error\n") ; sys.exit(-1) } |
|
252 } |
|
253 } |
|
254 |
|
255 case class ~[+A, +B](_1: A, _2: B) |
|
256 |
|
257 class SeqParser[I, T, S](p: => Parser[I, T], |
|
258 q: => Parser[I, S])(implicit ev: I => Seq[_]) extends Parser[I, ~[T, S]] { |
|
259 def parse(sb: I) = |
|
260 for ((head1, tail1) <- p.parse(sb); |
|
261 (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2) |
|
262 } |
|
263 |
|
264 class AltParser[I, T](p: => Parser[I, T], |
|
265 q: => Parser[I, T])(implicit ev: I => Seq[_]) extends Parser[I, T] { |
|
266 def parse(sb: I) = p.parse(sb) ++ q.parse(sb) |
|
267 } |
|
268 |
|
269 class FunParser[I, T, S](p: => Parser[I, T], |
|
270 f: T => S)(implicit ev: I => Seq[_]) extends Parser[I, S] { |
|
271 def parse(sb: I) = |
|
272 for ((head, tail) <- p.parse(sb)) yield (f(head), tail) |
|
273 } |
|
274 |
|
275 implicit def ParserOps[I, T](p: Parser[I, T])(implicit ev: I => Seq[_]) = new { |
|
276 def || (q : => Parser[I, T]) = new AltParser[I, T](p, q) |
|
277 def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f) |
|
278 def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q) |
|
279 } |
|
280 |
|
281 def ListParser[I, T, S](p: => Parser[I, T], |
|
282 q: => Parser[I, S])(implicit ev: I => Seq[_]): Parser[I, List[T]] = { |
|
283 (p ~ q ~ ListParser(p, q)) ==> { case x ~ _ ~ z => x :: z : List[T] } || |
|
284 (p ==> ((s) => List(s))) |
|
285 } |
|
286 |
|
287 case class TokParser(tok: Token) extends Parser[List[Token], Token] { |
|
288 def parse(ts: List[Token]) = ts match { |
|
289 case t::ts if (t == tok) => Set((t, ts)) |
|
290 case _ => Set () |
|
291 } |
|
292 } |
|
293 |
|
294 implicit def token2tparser(t: Token) = TokParser(t) |
|
295 |
|
296 implicit def TokOps(t: Token) = new { |
|
297 def || (q : => Parser[List[Token], Token]) = new AltParser[List[Token], Token](t, q) |
|
298 def ==>[S] (f: => Token => S) = new FunParser[List[Token], Token, S](t, f) |
|
299 def ~[S](q : => Parser[List[Token], S]) = new SeqParser[List[Token], Token, S](t, q) |
|
300 } |
|
301 |
|
302 case object NumParser extends Parser[List[Token], Int] { |
|
303 def parse(ts: List[Token]) = ts match { |
|
304 case T_NUM(n)::ts => Set((n, ts)) |
|
305 case _ => Set () |
|
306 } |
|
307 } |
|
308 |
|
309 case object IdParser extends Parser[List[Token], String] { |
|
310 def parse(ts: List[Token]) = ts match { |
|
311 case T_ID(s)::ts => Set((s, ts)) |
|
312 case _ => Set () |
|
313 } |
|
314 } |
|
315 |
|
316 |
|
317 |
|
318 // Abstract syntax trees for Fun |
|
319 abstract class Exp |
|
320 abstract class BExp |
|
321 abstract class Decl |
|
322 |
12 |
323 case class Def(name: String, args: List[String], body: Exp) extends Decl |
13 case class Def(name: String, args: List[String], body: Exp) extends Decl |
324 case class Main(e: Exp) extends Decl |
14 case class Main(e: Exp) extends Decl |
325 |
15 |
326 case class Call(name: String, args: List[Exp]) extends Exp |
16 case class Call(name: String, args: List[Exp]) extends Exp |
329 case class Var(s: String) extends Exp |
19 case class Var(s: String) extends Exp |
330 case class Num(i: Int) extends Exp |
20 case class Num(i: Int) extends Exp |
331 case class Aop(o: String, a1: Exp, a2: Exp) extends Exp |
21 case class Aop(o: String, a1: Exp, a2: Exp) extends Exp |
332 case class Sequence(e1: Exp, e2: Exp) extends Exp |
22 case class Sequence(e1: Exp, e2: Exp) extends Exp |
333 case class Bop(o: String, a1: Exp, a2: Exp) extends BExp |
23 case class Bop(o: String, a1: Exp, a2: Exp) extends BExp |
334 |
|
335 |
|
336 |
|
337 // Grammar Rules for Fun |
|
338 |
|
339 // arithmetic expressions |
|
340 lazy val Exp: Parser[List[Token], Exp] = |
|
341 (T_KWD("if") ~ BExp ~ T_KWD("then") ~ Exp ~ T_KWD("else") ~ Exp) ==> |
|
342 { case _ ~ x ~ _ ~ y ~ _ ~ z => If(x, y, z): Exp } || |
|
343 (M ~ T_SEMI ~ Exp) ==> { case x ~ _ ~ y => Sequence(x, y): Exp } || M |
|
344 lazy val M: Parser[List[Token], Exp] = |
|
345 (T_KWD("write") ~ L) ==> { case _ ~ y => Write(y): Exp } || L |
|
346 lazy val L: Parser[List[Token], Exp] = |
|
347 (T ~ T_OP("+") ~ Exp) ==> { case x ~ _ ~ z => Aop("+", x, z): Exp } || |
|
348 (T ~ T_OP("-") ~ Exp) ==> { case x ~ _ ~ z => Aop("-", x, z): Exp } || T |
|
349 lazy val T: Parser[List[Token], Exp] = |
|
350 (F ~ T_OP("*") ~ T) ==> { case x ~ _ ~ z => Aop("*", x, z): Exp } || |
|
351 (F ~ T_OP("/") ~ T) ==> { case x ~ _ ~ z => Aop("/", x, z): Exp } || |
|
352 (F ~ T_OP("%") ~ T) ==> { case x ~ _ ~ z => Aop("%", x, z): Exp } || F |
|
353 lazy val F: Parser[List[Token], Exp] = |
|
354 (IdParser ~ T_LPAREN ~ ListParser(Exp, T_COMMA) ~ T_RPAREN) ==> |
|
355 { case x ~ _ ~ z ~ _ => Call(x, z): Exp } || |
|
356 (T_LPAREN ~ Exp ~ T_RPAREN) ==> { case _ ~ y ~ _ => y: Exp } || |
|
357 IdParser ==> { case x => Var(x): Exp } || |
|
358 NumParser ==> { case x => Num(x): Exp } |
|
359 |
|
360 // boolean expressions |
|
361 lazy val BExp: Parser[List[Token], BExp] = |
|
362 (Exp ~ T_OP("==") ~ Exp) ==> { case x ~ _ ~ z => Bop("==", x, z): BExp } || |
|
363 (Exp ~ T_OP("!=") ~ Exp) ==> { case x ~ _ ~ z => Bop("!=", x, z): BExp } || |
|
364 (Exp ~ T_OP("<") ~ Exp) ==> { case x ~ _ ~ z => Bop("<", x, z): BExp } || |
|
365 (Exp ~ T_OP(">") ~ Exp) ==> { case x ~ _ ~ z => Bop("<", z, x): BExp } || |
|
366 (Exp ~ T_OP("<=") ~ Exp) ==> { case x ~ _ ~ z => Bop("<=", x, z): BExp } || |
|
367 (Exp ~ T_OP("=>") ~ Exp) ==> { case x ~ _ ~ z => Bop("<=", z, x): BExp } |
|
368 |
|
369 lazy val Defn: Parser[List[Token], Decl] = |
|
370 (T_KWD("def") ~ IdParser ~ T_LPAREN ~ ListParser(IdParser, T_COMMA) ~ T_RPAREN ~ T_OP("=") ~ Exp) ==> |
|
371 { case _ ~ y ~ _ ~ w ~ _ ~ _ ~ r => Def(y, w, r): Decl } |
|
372 |
|
373 lazy val Prog: Parser[List[Token], List[Decl]] = |
|
374 (Defn ~ T_SEMI ~ Prog) ==> { case x ~ _ ~ z => x :: z : List[Decl] } || |
|
375 (Exp ==> ((s) => List(Main(s)) : List[Decl])) |
|
376 |
24 |
377 |
25 |
378 // compiler - built-in functions |
26 // compiler - built-in functions |
379 // copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html |
27 // copied from http://www.ceng.metu.edu.tr/courses/ceng444/link/jvm-cpm.html |
380 // |
28 // |