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