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