-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexical.l
325 lines (303 loc) · 8.2 KB
/
lexical.l
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
%{
#include <stdio.h>
#include <stdlib.h>
#include "syntax.tab.h"
#include "syntax_tree.h"
#include "DebugMacro.h"
// 维护词法单元的位置信息
int yycolumn = 1;
#define YY_USER_ACTION \
yylloc.first_line = yylloc.last_line = yylineno; \
yylloc.first_column = yycolumn; \
yylloc.last_column = yycolumn + yyleng - 1; \
yycolumn += yyleng;
void print_loc_info(char* name);
void comment();
enum RelopType
{
less = 0,
more,
less_equal,
more_equal,
equal,
not_equal
};
%}
%option yylineno
digit [0-9]
letter [a-zA-Z_]
uppercase [A-Z]
lowercase [a-z]
ws [ \t]
%%
{ws}+ { print_loc_info("find white spaces"); }
"/*" { comment(); }
\n {
yycolumn = 1;
print_loc_info("SEMI");
yylval.type_node = (void *)new_token_node(yylloc.first_line, yylloc.first_column, "SEMI");
return SEMI;
}
"," {
print_loc_info("COMMA");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "COMMA");
return COMMA;
}
":" {
print_loc_info("COLON");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "COLON");
return COLON;
}
"|" {
print_loc_info("SINGLEOR");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "SINGLEOR");
return SINGLEOR;
}
"_" {
print_loc_info("PLACEHOLDER");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "PLACEHOLDER");
return PLACEHOLDER;
}
data {
print_loc_info("DATA");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "DATA");
return DATA;
}
class {
print_loc_info("CLASS");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "CLASS");
return CLASS;
}
instance {
print_loc_info("INSTANCE");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "INSTANCE");
return INSTANCE;
}
"->" {
print_loc_info("DEDUCT");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "DEDUCT");
return DEDUCT;
}
fn {
print_loc_info("FUNC");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "FUNC");
return FUNC;
}
refer {
print_loc_info("REFER");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "REFER");
return REFER;
}
"=" {
print_loc_info("ASSIGNOP");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "ASSIGNOP");
return ASSIGNOP;
}
"<" {
print_loc_info("RELOP-less");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "RELOP");
return RELOP;
}
">" {
print_loc_info("RELOP-more");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "RELOP");
return RELOP;
}
"<=" {
print_loc_info("RELOP-less_equal");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "RELOP");
return RELOP;
}
">=" {
print_loc_info("RELOP-more_equal");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "RELOP");
return RELOP;
}
"==" {
print_loc_info("RELOP-equal");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "RELOP");
return RELOP;
}
"!=" {
print_loc_info("RELOP-not_equal");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "RELOP");
return RELOP;
}
"+" {
print_loc_info("PLUS");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "PLUS");
return PLUS;
}
"-" {
print_loc_info("MINUS");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "MINUS");
return MINUS;
}
"*" {
print_loc_info("STAR");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "STAR");
return STAR;
}
"/" {
print_loc_info("DIV");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "DIV");
return DIV;
}
"&&" {
print_loc_info("AND");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "AND");
return AND;
}
"||" {
print_loc_info("OR");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "OR");
return OR;
}
"." {
print_loc_info("DOT");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "DOT");
return DOT;
}
"!" {
print_loc_info("NOT");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "NOT");
return NOT;
}
let {
print_loc_info("LET");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "LET");
return LET;
}
var {
print_loc_info("VAR");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "VAR");
return VAR;
}
Int {
print_loc_info("INT");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "INT");
return BUILDINTYPE;
}
Float {
print_loc_info("INT");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "FLOAT");
return BUILDINTYPE;
}
"(" {
print_loc_info("LP");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "LP");
return LP;
}
")" {
print_loc_info("RP");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "RP");
return RP;
}
"[" {
print_loc_info("LB");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "LB");
return LB;
}
"]" {
print_loc_info("RB");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "RB");
return RB;
}
"{" {
print_loc_info("LC");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "LC");
return LC;
}
"}" {
print_loc_info("RC");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "RC");
return RC;
}
return {
print_loc_info("RETURN");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "RETURN");
return RETURN;
}
if {
print_loc_info("IF");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "IF");
return IF;
}
else {
print_loc_info("ELSE");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "ELSE");
return ELSE;
}
while {
print_loc_info("WHILE");
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, "WHILE");
return WHILE;
}
0|[1-9]{digit}* {
//yylval=atoi(yytext);
char print_output[50];
int value = atoi(yytext);
snprintf(print_output, sizeof(print_output), "%d", value);
print_loc_info(print_output);
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, print_output);
return INT;
}
(({digit}*\.{digit}+)|({digit}+\.{digit}*))([Ee][+-]?[0-9]+)? {
//yylval.type_node = atof(yytext);
char print_output[50];
float value = atof(yytext);
snprintf(print_output, sizeof(print_output), "%f", value);
print_loc_info(print_output);
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, print_output);
return FLOAT;
}
{lowercase}({letter}|{digit})* {
char print_output[50];
snprintf(print_output, sizeof(print_output), "%s", yytext);
print_loc_info(print_output);
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, print_output);
return LOWERID;
}
{uppercase}({letter}|{digit})* {
char print_output[50];
snprintf(print_output, sizeof(print_output), "%s", yytext);
print_loc_info(print_output);
yylval.type_node = new_token_node(yylloc.first_line, yylloc.first_column, print_output);
return UPPERID;
}
. {
printf("Error Type A at line %d: ", yylloc.first_line);
printf("Mysterious character \"%s\"\n", yytext);
yyterminate();
}
%%
void print_loc_info(char* name)
{
#ifdef flex_debug_print
printf("%s: line--%d; column--%d\n", name, yylloc.first_line, yylloc.first_column);
#endif
}
void comment()
{
char c, c1;
loop:
while ((c = input()) != '*' && c != 0)
{
#ifdef flex_debug_print
putchar(c);
#endif
}
if ((c1 = input()) != '/' && c != 0)
{
#ifdef flex_debug_print
unput(c1);
#endif
goto loop;
}
if (c != 0)
{
#ifdef flex_debug_print
putchar(c1);
#endif
}
}