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