-
Notifications
You must be signed in to change notification settings - Fork 0
/
myc.l
182 lines (163 loc) · 5.91 KB
/
myc.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
%option yylineno
%option yymore
ALP [a-zA-Z]
DIG [0-9]
%{
/* Please read README for more information */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "y.tab.h"
#include "def.h"
#include "utility.h"
void ignore_a();
void ignore_b();
void common(int, char *);
int is_type_name();
%}
%%
"/*" { ignore_a(); }
"//" { ignore_b(); }
^#.*$ { }
"typedef" { common(0, "typedef"); return TYPEDEF_T; }
"void" { common(0, "void"); return VOID_T; }
"char" { common(0, "char"); return CHAR_T; }
"int" { common(0, "int"); return INT_T; }
"struct" { common(0, "struct"); return STRUCT_T; }
"union" { common(0, "union"); return UNION_T; }
"if" { common(1, "if"); return IF_C; }
"else" { common(1, "else"); return ELSE_C; }
"while" { common(1, "while"); return WHILE_C; }
"for" { common(1, "for"); return FOR_C; }
"continue" { common(1, "continue"); return CONTINUE_C; }
"break" { common(1, "break"); return BREAK_C; }
"return" { common(1, "return"); return RETURN_C; }
"sizeof" { common(2, "sizeof"); return SIZEOF_OP; }
({ALP}|_)({ALP}|_|{DIG})* { yylval.string = strdup(yytext); if(is_type_name()) { common(3, "TYPEDEF_NAME"); return TYPEDEF_NAME; } else {common(3, "IDENTIFIER"); return IDENTIFIER_O;} }
0[xX]({DIG}|[a-fA-F])+ { yylval.string = strdup(yytext); /*sscanf(yytext, "0%*[Xx]%x", &yylval.integer);*/ common(4, "INT_CONSTANT"); return INT_CONSTANT; }
0{DIG}+ { yylval.string = strdup(yytext); /*sscanf(yytext, "0%o", &yylval.integer);*/ common(4, "INT_CONSTANT"); return INT_CONSTANT; }
{DIG}+ { yylval.string = strdup(yytext); /*yylval.integer = atoi(yytext);*/ common(4, "INT_CONSTANT"); return INT_CONSTANT; }
'([^\\']|\\.)+' { yylval.string = strdup(yytext); /*int i=0; yylval.character = 0; while(yytext[i]) { yylval.character <<= 8; yylval.character |= yytext[i]; i ++; }*/ common(5, "CHAR_CONSTANT"); return CHAR_CONSTANT; }
\"([^\\\"]|\\.)*\" { yylval.string = strdup(yytext); common(6, "STRING_CONSTANT"); return STRING_CONSTANT; }
"<<=" { common(7, "<<="); return SHL_ASSIGN; }
">>=" { common(7, ">>="); return SHR_ASSIGN; }
"*=" { common(7, "*="); return MUL_ASSIGN; }
"/=" { common(7, "/="); return DIV_ASSIGN; }
"%=" { common(7, "%="); return MOD_ASSIGN; }
"+=" { common(7, "+="); return ADD_ASSIGN; }
"-=" { common(7, "-="); return SUB_ASSIGN; }
"&=" { common(7, "&="); return AND_ASSIGN; }
"^=" { common(7, "^="); return XOR_ASSIGN; }
"|=" { common(7, "|="); return OR_ASSIGN; }
"||" { common(8, "||"); return OR_OP; }
"&&" { common(8, "&&"); return AND_OP; }
"==" { common(9, "=="); return EQ_OP; }
"!=" { common(9, "!="); return NE_OP; }
"<=" { common(9, "<="); return LE_OP; }
">=" { common(9, ">="); return GE_OP; }
"<<" { common(10, "<<"); return SHL_OP; }
">>" { common(10, ">>"); return SHR_OP; }
"++" { common(11, "++"); return INC_OP; }
"--" { common(11, "--"); return DEC_OP; }
"->" { common(12, "->"); return PTR_OP; }
"..." { common(13, "..."); return ELLIPSIS_O; }
"(" { common(14, "("); return '('; }
")" { common(14, ")"); return ')'; }
";" { common(14, ";"); return ';'; }
"," { common(14, ","); return ','; }
"=" { common(14, "="); return '='; }
"{" { common(14, "{"); return '{'; }
"}" { common(14, "}"); return '}'; }
"[" { common(14, "["); return '['; }
"]" { common(14, "]"); return ']'; }
"|" { common(14, "|"); return '|'; }
"^" { common(14, "^"); return '^'; }
"&" { common(14, "&"); return '&'; }
"<" { common(14, "<"); return '<'; }
">" { common(14, ">"); return '>'; }
"+" { common(14, "+"); return '+'; }
"-" { common(14, "-"); return '-'; }
"*" { common(14, "*"); return '*'; }
"/" { common(14, "/"); return '/'; }
"%" { common(14, "%"); return '%'; }
"~" { common(14, "~"); return '~'; }
"!" { common(14, "!"); return '!'; }
"." { common(14, "."); return '.'; }
[ \t\f\v\r\n] { common(15, ""); /* */ }
. { report_error(yylineno, "syntax error (l)", yytext); }
%%
int is_type_name()
{
int s, l, r;
int i;
int f;
s = yyleng;
yytext[s] = input();
for(i=s; yytext[i]; i++) if(!isspace(yytext[i])) break;
l = i;
for(; yytext[i]; i++)
{
if(!isalpha(yytext[i]) && !isdigit(yytext[i]) && yytext[i]!='_' && yytext[i]!='*')
{
break;
}
}
r = i;
f = yytext[s];
unput(yytext[s]);
yytext[s] = 0;
if(r-l > 0 && f != ';')
return tnhash_query(yytext);
else
return 0;
}
void ignore_a()
{
char t;
do {
while((t=input()) && t!='*');
if(t != 0) t = input();
} while(t!=0 && t!='/');
}
void ignore_b()
{
char t;
while((t=input()) && t!='\n');
}
void common(int typenum, char *str)
{
#ifdef OUTPUT_TOKENS
switch(typenum)
{
case 3:
fprintf(yyout, "<%s, ", str[0]=='T'?"type":"id");
fwrite(yytext, yyleng, 1, yyout);
fprintf(yyout, ">\n");
break;
case 4:
fprintf(yyout, "<number, ");
fwrite(yytext, yyleng, 1, yyout);
fprintf(yyout, ">\n");
break;
case 5:
fprintf(yyout, "<char, ");
fwrite(yytext, yyleng, 1, yyout);
fprintf(yyout, ">\n");
break;
case 6:
fprintf(yyout, "<string, ");
fwrite(yytext, yyleng, 1, yyout);
fprintf(yyout, ">\n");
break;
default:
if(typenum <= 14)
{
fwrite(yytext, yyleng, 1, yyout);
fprintf(yyout, "\n");
}
break;
}
//printf("((-----[%d,%s]-----))", typenum, str);
#endif
}