-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathirlexer.l
127 lines (103 loc) · 3.09 KB
/
irlexer.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
%{
#include "irparser.h"
#include "irvm.h"
#include "error.h"
/* handle locations */
int yycolumn = 1;
#define MAX_LITERAL 32768
char strbuf[MAX_LITERAL];
int strpos;
#define string_append(c) do { \
if (strpos >= MAX_LITERAL) \
errl((struct location *)yylloc, 1, "literal too large"); \
strbuf[strpos++] = (c); \
} while (0)
#define YY_USER_ACTION yylloc->first_line = yylloc->last_line = yylineno; \
yylloc->first_column = yycolumn; yylloc->last_column = yycolumn+yyleng-1; \
yycolumn += yyleng;
%}
%option bison-bridge
%option bison-locations
%option yylineno
%option noyywrap
%option noinput
%option nounput
%x LITERAL
%x COMMENT
%%
[ \t]+ {};
\n|\r\n {yycolumn = 0;}
-?[0-9]+ {yylval->num = strtol(yytext, NULL, 10);; return integer; }
const {return CONST;}
name {return NAME;}
temp {return TEMP;}
binop {return BINOP;}
mem {return MEM;}
call {return CALL;}
seq {return SEQ;}
eseq {return ESEQ;}
move {return MOVE;}
sxp {return SXP;}
jump {return JUMP;}
cjump {return CJUMP;}
label {return LABEL;}
call[ \t]+end { return CALLEND;}
seq[ \t]+end { return SEQEND;}
"(+)"|"add" {yylval->num = ADD; return op;}
"(-)"|"sub" {yylval->num = SUB; return op;}
"(*)"|"mul" {yylval->num = MUL; return op;}
"(/)"|"div" {yylval->num = DIV; return op;}
"(%)"|"mod" {yylval->num = MOD; return op;}
"(<=)"|"le" {yylval->num = LE; return rop;}
"(>=)"|"ge" {yylval->num = GE; return rop;}
"(<>)"|"ne" {yylval->num = NE; return rop;}
"(<)"|"lt" {yylval->num = LT; return rop;}
"(>)"|"gt" {yylval->num = GT; return rop;}
"(=)"|"eq" {yylval->num = EQ; return rop;}
[$A-Za-z0-9_]+ {yylval->str = name2id(yytext); return string;}
\" {strpos = 0; BEGIN(LITERAL); }
<LITERAL>{
\n|\r\n {}
/* \" and \\ */
"\\\"" {string_append((char)0x22);}
"\\\\" {string_append((char)0x5c);}
/* C escape characters */
\\[aA] {string_append((char)0x07);}
\\[bB] {string_append((char)0x08);}
\\[tT] {string_append((char)0x09);}
\\[nN] {string_append((char)0x0a);}
\\[vV] {string_append((char)0x0b);}
\\[fF] {string_append((char)0x0c);}
\\[rR] {string_append((char)0x0d);}
/* \OOO and \xXX ascii escapes */
\\[0-3][0-7][0-7] {
/* Convert the octal characters to an integer */
char c = (char) strtol(yytext+1, NULL, 8);
string_append(c);
}
\\x([0-9a-fA-F]{2}) {
/* Convert the hex characters to an integer */
char c = (char) strtol(yytext+1, NULL, 16);
string_append(c);
}
/* All other characters except " and \ are accepted */
[^\\\"] {string_append(yytext[0]);}
/* end of string */
"\"" {
BEGIN(INITIAL);
string_append(0);
yylval->str = strdup(strbuf);
return literal;
}
<<EOF>> {
warnl((struct location *)yylloc, "unterminated string");
return 0;
}
}
"/*" {BEGIN(COMMENT);}
<COMMENT>"\n" {yycolumn = 1;}
<COMMENT>"*/" {BEGIN(INITIAL);}
<COMMENT>. {}
"#"[^\n]* {}
. { errl((struct location *)yylloc, 1, "unexpected token <%s>", yytext); };
%%