1/* 2 * eof_rules.lex : An example of using multiple buffers 3 * EOF rules, and start states 4 */ 5 6%{ 7 8#define MAX_NEST 10 9 10YY_BUFFER_STATE include_stack[MAX_NEST]; 11int include_count = -1; 12 13%} 14 15 16%x INCLUDE 17%x COMMENT 18 19 20%% 21 22"{" BEGIN(COMMENT); 23 24<COMMENT>"}" BEGIN(INITIAL); 25<COMMENT>"$include"[ \t]*"(" BEGIN(INCLUDE); 26<COMMENT>[ \t]* /* skip whitespace */ 27 28<INCLUDE>")" BEGIN(COMMENT); 29<INCLUDE>[ \t]* /* skip whitespace */ 30<INCLUDE>[^ \t\n() ]+ { /* get the include file name */ 31 if ( include_count >= MAX_NEST){ 32 fprintf( stderr, "Too many include files" ); 33 exit( 1 ); 34 } 35 36 include_stack[++include_count] = YY_CURRENT_BUFFER; 37 38 yyin = fopen( yytext, "r" ); 39 if ( ! yyin ){ 40 fprintf( stderr, "Unable to open %s",yytext); 41 exit( 1 ); 42 } 43 44 yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE)); 45 46 BEGIN(INITIAL); 47 } 48<INCLUDE><<EOF>> 49 { 50 fprintf( stderr, "EOF in include" ); 51 yyterminate(); 52 } 53<COMMENT><<EOF>> 54 { 55 fprintf( stderr, "EOF in comment" ); 56 yyterminate(); 57 } 58<<EOF>> { 59 if ( include_count <= 0 ){ 60 yyterminate(); 61 } else { 62 yy_delete_buffer(include_stack[include_count--] ); 63 yy_switch_to_buffer(include_stack[include_count] ); 64 BEGIN(INCLUDE); 65 } 66 } 67[a-z]+ ECHO; 68.|\n ECHO; 69 70 71 72 73 74 75 76 77 78 79