1 // A simple tokeniser based on the Sulzmann & Lu algorithm |
|
2 //========================================================= |
|
3 // |
|
4 // call with |
|
5 // |
|
6 // scala tokenise.scala fib.while |
|
7 // |
|
8 // scala tokenise.scala loops.while |
|
9 // |
|
10 // this will generate a .tks file that can be deserialised back |
|
11 // into a list of tokens |
|
12 // you can add -Xno-patmat-analysis in order to get rid of the |
|
13 // match-not-exhaustive warning |
|
14 |
|
15 object Tokenise { |
|
16 |
|
17 import scala.language.implicitConversions |
|
18 import scala.language.reflectiveCalls |
|
19 |
|
20 // regular expressions including records |
|
21 abstract class Rexp |
|
22 case object ZERO extends Rexp |
|
23 case object ONE extends Rexp |
|
24 case class CHAR(c: Char) extends Rexp |
|
25 case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
|
26 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
|
27 case class STAR(r: Rexp) extends Rexp |
|
28 case class RECD(x: String, r: Rexp) extends Rexp |
|
29 |
|
30 // values |
|
31 abstract class Val |
|
32 case object Empty extends Val |
|
33 case class Chr(c: Char) extends Val |
|
34 case class Sequ(v1: Val, v2: Val) extends Val |
|
35 case class Left(v: Val) extends Val |
|
36 case class Right(v: Val) extends Val |
|
37 case class Stars(vs: List[Val]) extends Val |
|
38 case class Rec(x: String, v: Val) extends Val |
|
39 |
|
40 // some convenience for typing in regular expressions |
|
41 def charlist2rexp(s : List[Char]): Rexp = s match { |
|
42 case Nil => ONE |
|
43 case c::Nil => CHAR(c) |
|
44 case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
|
45 } |
|
46 implicit def string2rexp(s : String) : Rexp = |
|
47 charlist2rexp(s.toList) |
|
48 |
|
49 implicit def RexpOps(r: Rexp) = new { |
|
50 def | (s: Rexp) = ALT(r, s) |
|
51 def % = STAR(r) |
|
52 def ~ (s: Rexp) = SEQ(r, s) |
|
53 } |
|
54 |
|
55 implicit def stringOps(s: String) = new { |
|
56 def | (r: Rexp) = ALT(s, r) |
|
57 def | (r: String) = ALT(s, r) |
|
58 def % = STAR(s) |
|
59 def ~ (r: Rexp) = SEQ(s, r) |
|
60 def ~ (r: String) = SEQ(s, r) |
|
61 def $ (r: Rexp) = RECD(s, r) |
|
62 } |
|
63 |
|
64 def nullable(r: Rexp) : Boolean = r match { |
|
65 case ZERO => false |
|
66 case ONE => true |
|
67 case CHAR(_) => false |
|
68 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
|
69 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
|
70 case STAR(_) => true |
|
71 case RECD(_, r1) => nullable(r1) |
|
72 } |
|
73 |
|
74 def der(c: Char, r: Rexp) : Rexp = r match { |
|
75 case ZERO => ZERO |
|
76 case ONE => ZERO |
|
77 case CHAR(d) => if (c == d) ONE else ZERO |
|
78 case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
|
79 case SEQ(r1, r2) => |
|
80 if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
|
81 else SEQ(der(c, r1), r2) |
|
82 case STAR(r) => SEQ(der(c, r), STAR(r)) |
|
83 case RECD(_, r1) => der(c, r1) |
|
84 } |
|
85 |
|
86 |
|
87 // extracts a string from value |
|
88 def flatten(v: Val) : String = v match { |
|
89 case Empty => "" |
|
90 case Chr(c) => c.toString |
|
91 case Left(v) => flatten(v) |
|
92 case Right(v) => flatten(v) |
|
93 case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
|
94 case Stars(vs) => vs.map(flatten).mkString |
|
95 case Rec(_, v) => flatten(v) |
|
96 } |
|
97 |
|
98 |
|
99 // extracts an environment from a value; |
|
100 // used for tokenise a string |
|
101 def env(v: Val) : List[(String, String)] = v match { |
|
102 case Empty => Nil |
|
103 case Chr(c) => Nil |
|
104 case Left(v) => env(v) |
|
105 case Right(v) => env(v) |
|
106 case Sequ(v1, v2) => env(v1) ::: env(v2) |
|
107 case Stars(vs) => vs.flatMap(env) |
|
108 case Rec(x, v) => (x, flatten(v))::env(v) |
|
109 } |
|
110 |
|
111 // The Injection Part of the lexer |
|
112 |
|
113 def mkeps(r: Rexp) : Val = r match { |
|
114 case ONE => Empty |
|
115 case ALT(r1, r2) => |
|
116 if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
|
117 case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
|
118 case STAR(r) => Stars(Nil) |
|
119 case RECD(x, r) => Rec(x, mkeps(r)) |
|
120 } |
|
121 |
|
122 def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
|
123 case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
|
124 case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
|
125 case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
|
126 case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
|
127 case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1)) |
|
128 case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
|
129 case (CHAR(d), Empty) => Chr(c) |
|
130 case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
|
131 } |
|
132 |
|
133 // some "rectification" functions for simplification |
|
134 def F_ID(v: Val): Val = v |
|
135 def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v)) |
|
136 def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v)) |
|
137 def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
|
138 case Right(v) => Right(f2(v)) |
|
139 case Left(v) => Left(f1(v)) |
|
140 } |
|
141 def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
|
142 case Sequ(v1, v2) => Sequ(f1(v1), f2(v2)) |
|
143 } |
|
144 def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = |
|
145 (v:Val) => Sequ(f1(Empty), f2(v)) |
|
146 def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = |
|
147 (v:Val) => Sequ(f1(v), f2(Empty)) |
|
148 def F_RECD(f: Val => Val) = (v:Val) => v match { |
|
149 case Rec(x, v) => Rec(x, f(v)) |
|
150 } |
|
151 def F_ERROR(v: Val): Val = throw new Exception("error") |
|
152 |
|
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 // inefficient representations for some extended regular |
|
195 // expressions |
|
196 def PLUS(r: Rexp) = r ~ r.% |
|
197 |
|
198 def Range(s : List[Char]) : Rexp = s match { |
|
199 case Nil => ZERO |
|
200 case c::Nil => CHAR(c) |
|
201 case c::s => ALT(CHAR(c), Range(s)) |
|
202 } |
|
203 def RANGE(s: String) = Range(s.toList) |
|
204 |
|
205 val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_") |
|
206 val DIGIT = RANGE("0123456789") |
|
207 val ID = SYM ~ (SYM | DIGIT).% |
|
208 val NUM = PLUS(DIGIT) |
|
209 val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" |
|
210 val SEMI: Rexp = ";" |
|
211 val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">" |
|
212 val WHITESPACE = PLUS(" " | "\n" | "\t") |
|
213 val RPAREN: Rexp = "{" |
|
214 val LPAREN: Rexp = "}" |
|
215 val STRING: Rexp = "\"" ~ SYM.% ~ "\"" |
|
216 |
|
217 |
|
218 val WHILE_REGS = (("k" $ KEYWORD) | |
|
219 ("i" $ ID) | |
|
220 ("o" $ OP) | |
|
221 ("n" $ NUM) | |
|
222 ("s" $ SEMI) | |
|
223 ("str" $ STRING) | |
|
224 ("p" $ (LPAREN | RPAREN)) | |
|
225 ("w" $ WHITESPACE)).% |
|
226 |
|
227 |
|
228 |
|
229 // Generate tokens for the WHILE language |
|
230 // and serialise them into a .tks file |
|
231 |
|
232 import java.io._ |
|
233 |
|
234 abstract class Token extends Serializable |
|
235 case object T_SEMI extends Token |
|
236 case object T_LPAREN extends Token |
|
237 case object T_RPAREN extends Token |
|
238 case class T_ID(s: String) extends Token |
|
239 case class T_OP(s: String) extends Token |
|
240 case class T_NUM(n: Int) extends Token |
|
241 case class T_KWD(s: String) extends Token |
|
242 case class T_STR(s: String) extends Token |
|
243 |
|
244 // transforms pairs into tokens |
|
245 val token : PartialFunction[(String, String), Token] = { |
|
246 case ("s", _) => T_SEMI |
|
247 case ("p", "{") => T_LPAREN |
|
248 case ("p", "}") => T_RPAREN |
|
249 case ("i", s) => T_ID(s) |
|
250 case ("o", s) => T_OP(s) |
|
251 case ("n", s) => T_NUM(s.toInt) |
|
252 case ("k", s) => T_KWD(s) |
|
253 case ("str", s) => T_STR(s) |
|
254 } |
|
255 |
|
256 // filters out all un-interesting tokens |
|
257 def tokenise(s: String) : List[Token] = |
|
258 lexing_simp(WHILE_REGS, s).collect(token) |
|
259 |
|
260 |
|
261 def serialise[T](fname: String, data: T) = { |
|
262 import scala.util.Using |
|
263 Using(new ObjectOutputStream(new FileOutputStream(fname))) { |
|
264 out => out.writeObject(data) |
|
265 } |
|
266 } |
|
267 |
|
268 def main(args: Array[String]) : Unit = { |
|
269 val fname = args(0) |
|
270 val tname = fname.stripSuffix(".while") ++ ".tks" |
|
271 val file = io.Source.fromFile(fname).mkString |
|
272 serialise(tname, tokenise(file)) |
|
273 } |
|
274 |
|
275 |
|
276 } |
|