aboutsummaryrefslogtreecommitdiff
path: root/src/c.l
blob: 43d60665d2f67b2a7583a34d34150f4a9bd5076e (plain)
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
/* This file is part of GNU cflow
   Copyright (C) 1997,2005 Sergey Poznyakoff

   GNU cflow is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   GNU cflow is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public
   License along with GNU cflow; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */

%x comment
%x string
%x stringwait
%x longline
%{
#include <cflow.h>
#include <ctype.h>
#include <parser.h>
     
struct obstack string_stk;

int line_num;
char *filename;
char *canonical_filename; 
YYSTYPE yylval;
unsigned input_file_count; /* Number of input files, processed by source() */
 
int ident();
void update_loc();
#define lex_error(msg) error_at_line(0, 0, filename, line_num, "%s", msg)

%}
FILENAME [^\n*?]*
ONUMBER (0[0-7]*)
HNUMBER (0[xX][0-9a-fA-F]*)
DNUMBER ([1-9][0-9]*)
DIGITS  [0-9][0-9]*
IDENT [a-zA-Z_][a-zA-Z0-9_]*
WS    [ \t\f\r]* 
%%
     /* comments */
"//".*\n                ++line_num;
"/*"			BEGIN(comment);
<comment>[^*\n]*	;
<comment>[^*\n]*\n	++line_num;
<comment>"*"+[^*/\n]*	;
<comment>"*"+[^*/\n]*\n	++line_num;
<comment>"*"+"/"	BEGIN(INITIAL); 
     /* Line directives */
^{WS}#{WS}line{WS}{DIGITS}.*\n |
^{WS}#{WS}{DIGITS}.*\n   { update_loc(); }
     /* skip any preproc */
^{WS}#.*\\\n             { BEGIN(longline); ++line_num; }
{WS}#.*\n                ++line_num;
<longline>.*\\\n         ++line_num; 
<longline>.*\n           { BEGIN(INITIAL); ++line_num; }
     /* keywords */
auto                 /* ignored */;
extern               return EXTERN; 
static               return STATIC;
typedef              return TYPEDEF;
struct               {yylval.str = "struct"; return STRUCT;}
union                {yylval.str = "union"; return STRUCT;}
enum                 {yylval.str = "enum"; return STRUCT;}
\*                   { yylval.str = "*";
		       return MODIFIER;
		   }
     /* Operators
      *
      */
"->"   |
"."                  return MEMBER_OF;
"*="   |                 
"/="   |                 
"%="   |                 
"+="   |                 
"-="   |                 
"<<="  |                 
">>="  |                 
"&="   |                 
"|="   |                 
"^="   |                 
"||"   |                 
"&&"   |                 
"=="   |                 
"!="   |                 
">="   |                 
">"    |                 
"<="   |                 
"<"    |                 
"<<"   |                 
">>"   |                 
"++"   |                 
"--"                     return OP;
'.'  |
'\\.' |
'\\[0-7]{2,3}' |
'\\[xX][0-9a-fA-F]{1,2}'  return STRING;
     /* Identifiers and constants
      *
      */
"..." |
{IDENT}			return ident();
{ONUMBER}               { obstack_grow(&string_stk, yytext, yyleng+1);
			  yylval.str = obstack_finish(&string_stk);
			  return WORD;
		      }
[+\-]?{DNUMBER}[^eE.]   { yyless(yyleng-1);
			  obstack_grow(&string_stk, yytext, yyleng+1);
			  yylval.str = obstack_finish(&string_stk);
			  return WORD;}
{HNUMBER} |               
[+\-]?{DIGITS}?\.{DIGITS}([eE][+\-]?{DIGITS})? |
[+\-]?{DIGITS}\.([eE][+\-]?{DIGITS})? {
    obstack_grow(&string_stk, yytext, yyleng+1);
    yylval.str = obstack_finish(&string_stk);
    return WORD;
}

      /* strings 
      * State map:
      *
      * "<string>blahblahblah"<stringwait>
      * <stringwait>"<string>blahblahblah"<stringwait>
      * <stringwait>.<INITIAL>
      */
