etc/services - sync with NetBSD-8
[minix.git] / external / bsd / flex / dist / examples / manual / pas_include.lex
blob58cf5903201435ed36c707346be94645cd016b3e
1 /*
2  * eof_rules.lex : An example of using multiple buffers
3  *                 EOF rules, and start states
4  */
6 %{
7                               
8 #define MAX_NEST 10                   
10 YY_BUFFER_STATE include_stack[MAX_NEST];
11 int             include_count = -1;
16 %x INCLUDE
17 %x COMMENT
22 "{"                          BEGIN(COMMENT);
24 <COMMENT>"}"                 BEGIN(INITIAL); 
25 <COMMENT>"$include"[ \t]*"(" BEGIN(INCLUDE);
26 <COMMENT>[ \t]*              /* skip whitespace */
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           }
36           include_stack[++include_count] = YY_CURRENT_BUFFER;
38           yyin = fopen( yytext, "r" );
39           if ( ! yyin ){
40              fprintf( stderr, "Unable to open %s",yytext);
41              exit( 1 );
42           }
44           yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE));
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;