-
Notifications
You must be signed in to change notification settings - Fork 5
/
flex_tokens.l
552 lines (505 loc) · 15.8 KB
/
flex_tokens.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/**
* Lexer for C Programming Language (ISO/IEC 9899:2018).
*
* @author: Denis Chernikov, Vladislav Kuleykin
*/
%pointer
%x COMMENT
%x PREP
%x INCL_FL
%x INCL_ST
%x ERROR_S
%x WARNING
%x CHR
%x STR
%{
#include <string.h>
#include "alloc_wrap.h"
#include "typedef_name.h"
#include "ast.h"
#include "string_tools.h"
#include "y.tab.h"
/// Token for the error notification.
#define ERROR 256
// Defined in `yacc_syntax.y'
extern int yyerror(const char *);
/// Print warning to user.
///
/// \param str Warning description to be printed
void yywarn(const char *);
/// Change input after EOF was reached.
/// NOTE: it is used by Flex.
///
/// \return 0 - new source is assigned, 1 - nothing more to read
int yywrap();
/// Change source file to read next.
///
/// \param name Name of new source file
void change_source(char *name);
/// Skip last `n' symbols and retry reading of a previous literal.
///
/// \param n Number of symbols to be dropped
void shift_yytext(int n);
/// Convert constant value to the corresponding AST node.
///
/// \param type Type of a new node
/// \param val Constant to put as content
/// \return New AST node for a given constant
AST_NODE *get_const_node(AST_NODE_TYPE type, char *val);
/// Is given character - trigraph suffix?
///
/// \param c Character to be checked
/// \return `true' - it is one of trigraph endings, `false' - otherwise
_Bool is_trigraph_suf(char c);
/// Expand escapes in string literal from `yytext'. NOTE: Needs to be freed.
///
/// \return Expanded string
char *readstr();
// ISO/IEC 9899:2017, 5.2.4.1 Translation limits, page 20
/// Maximum depth of the `#include' directive.
#define MAX_INCLUDE_DEPTH 15
/// Current source configuration.
typedef struct
{
FILE *file;
YY_BUFFER_STATE buffer;
int start_cond;
}
config;
/// Stack of source configurations.
config config_stack[MAX_INCLUDE_DEPTH];
/// Size of the stack of source configurations.
int file_stack_ptr = 0;
%}
O [0-7]
D [0-9]
H [0-9A-Fa-f]
ND [A-Za-z_]
DE [Ee][+-]?{D}+
HE [Pp][+-]?{D}+
LS L|l|LL|ll
IS [Uu]{LS}?|{LS}[Uu]?
FS [FfLl]
HASH "??="|"#"
LBRACKET "??("|"["
BS "??/"|"\\"
RBRACKET "??)"|"]"
CARET "??'"|"^"
LBRACE "??<"|"{"
VERTICAL "??!"|"|"
RBRACE "??>"|"}"
TILDE "??-"|"~"
PR_INS [ \t]+[^\n\r]*
NLE {BS}(\n|\r|\r\n)
UCN {BS}(u{H}{4}|U{H}{8})
ID {ND}({ND}|{D}|{UCN})*
ESC {BS}['"?\\abfnrtv]|{BS}"??/"|{BS}{O}{1,3}|{BS}x{H}+|{UCN}
CHAR [LUu]?'({ESC}|[^'\\\n\r])+'
STRL ([LUu]|u8)?\"({ESC}|[^"\\\n\r])+\"
WS [ \f\n\r\t\v]
%%
^[ \t]*({HASH}|"%:")[ \t]* { BEGIN PREP; }
<PREP>"if"{PR_INS} {
BEGIN INITIAL;
yywarn("Everything inside `#if' will be processed "
"considering condition as true! Syntax error may occur.");
}
<PREP>"ifdef"{PR_INS} {
BEGIN INITIAL;
yywarn("Everything inside `#ifdef' will be processed "
"considering pointed one as defined! Syntax error may occur.");
}
<PREP>"ifndef"{PR_INS} {
BEGIN INITIAL;
yywarn("Everything inside `#ifndef' will be processed "
"considering pointed one as not defined! Syntax error may occur.");
}
<PREP>"elif"{PR_INS} {
BEGIN INITIAL;
yywarn("Everything inside `#elif' will be processed "
"considering condition as true! Syntax error may occur.");
}
<PREP>"else"[^\n\r]*$ {
BEGIN INITIAL;
yywarn("Everything inside `#else' will be processed "
"considering previous condition as false! Syntax error may occur.");
}
<PREP>"endif"[^\n\r]*$ { BEGIN INITIAL; }
<PREP>"include"[ \t]*\" { BEGIN INCL_FL; }
<PREP>"include"[ \t]*< { BEGIN INCL_ST; }
<INCL_ST>[^>\n\r]*/> {
add_std_typedef(yytext);
yywarn("Standard libraries included will not be considered by lexer!\n"
"Only `typedef-name's described in ISO/IEC 9899:2018.");
}
<INCL_ST>[^>\n\r]*$ {
BEGIN INITIAL;
yyerror("Preprocessing error: Include name does not have a closing quote.");
}
<INCL_ST>>[ \t]*$ { BEGIN INITIAL; }
<INCL_FL>[^"\n\r]*/\" { change_source(yytext); }
<INCL_FL>[^"\n\r]*$ {
BEGIN INITIAL;
yyerror("Preprocessing error: Include name does not have a closing quote.");
}
<INCL_FL>\"[ \t]*$ { BEGIN INITIAL; }
<PREP>"define"{PR_INS} {
BEGIN INITIAL;
yywarn("Everything defined by `#define' will be processed "
"considering no `#define' was used! Syntax error may occur.");
}
<PREP>"undef"{PR_INS} { BEGIN INITIAL; }
<PREP>"line"{PR_INS} { BEGIN INITIAL; /* TODO change source notification */ }
<PREP>"error"{WS}* { BEGIN ERROR_S; }
<ERROR_S>[^\n\r]*$ {
BEGIN INITIAL;
yyerror(yytext);
}
<PREP>"warning"{WS}* { BEGIN WARNING; /* not in ISO/IEC 9899:2017 */ }
<WARNING>[^\n\r]*$ {
BEGIN INITIAL;
yywarn(yytext);
}
<PREP>"pragma"{PR_INS} { BEGIN INITIAL; /* TODO compiler pragmas */ }
<PREP>""$ {
BEGIN INITIAL;
if (yyleng > 0)
{
yyerror("Preprocessing error: Wrong preprocessing content found!");
}
}
<PREP>[^\n\r] { yymore(); }
"/"{NLE}?"/"({NLE}|[^\n\r])*$ { /* ignore inline comment */ }
"/"{NLE}?"*" { BEGIN COMMENT; }
<COMMENT>(.|\n)|"*"{NLE}? { /* ignore comment content */ }
<COMMENT>"*"{NLE}?"/" { BEGIN INITIAL; }
"auto" { return AUTO; }
"break" { return BREAK; }
"case" { return CASE; }
"char" { return CHAR; }
"const" { return CONST; }
"continue" { return CONTINUE; }
"default" { return DEFAULT; }
"do" { return DO; }
"double" { return DOUBLE; }
"else" { return ELSE; }
"enum" { return ENUM; }
"extern" { return EXTERN; }
"float" { return FLOAT; }
"for" { return FOR; }
"goto" { return GOTO; }
"if" { return IF; }
"inline" { return INLINE; }
"int" { return INT; }
"long" { return LONG; }
"register" { return REGISTER; }
"restrict" { return RESTRICT; }
"return" { return RETURN; }
"short" { return SHORT; }
"signed" { return SIGNED; }
"sizeof" { return SIZEOF; }
"static" { return STATIC; }
"struct" { return STRUCT; }
"switch" { return SWITCH; }
"typedef" { return TYPEDEF; }
"union" { return UNION; }
"unsigned" { return UNSIGNED; }
"void" { return VOID; }
"volatile" { return VOLATILE; }
"while" { return WHILE; }
"_Alignas" { return ALIGNAS; }
"_Alignof" { return ALIGNOF; }
"_Atomic" { return ATOMIC; }
"_Bool" { return BOOL; }
"_Complex" { return COMPLEX; }
"_Generic" { return GENERIC; }
"_Imaginary" { return IMAGINARY; }
"_Noreturn" { return NORETURN; }
"_Static_assert" { return STATIC_ASSERT; }
"_Thread_local" { return THREAD_LOCAL; }
{ID} {
yylval.node = get_const_node(Identifier, alloc_const_str(yytext));
if (is_typedef_name(yytext)) return TYPEDEF_NAME;
return IDENTIFIER;
// TODO check Universal character name, ISO/IEC 9899:2017, page 44
}
0[Xx]{H}+{IS}? |
0{O}+{IS}? |
{D}+{IS}? {
yylval.node = get_const_node(IntegerConstant, alloc_const_str(yytext));
return CONSTANT;
// TODO value conversion, ISO/IEC 9899:2017, page 45-46
}
{D}+{DE}{FS}? |
{D}*"."{D}+{DE}?{FS}? |
{D}+"."{D}*{DE}?{FS}? |
0[Xx]{H}+{HE}{FS}? |
0[Xx]{H}*"."{H}+{HE}?{FS}? |
0[Xx]{H}+"."{H}*{HE}?{FS}? {
yylval.node = get_const_node(FloatingConstant, alloc_const_str(yytext));
return CONSTANT;
// TODO value conversion, ISO/IEC 9899:2017, page 47-48
}
[LUu]?' {
BEGIN CHR;
/* TODO prefix considering, ISO/IEC 9899:2017, page 48-50 */
}
(L|U|u|u8)?\" {
BEGIN STR;
/* TODO prefix considering, ISO/IEC 9899:2017, page 50-52 */
}
<CHR>' {
BEGIN INITIAL;
char *lit = readstr();
if (!lit || strlen(lit) != 1)
{
free(lit);
return ERROR; // TODO error message
}
yylval.node = get_const_node(CharacterConstant, lit);
return CONSTANT;
// TODO value conversion, UTF-8, ISO/IEC 9899:2017, page 50-52
}
<STR>\" {
BEGIN INITIAL;
char *lit = readstr();
if (!lit)
{
free(lit);
return ERROR; // TODO error message
}
yylval.node = get_const_node(StringLiteral, lit);
return STRING_LITERAL;
// TODO UTF-8, ISO/IEC 9899:2017, page 50-52
}
<STR>\"{WS}*(L|U|u|u8)?\" {
int i;
for (i = 2;; ++i)
{
if (yytext[yyleng - i] == '"') break;
}
shift_yytext(i); // Skip and retry
}
<STR,CHR>(\n|\r|\r\n) {
yymore();
return ERROR; // TODO error message, correct handling
}
<STR,CHR>{ESC} { yymore(); }
<STR,CHR>. { yymore(); }
{LBRACKET}|"<:" { return LBRACKET; }
{RBRACKET}|":>" { return RBRACKET; }
"(" { return LPAREN; }
")" { return RPAREN; }
{LBRACE}|"<%" { return LBRACE; }
{RBRACE}|"%>" { return RBRACE; }
"." { return DOT; }
"->" { return ARROW; }
"++" { return DBL_PLUS; }
"--" { return DBL_MINUS; }
"&" { return AMPERSAND; }
"*" { return ASTERISK; }
"+" { return PLUS; }
"-" { return MINUS; }
{TILDE} { return TILDE; }
"!" { return EXCLAMATION; }
"/" { return SLASH; }
"%" { return PERCENT; }
"<<" { return LSHIFT; }
">>" { return RSHIFT; }
"<" { return LS; }
">" { return GR; }
"<=" { return LE; }
">=" { return GE; }
"==" { return EQ; }
"!=" { return NE; }
{CARET} { return CARET; }
{VERTICAL} { return VERTICAL; }
"&&" { return LOG_AND; }
{VERTICAL}{VERTICAL} { return LOG_OR; }
"?" { return QUESTION; }
":" { return COLON; }
";" { return SEMICOLON; }
"..." { return ELLIPSIS; }
"=" { return ASSIGN; }
"*=" { return MUL_ASSIGN; }
"/=" { return DIV_ASSIGN; }
"%=" { return MOD_ASSIGN; }
"+=" { return ADD_ASSIGN; }
"-=" { return SUB_ASSIGN; }
"<<=" { return LEFT_ASSIGN; }
">>=" { return RIGHT_ASSIGN; }
"&=" { return AND_ASSIGN; }
{CARET}= { return XOR_ASSIGN; }
{VERTICAL}= { return OR_ASSIGN; }
"," { return COMMA; }
<INITIAL,PREP,STR,CHR>[^ \f\n\r\t\v]*"??/"\r\n {
shift_yytext(5); // skip and retry
}
<INITIAL,PREP,STR,CHR>[^ \f\n\r\t\v]*"??/"[\r\n] {
shift_yytext(4); // skip and retry
}
<INITIAL,PREP,STR,CHR>[^ \f\n\r\t\v]*\\\r\n {
shift_yytext(3); // skip and retry
}
<INITIAL,PREP,STR,CHR>[^ \f\n\r\t\v]*\\[\n\r] {
shift_yytext(2); // skip and retry
}
<COMMENT,STR,CHR><<EOF>> { return ERROR; /* TODO error message */ }
{WS} { /* skip over whitespaces */ }
. { return ERROR; /* TODO error message */ }
%%
void yywarn(const char *str)
{
fprintf(stderr, "WARNING: %s\n", str);
}
int yywrap()
{
if (--file_stack_ptr < 0) return 1;
yy_delete_buffer(YY_CURRENT_BUFFER);
int res = fclose(yyin);
if (res == EOF)
{
fprintf(stderr, "Cannot close opened source file!\n");
exit(3);
}
config *old_conf = &config_stack[file_stack_ptr];
yyin = old_conf->file;
yy_switch_to_buffer(old_conf->buffer);
BEGIN old_conf->start_cond;
return 0;
}
void change_source(char *name)
{
if (file_stack_ptr >= MAX_INCLUDE_DEPTH)
{
fprintf(stderr,
"Includes nested too deeply (more than %d)\n", MAX_INCLUDE_DEPTH);
exit(1);
}
FILE *new_file = fopen(name, "r");
if (!new_file)
{
fprintf(stderr, "Cannot open for reading: %s\n", name);
exit(3);
}
config_stack[file_stack_ptr++] = (config) {yyin, YY_CURRENT_BUFFER, YY_START};
yyin = new_file;
yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
BEGIN INITIAL;
}
void shift_yytext(int n)
{
int i;
for (i = yyleng - n - 1; i >= 0; --i)
{
unput(yytext[i]);
}
}
AST_NODE *get_const_node(AST_NODE_TYPE type, char *val)
{
AST_NODE *res = ast_create_node(type, (AST_CONTENT) {.value = val}, 0);
return res;
}
_Bool is_trigraph_suf(char c)
{
return c == '=' || c == '(' || c == '/' || c == ')'
|| c == '\'' || c == '<' || c == '!' || c == '>' || c == '-';
}
char *readstr()
{
size_t i = 0, j = 0;
char to_put;
char *res = (char *) my_malloc(sizeof(char) * (yyleng + 1),
"STRING_LITERAL");
while (i < yyleng - 1)
{
if (yytext[i] == '\\' || yytext[i] == '?' && yytext[i+1] == '?' && yytext[i+2] == '/')
{
++i;
if (yytext[i-1] != '\\') i += 2;
if (i == yyleng - 1) return NULL;
switch (yytext[i])
{
case '?':
if (yytext[i+1] == '?' && is_trigraph_suf(yytext[i+2]))
{
i += 2;
if (yytext[i] == '/')
{
to_put = '\\';
}
else
{
return NULL;
}
}
else
{
to_put = '?';
}
break;
case '\'': to_put = '\''; break;
case '\"': to_put = '\"'; break;
case '\\': to_put = '\\'; break;
case 'a': to_put = '\a'; break;
case 'b': to_put = '\b'; break;
case 'f': to_put = '\f'; break;
case 'n': to_put = '\n'; break;
case 'r': to_put = '\r'; break;
case 't': to_put = '\t'; break;
case 'v': to_put = '\v'; break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
to_put = '\x1A'; // TODO: up to 3 octal
break;
case 'x':
++i;
to_put = '\x1A'; // TODO: closest hexes
break;
case 'u':
++i;
to_put = '\x1A'; // TODO: 4 hexes
i += 3;
break;
case 'U':
++i;
to_put = '\x1A'; // TODO: 8 hexes
i += 7;
break;
default:
return NULL;
}
}
else if (yytext[i] == '?' && yytext[i+1] == '?' && is_trigraph_suf(yytext[i+2]))
{
i += 2;
switch (yytext[i])
{
case '=': to_put = '#'; break;
case '(': to_put = '['; break;
case ')': to_put = ']'; break;
case '\'': to_put = '^'; break;
case '<': to_put = '{'; break;
case '!': to_put = '|'; break;
case '>': to_put = '}'; break;
case '-': to_put = '~'; break;
default: return NULL;
}
}
else
{
to_put = yytext[i];
}
res[j] = to_put;
++i;
++j;
}
res[j] = '\0';
return res;
}