|
1 // A simple lexer inspired by work of Sulzmann & Lu |
|
2 //================================================== |
|
3 // |
|
4 // Call the test cases with |
|
5 // |
|
6 // amm lexer.sc small |
|
7 // amm lexer.sc fib |
|
8 // amm lexer.sc loops |
|
9 // |
|
10 // amm lexer.sc all |
|
11 |
|
12 |
|
13 // regular expressions including records |
|
14 abstract class Rexp |
|
15 case object ZERO extends Rexp |
|
16 case object ONE extends Rexp |
|
17 case class CHAR(c: Char) extends Rexp |
|
18 case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
|
19 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
|
20 case class STAR(r: Rexp) extends Rexp |
|
21 case class RECD(x: String, r: Rexp) extends Rexp |
|
22 // records for extracting strings or tokens |
|
23 |
|
24 // values |
|
25 abstract class Val |
|
26 case object Empty extends Val |
|
27 case class Chr(c: Char) extends Val |
|
28 case class Sequ(v1: Val, v2: Val) extends Val |
|
29 case class Left(v: Val) extends Val |
|
30 case class Right(v: Val) extends Val |
|
31 case class Stars(vs: List[Val]) extends Val |
|
32 case class Rec(x: String, v: Val) extends Val |
|
33 |
|
34 // some convenience for typing in regular expressions |
|
35 import scala.language.implicitConversions |
|
36 import scala.language.reflectiveCalls |
|
37 |
|
38 def charlist2rexp(s : List[Char]): Rexp = s match { |
|
39 case Nil => ONE |
|
40 case c::Nil => CHAR(c) |
|
41 case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
|
42 } |
|
43 implicit def string2rexp(s : String) : Rexp = |
|
44 charlist2rexp(s.toList) |
|
45 |
|
46 implicit def RexpOps(r: Rexp) = new { |
|
47 def | (s: Rexp) = ALT(r, s) |
|
48 def % = STAR(r) |
|
49 def ~ (s: Rexp) = SEQ(r, s) |
|
50 } |
|
51 |
|
52 implicit def stringOps(s: String) = new { |
|
53 def | (r: Rexp) = ALT(s, r) |
|
54 def | (r: String) = ALT(s, r) |
|
55 def % = STAR(s) |
|
56 def ~ (r: Rexp) = SEQ(s, r) |
|
57 def ~ (r: String) = SEQ(s, r) |
|
58 def $ (r: Rexp) = RECD(s, r) |
|
59 } |
|
60 |
|
61 def nullable(r: Rexp) : Boolean = r match { |
|
62 case ZERO => false |
|
63 case ONE => true |
|
64 case CHAR(_) => false |
|
65 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
66 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
67 case STAR(_) => true |
|
68 case RECD(_, r1) => nullable(r1) |
|
69 } |
|
70 |
|
71 def der(c: Char, r: Rexp) : Rexp = r match { |
|
72 case ZERO => ZERO |
|
73 case ONE => ZERO |
|
74 case CHAR(d) => if (c == d) ONE else ZERO |
|
75 case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
76 case SEQ(r1, r2) => |
|
77 if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
78 else SEQ(der(c, r1), r2) |
|
79 case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
80 case RECD(_, r1) => der(c, r1) |
|
81 } |
|
82 |
|
83 |
|
84 // extracts a string from value |
|
85 def flatten(v: Val) : String = v match { |
|
86 case Empty => "" |
|
87 case Chr(c) => c.toString |
|
88 case Left(v) => flatten(v) |
|
89 case Right(v) => flatten(v) |
|
90 case Sequ(v1, v2) => flatten(v1) ++ flatten(v2) |
|
91 case Stars(vs) => vs.map(flatten).mkString |
|
92 case Rec(_, v) => flatten(v) |
|
93 } |
|
94 |
|
95 |
|
96 // extracts an environment from a value; |
|
97 // used for tokenising a string |
|
98 def env(v: Val) : List[(String, String)] = v match { |
|
99 case Empty => Nil |
|
100 case Chr(c) => Nil |
|
101 case Left(v) => env(v) |
|
102 case Right(v) => env(v) |
|
103 case Sequ(v1, v2) => env(v1) ::: env(v2) |
|
104 case Stars(vs) => vs.flatMap(env) |
|
105 case Rec(x, v) => (x, flatten(v))::env(v) |
|
106 } |
|
107 |
|
108 |
|
109 // The injection and mkeps part of the lexer |
|
110 //=========================================== |
|
111 |
|
112 def mkeps(r: Rexp) : Val = r match { |
|
113 case ONE => Empty |
|
114 case ALT(r1, r2) => |
|
115 if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
|
116 case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
|
117 case STAR(r) => Stars(Nil) |
|
118 case RECD(x, r) => Rec(x, mkeps(r)) |
|
119 } |
|
120 |
|
121 def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
|
122 case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
|
123 case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
|
124 case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
|
125 case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
|
126 case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1)) |
|
127 case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
|
128 case (CHAR(d), Empty) => Chr(c) |
|
129 case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
|
130 } |
|
131 |
|
132 // some "rectification" functions for simplification |
|
133 def F_ID(v: Val): Val = v |
|
134 def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v)) |
|
135 def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v)) |
|
136 def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
|
137 case Right(v) => Right(f2(v)) |
|
138 case Left(v) => Left(f1(v)) |
|
139 } |
|
140 def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
|
141 case Sequ(v1, v2) => Sequ(f1(v1), f2(v2)) |
|
142 } |
|
143 def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = |
|
144 (v:Val) => Sequ(f1(Empty), f2(v)) |
|
145 def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = |
|
146 (v:Val) => Sequ(f1(v), f2(Empty)) |
|
147 def F_RECD(f: Val => Val) = (v:Val) => v match { |
|
148 case Rec(x, v) => Rec(x, f(v)) |
|
149 } |
|
150 def F_ERROR(v: Val): Val = throw new Exception("error") |
|
151 |
|
152 // simplification |
|
153 def simp(r: Rexp): (Rexp, Val => Val) = r match { |
|
154 case ALT(r1, r2) => { |
|
155 val (r1s, f1s) = simp(r1) |
|
156 val (r2s, f2s) = simp(r2) |
|
157 (r1s, r2s) match { |
|
158 case (ZERO, _) => (r2s, F_RIGHT(f2s)) |
|
159 case (_, ZERO) => (r1s, F_LEFT(f1s)) |
|
160 case _ => if (r1s == r2s) (r1s, F_LEFT(f1s)) |
|
161 else (ALT (r1s, r2s), F_ALT(f1s, f2s)) |
|
162 } |
|
163 } |
|
164 case SEQ(r1, r2) => { |
|
165 val (r1s, f1s) = simp(r1) |
|
166 val (r2s, f2s) = simp(r2) |
|
167 (r1s, r2s) match { |
|
168 case (ZERO, _) => (ZERO, F_ERROR) |
|
169 case (_, ZERO) => (ZERO, F_ERROR) |
|
170 case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s)) |
|
171 case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s)) |
|
172 case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s)) |
|
173 } |
|
174 } |
|
175 case r => (r, F_ID) |
|
176 } |
|
177 |
|
178 // lexing functions including simplification |
|
179 def lex_simp(r: Rexp, s: List[Char]) : Val = s match { |
|
180 case Nil => if (nullable(r)) mkeps(r) else |
|
181 { throw new Exception("lexing error") } |
|
182 case c::cs => { |
|
183 val (r_simp, f_simp) = simp(der(c, r)) |
|
184 inj(r, c, f_simp(lex_simp(r_simp, cs))) |
|
185 } |
|
186 } |
|
187 |
|
188 def lexing_simp(r: Rexp, s: String) = |
|
189 env(lex_simp(r, s.toList)) |
|
190 |
|
191 |
|
192 // The Lexing Rules for the WHILE Language |
|
193 |
|
194 def PLUS(r: Rexp) = r ~ r.% |
|
195 |
|
196 def Range(s : List[Char]) : Rexp = s match { |
|
197 case Nil => ZERO |
|
198 case c::Nil => CHAR(c) |
|
199 case c::s => ALT(CHAR(c), Range(s)) |
|
200 } |
|
201 def RANGE(s: String) = Range(s.toList) |
|
202 |
|
203 val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_") |
|
204 val DIGIT = RANGE("0123456789") |
|
205 val ID = SYM ~ (SYM | DIGIT).% |
|
206 val NUM = PLUS(DIGIT) |
|
207 val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" |
|
208 val SEMI: Rexp = ";" |
|
209 val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">" |
|
210 val WHITESPACE = PLUS(" " | "\n" | "\t") |
|
211 val RPAREN: Rexp = "{" |
|
212 val LPAREN: Rexp = "}" |
|
213 val STRING: Rexp = "\"" ~ SYM.% ~ "\"" |
|
214 |
|
215 |
|
216 val WHILE_REGS = (("k" $ KEYWORD) | |
|
217 ("i" $ ID) | |
|
218 ("o" $ OP) | |
|
219 ("n" $ NUM) | |
|
220 ("s" $ SEMI) | |
|
221 ("str" $ STRING) | |
|
222 ("p" $ (LPAREN | RPAREN)) | |
|
223 ("w" $ WHITESPACE)).% |
|
224 |
|
225 |
|
226 // Two Simple While Tests |
|
227 //======================== |
|
228 |
|
229 @doc("small tests") |
|
230 @main |
|
231 def small() = { |
|
232 |
|
233 val prog0 = """read n""" |
|
234 println(s"test: $prog0") |
|
235 println(lexing_simp(WHILE_REGS, prog0)) |
|
236 |
|
237 val prog1 = """read n; write n""" |
|
238 println(s"test: $prog1") |
|
239 println(lexing_simp(WHILE_REGS, prog1)) |
|
240 } |
|
241 |
|
242 // Bigger Tests |
|
243 //============== |
|
244 |
|
245 // escapes strings and prints them out as "", "\n" and so on |
|
246 def esc(raw: String): String = { |
|
247 import scala.reflect.runtime.universe._ |
|
248 Literal(Constant(raw)).toString |
|
249 } |
|
250 |
|
251 def escape(tks: List[(String, String)]) = |
|
252 tks.map{ case (s1, s2) => (s1, esc(s2))} |
|
253 |
|
254 |
|
255 val prog2 = """ |
|
256 write "Fib"; |
|
257 read n; |
|
258 minus1 := 0; |
|
259 minus2 := 1; |
|
260 while n > 0 do { |
|
261 temp := minus2; |
|
262 minus2 := minus1 + minus2; |
|
263 minus1 := temp; |
|
264 n := n - 1 |
|
265 }; |
|
266 write "Result"; |
|
267 write minus2 |
|
268 """ |
|
269 |
|
270 @doc("Fibonacci test") |
|
271 @main |
|
272 def fib() = { |
|
273 println("lexing fib program") |
|
274 println(escape(lexing_simp(WHILE_REGS, prog2)).mkString("\n")) |
|
275 } |
|
276 |
|
277 |
|
278 val prog3 = """ |
|
279 start := 1000; |
|
280 x := start; |
|
281 y := start; |
|
282 z := start; |
|
283 while 0 < x do { |
|
284 while 0 < y do { |
|
285 while 0 < z do { |
|
286 z := z - 1 |
|
287 }; |
|
288 z := start; |
|
289 y := y - 1 |
|
290 }; |
|
291 y := start; |
|
292 x := x - 1 |
|
293 } |
|
294 """ |
|
295 |
|
296 @doc("Loops test") |
|
297 @main |
|
298 def loops() = { |
|
299 println("lexing Loops") |
|
300 println(escape(lexing_simp(WHILE_REGS, prog3)).mkString("\n")) |
|
301 } |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 @doc("All tests.") |
|
307 @main |
|
308 def all() = { small(); fib() ; loops() } |