-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexical.l
204 lines (182 loc) · 5.08 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
%option yylineno
%option noyywrap
%{
#include "node.h"
#include "syntax.tab.h"
#include "cmm-strtab.h"
#include <stdio.h>
#include <stdlib.h>
extern int is_syn_error;
void ErrorMsg(char *desc, char *lexeme, int lineno);
int yycolumn = 1;
// To support netsed multi-line comment, use this counter
// to record how many '/*' there are that haven't met a '*/'.
// This in deed is not a DFA way ;-)
// Note that we ignore '/*' and '*/' after '//' in a line.
static int n_unpaired = 0;
#define YY_USER_ACTION \
yylloc.first_line = yylloc.last_line = yylineno;\
yylloc.first_column = yycolumn;\
yylloc.last_column = yycolumn + yyleng - 1;\
yycolumn += yyleng;
static enum yytokentype num(int radix);
static enum yytokentype ascii();
static enum yytokentype op(enum yytokentype type);
static enum yytokentype sym(enum yytokentype type);
static enum yytokentype node(enum yytokentype type, enum ProductionTag tag);
static int check_oct(char *yytext, int yyleng, int yylineno);
static int check_hex(char *yytext, int yyleng, int yylineno);
%}
%x ONE_LINE_COMMENT MULTI_LINE_COMMENT ONE_LINE_IN_MULTI_LINE
digit [0-9]
letter [a-zA-Z]
%%
"=" return sym(ASSIGNOP);
"," return sym(COMMA);
";" return sym(SEMI);
"." return sym(DOT);
"(" return sym(LP);
")" return sym(RP);
"[" return sym(LB);
"]" return sym(RB);
"{" return sym(LC);
"}" return sym(RC);
"if" return sym(IF);
"for" return sym(FOR);
"else" return sym(ELSE);
"while" return sym(WHILE);
"struct" return sym(STRUCT);
"return" return sym(RETURN);
"+" return op(PLUS);
"-" return op(MINUS);
"*" return op(STAR);
"/" return op(DIV);
"!" return op(NOT);
"||" return op(OR);
"&&" return op(AND);
>|<|>=|<=|==|!= return op(RELOP);
"int"|"float"|"char" return node(TYPE, TERM_ID);
({letter}|_)({letter}|{digit}|_)* return node(ID, TERM_ID);
[0-9]+\.[0-9]+ {
yylval.nd = new_node();
yylval.nd->lineno = yylineno;
yylval.nd->tag = TERM_FLOAT;
yylval.nd->val.f = atof(yytext);
return FLOAT;
}
0(x|X)({digit}|{letter})+ {
if (!check_hex(yytext, yyleng, yylineno)) {
is_syn_error = 1;
}
return num(16);
}
0[0-9]+ {
if (!check_oct(yytext, yyleng, yylineno)) {
is_syn_error = 1;
}
return num(8);
}
0|[1-9][0-9]* return num(10);
\'.\' return ascii();
\'\\.\' return ascii();
"//" { BEGIN ONE_LINE_COMMENT; }
"/*" {
n_unpaired++;
BEGIN MULTI_LINE_COMMENT;
}
"\n" yycolumn = 1;
[[:blank:]] ;
. {
ErrorMsg("Mysterious character", yytext, yylineno);
is_syn_error = 1;
}
<MULTI_LINE_COMMENT>"/*" { n_unpaired++; }
<MULTI_LINE_COMMENT>"//" { BEGIN ONE_LINE_IN_MULTI_LINE; }
<MULTI_LINE_COMMENT>"*/" {
n_unpaired--;
if (n_unpaired == 0) {
BEGIN INITIAL;
}
}
<MULTI_LINE_COMMENT>"\n" ;
<MULTI_LINE_COMMENT>. ;
<ONE_LINE_IN_MULTI_LINE>"\n" { BEGIN MULTI_LINE_COMMENT; }
<ONE_LINE_IN_MULTI_LINE>. ;
<ONE_LINE_COMMENT>"\n" { BEGIN INITIAL; }
<ONE_LINE_COMMENT>. ;
%%
static enum yytokentype num(int radix)
{
yylval.nd = new_node();
yylval.nd->lineno = yylineno;
yylval.nd->tag = TERM_INT;
yylval.nd->val.i = strtoul(yytext, NULL, radix);
return INT;
}
static enum yytokentype ascii()
{
yylval.nd = new_node();
yylval.nd->lineno = yylineno;
yylval.nd->tag = TERM_CHAR;
if(yytext[1] != '\\'){
yylval.nd->val.i = yytext[1];
} else {
if(yytext[2] == 'n') yylval.nd->val.i = 0x0a;
else if(yytext[2] == 't') yylval.nd->val.i = 0x09;
else if(yytext[2] == '\\') yylval.nd->val.i = 0x5c;
else if(yytext[2] == '\0') yylval.nd->val.i = 0x00;
else yylval.nd->val.i = yytext[2];
}
return CHAR;
}
static enum yytokentype sym(enum yytokentype type)
{
yylval.lineno = yylineno;
return type;
}
static enum yytokentype op(enum yytokentype type)
{
yylval.op.lineno = yylineno;
yylval.op.s = register_str(yytext);
return type;
}
static enum yytokentype node(enum yytokentype type, enum ProductionTag tag)
{
yylval.nd = new_node();
yylval.nd->lineno = yylineno;
yylval.nd->val.s = register_str(yytext);
yylval.nd->tag = tag;
return type;
}
int check_oct(char *yytext, int yyleng, int yylineno)
{
// all zero here is considered oct
int i = 0;
for (; i < yyleng; i++) {
char ch = yytext[i];
if (ch == '8' || ch == '9') {
ErrorMsg("Illegal octal number", yytext, yylineno);
is_syn_error = 1;
return 0;
}
}
return 1;
}
int check_hex(char *yytext, int yyleng, int yylineno)
{
// Jump over '0x'
int i = 2;
for (; i < yyleng; i++) {
char ch = yytext[i];
if ((ch > 'f' && ch <= 'z') || (ch > 'F' && ch <= 'Z')) {
ErrorMsg("Illegal hexadecimal number", yytext, yylineno);
is_syn_error = 1;
return 0;
}
}
return 1;
}
void ErrorMsg(char *desc, char *lexeme, int lineno)
{
printf("Error type A at Line %d: %s '%s'\n", lineno, desc, lexeme);
}