-
Notifications
You must be signed in to change notification settings - Fork 11
/
lexer.l
515 lines (471 loc) · 9.54 KB
/
lexer.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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
%{
#include <stdio.h>
#include "parser.h"
#include "symbol_table.h"
int linenumber = 1;
int columnnumber = 1;
int curr_line = 1;
int assignment_flag = 0;
int for_loop_flag = 0;
int function_flag = 0;
int inside_loop = 0;
int brackets = 0;
void yyerror(char* s);
int yylex();
static void updating_column()
{
char* s;
yylloc.first_line = curr_line;
yylloc.first_column = columnnumber;
for(s = yytext; *s != '\0'; s++)
{
if(*s == '\n')
{
curr_line += 1;
columnnumber = 0;
}
else
{
columnnumber += 1;
}
}
yylloc.last_line = curr_line;
yylloc.last_column = columnnumber - 1;
}
#define YY_USER_ACTION updating_column();
%}
%option noyywrap
%x comment
whitespace (\t|" "|\r)
newline (\n)
braces ("{"|"}")
parenthesis ("("|")")
squares ("["|"]")
integer ([0-9]+|(("0x"|"0X")[a-fA-F0-9]+))
bool ("true"|"false")
char ("'"([ -~]|\t|\n|\r|\0)"'")
string (\"(\\.|[^"\\])*\")
datatype ("int"|"bool"|"char"|"uint8")
conditional ("if"|"elif"|"else")
loops ("for"|"in"|"while"|"break"|"continue")
arithmetic_operators ("+"|"-"|"*"|"/"|"%")
relational_operators (">"|">="|"<"|"<="|"=="|"!=")
bitwise_operators ("~"|"&"|"|"|"^"|">>"|"<<")
logical_operators ("not"|"and"|"or")
function ("return"|"void"|"def")
assignment_operator ([:][=])
io ("digital_read"|"digital_write"|"delay"|"pwm"|"start_counter"|"stop_counter"|"read_counter"|"init_message_channel"|"receive_message"|"send_message"|"send_int"|"send_char"|"send_bool"|"send_ints"|"send_chars"|"send_bools")
print ("print"|"println")
identifier ([a-zA-Z_][a-zA-Z0-9_]*)
terminal ([;])
separator ([:])
comma ([,])
%%
{whitespace} {
;
}
{newline} {
linenumber++;
}
{braces} {
if (!strcmp(yytext, "{"))
{
if (for_loop_flag == -1)
{
for_loop_flag = 0;
}
else if (function_flag == -1)
{
function_flag = 0;
}
else if (for_loop_flag == 0)
{
increment_scope();
}
if (inside_loop)
{
brackets += 1;
}
return LBRACE;
}
else if (!strcmp(yytext, "}"))
{
decrement_scope();
if (inside_loop)
{
brackets -= 1;
if (brackets == 0)
{
inside_loop = 0;
}
}
return RBRACE;
}
}
{parenthesis} {
if (!strcmp(yytext, "("))
{
return LPAREN;
}
else if (!strcmp(yytext, ")"))
{
return RPAREN;
}
}
{squares} {
if (!strcmp(yytext, "["))
{
return LSQUARE;
}
else if (!strcmp(yytext, "]"))
{
return RSQUARE;
}
}
{integer} {
/*octal if first digit is 0, hexadecimal if first two characters are 0x or 0X, decimal otherwise*/
yylval.integer = strtoll(yytext, NULL, 0);
return CONST_INT;
}
{bool} {
if (!strcmp(yytext, "true"))
{
yylval.boolean = 1;
}
else if (!strcmp(yytext, "false"))
{
yylval.boolean = 0;
}
return CONST_BOOL;
}
{char} {
yylval.integer = (int)yytext[1];
return CONST_CHAR;
}
{string} {
yylval.string = strdup(yytext);
return CONST_STRING;
}
{datatype} {
assignment_flag = 1;
if (!strcmp(yytext, "int"))
{
return DT_INT;
}
else if (!strcmp(yytext, "bool"))
{
return DT_BOOL;
}
else if (!strcmp(yytext, "char"))
{
return DT_CHAR;
}
else if (!strcmp(yytext, "uint8"))
{
return DT_CHAR;
}
}
{conditional} {
if (!strcmp(yytext, "if"))
{
return KW_IF;
}
else if (!strcmp(yytext, "elif"))
{
return KW_ELIF;
}
else if (!strcmp(yytext, "else"))
{
return KW_ELSE;
}
}
{loops} {
if (!strcmp(yytext, "for"))
{
for_loop_flag = 1;
inside_loop = 1;
increment_scope();
return KW_FOR;
}
else if (!strcmp(yytext, "in"))
{
return KW_IN;
}
else if (!strcmp(yytext, "while"))
{
inside_loop = 1;
return KW_WHILE;
}
else if (!strcmp(yytext, "break"))
{
if (!inside_loop)
{
yyerror("cannot call break outside loops");
}
return KW_BREAK;
}
else if (!strcmp(yytext, "continue"))
{
if (!inside_loop)
{
yyerror("cannot call break outside loops");
}
return KW_CONTINUE;
}
}
{arithmetic_operators} {
if (!strcmp(yytext,"+"))
{
return OPR_ADD;
}
else if (!strcmp(yytext,"-"))
{
return OPR_SUB;
}
else if (!strcmp(yytext,"*"))
{
return OPR_MUL;
}
else if (!strcmp(yytext,"/"))
{
return OPR_DIV;
}
else if (!strcmp(yytext, "%"))
{
return OPR_MOD;
}
}
{relational_operators} {
if (!strcmp(yytext,">"))
{
return OPR_GT;
}
else if (!strcmp(yytext, ">="))
{
return OPR_GE;
}
else if (!strcmp(yytext,"<"))
{
return OPR_LT;
}
else if (!strcmp(yytext, "<="))
{
return OPR_LE;
}
else if (!strcmp(yytext,"=="))
{
return OPR_EQ;
}
else if (!strcmp(yytext, "!="))
{
return OPR_NE;
}
}
{bitwise_operators} {
if (!strcmp(yytext, "~"))
{
return OPR_BW_NOT;
}
else if (!strcmp(yytext, "&"))
{
return OPR_BW_AND;
}
else if (!strcmp(yytext, "|"))
{
return OPR_BW_OR;
}
else if (!strcmp(yytext, "^"))
{
return OPR_BW_XOR;
}
else if (!strcmp(yytext, "<<"))
{
return OPR_BW_LFT;
}
else if (!strcmp(yytext, ">>"))
{
return OPR_BW_RGT;
}
}
{logical_operators} {
if (!strcmp(yytext, "not"))
{
return OPR_LGL_NOT;
}
else if (!strcmp(yytext, "and"))
{
return OPR_LGL_AND;
}
else if (!strcmp(yytext, "or"))
{
return OPR_LGL_OR;
}
}
{function} {
if (!strcmp(yytext, "return"))
{
return KW_RETURN;
}
else if (!strcmp(yytext, "void"))
{
return DT_VOID;
}
else if (!strcmp(yytext, "def"))
{
function_flag = 1;
return KW_DEF;
}
}
{assignment_operator} {
return OPR_ASSIGNMENT;
}
{io} {
if (!strcmp(yytext, "digital_write"))
{
return KW_DIGITAL_WRITE;
}
else if (!strcmp(yytext, "digital_read"))
{
return KW_DIGITAL_READ;
}
else if (!strcmp(yytext, "delay"))
{
return KW_DELAY;
}
else if (!strcmp(yytext, "pwm"))
{
return KW_PWM;
}
else if (!strcmp(yytext, "start_counter"))
{
return KW_START_COUNTER;
}
else if (!strcmp(yytext, "stop_counter"))
{
return KW_STOP_COUNTER;
}
else if (!strcmp(yytext, "read_counter"))
{
return KW_READ_COUNTER;
}
else if (!strcmp(yytext, "init_message_channel"))
{
return KW_INIT_RPMSG;
}
else if (!strcmp(yytext, "receive_message"))
{
return KW_RECV_RPMSG;
}
else if (!strcmp(yytext, "send_message"))
{
return KW_SEND_RPMSG_INT;
}
else if (!strcmp(yytext, "send_int"))
{
return KW_SEND_RPMSG_INT;
}
else if (!strcmp(yytext, "send_char"))
{
return KW_SEND_RPMSG_CHAR;
}
else if (!strcmp(yytext, "send_bool"))
{
return KW_SEND_RPMSG_BOOL;
}
else if (!strcmp(yytext, "send_ints"))
{
return KW_SEND_RPMSG_INTS;
}
else if (!strcmp(yytext, "send_chars"))
{
return KW_SEND_RPMSG_CHARS;
}
else if (!strcmp(yytext, "send_bools"))
{
return KW_SEND_RPMSG_BOOLS;
}
}
{print} {
if (!strcmp(yytext, "print"))
{
return KW_PRINT;
}
else if (!strcmp(yytext, "println"))
{
return KW_PRINTLN;
}
}
{identifier} {
yylval.symbol_handle = NULL;
if (function_flag == 1)
{
if (insert_symbol_table(yytext, DT_UNDEF, 1, 1) == 1)
{
yylval.symbol_handle = lookup_symbol_table(yytext, get_scope());
vec_init(&yylval.symbol_handle->params);
}
function_flag = -1;
increment_scope();
return IDENTIFIER;
}
if (assignment_flag == 1 || for_loop_flag == 1)
{
if (insert_symbol_table(yytext, DT_UNDEF, 1, 0) == 1)
{
yylval.symbol_handle = lookup_symbol_table(yytext, get_scope());
assignment_flag = 0;
if (for_loop_flag == 1)
{
for_loop_flag = -1;
}
}
}
else if (assignment_flag == 0)
{
int temp = for_loop_flag == -1 ? -1 : 0;
yylval.symbol_handle = lookup_symbol_table(yytext, get_scope() + temp);
if (yylval.symbol_handle != NULL && yylval.symbol_handle->data_type == DT_INTEGER)
{
return INT_IDENTIFIER;
}
else if (yylval.symbol_handle != NULL && yylval.symbol_handle->data_type == DT_BOOLEAN)
{
return BOOL_IDENTIFIER;
}
else if (yylval.symbol_handle != NULL && yylval.symbol_handle->data_type == DT_VOID_)
{
return VOID_IDENTIFIER;
}
else if (yylval.symbol_handle != NULL && yylval.symbol_handle->data_type == DT_CHAR_)
{
return CHAR_IDENTIFIER;
}
else if (yylval.symbol_handle != NULL && yylval.symbol_handle->data_type == DT_INT_ARR)
{
return INT_ARR_IDENTIFIER;
}
else if (yylval.symbol_handle != NULL && yylval.symbol_handle->data_type == DT_CHAR_ARR)
{
return CHAR_ARR_IDENTIFIER;
}
else if (yylval.symbol_handle != NULL && yylval.symbol_handle->data_type == DT_BOOL_ARR)
{
return BOOL_ARR_IDENTIFIER;
}
}
return IDENTIFIER;
}
{terminal} {
return SEMICOLON;
}
{separator} {
return COLON;
}
{comma} {
return COMMA;
}
"/*" {BEGIN(comment); }
<comment>"*/" {BEGIN(INITIAL); }
<comment>\n {linenumber++; }
<comment>. ;
%%