Sweep-fix comparing pointers with 0 (and assigning 0 to pointers).
[dragonfly.git] / usr.bin / lex / main.c
1 /* flex - tool to generate fast lexical analyzers */
2
3 /*-
4  * Copyright (c) 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Vern Paxson.
9  *
10  * The United States Government has rights in this work pursuant
11  * to contract no. DE-AC03-76SF00098 between the United States
12  * Department of Energy and the University of California.
13  *
14  * Redistribution and use in source and binary forms are permitted provided
15  * that: (1) source distributions retain this entire copyright notice and
16  * comment, and (2) distributions including binaries display the following
17  * acknowledgement:  ``This product includes software developed by the
18  * University of California, Berkeley and its contributors'' in the
19  * documentation or other materials provided with the distribution and in
20  * all advertising materials mentioning features or use of this software.
21  * Neither the name of the University nor the names of its contributors may
22  * be used to endorse or promote products derived from this software without
23  * specific prior written permission.
24  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
25  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27  *
28  * @(#) Copyright (c) 1990 The Regents of the University of California. All rights reserved.
29  * $Header: /home/daffy/u0/vern/flex/RCS/main.c,v 2.64 96/05/25 20:42:42 vern Exp $
30  */
31
32 /* $Header: /home/daffy/u0/vern/flex/RCS/main.c,v 2.64 96/05/25 20:42:42 vern Exp $ */
33 /* $FreeBSD: src/usr.bin/lex/main.c,v 1.7 1999/10/27 07:56:45 obrien Exp $ */
34
35
36 #include "flexdef.h"
37 #include "version.h"
38
39 static char flex_version[] = FLEX_VERSION;
40
41
42 /* declare functions that have forward references */
43
44 void flexinit PROTO((int, char**));
45 void readin PROTO((void));
46 void set_up_initial_allocations PROTO((void));
47
48 #ifdef NEED_ARGV_FIXUP
49 extern void argv_fixup PROTO((int *, char ***));
50 #endif
51
52
53 /* these globals are all defined and commented in flexdef.h */
54 int printstats, syntaxerror, eofseen, ddebug, trace, nowarn, spprdflt;
55 int interactive, caseins, lex_compat, do_yylineno, useecs, fulltbl, usemecs;
56 int fullspd, gen_line_dirs, performance_report, backing_up_report;
57 int C_plus_plus, long_align, use_read, yytext_is_array, do_yywrap, csize;
58 int yymore_used, reject, real_reject, continued_action, in_rule;
59 int yymore_really_used, reject_really_used;
60 int datapos, dataline, linenum, out_linenum;
61 FILE *skelfile = NULL;
62 int skel_ind = 0;
63 char *action_array;
64 int action_size, defs1_offset, prolog_offset, action_offset, action_index;
65 char *infilename = NULL, *outfilename = NULL;
66 int did_outfilename;
67 char *prefix, *yyclass;
68 int do_stdinit, use_stdout;
69 int onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE];
70 int onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp;
71 int current_mns, current_max_rules;
72 int num_rules, num_eof_rules, default_rule, lastnfa;
73 int *firstst, *lastst, *finalst, *transchar, *trans1, *trans2;
74 int *accptnum, *assoc_rule, *state_type;
75 int *rule_type, *rule_linenum, *rule_useful;
76 int current_state_type;
77 int variable_trailing_context_rules;
78 int numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP];
79 int protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE];
80 int numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs, tecfwd[CSIZE + 1];
81 int tecbck[CSIZE + 1];
82 int lastsc, *scset, *scbol, *scxclu, *sceof;
83 int current_max_scs;
84 char **scname;
85 int current_max_dfa_size, current_max_xpairs;
86 int current_max_template_xpairs, current_max_dfas;
87 int lastdfa, *nxt, *chk, *tnxt;
88 int *base, *def, *nultrans, NUL_ec, tblend, firstfree, **dss, *dfasiz;
89 union dfaacc_union *dfaacc;
90 int *accsiz, *dhash, numas;
91 int numsnpairs, jambase, jamstate;
92 int lastccl, *cclmap, *ccllen, *cclng, cclreuse;
93 int current_maxccls, current_max_ccl_tbl_size;
94 Char *ccltbl;
95 char nmstr[MAXLINE];
96 int sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs;
97 int tmpuses, totnst, peakpairs, numuniq, numdup, hshsave;
98 int num_backing_up, bol_needed;
99 FILE *backing_up_file;
100 int end_of_buffer_state;
101 char **input_files;
102 int num_input_files;
103
104 /* Make sure program_name is initialized so we don't crash if writing
105  * out an error message before getting the program name from argv[0].
106  */
107 char *program_name = "flex";
108
109 #ifndef SHORT_FILE_NAMES
110 static char *outfile_template = "lex.%s.%s";
111 static char *backing_name = "lex.backup";
112 #else
113 static char *outfile_template = "lex%s.%s";
114 static char *backing_name = "lex.bck";
115 #endif
116
117 static char outfile_path[MAXLINE];
118 static int outfile_created = 0;
119 static char *skelname = NULL;
120
121
122 int main(int argc, char **argv)
123         {
124         int i;
125
126 #ifdef NEED_ARGV_FIXUP
127         argv_fixup( &argc, &argv );
128 #endif
129
130         flexinit( argc, argv );
131
132         readin();
133
134         ntod();
135
136         for ( i = 1; i <= num_rules; ++i )
137                 if ( ! rule_useful[i] && i != default_rule )
138                         line_warning( _( "rule cannot be matched" ),
139                                         rule_linenum[i] );
140
141         if ( spprdflt && ! reject && rule_useful[default_rule] )
142                 line_warning(
143                         _( "-s option given but default rule can be matched" ),
144                         rule_linenum[default_rule] );
145
146         /* Generate the C state transition tables from the DFA. */
147         make_tables();
148
149         /* Note, flexend does not return.  It exits with its argument
150          * as status.
151          */
152         flexend( 0 );
153
154         return 0;       /* keep compilers/lint happy */
155         }
156
157
158 /* check_options - check user-specified options */
159
160 void check_options(void)
161         {
162         int i;
163
164         if ( lex_compat )
165                 {
166                 if ( C_plus_plus )
167                         flexerror( _( "Can't use -+ with -l option" ) );
168
169                 if ( fulltbl || fullspd )
170                         flexerror( _( "Can't use -f or -F with -l option" ) );
171
172                 /* Don't rely on detecting use of yymore() and REJECT,
173                  * just assume they'll be used.
174                  */
175                 yymore_really_used = reject_really_used = true;
176
177                 yytext_is_array = true;
178                 do_yylineno = true;
179                 use_read = false;
180                 }
181
182         if ( do_yylineno )
183                 /* This should really be "maintain_backup_tables = true" */
184                 reject_really_used = true;
185
186         if ( csize == unspecified )
187                 {
188                 if ( (fulltbl || fullspd) && ! useecs )
189                         csize = DEFAULT_CSIZE;
190                 else
191                         csize = CSIZE;
192                 }
193
194         if ( interactive == unspecified )
195                 {
196                 if ( fulltbl || fullspd )
197                         interactive = false;
198                 else
199                         interactive = true;
200                 }
201
202         if ( fulltbl || fullspd )
203                 {
204                 if ( usemecs )
205                         flexerror(
206                         _( "-Cf/-CF and -Cm don't make sense together" ) );
207
208                 if ( interactive )
209                         flexerror( _( "-Cf/-CF and -I are incompatible" ) );
210
211                 if ( lex_compat )
212                         flexerror(
213                 _( "-Cf/-CF are incompatible with lex-compatibility mode" ) );
214
215                 if ( do_yylineno )
216                         flexerror(
217                         _( "-Cf/-CF and %option yylineno are incompatible" ) );
218
219                 if ( fulltbl && fullspd )
220                         flexerror( _( "-Cf and -CF are mutually exclusive" ) );
221                 }
222
223         if ( C_plus_plus && fullspd )
224                 flexerror( _( "Can't use -+ with -CF option" ) );
225
226         if ( C_plus_plus && yytext_is_array )
227                 {
228                 warn( _( "%array incompatible with -+ option" ) );
229                 yytext_is_array = false;
230                 }
231
232         if ( useecs )
233                 { /* Set up doubly-linked equivalence classes. */
234
235                 /* We loop all the way up to csize, since ecgroup[csize] is
236                  * the position used for NUL characters.
237                  */
238                 ecgroup[1] = NIL;
239
240                 for ( i = 2; i <= csize; ++i )
241                         {
242                         ecgroup[i] = i - 1;
243                         nextecm[i - 1] = i;
244                         }
245
246                 nextecm[csize] = NIL;
247                 }
248
249         else
250                 {
251                 /* Put everything in its own equivalence class. */
252                 for ( i = 1; i <= csize; ++i )
253                         {
254                         ecgroup[i] = i;
255                         nextecm[i] = BAD_SUBSCRIPT;     /* to catch errors */
256                         }
257                 }
258
259         if ( ! use_stdout )
260                 {
261                 FILE *prev_stdout;
262
263                 if ( ! did_outfilename )
264                         {
265                         char *suffix;
266
267                         if ( C_plus_plus )
268                                 suffix = "cc";
269                         else
270                                 suffix = "c";
271
272                         sprintf( outfile_path, outfile_template,
273                                 prefix, suffix );
274
275                         outfilename = outfile_path;
276                         }
277
278                 prev_stdout = freopen( outfilename, "w", stdout );
279
280                 if ( prev_stdout == NULL )
281                         lerrsf( _( "could not create %s" ), outfilename );
282
283                 outfile_created = 1;
284                 }
285
286         if ( skelname && (skelfile = fopen( skelname, "r" )) == NULL )
287                 lerrsf( _( "can't open skeleton file %s" ), skelname );
288
289         if ( strcmp( prefix, "yy" ) )
290                 {
291 #define GEN_PREFIX(name) out_str3( "#define yy%s %s%s\n", name, prefix, name )
292                 if ( C_plus_plus )
293                         GEN_PREFIX( "FlexLexer" );
294                 else
295                         {
296                         GEN_PREFIX( "_create_buffer" );
297                         GEN_PREFIX( "_delete_buffer" );
298                         GEN_PREFIX( "_scan_buffer" );
299                         GEN_PREFIX( "_scan_string" );
300                         GEN_PREFIX( "_scan_bytes" );
301                         GEN_PREFIX( "_flex_debug" );
302                         GEN_PREFIX( "_init_buffer" );
303                         GEN_PREFIX( "_flush_buffer" );
304                         GEN_PREFIX( "_load_buffer_state" );
305                         GEN_PREFIX( "_switch_to_buffer" );
306                         GEN_PREFIX( "in" );
307                         GEN_PREFIX( "leng" );
308                         GEN_PREFIX( "lex" );
309                         GEN_PREFIX( "out" );
310                         GEN_PREFIX( "restart" );
311                         GEN_PREFIX( "text" );
312
313                         if ( do_yylineno )
314                                 GEN_PREFIX( "lineno" );
315                         }
316
317                 if ( do_yywrap )
318                         GEN_PREFIX( "wrap" );
319
320                 outn( "" );
321                 }
322
323         if ( did_outfilename )
324                 line_directive_out( stdout, 0 );
325
326         skelout();
327         }
328
329
330 /* flexend - terminate flex
331  *
332  * note
333  *    This routine does not return.
334  */
335
336 void flexend(int exit_status)
337         {
338         int tblsiz;
339         int unlink();
340
341         if ( skelfile != NULL )
342                 {
343                 if ( ferror( skelfile ) )
344                         lerrsf( _( "input error reading skeleton file %s" ),
345                                 skelname );
346
347                 else if ( fclose( skelfile ) )
348                         lerrsf( _( "error closing skeleton file %s" ),
349                                 skelname );
350                 }
351
352         if ( exit_status != 0 && outfile_created )
353                 {
354                 if ( ferror( stdout ) )
355                         lerrsf( _( "error writing output file %s" ),
356                                 outfilename );
357
358                 else if ( fclose( stdout ) )
359                         lerrsf( _( "error closing output file %s" ),
360                                 outfilename );
361
362                 else if ( unlink( outfilename ) )
363                         lerrsf( _( "error deleting output file %s" ),
364                                 outfilename );
365                 }
366
367         if ( backing_up_report && backing_up_file )
368                 {
369                 if ( num_backing_up == 0 )
370                         fprintf( backing_up_file, _( "No backing up.\n" ) );
371                 else if ( fullspd || fulltbl )
372                         fprintf( backing_up_file,
373                                 _( "%d backing up (non-accepting) states.\n" ),
374                                 num_backing_up );
375                 else
376                         fprintf( backing_up_file,
377                                 _( "Compressed tables always back up.\n" ) );
378
379                 if ( ferror( backing_up_file ) )
380                         lerrsf( _( "error writing backup file %s" ),
381                                 backing_name );
382
383                 else if ( fclose( backing_up_file ) )
384                         lerrsf( _( "error closing backup file %s" ),
385                                 backing_name );
386                 }
387
388         if ( printstats )
389                 {
390                 fprintf( stderr, _( "%s version %s usage statistics:\n" ),
391                         program_name, flex_version );
392
393                 fprintf( stderr, _( "  scanner options: -" ) );
394
395                 if ( C_plus_plus )
396                         putc( '+', stderr );
397                 if ( backing_up_report )
398                         putc( 'b', stderr );
399                 if ( ddebug )
400                         putc( 'd', stderr );
401                 if ( caseins )
402                         putc( 'i', stderr );
403                 if ( lex_compat )
404                         putc( 'l', stderr );
405                 if ( performance_report > 0 )
406                         putc( 'p', stderr );
407                 if ( performance_report > 1 )
408                         putc( 'p', stderr );
409                 if ( spprdflt )
410                         putc( 's', stderr );
411                 if ( use_stdout )
412                         putc( 't', stderr );
413                 if ( printstats )
414                         putc( 'v', stderr );    /* always true! */
415                 if ( nowarn )
416                         putc( 'w', stderr );
417                 if ( interactive == false )
418                         putc( 'B', stderr );
419                 if ( interactive == true )
420                         putc( 'I', stderr );
421                 if ( ! gen_line_dirs )
422                         putc( 'L', stderr );
423                 if ( trace )
424                         putc( 'T', stderr );
425
426                 if ( csize == unspecified )
427                         /* We encountered an error fairly early on, so csize
428                          * never got specified.  Define it now, to prevent
429                          * bogus table sizes being written out below.
430                          */
431                         csize = 256;
432
433                 if ( csize == 128 )
434                         putc( '7', stderr );
435                 else
436                         putc( '8', stderr );
437
438                 fprintf( stderr, " -C" );
439
440                 if ( long_align )
441                         putc( 'a', stderr );
442                 if ( fulltbl )
443                         putc( 'f', stderr );
444                 if ( fullspd )
445                         putc( 'F', stderr );
446                 if ( useecs )
447                         putc( 'e', stderr );
448                 if ( usemecs )
449                         putc( 'm', stderr );
450                 if ( use_read )
451                         putc( 'r', stderr );
452
453                 if ( did_outfilename )
454                         fprintf( stderr, " -o%s", outfilename );
455
456                 if ( skelname )
457                         fprintf( stderr, " -S%s", skelname );
458
459                 if ( strcmp( prefix, "yy" ) )
460                         fprintf( stderr, " -P%s", prefix );
461
462                 putc( '\n', stderr );
463
464                 fprintf( stderr, _( "  %d/%d NFA states\n" ),
465                         lastnfa, current_mns );
466                 fprintf( stderr, _( "  %d/%d DFA states (%d words)\n" ),
467                         lastdfa, current_max_dfas, totnst );
468                 fprintf( stderr, _( "  %d rules\n" ),
469                 num_rules + num_eof_rules - 1 /* - 1 for def. rule */ );
470
471                 if ( num_backing_up == 0 )
472                         fprintf( stderr, _( "  No backing up\n" ) );
473                 else if ( fullspd || fulltbl )
474                         fprintf( stderr,
475                         _( "  %d backing-up (non-accepting) states\n" ),
476                                 num_backing_up );
477                 else
478                         fprintf( stderr,
479                                 _( "  Compressed tables always back-up\n" ) );
480
481                 if ( bol_needed )
482                         fprintf( stderr,
483                                 _( "  Beginning-of-line patterns used\n" ) );
484
485                 fprintf( stderr, _( "  %d/%d start conditions\n" ), lastsc,
486                         current_max_scs );
487                 fprintf( stderr,
488                         _( "  %d epsilon states, %d double epsilon states\n" ),
489                         numeps, eps2 );
490
491                 if ( lastccl == 0 )
492                         fprintf( stderr, _( "  no character classes\n" ) );
493                 else
494                         fprintf( stderr,
495 _( "  %d/%d character classes needed %d/%d words of storage, %d reused\n" ),
496                                 lastccl, current_maxccls,
497                                 cclmap[lastccl] + ccllen[lastccl],
498                                 current_max_ccl_tbl_size, cclreuse );
499
500                 fprintf( stderr, _( "  %d state/nextstate pairs created\n" ),
501                         numsnpairs );
502                 fprintf( stderr, _( "  %d/%d unique/duplicate transitions\n" ),
503                         numuniq, numdup );
504
505                 if ( fulltbl )
506                         {
507                         tblsiz = lastdfa * numecs;
508                         fprintf( stderr, _( "  %d table entries\n" ), tblsiz );
509                         }
510
511                 else
512                         {
513                         tblsiz = 2 * (lastdfa + numtemps) + 2 * tblend;
514
515                         fprintf( stderr,
516                                 _( "  %d/%d base-def entries created\n" ),
517                                 lastdfa + numtemps, current_max_dfas );
518                         fprintf( stderr,
519                         _( "  %d/%d (peak %d) nxt-chk entries created\n" ),
520                                 tblend, current_max_xpairs, peakpairs );
521                         fprintf( stderr,
522                 _( "  %d/%d (peak %d) template nxt-chk entries created\n" ),
523                                 numtemps * nummecs,
524                                 current_max_template_xpairs,
525                                 numtemps * numecs );
526                         fprintf( stderr, _( "  %d empty table entries\n" ),
527                                 nummt );
528                         fprintf( stderr, _( "  %d protos created\n" ),
529                                 numprots );
530                         fprintf( stderr,
531                                 _( "  %d templates created, %d uses\n" ),
532                                 numtemps, tmpuses );
533                         }
534
535                 if ( useecs )
536                         {
537                         tblsiz = tblsiz + csize;
538                         fprintf( stderr,
539                                 _( "  %d/%d equivalence classes created\n" ),
540                                 numecs, csize );
541                         }
542
543                 if ( usemecs )
544                         {
545                         tblsiz = tblsiz + numecs;
546                         fprintf( stderr,
547                         _( "  %d/%d meta-equivalence classes created\n" ),
548                                 nummecs, csize );
549                         }
550
551                 fprintf( stderr,
552                 _( "  %d (%d saved) hash collisions, %d DFAs equal\n" ),
553                         hshcol, hshsave, dfaeql );
554                 fprintf( stderr, _( "  %d sets of reallocations needed\n" ),
555                         num_reallocs );
556                 fprintf( stderr, _( "  %d total table entries needed\n" ),
557                         tblsiz );
558                 }
559
560         exit( exit_status );
561         }
562
563
564 /* flexinit - initialize flex */
565
566 void flexinit(int argc, char **argv)
567         {
568         int i, sawcmpflag;
569         char *arg;
570
571         printstats = syntaxerror = trace = spprdflt = caseins = false;
572         lex_compat = C_plus_plus = backing_up_report = ddebug = fulltbl = false;
573         fullspd = long_align = nowarn = yymore_used = continued_action = false;
574         do_yylineno = yytext_is_array = in_rule = reject = do_stdinit = false;
575         yymore_really_used = reject_really_used = unspecified;
576         interactive = csize = unspecified;
577         do_yywrap = gen_line_dirs = usemecs = useecs = true;
578         performance_report = 0;
579         did_outfilename = 0;
580         prefix = "yy";
581         yyclass = NULL;
582         use_read = use_stdout = false;
583
584         sawcmpflag = false;
585
586         /* Initialize dynamic array for holding the rule actions. */
587         action_size = 2048;     /* default size of action array in bytes */
588         action_array = allocate_character_array( action_size );
589         defs1_offset = prolog_offset = action_offset = action_index = 0;
590         action_array[0] = '\0';
591
592         program_name = argv[0];
593
594         if ( program_name[0] != '\0' &&
595              program_name[strlen( program_name ) - 1] == '+' )
596                 C_plus_plus = true;
597
598         /* read flags */
599         for ( --argc, ++argv; argc ; --argc, ++argv )
600                 {
601                 arg = argv[0];
602
603                 if ( arg[0] != '-' || arg[1] == '\0' )
604                         break;
605
606                 if ( arg[1] == '-' )
607                         { /* --option */
608                         if ( ! strcmp( arg, "--help" ) )
609                                 arg = "-h";
610
611                         else if ( ! strcmp( arg, "--version" ) )
612                                 arg = "-V";
613
614                         else if ( ! strcmp( arg, "--" ) )
615                                 { /* end of options */
616                                 --argc;
617                                 ++argv;
618                                 break;
619                                 }
620                         }
621
622                 for ( i = 1; arg[i] != '\0'; ++i )
623                         switch ( arg[i] )
624                                 {
625                                 case '+':
626                                         C_plus_plus = true;
627                                         break;
628
629                                 case 'B':
630                                         interactive = false;
631                                         break;
632
633                                 case 'b':
634                                         backing_up_report = true;
635                                         break;
636
637                                 case 'c':
638                                         break;
639
640                                 case 'C':
641                                         if ( i != 1 )
642                                                 flexerror(
643                                 _( "-C flag must be given separately" ) );
644
645                                         if ( ! sawcmpflag )
646                                                 {
647                                                 useecs = false;
648                                                 usemecs = false;
649                                                 fulltbl = false;
650                                                 sawcmpflag = true;
651                                                 }
652
653                                         for ( ++i; arg[i] != '\0'; ++i )
654                                                 switch ( arg[i] )
655                                                         {
656                                                         case 'a':
657                                                                 long_align =
658                                                                         true;
659                                                                 break;
660
661                                                         case 'e':
662                                                                 useecs = true;
663                                                                 break;
664
665                                                         case 'F':
666                                                                 fullspd = true;
667                                                                 break;
668
669                                                         case 'f':
670                                                                 fulltbl = true;
671                                                                 break;
672
673                                                         case 'm':
674                                                                 usemecs = true;
675                                                                 break;
676
677                                                         case 'r':
678                                                                 use_read = true;
679                                                                 break;
680
681                                                         default:
682                                                                 lerrif(
683                                                 _( "unknown -C option '%c'" ),
684                                                                 (int) arg[i] );
685                                                                 break;
686                                                         }
687
688                                         goto get_next_arg;
689
690                                 case 'd':
691                                         ddebug = true;
692                                         break;
693
694                                 case 'f':
695                                         useecs = usemecs = false;
696                                         use_read = fulltbl = true;
697                                         break;
698
699                                 case 'F':
700                                         useecs = usemecs = false;
701                                         use_read = fullspd = true;
702                                         break;
703
704                                 case '?':
705                                 case 'h':
706                                         usage();
707                                         exit( 0 );
708
709                                 case 'I':
710                                         interactive = true;
711                                         break;
712
713                                 case 'i':
714                                         caseins = true;
715                                         break;
716
717                                 case 'l':
718                                         lex_compat = true;
719                                         break;
720
721                                 case 'L':
722                                         gen_line_dirs = false;
723                                         break;
724
725                                 case 'n':
726                                         /* Stupid do-nothing deprecated
727                                          * option.
728                                          */
729                                         break;
730
731                                 case 'o':
732                                         if ( i != 1 )
733                                                 flexerror(
734                                 _( "-o flag must be given separately" ) );
735
736                                         outfilename = arg + i + 1;
737                                         did_outfilename = 1;
738                                         goto get_next_arg;
739
740                                 case 'P':
741                                         if ( i != 1 )
742                                                 flexerror(
743                                 _( "-P flag must be given separately" ) );
744
745                                         prefix = arg + i + 1;
746                                         goto get_next_arg;
747
748                                 case 'p':
749                                         ++performance_report;
750                                         break;
751
752                                 case 'S':
753                                         if ( i != 1 )
754                                                 flexerror(
755                                 _( "-S flag must be given separately" ) );
756
757                                         skelname = arg + i + 1;
758                                         goto get_next_arg;
759
760                                 case 's':
761                                         spprdflt = true;
762                                         break;
763
764                                 case 't':
765                                         use_stdout = true;
766                                         break;
767
768                                 case 'T':
769                                         trace = true;
770                                         break;
771
772                                 case 'v':
773                                         printstats = true;
774                                         break;
775
776                                 case 'V':
777                                         printf( _( "%s version %s\n" ),
778                                                 program_name, flex_version );
779                                         exit( 0 );
780
781                                 case 'w':
782                                         nowarn = true;
783                                         break;
784
785                                 case '7':
786                                         csize = 128;
787                                         break;
788
789                                 case '8':
790                                         csize = CSIZE;
791                                         break;
792
793                                 default:
794                                         fprintf( stderr,
795                 _( "%s: unknown flag '%c'.  For usage, try\n\t%s --help\n" ),
796                                                 program_name, (int) arg[i],
797                                                 program_name );
798                                         exit( 1 );
799                                 }
800
801                 /* Used by -C, -S, -o, and -P flags in lieu of a "continue 2"
802                  * control.
803                  */
804                 get_next_arg: ;
805                 }
806
807         num_input_files = argc;
808         input_files = argv;
809         set_input_file( num_input_files > 0 ? input_files[0] : NULL );
810
811         lastccl = lastsc = lastdfa = lastnfa = 0;
812         num_rules = num_eof_rules = default_rule = 0;
813         numas = numsnpairs = tmpuses = 0;
814         numecs = numeps = eps2 = num_reallocs = hshcol = dfaeql = totnst = 0;
815         numuniq = numdup = hshsave = eofseen = datapos = dataline = 0;
816         num_backing_up = onesp = numprots = 0;
817         variable_trailing_context_rules = bol_needed = false;
818
819         out_linenum = linenum = sectnum = 1;
820         firstprot = NIL;
821
822         /* Used in mkprot() so that the first proto goes in slot 1
823          * of the proto queue.
824          */
825         lastprot = 1;
826
827         set_up_initial_allocations();
828         }
829
830
831 /* readin - read in the rules section of the input file(s) */
832
833 void readin(void)
834         {
835         static char yy_stdinit[] = "FILE *yyin = stdin, *yyout = stdout;";
836         static char yy_nostdinit[] =
837                 "FILE *yyin = NULL, *yyout = NULL;";
838
839         line_directive_out( NULL, 1 );
840
841         if ( yyparse() )
842                 {
843                 pinpoint_message( _( "fatal parse error" ) );
844                 flexend( 1 );
845                 }
846
847         if ( syntaxerror )
848                 flexend( 1 );
849
850         if ( backing_up_report )
851                 {
852                 backing_up_file = fopen( backing_name, "w" );
853                 if ( backing_up_file == NULL )
854                         lerrsf(
855                         _( "could not create backing-up info file %s" ),
856                                 backing_name );
857                 }
858
859         else
860                 backing_up_file = NULL;
861
862         if ( yymore_really_used == true )
863                 yymore_used = true;
864         else if ( yymore_really_used == false )
865                 yymore_used = false;
866
867         if ( reject_really_used == true )
868                 reject = true;
869         else if ( reject_really_used == false )
870                 reject = false;
871
872         if ( performance_report > 0 )
873                 {
874                 if ( lex_compat )
875                         {
876                         fprintf( stderr,
877 _( "-l AT&T lex compatibility option entails a large performance penalty\n" ) );
878                         fprintf( stderr,
879 _( " and may be the actual source of other reported performance penalties\n" ) );
880                         }
881
882                 else if ( do_yylineno )
883                         {
884                         fprintf( stderr,
885         _( "%%option yylineno entails a large performance penalty\n" ) );
886                         }
887
888                 if ( performance_report > 1 )
889                         {
890                         if ( interactive )
891                                 fprintf( stderr,
892         _( "-I (interactive) entails a minor performance penalty\n" ) );
893
894                         if ( yymore_used )
895                                 fprintf( stderr,
896                 _( "yymore() entails a minor performance penalty\n" ) );
897                         }
898
899                 if ( reject )
900                         fprintf( stderr,
901                         _( "REJECT entails a large performance penalty\n" ) );
902
903                 if ( variable_trailing_context_rules )
904                         fprintf( stderr,
905 _( "Variable trailing context rules entail a large performance penalty\n" ) );
906                 }
907
908         if ( reject )
909                 real_reject = true;
910
911         if ( variable_trailing_context_rules )
912                 reject = true;
913
914         if ( (fulltbl || fullspd) && reject )
915                 {
916                 if ( real_reject )
917                         flexerror(
918                                 _( "REJECT cannot be used with -f or -F" ) );
919                 else if ( do_yylineno )
920                         flexerror(
921                         _( "%option yylineno cannot be used with -f or -F" ) );
922                 else
923                         flexerror(
924         _( "variable trailing context rules cannot be used with -f or -F" ) );
925                 }
926
927         if ( reject )
928                 outn( "\n#define YY_USES_REJECT" );
929
930         if ( ! do_yywrap )
931                 {
932                 outn( "\n#define yywrap() 1" );
933                 outn( "#define YY_SKIP_YYWRAP" );
934                 }
935
936         if ( ddebug )
937                 outn( "\n#define FLEX_DEBUG" );
938
939         if ( csize == 256 )
940                 outn( "typedef unsigned char YY_CHAR;" );
941         else
942                 outn( "typedef char YY_CHAR;" );
943
944         if ( C_plus_plus )
945                 {
946                 outn( "#define yytext_ptr yytext" );
947
948                 if ( interactive )
949                         outn( "#define YY_INTERACTIVE" );
950                 }
951
952         else
953                 {
954                 if ( do_stdinit )
955                         {
956                         outn( yy_stdinit );
957                         }
958
959                 else
960                         outn( yy_nostdinit );
961                 }
962
963         if ( fullspd )
964                 outn( "typedef yyconst struct yy_trans_info *yy_state_type;" );
965         else if ( ! C_plus_plus )
966                 outn( "typedef int yy_state_type;" );
967
968         if ( ddebug )
969                 outn( "\n#define FLEX_DEBUG" );
970
971         if ( lex_compat )
972                 outn( "#define YY_FLEX_LEX_COMPAT" );
973
974         if ( do_yylineno && ! C_plus_plus )
975                 {
976                 outn( "extern int yylineno;" );
977                 outn( "int yylineno = 1;" );
978                 }
979
980         if ( C_plus_plus )
981                 {
982                 outn( "\n#include <FlexLexer.h>" );
983
984                 if ( yyclass )
985                         {
986                         outn( "int yyFlexLexer::yylex()" );
987                         outn( "\t{" );
988                         outn(
989 "\tLexerError( \"yyFlexLexer::yylex invoked but %option yyclass used\" );" );
990                         outn( "\treturn 0;" );
991                         outn( "\t}" );
992         
993                         out_str( "\n#define YY_DECL int %s::yylex()\n",
994                                 yyclass );
995                         }
996                 }
997
998         else
999                 {
1000                 if ( yytext_is_array )
1001                         outn( "extern char yytext[];\n" );
1002
1003                 else
1004                         {
1005                         outn( "extern char *yytext;" );
1006                         outn( "#define yytext_ptr yytext" );
1007                         }
1008
1009                 if ( yyclass )
1010                         flexerror(
1011                 _( "%option yyclass only meaningful for C++ scanners" ) );
1012                 }
1013
1014         if ( useecs )
1015                 numecs = cre8ecs( nextecm, ecgroup, csize );
1016         else
1017                 numecs = csize;
1018
1019         /* Now map the equivalence class for NUL to its expected place. */
1020         ecgroup[0] = ecgroup[csize];
1021         NUL_ec = ABS( ecgroup[0] );
1022
1023         if ( useecs )
1024                 ccl2ecl();
1025         }
1026
1027
1028 /* set_up_initial_allocations - allocate memory for internal tables */
1029
1030 void set_up_initial_allocations(void)
1031         {
1032         current_mns = INITIAL_MNS;
1033         firstst = allocate_integer_array( current_mns );
1034         lastst = allocate_integer_array( current_mns );
1035         finalst = allocate_integer_array( current_mns );
1036         transchar = allocate_integer_array( current_mns );
1037         trans1 = allocate_integer_array( current_mns );
1038         trans2 = allocate_integer_array( current_mns );
1039         accptnum = allocate_integer_array( current_mns );
1040         assoc_rule = allocate_integer_array( current_mns );
1041         state_type = allocate_integer_array( current_mns );
1042
1043         current_max_rules = INITIAL_MAX_RULES;
1044         rule_type = allocate_integer_array( current_max_rules );
1045         rule_linenum = allocate_integer_array( current_max_rules );
1046         rule_useful = allocate_integer_array( current_max_rules );
1047
1048         current_max_scs = INITIAL_MAX_SCS;
1049         scset = allocate_integer_array( current_max_scs );
1050         scbol = allocate_integer_array( current_max_scs );
1051         scxclu = allocate_integer_array( current_max_scs );
1052         sceof = allocate_integer_array( current_max_scs );
1053         scname = allocate_char_ptr_array( current_max_scs );
1054
1055         current_maxccls = INITIAL_MAX_CCLS;
1056         cclmap = allocate_integer_array( current_maxccls );
1057         ccllen = allocate_integer_array( current_maxccls );
1058         cclng = allocate_integer_array( current_maxccls );
1059
1060         current_max_ccl_tbl_size = INITIAL_MAX_CCL_TBL_SIZE;
1061         ccltbl = allocate_Character_array( current_max_ccl_tbl_size );
1062
1063         current_max_dfa_size = INITIAL_MAX_DFA_SIZE;
1064
1065         current_max_xpairs = INITIAL_MAX_XPAIRS;
1066         nxt = allocate_integer_array( current_max_xpairs );
1067         chk = allocate_integer_array( current_max_xpairs );
1068
1069         current_max_template_xpairs = INITIAL_MAX_TEMPLATE_XPAIRS;
1070         tnxt = allocate_integer_array( current_max_template_xpairs );
1071
1072         current_max_dfas = INITIAL_MAX_DFAS;
1073         base = allocate_integer_array( current_max_dfas );
1074         def = allocate_integer_array( current_max_dfas );
1075         dfasiz = allocate_integer_array( current_max_dfas );
1076         accsiz = allocate_integer_array( current_max_dfas );
1077         dhash = allocate_integer_array( current_max_dfas );
1078         dss = allocate_int_ptr_array( current_max_dfas );
1079         dfaacc = allocate_dfaacc_union( current_max_dfas );
1080
1081         nultrans = NULL;
1082         }
1083
1084
1085 void usage(void)
1086         {
1087         FILE *f = stdout;
1088
1089         fprintf( f,
1090 _( "%s [-bcdfhilnpstvwBFILTV78+? -C[aefFmr] -ooutput -Pprefix -Sskeleton]\n" ),
1091                 program_name );
1092         fprintf( f, _( "\t[--help --version] [file ...]\n" ) );
1093
1094         fprintf( f, _( "\t-b  generate backing-up information to %s\n" ),
1095                 backing_name );
1096         fprintf( f, _( "\t-c  do-nothing POSIX option\n" ) );
1097         fprintf( f, _( "\t-d  turn on debug mode in generated scanner\n" ) );
1098         fprintf( f, _( "\t-f  generate fast, large scanner\n" ) );
1099         fprintf( f, _( "\t-h  produce this help message\n" ) );
1100         fprintf( f, _( "\t-i  generate case-insensitive scanner\n" ) );
1101         fprintf( f, _( "\t-l  maximal compatibility with original lex\n" ) );
1102         fprintf( f, _( "\t-n  do-nothing POSIX option\n" ) );
1103         fprintf( f, _( "\t-p  generate performance report to stderr\n" ) );
1104         fprintf( f,
1105                 _( "\t-s  suppress default rule to ECHO unmatched text\n" ) );
1106
1107         if ( ! did_outfilename )
1108                 {
1109                 sprintf( outfile_path, outfile_template,
1110                         prefix, C_plus_plus ? "cc" : "c" );
1111                 outfilename = outfile_path;
1112                 }
1113
1114         fprintf( f,
1115                 _( "\t-t  write generated scanner on stdout instead of %s\n" ),
1116                 outfilename );
1117
1118         fprintf( f,
1119                 _( "\t-v  write summary of scanner statistics to f\n" ) );
1120         fprintf( f, _( "\t-w  do not generate warnings\n" ) );
1121         fprintf( f, _( "\t-B  generate batch scanner (opposite of -I)\n" ) );
1122         fprintf( f,
1123                 _( "\t-F  use alternative fast scanner representation\n" ) );
1124         fprintf( f,
1125                 _( "\t-I  generate interactive scanner (opposite of -B)\n" ) );
1126         fprintf( f, _( "\t-L  suppress #line directives in scanner\n" ) );
1127         fprintf( f, _( "\t-T  %s should run in trace mode\n" ), program_name );
1128         fprintf( f, _( "\t-V  report %s version\n" ), program_name );
1129         fprintf( f, _( "\t-7  generate 7-bit scanner\n" ) );
1130         fprintf( f, _( "\t-8  generate 8-bit scanner\n" ) );
1131         fprintf( f, _( "\t-+  generate C++ scanner class\n" ) );
1132         fprintf( f, _( "\t-?  produce this help message\n" ) );
1133         fprintf( f,
1134 _( "\t-C  specify degree of table compression (default is -Cem):\n" ) );
1135         fprintf( f,
1136 _( "\t\t-Ca  trade off larger tables for better memory alignment\n" ) );
1137         fprintf( f, _( "\t\t-Ce  construct equivalence classes\n" ) );
1138         fprintf( f,
1139 _( "\t\t-Cf  do not compress scanner tables; use -f representation\n" ) );
1140         fprintf( f,
1141 _( "\t\t-CF  do not compress scanner tables; use -F representation\n" ) );
1142         fprintf( f, _( "\t\t-Cm  construct meta-equivalence classes\n" ) );
1143         fprintf( f,
1144         _( "\t\t-Cr  use read() instead of stdio for scanner input\n" ) );
1145         fprintf( f, _( "\t-o  specify output filename\n" ) );
1146         fprintf( f, _( "\t-P  specify scanner prefix other than \"yy\"\n" ) );
1147         fprintf( f, _( "\t-S  specify skeleton file\n" ) );
1148         fprintf( f, _( "\t--help     produce this help message\n" ) );
1149         fprintf( f, _( "\t--version  report %s version\n" ), program_name );
1150         }