1 /* scan.l - scanner for flex input */
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
11 * The United States Government has rights in this work pursuant
12 * to contract no. DE-AC03-76SF00098 between the United States
13 * Department of Energy and the University of California.
15 * Redistribution and use in source and binary forms are permitted provided
16 * that: (1) source distributions retain this entire copyright notice and
17 * comment, and (2) distributions including binaries display the following
18 * acknowledgement: ``This product includes software developed by the
19 * University of California, Berkeley and its contributors'' in the
20 * documentation or other materials provided with the distribution and in
21 * all advertising materials mentioning features or use of this software.
22 * Neither the name of the University nor the names of its contributors may
23 * be used to endorse or promote products derived from this software without
24 * specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 /* $Header: /home/daffy/u0/vern/flex/RCS/scan.l,v 2.56 95/04/24 12:17:19 vern Exp $ */
36 #define ACTION_ECHO add_action( yytext )
37 #define ACTION_IFDEF(def, should_define) \
39 if ( should_define ) \
40 action_define( def, 1 ); \
43 #define MARK_END_OF_PROLOG mark_prolog();
49 yylval = (unsigned char) yytext[0]; \
53 strcpy( nmstr, yytext ); \
56 #define PUT_BACK_STRING(str, start) \
57 for ( i = strlen( str ) - 1; i >= start; --i ) \
60 #define CHECK_REJECT(str) \
61 if ( all_upper( str ) ) \
64 #define CHECK_YYMORE(str) \
65 if ( all_lower( str ) ) \
69 %option caseless nodefault outfile="scan.c" stack noyy_top_state
72 %x SECT2 SECT2PROLOG SECT3 CODEBLOCK PICKUPDEF SC CARETISBOL NUM QUOTE
73 %x FIRSTCCL CCL ACTION RECOVER COMMENT ACTION_STRING PERCENT_BRACE_ACTION
82 NAME ([[:alpha:]_][[:alnum:]_-]*)
83 NOT_NAME [^[:alpha:]_*\n]+
87 ESCSEQ (\\([^\n]|[0-7]{1,3}|x[[:xdigit:]]{1,2}))
89 FIRST_CCL_CHAR ([^\\\n]|{ESCSEQ})
90 CCL_CHAR ([^\\\n\]]|{ESCSEQ})
91 CCL_EXPR ("[:"[[:alpha:]]+":]")
96 static int bracelevel, didadef, indented_code;
97 static int doing_rule_action = false;
98 static int option_sense;
100 int doing_codeblock = false;
102 Char nmdef[MAXLINE], myesc();
106 ^{WS} indented_code = true; BEGIN(CODEBLOCK);
107 ^"/*" ACTION_ECHO; yy_push_state( COMMENT );
108 ^#{OPTWS}line{WS} yy_push_state( LINEDIR );
109 ^"%s"{NAME}? return SCDECL;
110 ^"%x"{NAME}? return XSCDECL;
113 line_directive_out( (FILE *) 0, 1 );
114 indented_code = false;
124 line_directive_out( (FILE *) 0, 1 );
129 ^"%pointer".*{NL} yytext_is_array = false; ++linenum;
130 ^"%array".*{NL} yytext_is_array = true; ++linenum;
132 ^"%option" BEGIN(OPTION); return OPTION_OP;
134 ^"%"{LEXOPT}{OPTWS}[[:digit:]]*{OPTWS}{NL} ++linenum; /* ignore */
135 ^"%"{LEXOPT}{WS}.*{NL} ++linenum; /* ignore */
137 ^"%"[^sxaceknopr{}].* synerr( _( "unrecognized '%' directive" ) );
140 strcpy( nmstr, yytext );
146 ^{OPTWS}{NL} ++linenum; /* allows blank lines in section 1 */
147 {OPTWS}{NL} ACTION_ECHO; ++linenum; /* maybe end of comment line */
152 "*/" ACTION_ECHO; yy_pop_state();
155 [^*\n]*{NL} ++linenum; ACTION_ECHO;
160 [[:digit:]]+ linenum = myctoi( yytext );
163 flex_free( (void *) infilename );
164 infilename = copy_string( yytext + 1 );
165 infilename[strlen( infilename ) - 1] = '\0';
167 . /* ignore spurious characters */
171 ^"%}".*{NL} ++linenum; BEGIN(INITIAL);
173 {NAME}|{NOT_NAME}|. ACTION_ECHO;
185 {WS} /* separates name and definition */
188 strcpy( (char *) nmdef, yytext );
190 /* Skip trailing whitespace. */
191 for ( i = strlen( (char *) nmdef ) - 1;
192 i >= 0 && (nmdef[i] == ' ' || nmdef[i] == '\t');
198 ndinstal( nmstr, nmdef );
204 synerr( _( "incomplete name definition" ) );
212 {NL} ++linenum; BEGIN(INITIAL);
213 {WS} option_sense = true;
217 no option_sense = ! option_sense;
219 7bit csize = option_sense ? 128 : 256;
220 8bit csize = option_sense ? 256 : 128;
222 align long_align = option_sense;
224 action_define( "YY_ALWAYS_INTERACTIVE", option_sense );
226 array yytext_is_array = option_sense;
227 backup backing_up_report = option_sense;
228 batch interactive = ! option_sense;
229 "c++" C_plus_plus = option_sense;
230 caseful|case-sensitive caseins = ! option_sense;
231 caseless|case-insensitive caseins = option_sense;
232 debug ddebug = option_sense;
233 default spprdflt = ! option_sense;
234 ecs useecs = option_sense;
236 useecs = usemecs = false;
237 use_read = fullspd = true;
240 useecs = usemecs = false;
241 use_read = fulltbl = true;
243 input ACTION_IFDEF("YY_NO_INPUT", ! option_sense);
244 interactive interactive = option_sense;
245 lex-compat lex_compat = option_sense;
247 action_define( "YY_MAIN", option_sense );
248 do_yywrap = ! option_sense;
250 meta-ecs usemecs = option_sense;
252 action_define( "YY_NEVER_INTERACTIVE", option_sense );
254 perf-report performance_report += option_sense ? 1 : -1;
255 pointer yytext_is_array = ! option_sense;
256 read use_read = option_sense;
257 reject reject_really_used = option_sense;
258 stack action_define( "YY_STACK_USED", option_sense );
259 stdinit do_stdinit = option_sense;
260 stdout use_stdout = option_sense;
261 unput ACTION_IFDEF("YY_NO_UNPUT", ! option_sense);
262 verbose printstats = option_sense;
263 warn nowarn = ! option_sense;
264 yylineno do_yylineno = option_sense;
265 yymore yymore_really_used = option_sense;
266 yywrap do_yywrap = option_sense;
268 yy_push_state ACTION_IFDEF("YY_NO_PUSH_STATE", ! option_sense);
269 yy_pop_state ACTION_IFDEF("YY_NO_POP_STATE", ! option_sense);
270 yy_top_state ACTION_IFDEF("YY_NO_TOP_STATE", ! option_sense);
272 yy_scan_buffer ACTION_IFDEF("YY_NO_SCAN_BUFFER", ! option_sense);
273 yy_scan_bytes ACTION_IFDEF("YY_NO_SCAN_BYTES", ! option_sense);
274 yy_scan_string ACTION_IFDEF("YY_NO_SCAN_STRING", ! option_sense);
276 outfile return OPT_OUTFILE;
277 prefix return OPT_PREFIX;
278 yyclass return OPT_YYCLASS;
281 strcpy( nmstr, yytext + 1 );
282 nmstr[strlen( nmstr ) - 1] = '\0';
286 (([a-mo-z]|n[a-np-z])[[:alpha:]\-+]*)|. {
287 format_synerr( _( "unrecognized %%option: %s" ),
293 <RECOVER>.*{NL} ++linenum; BEGIN(INITIAL);
297 ^"%{".* ++bracelevel; yyless( 2 ); /* eat only %{ */
298 ^"%}".* --bracelevel; yyless( 2 ); /* eat only %} */
300 ^{WS}.* ACTION_ECHO; /* indented code in prolog */
302 ^{NOT_WS}.* { /* non-indented code */
303 if ( bracelevel <= 0 )
304 { /* not in %{ ... %} */
305 yyless( 0 ); /* put it all back */
315 {NL} ++linenum; ACTION_ECHO;
320 yyterminate(); /* to stop the parser */
325 ^{OPTWS}{NL} ++linenum; /* allow blank lines in section 2 */
328 indented_code = false;
329 doing_codeblock = true;
331 BEGIN(PERCENT_BRACE_ACTION);
334 ^{OPTWS}"<" BEGIN(SC); return '<';
335 ^{OPTWS}"^" return '^';
336 \" BEGIN(QUOTE); return '"';
337 "{"/[[:digit:]] BEGIN(NUM); return '{';
338 "$"/([[:blank:]]|{NL}) return '$';
342 BEGIN(PERCENT_BRACE_ACTION);
346 doing_rule_action = true;
351 {WS}"|".*{NL} continued_action = true; ++linenum; return '\n';
354 yyless( yyleng - 2 ); /* put back '/', '*' */
356 continued_action = false;
360 ^{WS} /* allow indented rules */
363 /* This rule is separate from the one below because
364 * otherwise we get variable trailing context, so
365 * we can't build the scanner using -{f,F}.
368 continued_action = false;
373 doing_rule_action = true;
381 continued_action = false;
383 unput( '\n' ); /* so <ACTION> sees it */
387 doing_rule_action = true;
394 "<<EOF>>" return EOF_OP;
399 yyterminate(); /* to stop the parser */
402 "["({FIRST_CCL_CHAR}|{CCL_EXPR})({CCL_CHAR}|{CCL_EXPR})* {
405 strcpy( nmstr, yytext );
407 /* Check to see if we've already encountered this
410 if ( (cclval = ccllookup( (Char *) nmstr )) != 0 )
412 if ( input() != ']' )
413 synerr( _( "bad character class" ) );
421 /* We fudge a bit. We know that this ccl will
422 * soon be numbered as lastccl + 1 by cclinit.
424 cclinstal( (Char *) nmstr, lastccl + 1 );
426 /* Push back everything but the leading bracket
427 * so the ccl can be rescanned.
437 register Char *nmdefptr;
440 strcpy( nmstr, yytext + 1 );
441 nmstr[yyleng - 2] = '\0'; /* chop trailing brace */
443 if ( (nmdefptr = ndlookup( nmstr )) == 0 )
445 _( "undefined definition {%s}" ),
449 { /* push back name surrounded by ()'s */
450 int len = strlen( (char *) nmdefptr );
452 if ( lex_compat || nmdefptr[0] == '^' ||
453 (len > 0 && nmdefptr[len - 1] == '$') )
454 { /* don't use ()'s after all */
455 PUT_BACK_STRING((char *) nmdefptr, 0);
457 if ( nmdefptr[0] == '^' )
464 PUT_BACK_STRING((char *) nmdefptr, 0);
470 [/|*+?.(){}] return (unsigned char) yytext[0];
476 [,*] return (unsigned char) yytext[0];
477 ">" BEGIN(SECT2); return '>';
478 ">"/^ BEGIN(CARETISBOL); return '>';
481 format_synerr( _( "bad <start condition>: %s" ),
486 <CARETISBOL>"^" BEGIN(SECT2); return '^';
491 \" BEGIN(SECT2); return '"';
494 synerr( _( "missing quote" ) );
503 "^"/[^-\]\n] BEGIN(CCL); return '^';
504 "^"/("-"|"]") return '^';
505 . BEGIN(CCL); RETURNCHAR;
509 -/[^\]\n] return '-';
511 "]" BEGIN(SECT2); return ']';
513 synerr( _( "bad character class" ) );
520 "[:alnum:]" BEGIN(CCL); return CCE_ALNUM;
521 "[:alpha:]" BEGIN(CCL); return CCE_ALPHA;
522 "[:blank:]" BEGIN(CCL); return CCE_BLANK;
523 "[:cntrl:]" BEGIN(CCL); return CCE_CNTRL;
524 "[:digit:]" BEGIN(CCL); return CCE_DIGIT;
525 "[:graph:]" BEGIN(CCL); return CCE_GRAPH;
526 "[:lower:]" BEGIN(CCL); return CCE_LOWER;
527 "[:print:]" BEGIN(CCL); return CCE_PRINT;
528 "[:punct:]" BEGIN(CCL); return CCE_PUNCT;
529 "[:space:]" BEGIN(CCL); return CCE_SPACE;
530 "[:upper:]" BEGIN(CCL); return CCE_UPPER;
531 "[:xdigit:]" BEGIN(CCL); return CCE_XDIGIT;
534 _( "bad character class expression: %s" ),
536 BEGIN(CCL); return CCE_ALNUM;
542 yylval = myctoi( yytext );
547 "}" BEGIN(SECT2); return '}';
550 synerr( _( "bad character inside {}'s" ) );
556 synerr( _( "missing }" ) );
564 <PERCENT_BRACE_ACTION>{
565 {OPTWS}"%}".* bracelevel = 0;
567 <ACTION>"/*" ACTION_ECHO; yy_push_state( COMMENT );
572 CHECK_REJECT(yytext);
576 CHECK_YYMORE(yytext);
580 {NAME}|{NOT_NAME}|. ACTION_ECHO;
584 if ( bracelevel == 0 ||
585 (doing_codeblock && indented_code) )
587 if ( doing_rule_action )
588 add_action( "\tYY_BREAK\n" );
590 doing_rule_action = doing_codeblock = false;
597 /* Reject and YYmore() are checked for above, in PERCENT_BRACE_ACTION */
599 "{" ACTION_ECHO; ++bracelevel;
600 "}" ACTION_ECHO; --bracelevel;
601 [^[:alpha:]_{}"'/\n]+ ACTION_ECHO;
603 "'"([^'\\\n]|\\.)*"'" ACTION_ECHO; /* character constant */
604 \" ACTION_ECHO; BEGIN(ACTION_STRING);
608 if ( bracelevel == 0 )
610 if ( doing_rule_action )
611 add_action( "\tYY_BREAK\n" );
613 doing_rule_action = false;
621 [^"\\\n]+ ACTION_ECHO;
623 {NL} ++linenum; ACTION_ECHO;
624 \" ACTION_ECHO; BEGIN(ACTION);
628 <COMMENT,ACTION,ACTION_STRING><<EOF>> {
629 synerr( _( "EOF encountered inside an action" ) );
634 <SECT2,QUOTE,FIRSTCCL,CCL>{ESCSEQ} {
635 yylval = myesc( (Char *) yytext );
637 if ( YY_START == FIRSTCCL )
646 <<EOF>> sectnum = 0; yyterminate();
649 <*>.|\n format_synerr( _( "bad character: %s" ), yytext );
656 if ( --num_input_files > 0 )
658 set_input_file( *++input_files );
667 /* set_input_file - open the given file (if NULL, stdin) for scanning */
669 void set_input_file( file )
672 if ( file && strcmp( file, "-" ) )
674 infilename = copy_string( file );
675 yyin = fopen( infilename, "r" );
678 lerrsf( _( "can't open %s" ), file );
684 infilename = copy_string( "<stdin>" );
691 /* Wrapper routines for accessing the scanner's malloc routines. */
693 void *flex_alloc( size )
696 return (void *) malloc( size );
699 void *flex_realloc( ptr, size )
703 return (void *) realloc( ptr, size );
706 void flex_free( ptr )