-
Notifications
You must be signed in to change notification settings - Fork 0
/
tigerlex.sml
422 lines (412 loc) · 12.1 KB
/
tigerlex.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
local open Obj Lexing in
open tigergrm
val num_linea = tigernlin.num_linea
val ncom = ref 0
val atoi = valOf o Int.fromString
fun inc r = r:=(!r+1)
fun dec r = (r:=(!r-1); !r)
fun aHex n =
"\\x"^(if n<=15 then "0" else "")^
(Int.fmt StringCvt.HEX n)
fun ctrl0 "\\n" = "\\x0a"
| ctrl0 "\\t" = "\\x09"
| ctrl0 _ = raise Fail "error interno 1 en lex!"
fun ctrl1 s =
let val c = ord(hd(tl(tl(explode s))))
in aHex(c-ord(#"@")) end
fun ctrl2 s =
let val n = ord(valOf(Char.fromString s))
in aHex n end
exception noEsta
val pal_claves = Polyhash.mkPolyTable(20, noEsta)
val _ = List.app (fn(c, v) => Polyhash.insert pal_claves (c, v))
[("type", TYPE),
("array", ARRAY),
("of", OF),
("var", VAR),
("function", FUNCTION),
("let", LET),
("in", IN),
("end", END),
("if", IF),
("then", THEN),
("else", ELSE),
("while", WHILE),
("do", DO),
("for", FOR),
("to", TO),
("break", BREAK),
("nil", NIL)]
fun clav_id(s) = (Polyhash.find pal_claves s) handle noEsta => ID s
fun action_50 lexbuf = (
raise Fail("lex:["^getLexeme(lexbuf)^"]!") )
and action_49 lexbuf = (
ID(getLexeme lexbuf) )
and action_48 lexbuf = (
clav_id(getLexeme lexbuf) )
and action_47 lexbuf = (
NRO(atoi(getLexeme lexbuf)) )
and action_46 lexbuf = (
LITERAL(literal lexbuf) )
and action_45 lexbuf = (
DIV )
and action_44 lexbuf = (
POR )
and action_43 lexbuf = (
MENOS )
and action_42 lexbuf = (
MAS )
and action_41 lexbuf = (
DIST )
and action_40 lexbuf = (
MAYIG )
and action_39 lexbuf = (
MAYOR )
and action_38 lexbuf = (
MENIG )
and action_37 lexbuf = (
MENOR )
and action_36 lexbuf = (
PIPE )
and action_35 lexbuf = (
AMPER )
and action_34 lexbuf = (
LD )
and action_33 lexbuf = (
LI )
and action_32 lexbuf = (
CD )
and action_31 lexbuf = (
CI )
and action_30 lexbuf = (
PD )
and action_29 lexbuf = (
PI )
and action_28 lexbuf = (
IGUAL )
and action_27 lexbuf = (
PCOMA )
and action_26 lexbuf = (
COMA )
and action_25 lexbuf = (
DOSPIG )
and action_24 lexbuf = (
DOSP )
and action_23 lexbuf = (
PTO )
and action_22 lexbuf = (
Tok lexbuf )
and action_21 lexbuf = (
inc num_linea; Tok lexbuf )
and action_20 lexbuf = (
EOF )
and action_19 lexbuf = (
inc ncom; Com lexbuf )
and action_18 lexbuf = (
let val s = getLexeme lexbuf
in
if s > "\^_" then s^literal(lexbuf)
else raise Fail "caracter ilegal!"
end
)
and action_17 lexbuf = (
literal1 lexbuf )
and action_16 lexbuf = (
ctrl2(getLexeme lexbuf)^literal(lexbuf) )
and action_15 lexbuf = (
ctrl1(getLexeme lexbuf)^literal(lexbuf) )
and action_14 lexbuf = (
ctrl0(getLexeme lexbuf)^literal(lexbuf) )
and action_13 lexbuf = (
getLexeme(lexbuf)^literal(lexbuf) )
and action_12 lexbuf = (
"" )
and action_11 lexbuf = (
raise Fail "NL en string!" )
and action_10 lexbuf = (
raise Fail "string sin terminar! " )
and action_9 lexbuf = (
raise Fail "\\\\ malformada!" )
and action_8 lexbuf = (
literal lexbuf )
and action_7 lexbuf = (
literal1 lexbuf )
and action_6 lexbuf = (
inc num_linea; literal1 lexbuf )
and action_5 lexbuf = (
raise Fail "string incompleta!" )
and action_4 lexbuf = (
Com lexbuf )
and action_3 lexbuf = (
inc num_linea; Com lexbuf )
and action_2 lexbuf = (
raise Fail "coment incompleto!" )
and action_1 lexbuf = (
inc ncom; Com lexbuf )
and action_0 lexbuf = (
(if dec ncom=0 then Tok else Com) lexbuf )
and state_0 lexbuf = (
let val currChar = getNextChar lexbuf in
case currChar of
#"/" => state_62 lexbuf
| #"*" => state_61 lexbuf
| #"\n" => action_3 lexbuf
| #"\^@" => action_2 lexbuf
| _ => action_4 lexbuf
end)
and state_1 lexbuf = (
let val currChar = getNextChar lexbuf in
case currChar of
#"\t" => state_54 lexbuf
| #"\r" => state_54 lexbuf
| #"\f" => state_54 lexbuf
| #" " => state_54 lexbuf
| #"\\" => action_8 lexbuf
| #"\n" => action_6 lexbuf
| #"\^@" => action_5 lexbuf
| _ => action_9 lexbuf
end)
and state_2 lexbuf = (
let val currChar = getNextChar lexbuf in
case currChar of
#"\\" => state_44 lexbuf
| #"\"" => action_12 lexbuf
| #"\n" => action_11 lexbuf
| #"\^@" => action_10 lexbuf
| _ => action_18 lexbuf
end)
and state_3 lexbuf = (
let val currChar = getNextChar lexbuf in
if currChar >= #"a" andalso currChar <= #"z" then state_27 lexbuf
else if currChar >= #"A" andalso currChar <= #"Z" then state_24 lexbuf
else if currChar >= #"0" andalso currChar <= #"9" then state_18 lexbuf
else case currChar of
#"\t" => state_6 lexbuf
| #"\r" => state_6 lexbuf
| #"\f" => state_6 lexbuf
| #" " => state_6 lexbuf
| #"}" => action_34 lexbuf
| #"|" => action_36 lexbuf
| #"{" => action_33 lexbuf
| #"]" => action_32 lexbuf
| #"[" => action_31 lexbuf
| #">" => state_23 lexbuf
| #"=" => action_28 lexbuf
| #"<" => state_21 lexbuf
| #";" => action_27 lexbuf
| #":" => state_19 lexbuf
| #"/" => state_17 lexbuf
| #"." => action_23 lexbuf
| #"-" => action_43 lexbuf
| #"," => action_26 lexbuf
| #"+" => action_42 lexbuf
| #"*" => action_44 lexbuf
| #")" => action_30 lexbuf
| #"(" => action_29 lexbuf
| #"&" => action_35 lexbuf
| #"\"" => action_46 lexbuf
| #"\n" => action_21 lexbuf
| #"\^@" => action_20 lexbuf
| _ => action_50 lexbuf
end)
and state_6 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_22);
let val currChar = getNextChar lexbuf in
case currChar of
#"\t" => state_39 lexbuf
| #"\r" => state_39 lexbuf
| #"\f" => state_39 lexbuf
| #" " => state_39 lexbuf
| _ => backtrack lexbuf
end)
and state_17 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_45);
let val currChar = getNextChar lexbuf in
case currChar of
#"*" => action_19 lexbuf
| _ => backtrack lexbuf
end)
and state_18 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_47);
let val currChar = getNextChar lexbuf in
if currChar >= #"0" andalso currChar <= #"9" then state_37 lexbuf
else backtrack lexbuf
end)
and state_19 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_24);
let val currChar = getNextChar lexbuf in
case currChar of
#"=" => action_25 lexbuf
| _ => backtrack lexbuf
end)
and state_21 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_37);
let val currChar = getNextChar lexbuf in
case currChar of
#">" => action_41 lexbuf
| #"=" => action_38 lexbuf
| _ => backtrack lexbuf
end)
and state_23 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_39);
let val currChar = getNextChar lexbuf in
case currChar of
#"=" => action_40 lexbuf
| _ => backtrack lexbuf
end)
and state_24 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_49);
let val currChar = getNextChar lexbuf in
if currChar >= #"0" andalso currChar <= #"9" then state_31 lexbuf
else if currChar >= #"A" andalso currChar <= #"Z" then state_31 lexbuf
else if currChar >= #"a" andalso currChar <= #"z" then state_31 lexbuf
else case currChar of
#"_" => state_31 lexbuf
| _ => backtrack lexbuf
end)
and state_27 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_48);
let val currChar = getNextChar lexbuf in
if currChar >= #"0" andalso currChar <= #"9" then state_31 lexbuf
else if currChar >= #"A" andalso currChar <= #"Z" then state_31 lexbuf
else if currChar >= #"a" andalso currChar <= #"z" then state_32 lexbuf
else case currChar of
#"_" => state_31 lexbuf
| _ => backtrack lexbuf
end)
and state_31 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_49);
let val currChar = getNextChar lexbuf in
if currChar >= #"0" andalso currChar <= #"9" then state_31 lexbuf
else if currChar >= #"A" andalso currChar <= #"Z" then state_31 lexbuf
else if currChar >= #"a" andalso currChar <= #"z" then state_31 lexbuf
else case currChar of
#"_" => state_31 lexbuf
| _ => backtrack lexbuf
end)
and state_32 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_48);
let val currChar = getNextChar lexbuf in
if currChar >= #"0" andalso currChar <= #"9" then state_31 lexbuf
else if currChar >= #"A" andalso currChar <= #"Z" then state_31 lexbuf
else if currChar >= #"a" andalso currChar <= #"z" then state_32 lexbuf
else case currChar of
#"_" => state_31 lexbuf
| _ => backtrack lexbuf
end)
and state_37 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_47);
let val currChar = getNextChar lexbuf in
if currChar >= #"0" andalso currChar <= #"9" then state_37 lexbuf
else backtrack lexbuf
end)
and state_39 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_22);
let val currChar = getNextChar lexbuf in
case currChar of
#"\t" => state_39 lexbuf
| #"\r" => state_39 lexbuf
| #"\f" => state_39 lexbuf
| #" " => state_39 lexbuf
| _ => backtrack lexbuf
end)
and state_44 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_17);
let val currChar = getNextChar lexbuf in
if currChar >= #"0" andalso currChar <= #"9" then state_46 lexbuf
else case currChar of
#"n" => action_14 lexbuf
| #"t" => action_14 lexbuf
| #"\"" => action_13 lexbuf
| #"\\" => action_13 lexbuf
| #"^" => state_47 lexbuf
| _ => backtrack lexbuf
end)
and state_46 lexbuf = (
let val currChar = getNextChar lexbuf in
if currChar >= #"0" andalso currChar <= #"9" then state_50 lexbuf
else backtrack lexbuf
end)
and state_47 lexbuf = (
let val currChar = getNextChar lexbuf in
if currChar >= #"@" andalso currChar <= #"_" then action_15 lexbuf
else backtrack lexbuf
end)
and state_50 lexbuf = (
let val currChar = getNextChar lexbuf in
if currChar >= #"0" andalso currChar <= #"9" then action_16 lexbuf
else backtrack lexbuf
end)
and state_54 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_7);
let val currChar = getNextChar lexbuf in
case currChar of
#"\t" => state_57 lexbuf
| #"\r" => state_57 lexbuf
| #"\f" => state_57 lexbuf
| #" " => state_57 lexbuf
| _ => backtrack lexbuf
end)
and state_57 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_7);
let val currChar = getNextChar lexbuf in
case currChar of
#"\t" => state_57 lexbuf
| #"\r" => state_57 lexbuf
| #"\f" => state_57 lexbuf
| #" " => state_57 lexbuf
| _ => backtrack lexbuf
end)
and state_61 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_4);
let val currChar = getNextChar lexbuf in
case currChar of
#"/" => action_0 lexbuf
| _ => backtrack lexbuf
end)
and state_62 lexbuf = (
setLexLastPos lexbuf (getLexCurrPos lexbuf);
setLexLastAction lexbuf (magic action_4);
let val currChar = getNextChar lexbuf in
case currChar of
#"*" => action_1 lexbuf
| _ => backtrack lexbuf
end)
and Tok lexbuf =
(setLexLastAction lexbuf (magic dummyAction);
setLexStartPos lexbuf (getLexCurrPos lexbuf);
state_3 lexbuf)
and literal lexbuf =
(setLexLastAction lexbuf (magic dummyAction);
setLexStartPos lexbuf (getLexCurrPos lexbuf);
state_2 lexbuf)
and literal1 lexbuf =
(setLexLastAction lexbuf (magic dummyAction);
setLexStartPos lexbuf (getLexCurrPos lexbuf);
state_1 lexbuf)
and Com lexbuf =
(setLexLastAction lexbuf (magic dummyAction);
setLexStartPos lexbuf (getLexCurrPos lexbuf);
state_0 lexbuf)
(* The following checks type consistency of actions *)
val _ = fn _ => [action_50, action_49, action_48, action_47, action_46, action_45, action_44, action_43, action_42, action_41, action_40, action_39, action_38, action_37, action_36, action_35, action_34, action_33, action_32, action_31, action_30, action_29, action_28, action_27, action_26, action_25, action_24, action_23, action_22, action_21, action_20, action_19];
val _ = fn _ => [action_18, action_17, action_16, action_15, action_14, action_13, action_12, action_11, action_10];
val _ = fn _ => [action_9, action_8, action_7, action_6, action_5];
val _ = fn _ => [action_4, action_3, action_2, action_1, action_0];
end