\"    BEGIN(string);
<string>[^\\"\n]*       ;
<string>\n              { ++line_num; lex_error(_("unterminated string?")); } 
<string>\\.             ;
<string>\\\n            ++line_num;
<string>\"              BEGIN(stringwait);
<stringwait>{WS}        ;
<stringwait>\n          ++line_num; 
<stringwait>\"          BEGIN(string);
<stringwait>.           {
     BEGIN(INITIAL);
     yyless(0); /* put the symbol back */
     return STRING;
}
\n                      ++line_num;
{WS}                    ;
       /*\f                      ;*/
^\{                     return LBRACE0;
^\}                     return RBRACE0;
.                       return yytext[0];
%%

static char *keywords[] = {
    "break",
    "case",
    "continue",
    "default",
    "do",
    "else",
    "for",
    "goto",
    "if",
    "return",
    "sizeof",
    "switch",
    "while"
};

static char *types[] = {
    "char",
    "const",
    "double",
    "float",
    "int",
    "long",
    "register",
    "restrict",
    "short",
    "signed",
    "unsigned",
    "void",
    "volatile",
};

void
init_lex(int debug_level)
{
     int i;
     Symbol *sp;
    
     yy_flex_debug = debug_level;
     
     obstack_init(&string_stk);

     for (i = 0; i < NUMITEMS(keywords); i++) {
	  sp = install(keywords[i]);
	  sp->type = SymToken;
	  sp->token_type = WORD;
     }
     
     for (i = 0; i < NUMITEMS(types); i++) {
	  sp = install(types[i]);
	  sp->type = SymToken;
	  sp->token_type = TYPE;
	  sp->source = NULL;
	  sp->def_line = -1;
	  sp->ref_line = NULL;
     }	
     sp = install("...");
     sp->type = SymToken;
     sp->token_type = IDENTIFIER;
     sp->source = NULL;
     sp->def_line = -1;
     sp->ref_line = NULL;
}

int
ident()
{
     Symbol *sp;

     sp = lookup(yytext);
     if (sp && sp->type == SymToken) {
	  yylval.str = sp->name;
	  return sp->token_type;
     }
     obstack_grow(&string_stk, yytext, yyleng);
     obstack_1grow(&string_stk, 0);
     yylval.str = obstack_finish(&string_stk);
     return IDENTIFIER;
}



char *pp_bin;
char *pp_opts;
static struct obstack *opt_stack;

void
set_preprocessor(const char *arg)
{
     pp_bin = arg ? xstrdup(arg) : NULL;
}

void
pp_option(const char *arg)
{
     if (!opt_stack) {
	  if (!pp_bin)
	       pp_bin = CFLOW_PREPROC;
	  opt_stack = xmalloc(sizeof *opt_stack);
	  obstack_init(opt_stack);
     }
     obstack_1grow(opt_stack, ' ');
     obstack_grow(opt_stack, arg, strlen (arg));
}

void
pp_finalize()
{
     char *s = obstack_finish(opt_stack);
     if (!pp_opts)
	  pp_opts = xstrdup(s);
     else {
	  pp_opts = xrealloc(pp_opts, strlen(pp_opts) + strlen(s) + 1);
	  strcat(pp_opts, s);
     }
     obstack_free(opt_stack, s);
     free(opt_stack);
     opt_stack = NULL;
}

FILE *
pp_open(const char *name)
{
     FILE *fp;
     char *s;
     size_t size;
     
     if (opt_stack)
	  pp_finalize();
     size = strlen(pp_bin) + 1 + strlen(name) + 1;
     if (pp_opts)
	  size += strlen(pp_opts);
     s = xmalloc(size);
     strcpy(s, pp_bin);
     if (pp_opts)
	  strcat(s, pp_opts);
     strcat(s, " ");
     strcat(s, name);
     if (debug)
	  printf(_("Command line: %s\n"), s);
     fp = popen(s, "r");
     if (!fp)
	  error(0, errno, _("cannot execute `%s'"), s);
     free(s);
     return fp;
}

void
pp_close(FILE *fp)
{
     pclose(fp);
}



int
yywrap()
{
     if (!yyin)
	  return 1;
     if (preprocess_option)
	  pp_close(yyin);
     else
	  fclose(yyin);
     yyin = NULL;
#ifdef FLEX_SCANNER
     yy_delete_buffer(yy_current_buffer);
#endif
     delete_statics();
     return 1;
}

int
get_token()
{
     return yyin ? yylex() : 0;
}

int
source(char *name)
{
     FILE *fp;

     fp = fopen(name, "r");
     if (!fp) {
	  error(0, errno, _("cannot open `%s'"), name);
	  return 1;
     }
     if (preprocess_option) {
	  fclose(fp);
	  fp = pp_open(name);
	  if (!fp)
	       return 1;
     }
     obstack_grow(&string_stk, name, strlen(name)+1);
     filename = obstack_finish(&string_stk);
     canonical_filename = filename;
     line_num = 1;
     input_file_count++;
     
     yyrestart(fp);
     return 0;
}

static int
getnum(unsigned  base, int  count)
{
     int  c, n;
     unsigned i;

     for (n = 0; count; count--) {
	  if (isdigit(c = input()))
	       i = c - '0';
	  else
	       i = toupper(c) - 'A' + 10;
	  if (i > base) {
	       unput(c);
	       break;
	  }
	  n = n * base + i;
     }
     return n;
}

int
backslash()
{                                  
     int c;
     
     switch (c = input()) {                        
     case 'a':   return '\a';                  
     case 'b':   return '\b';                  
     case 'f':   return '\f';                  
     case 'n':   return '\n';                  
     case 'r':   return '\r';                  
     case 't':   return '\t';                  
     case 'x':   return getnum(16,2);          
     case '0':   return getnum(8,3);           
     }                                             
     return c;                                         
}                                                     

void
update_loc()
{
     char *p;
     
     for (p = strchr(yytext, '#')+1; *p && isspace(*p); p++)
	  ;
     if (p[0] == 'l') /* line */
	  p += 4;

     line_num = strtoul(p, &p, 10);
     for ( ; *p && isspace(*p); p++)
	  ;
     if (p[0] == '"') {
	  int n;
	  
	  for (p++, n = 0; p[n] && p[n] != '"'; n++)
	       ;
	  obstack_grow(&string_stk, p, n);
	  obstack_1grow(&string_stk, 0);
	  filename = obstack_finish(&string_stk);
     }
     if (debug > 1)
	  printf(_("New location: %s:%d\n"), filename, line_num);
}
          

Return to:

Send suggestions and report system problems to the System administrator.