1 // A Simple Lexer according to Sulzmann & Lu |
1 // A simple lexer inspired by work of Sulzmann & Lu |
|
2 //================================================== |
|
3 |
2 |
4 |
3 import scala.language.implicitConversions |
5 import scala.language.implicitConversions |
4 import scala.language.reflectiveCalls |
6 import scala.language.reflectiveCalls |
5 |
7 |
|
8 // regular expressions including records |
6 abstract class Rexp |
9 abstract class Rexp |
7 case object ZERO extends Rexp |
10 case object ZERO extends Rexp |
8 case object ONE extends Rexp |
11 case object ONE extends Rexp |
9 case class CHAR(c: Char) extends Rexp |
12 case class CHAR(c: Char) extends Rexp |
10 case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
13 case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
11 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
14 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
12 case class STAR(r: Rexp) extends Rexp |
15 case class STAR(r: Rexp) extends Rexp |
13 case class RECD(x: String, r: Rexp) extends Rexp |
16 case class RECD(x: String, r: Rexp) extends Rexp |
14 |
17 |
|
18 // values |
15 abstract class Val |
19 abstract class Val |
16 case object Empty extends Val |
20 case object Empty extends Val |
17 case class Chr(c: Char) extends Val |
21 case class Chr(c: Char) extends Val |
18 case class Sequ(v1: Val, v2: Val) extends Val |
22 case class Sequ(v1: Val, v2: Val) extends Val |
19 case class Left(v: Val) extends Val |
23 case class Left(v: Val) extends Val |
43 def ~ (r: Rexp) = SEQ(s, r) |
47 def ~ (r: Rexp) = SEQ(s, r) |
44 def ~ (r: String) = SEQ(s, r) |
48 def ~ (r: String) = SEQ(s, r) |
45 def $ (r: Rexp) = RECD(s, r) |
49 def $ (r: Rexp) = RECD(s, r) |
46 } |
50 } |
47 |
51 |
48 // A test for more conveninet syntax |
52 def nullable(r: Rexp) : Boolean = r match { |
49 val re : Rexp = ("ab" | "a") ~ ("b" | ONE) |
|
50 |
|
51 // the nullable function: tests whether the regular |
|
52 // expression can recognise the empty string |
|
53 def nullable (r: Rexp) : Boolean = r match { |
|
54 case ZERO => false |
53 case ZERO => false |
55 case ONE => true |
54 case ONE => true |
56 case CHAR(_) => false |
55 case CHAR(_) => false |
57 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
56 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
58 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
57 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
59 case STAR(_) => true |
58 case STAR(_) => true |
60 case RECD(_, r1) => nullable(r1) |
59 case RECD(_, r1) => nullable(r1) |
61 } |
60 } |
62 |
61 |
63 // the derivative of a regular expression w.r.t. a character |
62 def der(c: Char, r: Rexp) : Rexp = r match { |
64 def der (c: Char, r: Rexp) : Rexp = r match { |
|
65 case ZERO => ZERO |
63 case ZERO => ZERO |
66 case ONE => ZERO |
64 case ONE => ZERO |
67 case CHAR(d) => if (c == d) ONE else ZERO |
65 case CHAR(d) => if (c == d) ONE else ZERO |
68 case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
66 case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
69 case SEQ(r1, r2) => |
67 case SEQ(r1, r2) => |
88 case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
81 case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
89 case Stars(vs) => vs.map(flatten).mkString |
82 case Stars(vs) => vs.map(flatten).mkString |
90 case Rec(_, v) => flatten(v) |
83 case Rec(_, v) => flatten(v) |
91 } |
84 } |
92 |
85 |
|
86 |
93 // extracts an environment from a value; |
87 // extracts an environment from a value; |
94 // used for lexing a string |
88 // used for tokenise a string |
95 def env(v: Val) : List[(String, String)] = v match { |
89 def env(v: Val) : List[(String, String)] = v match { |
96 case Empty => Nil |
90 case Empty => Nil |
97 case Chr(c) => Nil |
91 case Chr(c) => Nil |
98 case Left(v) => env(v) |
92 case Left(v) => env(v) |
99 case Right(v) => env(v) |
93 case Right(v) => env(v) |
100 case Sequ(v1, v2) => env(v1) ::: env(v2) |
94 case Sequ(v1, v2) => env(v1) ::: env(v2) |
101 case Stars(vs) => vs.flatMap(env) |
95 case Stars(vs) => vs.flatMap(env) |
102 case Rec(x, v) => (x, flatten(v))::env(v) |
96 case Rec(x, v) => (x, flatten(v))::env(v) |
103 } |
97 } |
104 |
98 |
105 // The Injection Part of the Lexer |
99 // The Injection Part of the lexer |
106 |
100 |
107 // calculates a value for how a nullable regex |
|
108 // matches the empty string |
|
109 def mkeps(r: Rexp) : Val = r match { |
101 def mkeps(r: Rexp) : Val = r match { |
110 case ONE => Empty |
102 case ONE => Empty |
111 case ALT(r1, r2) => |
103 case ALT(r1, r2) => |
112 if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
104 if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
113 case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
105 case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
114 case STAR(r) => Stars(Nil) |
106 case STAR(r) => Stars(Nil) |
115 case RECD(x, r) => Rec(x, mkeps(r)) |
107 case RECD(x, r) => Rec(x, mkeps(r)) |
116 } |
108 } |
117 |
109 |
118 // injects back a character into a value |
|
119 def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
110 def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
120 case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
111 case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
121 case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
112 case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
122 case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
113 case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
123 case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
114 case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
125 case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
116 case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
126 case (CHAR(d), Empty) => Chr(c) |
117 case (CHAR(d), Empty) => Chr(c) |
127 case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
118 case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
128 } |
119 } |
129 |
120 |
130 // the main lexing function (produces a value) |
|
131 def lex(r: Rexp, s: List[Char]) : Val = s match { |
|
132 case Nil => if (nullable(r)) mkeps(r) |
|
133 else throw new Exception("Not matched") |
|
134 case c::cs => inj(r, c, lex(der(c, r), cs)) |
|
135 } |
|
136 |
|
137 def lexing(r: Rexp, s: String) : Val = lex(r, s.toList) |
|
138 |
|
139 // a simple test for extracting an environment |
|
140 val re1 : Rexp = ("first" $ ("a" | "ab")) ~ ("second" $ ("b" | ONE)) |
|
141 env(lexing(re1, "ab")) |
|
142 |
|
143 // some "rectification" functions for simplification |
121 // some "rectification" functions for simplification |
144 def F_ID(v: Val): Val = v |
122 def F_ID(v: Val): Val = v |
145 def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v)) |
123 def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v)) |
146 def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v)) |
124 def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v)) |
147 def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
125 def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
191 case r => (r, F_ID) |
167 case r => (r, F_ID) |
192 } |
168 } |
193 |
169 |
194 // lexing functions including simplification |
170 // lexing functions including simplification |
195 def lex_simp(r: Rexp, s: List[Char]) : Val = s match { |
171 def lex_simp(r: Rexp, s: List[Char]) : Val = s match { |
196 case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched") |
172 case Nil => if (nullable(r)) mkeps(r) else |
|
173 { throw new Exception("lexing error") } |
197 case c::cs => { |
174 case c::cs => { |
198 val (r_simp, f_simp) = simp(der(c, r)) |
175 val (r_simp, f_simp) = simp(der(c, r)) |
199 inj(r, c, f_simp(lex_simp(r_simp, cs))) |
176 inj(r, c, f_simp(lex_simp(r_simp, cs))) |
200 } |
177 } |
201 } |
178 } |
202 |
179 |
203 def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList) |
180 def lexing_simp(r: Rexp, s: String) = |
204 |
181 env(lex_simp(r, s.toList)) |
205 lexing_simp(("a" | "ab") ~ ("b" | ""), "ab") |
182 |
206 |
183 |
207 // The Lexing Rules for a Small While Language |
184 // The Lexing Rules for the Fun Language |
208 |
185 |
209 def PLUS(r: Rexp) = r ~ r.% |
186 def PLUS(r: Rexp) = r ~ r.% |
210 |
187 |
211 val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" |
188 def Range(s : List[Char]) : Rexp = s match { |
212 val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |
189 case Nil => ZERO |
|
190 case c::Nil => CHAR(c) |
|
191 case c::s => ALT(CHAR(c), Range(s)) |
|
192 } |
|
193 def RANGE(s: String) = Range(s.toList) |
|
194 |
|
195 val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_") |
|
196 val DIGIT = RANGE("0123456789") |
213 val ID = SYM ~ (SYM | DIGIT).% |
197 val ID = SYM ~ (SYM | DIGIT).% |
214 val NUM = PLUS(DIGIT) |
198 val NUM = PLUS(DIGIT) |
215 val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false" |
199 val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" |
216 val SEMI: Rexp = ";" |
200 val SEMI: Rexp = ";" |
217 val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/" |
201 val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">" |
218 val WHITESPACE = PLUS(" " | "\n" | "\t") |
202 val WHITESPACE = PLUS(" " | "\n" | "\t") |
219 val RPAREN: Rexp = ")" |
203 val RPAREN: Rexp = "{" |
220 val LPAREN: Rexp = "(" |
204 val LPAREN: Rexp = "}" |
221 val BEGIN: Rexp = "{" |
|
222 val END: Rexp = "}" |
|
223 val STRING: Rexp = "\"" ~ SYM.% ~ "\"" |
205 val STRING: Rexp = "\"" ~ SYM.% ~ "\"" |
224 |
206 |
225 |
207 |
226 val WHILE_REGS = (("k" $ KEYWORD) | |
208 val WHILE_REGS = (("k" $ KEYWORD) | |
227 ("i" $ ID) | |
209 ("i" $ ID) | |
228 ("o" $ OP) | |
210 ("o" $ OP) | |
229 ("n" $ NUM) | |
211 ("n" $ NUM) | |
230 ("s" $ SEMI) | |
212 ("s" $ SEMI) | |
231 ("str" $ STRING) | |
213 ("str" $ STRING) | |
232 ("p" $ (LPAREN | RPAREN)) | |
214 ("p" $ (LPAREN | RPAREN)) | |
233 ("b" $ (BEGIN | END)) | |
|
234 ("w" $ WHITESPACE)).% |
215 ("w" $ WHITESPACE)).% |
235 |
|
236 // Testing |
|
237 //============ |
|
238 |
|
239 def time[T](code: => T) = { |
|
240 val start = System.nanoTime() |
|
241 val result = code |
|
242 val end = System.nanoTime() |
|
243 println((end - start)/1.0e9) |
|
244 result |
|
245 } |
|
246 |
|
247 val r1 = ("a" | "ab") ~ ("bcd" | "c") |
|
248 println(lexing(r1, "abcd")) |
|
249 |
|
250 val r2 = ("" | "a") ~ ("ab" | "b") |
|
251 println(lexing(r2, "ab")) |
|
252 |
216 |
253 |
217 |
254 // Two Simple While Tests |
218 // Two Simple While Tests |
255 //======================== |
219 //======================== |
256 println("prog0 test") |
220 |
257 |
221 println("test: read n") |
258 val prog0 = """read if""" |
222 |
259 println(env(lexing_simp(WHILE_REGS, prog0))) |
223 val prog0 = """read n""" |
260 |
224 println(lexing_simp(WHILE_REGS, prog0)) |
261 println("prog1 test") |
225 |
262 |
226 println("test: read n; write n ") |
263 val prog1 = """read n; write (n)""" |
227 |
264 println(env(lexing_simp(WHILE_REGS, prog1))) |
228 val prog1 = """read n; write n""" |
265 |
229 println(lexing_simp(WHILE_REGS, prog1)) |
266 |
230 |
267 // Bigger Test |
231 |
268 //============= |
232 // Bigger Tests |
|
233 //============== |
|
234 |
|
235 // escapes strings and prints them out as "", "\n" and so on |
|
236 def esc(raw: String): String = { |
|
237 import scala.reflect.runtime.universe._ |
|
238 Literal(Constant(raw)).toString |
|
239 } |
|
240 |
|
241 def escape(tks: List[(String, String)]) = |
|
242 tks.map{ case (s1, s2) => (s1, esc(s2))} |
269 |
243 |
270 val prog2 = """ |
244 val prog2 = """ |
271 write "fib"; |
245 write "Fib"; |
272 read n; |
246 read n; |
273 minus1 := 0; |
247 minus1 := 0; |
274 minus2 := 1; |
248 minus2 := 1; |
275 while n > 0 do { |
249 while n > 0 do { |
276 temp := minus2; |
250 temp := minus2; |
277 minus2 := minus1 + minus2; |
251 minus2 := minus1 + minus2; |
278 minus1 := temp; |
252 minus1 := temp; |
279 n := n - 1 |
253 n := n - 1 |
280 }; |
254 }; |
281 write "result"; |
|
282 write minus2 |
|
283 """ |
|
284 |
|
285 println("Tokens") |
|
286 println(env(lexing_simp(WHILE_REGS, prog2))) |
|
287 println(env(lexing_simp(WHILE_REGS, prog2)).filterNot{_._1 == "w"}.mkString("\n")) |
|
288 |
|
289 // some more timing tests with |
|
290 // i copies of the program |
|
291 |
|
292 for (i <- 0 to 20 by 10) { |
|
293 print(i.toString + ": ") |
|
294 time(lexing_simp(WHILE_REGS, prog2 * i)) |
|
295 } |
|
296 |
|
297 |
|
298 val fib = """ |
|
299 write "Fib"; |
|
300 read n; |
|
301 minus1 := 0; |
|
302 minus2 := 1; |
|
303 while n > 0 do { |
|
304 temp := minus2; |
|
305 minus2 := minus1 + minus2; |
|
306 minus1 := temp; |
|
307 n := n - 1 |
|
308 }; |
|
309 write "Result"; |
255 write "Result"; |
310 write minus2 |
256 write minus2 |
311 """ |
257 """ |
312 |
258 |
313 println(env(lexing_simp(WHILE_REGS, prog2)).filterNot{_._1 == "w"}) |
259 println("lexing Fib") |
|
260 println(escape(lexing_simp(WHILE_REGS, prog2)).mkString("\n")) |
|
261 |
|
262 |
|
263 |
|
264 val prog3 = """ |
|
265 start := 1000; |
|
266 x := start; |
|
267 y := start; |
|
268 z := start; |
|
269 while 0 < x do { |
|
270 while 0 < y do { |
|
271 while 0 < z do { |
|
272 z := z - 1 |
|
273 }; |
|
274 z := start; |
|
275 y := y - 1 |
|
276 }; |
|
277 y := start; |
|
278 x := x - 1 |
|
279 } |
|
280 """ |
|
281 |
|
282 println("lexing Loops") |
|
283 println(escape(lexing_simp(WHILE_REGS, prog3)).mkString("\n")) |
|
284 |