-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkscan.l
436 lines (389 loc) · 11.5 KB
/
kscan.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
%{
/*
This software was developed by employees of the National Institute
of Standards and Technology (NIST), an agency of the Federal
Government and is being made available as a public service. Pursuant
to title 17 United States Code Section 105, works of NIST employees
are not subject to copyright protection in the United States. This
software may be subject to foreign copyright. Permission in the
United States and in foreign countries, to the extent that NIST may
hold copyright, to use, copy, modify, create derivative works, and
distribute this software and its documentation without fee is hereby
granted on a non-exclusive basis, provided that this notice and
disclaimer of warranty appears in all copies.
THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND,
EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED
TO, ANY WARRANTY THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS,
ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, AND FREEDOM FROM INFRINGEMENT, AND ANY WARRANTY THAT THE
DOCUMENTATION WILL CONFORM TO THE SOFTWARE, OR ANY WARRANTY THAT THE
SOFTWARE WILL BE ERROR FREE. IN NO EVENT SHALL NIST BE LIABLE FOR
ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, SPECIAL
OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, OR IN ANY
WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY
PERSONS OR PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS
SUSTAINED FROM, OR AROSE OUT OF THE RESULTS OF, OR USE OF, THE
SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/
# include "tokens.h"
# include "ansi_parse.h"
# include "lif.h"
# define Q want_id=1; /* printf ("WID on\n"); */
# define T type_seen=0;
# define R in_enum=1;
# define S in_init=1;
/*
static char sccsid[] = "@(#)kscan.l 1.4 7/20/94";
*/
void eat_string();
void count();
void comment();
int reserved_word(int tok);
int check_type(char *t);
void reset_line(void);
int is_type_name (char *t, int *i);
/*static*/
int in_struct = 0,
recent_comma = 0;
int in_typedef = 0,
braces = 0,
in_init = 0,
need_brace = 0,
type_seen = 0,
in_local = 0,
in_enum = 0;
int want_id = 0;
int last_was_dot = 0;
%}
D [0-9]
L [a-zA-Z_]
H [a-fA-F0-9]
E [Ee][+-]?{D}+
FS (f|F|l|L)
IS (u|U|l|L)*
%%
"/*" { comment(); }
"auto" {T return reserved_word (AUTO); }
"break" { return reserved_word (BREAK); }
"case" { return reserved_word (CASE); }
"char" {Q return reserved_word (CHAR); }
"const" { return reserved_word (CONST); }
"continue" { return reserved_word (CONTINUE); }
"default" { return reserved_word (DEFAULT); }
"do" { return reserved_word (DO); }
"double" {Q return reserved_word (DOUBLE); }
"else" { return reserved_word (ELSE); }
"enum" {Q R return reserved_word (ENUM); }
"extern" {T return reserved_word (EXTERN); }
"float" {Q return reserved_word (FLOAT); }
"for" { return reserved_word (FOR); }
"goto" { return reserved_word (GOTO); }
"if" { return reserved_word (IF); }
"int" {Q return reserved_word (INT); }
"long" {Q return reserved_word (LONG); }
"register" {T ;return reserved_word (REGISTER); }
"return" { return reserved_word (RETURN); }
"short" {Q return reserved_word (SHORT); }
"signed" {Q return reserved_word (SIGNED); }
"sizeof" { return reserved_word (SIZEOF); }
"static" {T want_id = 0; return reserved_word (STATIC); }
"struct" {Q in_struct++;
need_brace = 2;
return reserved_word (STRUCT); }
"switch" { return reserved_word (SWITCH); }
"typedef" {
in_typedef = 1;
type_seen = 0;
return reserved_word (TYPEDEF); }
"union" {Q in_struct++;
need_brace = 2;
return reserved_word (UNION); }
"unsigned" {Q return reserved_word (UNSIGNED); }
"void" {Q return reserved_word (VOID); }
"volatile" { return reserved_word (VOLATILE); }
"while" { return reserved_word (WHILE); }
__extension__ {count(); /* eat gcc extension keyword */}
^#.*\n { reset_line(); }
{L}({L}|{D})* { int type_code;
if (need_brace > 0) need_brace--;
type_code = check_type(yytext);
if (type_code) return type_code;
}
0[xX]{H}+{IS}? { count(); return(CONSTANT); }
0{D}+{IS}? { count(); return(CONSTANT); }
{D}+{IS}? { count(); return(CONSTANT); }
L?'(\\.|[^\\'])+' { count(); return(CONSTANT); }
{D}+{E}{FS}? { count(); return(CONSTANT); }
{D}*"."{D}+({E})?{FS}? { count(); return(CONSTANT); }
{D}+"."{D}*({E})?{FS}? { count(); return(CONSTANT); }
L?\" {eat_string(); return(STRING_LITERAL);
/*
L?\"([^\n"\\]|(\\[^\n]))*\" {
count(); return(STRING_LITERAL);
\"([^"\n]|\\["\n])*\" { count(); return(STRING_LITERAL); }
*/}
">>=" { count(); return(RIGHT_ASSIGN); }
"<<=" { count(); return(LEFT_ASSIGN); }
"+=" { count(); return(ADD_ASSIGN); }
"-=" { count(); return(SUB_ASSIGN); }
"*=" { count(); return(MUL_ASSIGN); }
"/=" { count(); return(DIV_ASSIGN); }
"%=" { count(); return(MOD_ASSIGN); }
"&=" { count(); return(AND_ASSIGN); }
"^=" { count(); return(XOR_ASSIGN); }
"|=" { count(); return(OR_ASSIGN); }
">>" { count(); return(RIGHT_OP); }
"<<" { count(); return(LEFT_OP); }
"++" { yylval.token = create_token(NULL);
count(); return(INC_OP); }
"--" { yylval.token = create_token(NULL);
count(); return(DEC_OP); }
"->" { count(); last_was_dot = 1; return(PTR_OP); }
"&&" { count(); return(AND_OP); }
"||" { count(); return(OR_OP); }
"<=" { count(); return(LE_OP); }
">=" { count(); return(GE_OP); }
"==" { count(); return(EQ_OP); }
"!=" { count(); return(NE_OP); }
"..." { count(); return ELIPSIS; }
"," { count(); recent_comma = 1;
if (in_parms) want_id = 0;
if (k_opt) printf ("SEE COMMA NOW ip %d wi %d\n",
in_parms,want_id);
if (need_brace) in_struct--; return(','); }
"=" {S count(); return('='); }
"[" { count(); return('['); }
"]" { count(); return(']'); }
"." { count(); last_was_dot = 1; return('.'); }
"&" { count(); return('&'); }
"!" { count(); return('!'); }
"~" { count(); return('~'); }
"-" { count(); return('-'); }
"+" { count(); return('+'); }
"*" {
yylval.token = create_token(NULL);
count(); return('*'); }
"/" { count(); return('/'); }
"%" { count(); return('%'); }
"<" { count(); return('<'); }
">" { count(); return('>'); }
"^" { count(); return('^'); }
"|" { count(); return('|'); }
"?" { count(); return('?'); }
"}" {
braces--;
/*
if (in_struct){Q in_struct--;}
*/
yylval.token = create_token(NULL);
count();
return yytext[0];
}
[;{] {
want_id = 0;
if (k_opt) printf (" want id = %d :%s: \n",want_id,
yytext);
if (yytext[0] == '{') braces++;
if (yytext[0] == '{') need_brace = 0;
if ((!in_struct) && (in_typedef)) in_typedef = 0;
if(yytext[0] == ';'){
type_seen = 0;
in_enum = 0;
in_init = 0;
if (!braces) in_struct = 0;
}
else if (in_struct) type_seen = 0;
yylval.token = create_token(NULL);
count();
return yytext[0];
}
[():] { yylval.token = create_token(NULL);
if (yytext[0] == ')') want_id = 0;
if (yytext[0] == '(') want_id = 0;
if (k_opt) printf (" want id = %d :%s: \n",want_id,
yytext);
count();
return yytext[0];
}
[ \t\v\n\f] {int save;
save = last_was_dot;
count();
last_was_dot = save;}
. { count(); /* ignore bad characters */ }
%%
int yywrap(void)
{
return(1);
}
void comment(void)
{
char c, c1;
loop:
while ((c = input()) != '*' && c != 0)
/*putchar(c) */;
if ((c1 = input()) != '/' && c != 0)
{
unput(c1);
goto loop;
}
if (c != 0)
/* putchar(c1) */;
}
void count(void)
{
int i;
recent_comma = 0;
for (i = 0; yytext[i] != '\0'; i++)
if (yytext[i] == '\n'){
current_column = 1;
lineno++;
}
else
current_column++;
/*
for (i = 0; yytext[i] != '\0'; i++)
if (yytext[i] == '\n')
current_column = 0;
else if (yytext[i] == '\t')
current_column += 8 - (current_column % 8);
else
current_column++;
ECHO;
printf ("count token (%s) dot(%d)\n",yytext,last_was_dot);
*/
last_was_dot = 0;
}
void eat_string(void)
{
char c;
int more = 1,
in_esc = 0;
count();
while (more) {
c = input();
current_column++;
if (in_esc) in_esc = 0;
else if (c == '"') more = 0;
else if (c == '\\') in_esc = 1;
}
}
/*
**************************************************************
* *
* keep a global string table in <strings> *
* *
* find_string saves the text of an unseen string in the *
* string table and returns a pointer to a copy of the *
* string, or returns a pointer to the copy already saved. *
* *
**************************************************************
*/
char *find_string (char *s)
{
static string_ptr strings = NULL;
string_ptr at;
at = strings;
while (at){ /* return pointer to string if found */
if (strcmp(at->text,s) == 0) return at->text;
at = at -> next;
}
/*
**************************************************************
* alloc space for string if not seen before *
**************************************************************
*/
strings = alloc_string(s,strings);
return strings->text;
}
/*
**************************************************************
* *
* Create a token record for tokens that are used to *
* anchor statements (nodes) to places in the source *
* *
**************************************************************
*/
token_ptr create_token(char *s)
{
token_ptr new;
new = alloc_token();
new -> next = NULL;
new -> desc.any = NULL;
new -> at.line_start = lineno;
new -> at.line_end = lineno;
new -> at.col_start = current_column;
new -> at.col_end = current_column + strlen(yytext) - 1;
if (s) new->text = find_string(s);
else new -> text = NULL;
if (k_opt){
printf ("create token (%s) at line %d(%d,%d)",
yytext,lineno,current_column,new->at.col_end);
if (in_struct) printf (" struct");
if (in_typedef) printf (" typedef");
printf ("scope %d in %d type %d",top_scope(),
in_local,type_seen);
printf ("\n");
}
return new;
}
int check_type(char *t)
{
int is_type,is_local;
if (is_type_name(t,&is_local)) {
/*
if ((!is_local) && want_id)is_type = IDENTIFIER;
*/
if (k_opt) printf (" wID %d ",want_id);
if (want_id || last_was_dot){
want_id = 0; is_type = IDENTIFIER;}
else { Q is_type = TYPE_NAME;}
if (k_opt)
printf ("in %d is %d T %d I %d S %d want_id %d ip %d ",
in_local,is_local,type_seen,in_init,in_struct,want_id,
in_parms);
}
else is_type = IDENTIFIER;
if (k_opt) printf (" %s (%d,%d[.,->])\n",is_type == TYPE_NAME?"Type":"Id",
want_id,last_was_dot);
yylval.token = create_token(t);
count();
return is_type ;
}
int reserved_word(int t)
{
yylval.token = create_token(NULL);
count();
return t ;
}
void start_local_decl(void)
{
in_local = 1;
}
void end_init(void)
{
in_init = 0;
}
void clear_type_flag(void)
{
type_seen = 0;
}
void end_local_decl(void)
{
in_init = 0;
in_local = 0;
}
void reset_line (void) /* adjust line and column after # include */
{
char *at;
at = yytext;
/*
sscanf(at,"#%d \"%s\"",&lineno,the_current_file);
*/
sscanf(at,"#%d \"%[^\"]",&lineno,the_current_file);
if(h_file)fprintf(h_file,"@ %s\n",the_current_file);
current_column = 1;
at[strlen(at) - 2] = '\0';
if(k_opt)printf ("reset to line %d in [%s] (%s)\n",lineno,
the_current_file,at);
}