-
Notifications
You must be signed in to change notification settings - Fork 0
/
15_A5.l
63 lines (50 loc) · 1.24 KB
/
15_A5.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
%{
#include <string.h>
#include <stdlib.h>
#ifndef NO_YY
#include "15_A5_translator.h"
#include "15_A5.tab.h"
#endif
%}
IDENT [a-zA-Z\_][0-9a-zA-Z\_]*
ESCAPE \\'|\\\?|\\\"|\\\\|\\a|\\b|\\f|\\n|\\r|\\t|\\v
CHAR [^\\'\n]|{ESCAPE}
CHARCONST \'{CHAR}+\'
INTCONST [0-9]*
STRCHAR [^\"\\\n]|{ESCAPE}
STRLIT \"{STRCHAR}*\"
COMMENTSINGLE \/\/([^\n])*\n
COMMENTMULTI \/\*([^\*]|\*[^\/])*\*\/
%%
"void" { return VOID; }
"char" { return CHAR; }
"int" { return INT; }
"if" { return IF; }
"else" { return ELSE; }
"for" { return FOR; }
"return" { return RETURN; }
{IDENT} { yylval.string = strdup(yytext); return IDENTIFIER;}
{INTCONST} { yylval.val = atoi(yytext); return INTCONST; }
/* TODO: Account for multibyte character constants (shift and accumulate) */
{CHARCONST} {
yylval.val = (int) yytext[1]; return CHARCONST;
}
{STRLIT} {
size_t len = strlen(yytext);
char *val = malloc(len - 1);
memcpy(val, yytext + 1, len - 2);
val[len - 2] = '\0';
yylval.string = val; return STRING_LITERAL;
}
/* "->" { return ARROW; } */
">=" { return GEQ; }
"<=" { return LEQ; }
"==" { return CEQ; }
"!=" { return NEQ; }
"&&" { return LAND; }
"||" { return LOR; }
{COMMENTSINGLE}|{COMMENTMULTI} {}
[ \n\t\r]* {}
. { return yytext[0]; }
%%
int yywrap() { return 1; }