ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/BASS_parse/BASS.l
Revision: 131
Committed: Wed Oct 9 22:29:40 2002 UTC (21 years, 9 months ago) by chuckv
File size: 9185 byte(s)
Log Message:
*** empty log message ***

File Contents

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