1 |
|
2 |
/* define some search patterns */ |
3 |
|
4 |
digit [0-9] |
5 |
letter [A-Za-z] |
6 |
natural {digit}+ |
7 |
signedNat ("+"|"-")?{natural} |
8 |
number {signedNat}("."{natural})?([Ee]{signedNat})? |
9 |
identifier ("_"|{letter})({letter}|{digit}|"-"|"_")* |
10 |
|
11 |
/* define start states */ |
12 |
|
13 |
%x PRE_P |
14 |
%x INCL |
15 |
%x DEF |
16 |
%x IFDEF |
17 |
%x IFNDEF |
18 |
%x ESCAPE |
19 |
%x CHECK_ESCAPE |
20 |
|
21 |
/* what to put at the top of the lex code */ |
22 |
|
23 |
%{ |
24 |
#include <stdlib.h> |
25 |
#include <stdio.h> |
26 |
#include <string.h> |
27 |
|
28 |
#include "BASSyacc.h" |
29 |
#include "BASS_parse.h" |
30 |
#include "simError.h" |
31 |
#ifdef IS_MPI |
32 |
#define __is_lex__ |
33 |
#include "mpiBASS.h" |
34 |
#endif |
35 |
|
36 |
typedef unsigned short int r_short; |
37 |
typedef unsigned short my_short; |
38 |
|
39 |
extern void change_in_file( FILE* in_file ); |
40 |
|
41 |
// the following is used by the include start state |
42 |
|
43 |
#define MAX_BUFFER_DEPTH 10 |
44 |
YY_BUFFER_STATE buffer_stack[MAX_BUFFER_DEPTH]; // a stack of the include buffers |
45 |
int buffer_stack_ptr = 0; |
46 |
struct filename_list{ |
47 |
char my_name[300]; |
48 |
struct filename_list* next; |
49 |
}; |
50 |
struct filename_list* yyfile_name; |
51 |
struct filename_list* temp_yyfile_name; |
52 |
int yylineno_stack[MAX_BUFFER_DEPTH]; |
53 |
|
54 |
|
55 |
// the following is a check against the define buffer length |
56 |
|
57 |
void check_def_buff( char* defined, int index ); |
58 |
|
59 |
//these are used by the ifdef and ifndef statements |
60 |
|
61 |
int escape_stack_ptr = 0; //keeps track of the escape stack |
62 |
|
63 |
|
64 |
%} |
65 |
|
66 |
%option yylineno |
67 |
|
68 |
%% |
69 |
|
70 |
{signedNat} { |
71 |
yylval.i_val = atoi( yytext ); |
72 |
return INTEGER; |
73 |
} |
74 |
|
75 |
{number} { |
76 |
yylval.d_val = atof( yytext ); |
77 |
return DOUBLE; |
78 |
} |
79 |
|
80 |
{identifier} { |
81 |
int token; |
82 |
token = res_word( yytext ); |
83 |
|
84 |
if( token == DEFINED ){ |
85 |
|
86 |
if( buffer_stack_ptr >= MAX_BUFFER_DEPTH ){ |
87 |
fprintf( stderr, |
88 |
"Maximum buffer depth exceeded for %s.\n", |
89 |
yytext ); |
90 |
exit(1); |
91 |
} |
92 |
|
93 |
buffer_stack[buffer_stack_ptr] = YY_CURRENT_BUFFER; |
94 |
buffer_stack_ptr++; |
95 |
|
96 |
yy_scan_string( get_definition( yytext ) ); |
97 |
} |
98 |
else if( token ){ |
99 |
return token; |
100 |
} |
101 |
else{ |
102 |
yylval.s_ptr = strdup( yytext ); |
103 |
return IDENTIFIER; |
104 |
} |
105 |
} |
106 |
|
107 |
\".*\" { |
108 |
/* little routine to strip off the quotes */ |
109 |
|
110 |
my_short i; |
111 |
i = 0; // index |
112 |
while( yytext[i+1] != '\"' ){ |
113 |
|
114 |
yytext[i] = yytext[i+1]; |
115 |
i++; |
116 |
} |
117 |
yytext[i] = '\0'; |
118 |
yylval.s_ptr = strdup( yytext ); |
119 |
return QUOTED_STRING; |
120 |
} |
121 |
|
122 |
\[{natural}\] { |
123 |
int index; |
124 |
sscanf(yytext, "[%d]", &index); |
125 |
yylval.i_val = index; |
126 |
return ARRAY_INDEX; |
127 |
} |
128 |
|
129 |
[ \t\n]+ /* ignore whitespace */; |
130 |
|
131 |
"/*" { |
132 |
/* ignore comments */ |
133 |
my_short done; |
134 |
char c; |
135 |
|
136 |
done = 0; |
137 |
while( !done ){ |
138 |
|
139 |
c = input(); |
140 |
while( c != '*' ){ |
141 |
|
142 |
c = input(); |
143 |
} |
144 |
while( c == '*' ){ |
145 |
|
146 |
c = input(); |
147 |
} |
148 |
if( c == '/' ) done = 1; |
149 |
} |
150 |
} |
151 |
|
152 |
"//".*\n /* ignore comments */; |
153 |
|
154 |
"#" BEGIN(PRE_P); |
155 |
|
156 |
. { |
157 |
// pass everything else to yacc |
158 |
return yytext[0]; |
159 |
} |
160 |
|
161 |
<PRE_P>"include" BEGIN(INCL); |
162 |
<PRE_P>"define" BEGIN(DEF); |
163 |
<PRE_P>"ifdef" BEGIN(IFDEF); |
164 |
<PRE_P>"ifndef" BEGIN(IFNDEF); |
165 |
<PRE_P>"endif" /* do nothing */; |
166 |
<PRE_P>\n BEGIN(INITIAL); |
167 |
|
168 |
<INCL>[ \t]* /* eat white space */ |
169 |
<INCL>\".*\" { |
170 |
char foo_name[300]; |
171 |
|
172 |
// little routine to strip off the quotes |
173 |
|
174 |
my_short i; |
175 |
i = 0; // index |
176 |
while( yytext[i+1] != '\"' ){ |
177 |
|
178 |
yytext[i] = yytext[i+1]; |
179 |
i++; |
180 |
} |
181 |
|
182 |
yytext[i] = '\0'; |
183 |
strcpy( foo_name, yytext ); |
184 |
|
185 |
// now we have the include file name |
186 |
|
187 |
if( buffer_stack_ptr >= MAX_BUFFER_DEPTH ){ |
188 |
|
189 |
fprintf( stderr, "Includes nested too deeply\n" ); |
190 |
exit(1); |
191 |
} |
192 |
|
193 |
buffer_stack[buffer_stack_ptr] = YY_CURRENT_BUFFER; |
194 |
yylineno_stack[buffer_stack_ptr] = yylineno; |
195 |
buffer_stack_ptr++; |
196 |
|
197 |
yyin = fopen( yytext, "r" ); |
198 |
if( yyin == NULL ){ |
199 |
fprintf( stderr, "Unable to include file %s\n", yytext ); |
200 |
exit(1); |
201 |
} |
202 |
|
203 |
yy_switch_to_buffer( yy_create_buffer( yyin, YY_BUF_SIZE ) ); |
204 |
yylineno = 0; |
205 |
|
206 |
temp_yyfile_name = (struct filename_list* )malloc( sizeof( struct filename_list ) ); |
207 |
temp_yyfile_name->next = yyfile_name; |
208 |
yyfile_name = temp_yyfile_name; |
209 |
strcpy( yyfile_name->my_name, foo_name ); |
210 |
|
211 |
|
212 |
BEGIN(INITIAL); |
213 |
} |
214 |
<INCL>\n BEGIN(INITIAL); |
215 |
<INCL>. /* ignore everything else */ |
216 |
|
217 |
<DEF>[ \t]* /* eat white space */; |
218 |
<DEF>{identifier} { |
219 |
char c; |
220 |
char definition[ DEFINED_BUFFER_SIZE ]; |
221 |
short int done; |
222 |
short int c_done; // a done marker for the comments |
223 |
int def_ptr; |
224 |
|
225 |
// initialize the definition buffer |
226 |
|
227 |
for( def_ptr = 0; def_ptr < DEFINED_BUFFER_SIZE; def_ptr++ ){ |
228 |
definition[def_ptr] = '\0'; |
229 |
} |
230 |
|
231 |
def_ptr =0; |
232 |
done = 0; |
233 |
c_done = 0; |
234 |
|
235 |
while( !done ){ |
236 |
|
237 |
c = input(); |
238 |
if( c == '\"' ){ |
239 |
|
240 |
// shove the whole quoted string into the macro |
241 |
|
242 |
definition[def_ptr] = c; |
243 |
//fprintf( stderr, "%c", c ); |
244 |
def_ptr++; |
245 |
check_def_buff( yytext, def_ptr ); |
246 |
|
247 |
c = input(); |
248 |
while( c != '\"' ){ |
249 |
|
250 |
definition[def_ptr] = c; |
251 |
//fprintf( stderr, "%c", c ); |
252 |
def_ptr++; |
253 |
check_def_buff( yytext, def_ptr ); |
254 |
|
255 |
c = input(); |
256 |
} |
257 |
definition[def_ptr] = c; |
258 |
//fprintf( stderr, "%c", c ); |
259 |
def_ptr++; |
260 |
check_def_buff( yytext, def_ptr ); |
261 |
c = input(); |
262 |
} |
263 |
|
264 |
// handle comments |
265 |
|
266 |
if( c == '/' ){ |
267 |
c = input(); |
268 |
switch( c ){ |
269 |
|
270 |
case '/': |
271 |
while( c != '\n' && c != '\\' ){ |
272 |
c = input(); |
273 |
} |
274 |
break; |
275 |
|
276 |
case '*': |
277 |
c_done = 0; |
278 |
while( !c_done ){ |
279 |
c = input(); |
280 |
while( c != '*' ){ |
281 |
c = input(); |
282 |
} |
283 |
while( c == '*' ){ |
284 |
c = input(); |
285 |
} |
286 |
if( c == '/' ) c_done = 1; |
287 |
} |
288 |
c = input(); |
289 |
break; |
290 |
|
291 |
default: |
292 |
// the '/' char was a normal symbol |
293 |
definition[def_ptr] = '/'; |
294 |
//fprintf( stderr, "%c", c ); |
295 |
def_ptr++; |
296 |
check_def_buff( yytext, def_ptr ); |
297 |
break; |
298 |
} |
299 |
} |
300 |
|
301 |
|
302 |
if( c == '\n' ){ |
303 |
done = 1; |
304 |
} |
305 |
|
306 |
else{ |
307 |
|
308 |
// check for the line wrap character '\' |
309 |
|
310 |
if( c == '\\' ){ |
311 |
|
312 |
// skip the rest of the line until the line return |
313 |
|
314 |
c = input(); |
315 |
while( c != '\n' ){ |
316 |
c = input(); |
317 |
} |
318 |
} |
319 |
|
320 |
else{ |
321 |
|
322 |
// we now know the character is a good one |
323 |
|
324 |
definition[def_ptr] = c; |
325 |
//fprintf( stderr, "%c", c ); |
326 |
def_ptr++; |
327 |
check_def_buff( yytext, def_ptr ); |
328 |
} |
329 |
} |
330 |
} |
331 |
|
332 |
insert_define( yytext, definition ); |
333 |
BEGIN(INITIAL); |
334 |
} |
335 |
<DEF>\n BEGIN(INITIAL); |
336 |
<DEF>. /* ignore everything else */; |
337 |
|
338 |
<IFDEF>[ \t]* /* eat white space */; |
339 |
<IFDEF>{identifier} { |
340 |
if( !is_defined( yytext ) ){ |
341 |
escape_stack_ptr++; |
342 |
BEGIN(ESCAPE); |
343 |
} |
344 |
} |
345 |
<IFDEF>\n BEGIN(INITIAL); |
346 |
<IFDEF>. /* ignore everything else */; |
347 |
|
348 |
<IFNDEF>[ \t]* /* eat the white space */; |
349 |
<IFNDEF>{identifier} { |
350 |
if( is_defined( yytext ) ){ |
351 |
escape_stack_ptr++; |
352 |
BEGIN(ESCAPE); |
353 |
} |
354 |
} |
355 |
<IFNDEF>\n BEGIN(INITIAL); |
356 |
<IFNDEF>. /* ignore everything else */; |
357 |
|
358 |
<ESCAPE>\".*\" /* do nothing */; |
359 |
<ESCAPE>"//".*\n /* ignore comments */; |
360 |
<ESCAPE>"/*" { |
361 |
/* ignore comments */ |
362 |
my_short done; |
363 |
char c; |
364 |
|
365 |
done = 0; |
366 |
while( !done ){ |
367 |
|
368 |
c = input(); |
369 |
while( c != '*' ){ |
370 |
|
371 |
c = input(); |
372 |
} |
373 |
|
374 |
while( c == '*' ){ |
375 |
|
376 |
c = input(); |
377 |
} |
378 |
|
379 |
if( c == '/' ) done = 1; |
380 |
} |
381 |
} |
382 |
<ESCAPE>"#" BEGIN(CHECK_ESCAPE); |
383 |
<ESCAPE>. /* ignore everything else */; |
384 |
|
385 |
<CHECK_ESCAPE>[ \t] /* ignore whitespace */; |
386 |
<CHECK_ESCAPE>"ifdef" { escape_stack_ptr++; BEGIN(ESCAPE); } |
387 |
<CHECK_ESCAPE>"ifndef" { escape_stack_ptr++; BEGIN(ESCAPE); } |
388 |
<CHECK_ESCAPE>"endif" { |
389 |
escape_stack_ptr--; |
390 |
if( escape_stack_ptr <= 0){ |
391 |
escape_stack_ptr = 0; // just in case something flubbed |
392 |
BEGIN(INITIAL); |
393 |
} |
394 |
} |
395 |
<CHECK_ESCAPE>\n BEGIN(ESCAPE); |
396 |
<CHECK_ESCAPE>. /* ignore everything else */; |
397 |
|
398 |
<<EOF>> { |
399 |
buffer_stack_ptr--; |
400 |
if( buffer_stack_ptr < 0 ){ |
401 |
|
402 |
yyterminate(); |
403 |
} |
404 |
|
405 |
else{ |
406 |
|
407 |
yy_delete_buffer( YY_CURRENT_BUFFER ); |
408 |
yy_switch_to_buffer( buffer_stack[buffer_stack_ptr] ); |
409 |
yylineno = yylineno_stack[buffer_stack_ptr]; |
410 |
|
411 |
temp_yyfile_name = yyfile_name; |
412 |
yyfile_name = temp_yyfile_name->next; |
413 |
free( temp_yyfile_name ); |
414 |
} |
415 |
} |
416 |
|
417 |
%% |
418 |
|
419 |
void check_def_buff( char* defined, int index ){ |
420 |
|
421 |
if( index >= DEFINED_BUFFER_SIZE ){ |
422 |
|
423 |
sprintf( painCave.errMsg, "Define buffer size exceeded for %s\n", defined ); |
424 |
painCave.isFatal = 1; |
425 |
simError(); |
426 |
} |
427 |
} |
428 |
|
429 |
int yywrap(void){ |
430 |
return 1; |
431 |
} |
432 |
|
433 |
void change_in_file( FILE* in_file ){ |
434 |
|
435 |
yyin = in_file; |
436 |
} |