1 /* flex - tool to generate fast lexical analyzers */
3 /* Copyright (c) 1990 The Regents of the University of California. */
4 /* All rights reserved. */
6 /* This code is derived from software contributed to Berkeley by */
9 /* The United States Government has rights in this work pursuant */
10 /* to contract no. DE-AC03-76SF00098 between the United States */
11 /* Department of Energy and the University of California. */
13 /* This file is part of flex. */
15 /* Redistribution and use in source and binary forms, with or without */
16 /* modification, are permitted provided that the following conditions */
19 /* 1. Redistributions of source code must retain the above copyright */
20 /* notice, this list of conditions and the following disclaimer. */
21 /* 2. Redistributions in binary form must reproduce the above copyright */
22 /* notice, this list of conditions and the following disclaimer in the */
23 /* documentation and/or other materials provided with the distribution. */
25 /* Neither the name of the University nor the names of its contributors */
26 /* may be used to endorse or promote products derived from this software */
27 /* without specific prior written permission. */
29 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
30 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
31 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
40 static char flex_version[] = FLEX_VERSION;
42 /* declare functions that have forward references */
44 void flexinit PROTO ((int, char **));
45 void readin PROTO ((void));
46 void set_up_initial_allocations PROTO ((void));
47 static char *basename2 PROTO ((char *path, int should_strip_ext));
50 /* these globals are all defined and commented in flexdef.h */
51 int printstats, syntaxerror, eofseen, ddebug, trace, nowarn, spprdflt;
52 int interactive, lex_compat, posix_compat, do_yylineno,
53 useecs, fulltbl, usemecs;
54 int fullspd, gen_line_dirs, performance_report, backing_up_report;
55 int C_plus_plus, long_align, use_read, yytext_is_array, do_yywrap,
57 int reentrant, bison_bridge_lval, bison_bridge_lloc;
58 int yymore_used, reject, real_reject, continued_action, in_rule;
59 int yymore_really_used, reject_really_used;
60 int datapos, dataline, linenum;
61 FILE *skelfile = NULL;
64 int action_size, defs1_offset, prolog_offset, action_offset,
66 char *infilename = NULL, *outfilename = NULL, *headerfilename = NULL;
68 char *prefix, *yyclass, *extra_type = NULL;
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 maximum_mns, 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,
83 int tecbck[CSIZE + 1];
84 int lastsc, *scset, *scbol, *scxclu, *sceof;
87 int current_max_dfa_size, current_max_xpairs;
88 int current_max_template_xpairs, current_max_dfas;
89 int lastdfa, *nxt, *chk, *tnxt;
90 int *base, *def, *nultrans, NUL_ec, tblend, firstfree, **dss, *dfasiz;
91 union dfaacc_union *dfaacc;
92 int *accsiz, *dhash, numas;
93 int numsnpairs, jambase, jamstate;
94 int lastccl, *cclmap, *ccllen, *cclng, cclreuse;
95 int current_maxccls, current_max_ccl_tbl_size;
98 int sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs;
99 int tmpuses, totnst, peakpairs, numuniq, numdup, hshsave;
100 int num_backing_up, bol_needed;
101 FILE *backing_up_file;
102 int end_of_buffer_state;
105 jmp_buf flex_main_jmp_buf;
106 bool *rule_has_nl, *ccl_has_nl;
108 bool ansi_func_defs, ansi_func_protos;
110 bool tablesext, tablesverify, gentables;
111 char *tablesfilename=0,*tablesname=0;
112 struct yytbl_writer tableswr;
114 /* Make sure program_name is initialized so we don't crash if writing
115 * out an error message before getting the program name from argv[0].
117 char *program_name = "flex";
119 #ifndef SHORT_FILE_NAMES
120 static char *outfile_template = "lex.%s.%s";
121 static char *backing_name = "lex.backup";
122 static char *tablesfile_template = "lex.%s.tables";
124 static char *outfile_template = "lex%s.%s";
125 static char *backing_name = "lex.bck";
126 static char *tablesfile_template = "lex%s.tbl";
130 extern unsigned _stklen = 16384;
136 static char outfile_path[MAXLINE];
137 static int outfile_created = 0;
138 static char *skelname = NULL;
139 static int _stdout_closed = 0; /* flag to prevent double-fclose() on stdout. */
140 const char *escaped_qstart = "[[]]M4_YY_NOOP[M4_YY_NOOP[M4_YY_NOOP[[]]";
141 const char *escaped_qend = "[[]]M4_YY_NOOP]M4_YY_NOOP]M4_YY_NOOP[[]]";
143 /* For debugging. The max number of filters to apply to skeleton. */
144 static int preproc_level = 1000;
146 int flex_main PROTO ((int argc, char *argv[]));
147 int main PROTO ((int argc, char *argv[]));
149 int flex_main (argc, argv)
153 int i, exit_status, child_status;
155 /* Set a longjmp target. Yes, I know it's a hack, but it gets worse: The
156 * return value of setjmp, if non-zero, is the desired exit code PLUS ONE.
157 * For example, if you want 'main' to return with code '2', then call
158 * longjmp() with an argument of 3. This is because it is invalid to
159 * specify a value of 0 to longjmp. FLEX_EXIT(n) should be used instead of
162 exit_status = setjmp (flex_main_jmp_buf);
164 if (stdout && !_stdout_closed && !ferror(stdout)){
168 while (wait(&child_status) > 0){
169 if (!WIFEXITED (child_status)
170 || WEXITSTATUS (child_status) != 0){
171 /* report an error of a child
173 if( exit_status <= 1 )
178 return exit_status - 1;
181 flexinit (argc, argv);
189 for (i = 1; i <= num_rules; ++i)
190 if (!rule_useful[i] && i != default_rule)
191 line_warning (_("rule cannot be matched"),
194 if (spprdflt && !reject && rule_useful[default_rule])
196 ("-s option given but default rule can be matched"),
197 rule_linenum[default_rule]);
199 /* Generate the C state transition tables from the DFA. */
202 /* Note, flexend does not return. It exits with its argument
207 return 0; /* keep compilers/lint happy */
210 /* Wrapper around flex_main, so flex_main can be built as a library. */
211 int main (argc, argv)
217 setlocale (LC_MESSAGES, "");
218 setlocale (LC_CTYPE, "");
219 textdomain (PACKAGE);
220 bindtextdomain (PACKAGE, LOCALEDIR);
224 return flex_main (argc, argv);
227 /* check_options - check user-specified options */
229 void check_options ()
232 const char * m4 = NULL;
236 flexerror (_("Can't use -+ with -l option"));
238 if (fulltbl || fullspd)
239 flexerror (_("Can't use -f or -F with -l option"));
241 if (reentrant || bison_bridge_lval)
243 ("Can't use --reentrant or --bison-bridge with -l option"));
245 yytext_is_array = true;
252 /* This makes no sense whatsoever. I'm removing it. */
254 /* This should really be "maintain_backup_tables = true" */
255 reject_really_used = true;
258 if (csize == unspecified) {
259 if ((fulltbl || fullspd) && !useecs)
260 csize = DEFAULT_CSIZE;
265 if (interactive == unspecified) {
266 if (fulltbl || fullspd)
272 if (fulltbl || fullspd) {
275 ("-Cf/-CF and -Cm don't make sense together"));
278 flexerror (_("-Cf/-CF and -I are incompatible"));
282 ("-Cf/-CF are incompatible with lex-compatibility mode"));
285 if (fulltbl && fullspd)
287 ("-Cf and -CF are mutually exclusive"));
290 if (C_plus_plus && fullspd)
291 flexerror (_("Can't use -+ with -CF option"));
293 if (C_plus_plus && yytext_is_array) {
294 warn (_("%array incompatible with -+ option"));
295 yytext_is_array = false;
298 if (C_plus_plus && (reentrant))
299 flexerror (_("Options -+ and --reentrant are mutually exclusive."));
301 if (C_plus_plus && bison_bridge_lval)
302 flexerror (_("bison bridge not supported for the C++ scanner."));
305 if (useecs) { /* Set up doubly-linked equivalence classes. */
307 /* We loop all the way up to csize, since ecgroup[csize] is
308 * the position used for NUL characters.
312 for (i = 2; i <= csize; ++i) {
317 nextecm[csize] = NIL;
321 /* Put everything in its own equivalence class. */
322 for (i = 1; i <= csize; ++i) {
324 nextecm[i] = BAD_SUBSCRIPT; /* to catch errors */
329 buf_m4_define( &m4defs_buf, "M4_YY_NO_ANSI_FUNC_DEFS", NULL);
331 if (!ansi_func_protos)
332 buf_m4_define( &m4defs_buf, "M4_YY_NO_ANSI_FUNC_PROTOS", NULL);
335 buf_m4_define( &m4defs_buf, "M4_EXTRA_TYPE_DEFS", extra_type);
340 if (!did_outfilename) {
348 snprintf (outfile_path, sizeof(outfile_path), outfile_template,
351 outfilename = outfile_path;
354 prev_stdout = freopen (outfilename, "w+", stdout);
356 if (prev_stdout == NULL)
357 lerrsf (_("could not create %s"), outfilename);
363 /* Setup the filter chain. */
364 output_chain = filter_create_int(NULL, filter_tee_header, headerfilename);
365 if ( !(m4 = getenv("M4")))
367 filter_create_ext(output_chain, m4, "-gP", 0);
368 filter_create_int(output_chain, filter_fix_linedirs, NULL);
370 /* For debugging, only run the requested number of filters. */
371 if (preproc_level > 0) {
372 filter_truncate(output_chain, preproc_level);
373 filter_apply_chain(output_chain);
378 /* always generate the tablesverify flag. */
379 buf_m4_define (&m4defs_buf, "M4_YY_TABLES_VERIFY", tablesverify ? "1" : "0");
384 /* force generation of C tables. */
390 struct yytbl_hdr hdr;
394 buf_m4_define (&m4defs_buf, "M4_YY_TABLES_EXTERNAL", NULL);
396 if (!tablesfilename) {
397 nbytes = strlen (prefix) + strlen (tablesfile_template) + 2;
398 tablesfilename = pname = (char *) calloc (nbytes, 1);
399 snprintf (pname, nbytes, tablesfile_template, prefix);
402 if ((tablesout = fopen (tablesfilename, "w")) == NULL)
403 lerrsf (_("could not create %s"), tablesfilename);
408 yytbl_writer_init (&tableswr, tablesout);
410 nbytes = strlen (prefix) + strlen ("tables") + 2;
411 tablesname = (char *) calloc (nbytes, 1);
412 snprintf (tablesname, nbytes, "%stables", prefix);
413 yytbl_hdr_init (&hdr, flex_version, tablesname);
415 if (yytbl_hdr_fwrite (&tableswr, &hdr) <= 0)
416 flexerror (_("could not write tables header"));
419 if (skelname && (skelfile = fopen (skelname, "r")) == NULL)
420 lerrsf (_("can't open skeleton file %s"), skelname);
423 buf_m4_define (&m4defs_buf, "M4_YY_REENTRANT", NULL);
425 buf_m4_define (&m4defs_buf, "M4_YY_TEXT_IS_ARRAY", NULL);
428 if ( bison_bridge_lval)
429 buf_m4_define (&m4defs_buf, "M4_YY_BISON_LVAL", NULL);
431 if ( bison_bridge_lloc)
432 buf_m4_define (&m4defs_buf, "<M4_YY_BISON_LLOC>", NULL);
434 buf_m4_define(&m4defs_buf, "M4_YY_PREFIX", prefix);
437 line_directive_out (stdout, 0);
440 buf_m4_define (&m4defs_buf, "M4_YY_USE_LINENO", NULL);
442 /* Create the alignment type. */
443 buf_strdefine (&userdef_buf, "YY_INT_ALIGNED",
444 long_align ? "long int" : "short int");
446 /* Define the start condition macros. */
449 buf_init(&tmpbuf, sizeof(char));
450 for (i = 1; i <= lastsc; i++) {
451 char *str, *fmt = "#define %s %d\n";
454 str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(scname[i]) + NUMCHARLINES + 2);
456 flexfatal(_("allocation of macro definition failed"));
457 snprintf(str, strsz, fmt, scname[i], i - 1);
458 buf_strappend(&tmpbuf, str);
461 buf_m4_define(&m4defs_buf, "M4_YY_SC_DEFS", tmpbuf.elts);
462 buf_destroy(&tmpbuf);
465 /* This is where we begin writing to the file. */
467 /* Dump the %top code. */
469 outn((char*) top_buf.elts);
471 /* Dump the m4 definitions. */
472 buf_print_strings(&m4defs_buf, stdout);
473 m4defs_buf.nelts = 0; /* memory leak here. */
475 /* Place a bogus line directive, it will be fixed in the filter. */
476 outn("#line 0 \"M4_YY_OUTFILE_NAME\"\n");
478 /* Dump the user defined preproc directives. */
479 if (userdef_buf.elts)
480 outn ((char *) (userdef_buf.elts));
486 /* flexend - terminate flex
489 * This routine does not return.
492 void flexend (exit_status)
496 static int called_before = -1; /* prevent infinite recursion. */
500 FLEX_EXIT (exit_status);
502 if (skelfile != NULL) {
503 if (ferror (skelfile))
504 lerrsf (_("input error reading skeleton file %s"),
507 else if (fclose (skelfile))
508 lerrsf (_("error closing skeleton file %s"),
514 "#ifdef YY_HEADER_EXPORT_START_CONDITIONS\n");
516 "/* Beware! Start conditions are not prefixed. */\n");
518 /* Special case for "INITIAL" */
520 "#undef INITIAL\n#define INITIAL 0\n");
521 for (i = 2; i <= lastsc; i++)
522 fprintf (header_out, "#define %s %d\n", scname[i], i - 1);
524 "#endif /* YY_HEADER_EXPORT_START_CONDITIONS */\n\n");
526 /* Kill ALL flex-related macros. This is so the user
527 * can #include more than one generated header file. */
528 fprintf (header_out, "#ifndef YY_HEADER_NO_UNDEFS\n");
530 "/* Undefine all internal macros, etc., that do no belong in the header. */\n\n");
533 const char * undef_list[] = {
537 "EOB_ACT_CONTINUE_SCAN",
538 "EOB_ACT_END_OF_FILE",
539 "EOB_ACT_LAST_MATCH",
551 "YY_BUFFER_EOF_PENDING",
555 "M4_YY_CALL_LAST_ARG",
556 "M4_YY_CALL_ONLY_ARG",
559 "M4_YY_DECL_LAST_ARG",
560 "M4_YY_DEF_LAST_ARG",
561 "M4_YY_DEF_ONLY_ARG",
562 "YY_DO_BEFORE_ACTION",
564 "YY_END_OF_BUFFER_CHAR",
568 "YY_FLEX_DEFINED_ECHO",
569 "YY_FLEX_LEX_COMPAT",
570 "YY_FLEX_MAJOR_VERSION",
571 "YY_FLEX_MINOR_VERSION",
572 "YY_FLEX_SUBMINOR_VERSION",
581 "YY_LEX_DECLARATION",
592 "M4_YY_PROTO_LAST_ARG",
593 "M4_YY_PROTO_ONLY_ARG void",
596 "YY_RESTORE_YY_MORE_OFFSET",
601 "YY_START_STACK_INCR",
604 "YY_TRAILING_HEAD_MASK",
616 "yy_load_buffer_state",
622 "yy_set_interactive",
623 "yy_switch_to_buffer",
624 "yypush_buffer_state",
625 "yypop_buffer_state",
626 "yyensure_buffer_stack",
668 /* must be null-terminated */
672 for (i=0; undef_list[i] != NULL; i++)
673 fprintf (header_out, "#undef %s\n", undef_list[i]);
676 /* undef any of the auto-generated symbols. */
677 for (i = 0; i < defs_buf.nelts; i++) {
679 /* don't undef start conditions */
680 if (sclookup (((char **) defs_buf.elts)[i]) > 0)
682 fprintf (header_out, "#undef %s\n",
683 ((char **) defs_buf.elts)[i]);
687 "#endif /* !YY_HEADER_NO_UNDEFS */\n");
688 fprintf (header_out, "\n");
689 fprintf (header_out, "#undef %sIN_HEADER\n", prefix);
690 fprintf (header_out, "#endif /* %sHEADER_H */\n", prefix);
692 if (ferror (header_out))
693 lerrsf (_("error creating header file %s"),
699 if (exit_status != 0 && outfile_created) {
701 lerrsf (_("error writing output file %s"),
704 else if ((_stdout_closed = 1) && fclose (stdout))
705 lerrsf (_("error closing output file %s"),
708 else if (unlink (outfilename))
709 lerrsf (_("error deleting output file %s"),
714 if (backing_up_report && backing_up_file) {
715 if (num_backing_up == 0)
716 fprintf (backing_up_file, _("No backing up.\n"));
717 else if (fullspd || fulltbl)
718 fprintf (backing_up_file,
720 ("%d backing up (non-accepting) states.\n"),
723 fprintf (backing_up_file,
724 _("Compressed tables always back up.\n"));
726 if (ferror (backing_up_file))
727 lerrsf (_("error writing backup file %s"),
730 else if (fclose (backing_up_file))
731 lerrsf (_("error closing backup file %s"),
736 fprintf (stderr, _("%s version %s usage statistics:\n"),
737 program_name, flex_version);
739 fprintf (stderr, _(" scanner options: -"));
743 if (backing_up_report)
753 if (performance_report > 0)
755 if (performance_report > 1)
760 fputs ("--reentrant", stderr);
761 if (bison_bridge_lval)
762 fputs ("--bison-bridge", stderr);
763 if (bison_bridge_lloc)
764 fputs ("--bison-locations", stderr);
768 putc ('v', stderr); /* always true! */
771 if (interactive == false)
773 if (interactive == true)
780 if (csize == unspecified)
781 /* We encountered an error fairly early on, so csize
782 * never got specified. Define it now, to prevent
783 * bogus table sizes being written out below.
792 fprintf (stderr, " -C");
808 fprintf (stderr, " -o%s", outfilename);
811 fprintf (stderr, " -S%s", skelname);
813 if (strcmp (prefix, "yy"))
814 fprintf (stderr, " -P%s", prefix);
818 fprintf (stderr, _(" %d/%d NFA states\n"),
819 lastnfa, current_mns);
820 fprintf (stderr, _(" %d/%d DFA states (%d words)\n"),
821 lastdfa, current_max_dfas, totnst);
822 fprintf (stderr, _(" %d rules\n"),
823 num_rules + num_eof_rules -
824 1 /* - 1 for def. rule */ );
826 if (num_backing_up == 0)
827 fprintf (stderr, _(" No backing up\n"));
828 else if (fullspd || fulltbl)
831 (" %d backing-up (non-accepting) states\n"),
836 (" Compressed tables always back-up\n"));
840 _(" Beginning-of-line patterns used\n"));
842 fprintf (stderr, _(" %d/%d start conditions\n"), lastsc,
846 (" %d epsilon states, %d double epsilon states\n"),
850 fprintf (stderr, _(" no character classes\n"));
854 (" %d/%d character classes needed %d/%d words of storage, %d reused\n"),
855 lastccl, current_maxccls,
856 cclmap[lastccl] + ccllen[lastccl],
857 current_max_ccl_tbl_size, cclreuse);
859 fprintf (stderr, _(" %d state/nextstate pairs created\n"),
862 _(" %d/%d unique/duplicate transitions\n"),
866 tblsiz = lastdfa * numecs;
867 fprintf (stderr, _(" %d table entries\n"),
872 tblsiz = 2 * (lastdfa + numtemps) + 2 * tblend;
875 _(" %d/%d base-def entries created\n"),
876 lastdfa + numtemps, current_max_dfas);
879 (" %d/%d (peak %d) nxt-chk entries created\n"),
880 tblend, current_max_xpairs, peakpairs);
883 (" %d/%d (peak %d) template nxt-chk entries created\n"),
885 current_max_template_xpairs,
887 fprintf (stderr, _(" %d empty table entries\n"),
889 fprintf (stderr, _(" %d protos created\n"),
892 _(" %d templates created, %d uses\n"),
897 tblsiz = tblsiz + csize;
900 (" %d/%d equivalence classes created\n"),
905 tblsiz = tblsiz + numecs;
908 (" %d/%d meta-equivalence classes created\n"),
914 (" %d (%d saved) hash collisions, %d DFAs equal\n"),
915 hshcol, hshsave, dfaeql);
916 fprintf (stderr, _(" %d sets of reallocations needed\n"),
918 fprintf (stderr, _(" %d total table entries needed\n"),
922 FLEX_EXIT (exit_status);
926 /* flexinit - initialize flex */
928 void flexinit (argc, argv)
932 int i, sawcmpflag, rv, optind;
936 printstats = syntaxerror = trace = spprdflt = false;
937 lex_compat = posix_compat = C_plus_plus = backing_up_report =
938 ddebug = fulltbl = false;
939 fullspd = long_align = nowarn = yymore_used = continued_action =
941 do_yylineno = yytext_is_array = in_rule = reject = do_stdinit =
943 yymore_really_used = reject_really_used = unspecified;
944 interactive = csize = unspecified;
945 do_yywrap = gen_line_dirs = usemecs = useecs = true;
946 reentrant = bison_bridge_lval = bison_bridge_lloc = false;
947 performance_report = 0;
951 use_read = use_stdout = false;
952 tablesext = tablesverify = false;
954 tablesfilename = tablesname = NULL;
955 ansi_func_defs = ansi_func_protos = true;
959 /* Initialize dynamic array for holding the rule actions. */
960 action_size = 2048; /* default size of action array in bytes */
961 action_array = allocate_character_array (action_size);
962 defs1_offset = prolog_offset = action_offset = action_index = 0;
963 action_array[0] = '\0';
965 /* Initialize any buffers. */
966 buf_init (&userdef_buf, sizeof (char)); /* one long string */
967 buf_init (&defs_buf, sizeof (char *)); /* list of strings */
968 buf_init (&yydmap_buf, sizeof (char)); /* one long string */
969 buf_init (&top_buf, sizeof (char)); /* one long string */
972 const char * m4defs_init_str[] = {"m4_changequote\n",
973 "m4_changequote([[, ]])\n"};
974 buf_init (&m4defs_buf, sizeof (char *));
975 buf_append (&m4defs_buf, &m4defs_init_str, 2);
980 /* initialize regex lib */
983 /* Enable C++ if program name ends with '+'. */
984 program_name = basename2 (argv[0], 0);
986 if (program_name[0] != '\0' &&
987 program_name[strlen (program_name) - 1] == '+')
991 sopt = scanopt_init (flexopts, argc, argv, 0);
993 /* This will only happen when flexopts array is altered. */
995 _("Internal error. flexopts are malformed.\n"));
999 while ((rv = scanopt (sopt, &arg, &optind)) != 0) {
1002 /* Scanopt has already printed an option-specific error message. */
1005 ("Try `%s --help' for more information.\n"),
1010 switch ((enum flexopt_flag_t) rv) {
1016 interactive = false;
1020 backing_up_report = true;
1026 case OPT_COMPRESSION:
1034 for (i = 0; arg && arg[i] != '\0'; i++)
1062 ("unknown -C option '%c'"),
1077 useecs = usemecs = false;
1078 use_read = fulltbl = true;
1082 useecs = usemecs = false;
1083 use_read = fullspd = true;
1090 case OPT_INTERACTIVE:
1094 case OPT_CASE_INSENSITIVE:
1095 sf_set_case_ins(true);
1098 case OPT_LEX_COMPAT:
1102 case OPT_POSIX_COMPAT:
1103 posix_compat = true;
1106 case OPT_PREPROC_LEVEL:
1107 preproc_level = strtol(arg,NULL,0);
1111 buf_strdefine (&userdef_buf, "YY_MAIN", "1");
1116 buf_strdefine (&userdef_buf, "YY_MAIN", "0");
1120 gen_line_dirs = false;
1125 did_outfilename = 1;
1132 case OPT_PERF_REPORT:
1133 ++performance_report;
1136 case OPT_BISON_BRIDGE:
1137 bison_bridge_lval = true;
1140 case OPT_BISON_BRIDGE_LOCATIONS:
1141 bison_bridge_lval = bison_bridge_lloc = true;
1148 case OPT_NO_REENTRANT:
1160 case OPT_NO_DEFAULT:
1168 case OPT_NO_UNISTD_H:
1169 //buf_strdefine (&userdef_buf, "YY_NO_UNISTD_H", "1");
1170 buf_m4_define( &m4defs_buf, "M4_YY_NO_UNISTD_H",0);
1173 case OPT_TABLES_FILE:
1175 tablesfilename = arg;
1178 case OPT_TABLES_VERIFY:
1179 tablesverify = true;
1191 printf (_("%s %s\n"), program_name, flex_version);
1218 case OPT_ALWAYS_INTERACTIVE:
1219 buf_m4_define (&m4defs_buf, "M4_YY_ALWAYS_INTERACTIVE", 0);
1222 case OPT_NEVER_INTERACTIVE:
1223 buf_m4_define( &m4defs_buf, "M4_YY_NEVER_INTERACTIVE", 0);
1227 yytext_is_array = true;
1231 yytext_is_array = false;
1242 case OPT_HEADER_FILE:
1243 headerfilename = arg;
1250 case OPT_NO_META_ECS:
1254 case OPT_PREPROCDEFINE:
1256 /* arg is "symbol" or "symbol=definition". */
1260 *def != '\0' && *def != '='; ++def) ;
1262 buf_strappend (&userdef_buf, "#define ");
1264 buf_strappend (&userdef_buf, arg);
1265 buf_strappend (&userdef_buf,
1269 buf_strnappend (&userdef_buf, arg,
1271 buf_strappend (&userdef_buf, " ");
1272 buf_strappend (&userdef_buf,
1274 buf_strappend (&userdef_buf, "\n");
1284 //buf_strdefine (&userdef_buf, "YY_STACK_USED", "1");
1285 buf_m4_define( &m4defs_buf, "M4_YY_STACK_USED",0);
1292 case OPT_NO_STDINIT:
1304 case OPT_NO_YYLINENO:
1305 do_yylineno = false;
1317 yymore_really_used = true;
1321 yymore_really_used = false;
1325 reject_really_used = true;
1329 reject_really_used = false;
1332 case OPT_NO_ANSI_FUNC_DEFS:
1333 ansi_func_defs = false;
1336 case OPT_NO_ANSI_FUNC_PROTOS:
1337 ansi_func_protos = false;
1340 case OPT_NO_YY_PUSH_STATE:
1341 //buf_strdefine (&userdef_buf, "YY_NO_PUSH_STATE", "1");
1342 buf_m4_define( &m4defs_buf, "M4_YY_NO_PUSH_STATE",0);
1344 case OPT_NO_YY_POP_STATE:
1345 //buf_strdefine (&userdef_buf, "YY_NO_POP_STATE", "1");
1346 buf_m4_define( &m4defs_buf, "M4_YY_NO_POP_STATE",0);
1348 case OPT_NO_YY_TOP_STATE:
1349 //buf_strdefine (&userdef_buf, "YY_NO_TOP_STATE", "1");
1350 buf_m4_define( &m4defs_buf, "M4_YY_NO_TOP_STATE",0);
1353 //buf_strdefine (&userdef_buf, "YY_NO_UNPUT", "1");
1354 buf_m4_define( &m4defs_buf, "M4_YY_NO_UNPUT",0);
1356 case OPT_NO_YY_SCAN_BUFFER:
1357 //buf_strdefine (&userdef_buf, "YY_NO_SCAN_BUFFER", "1");
1358 buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_BUFFER",0);
1360 case OPT_NO_YY_SCAN_BYTES:
1361 //buf_strdefine (&userdef_buf, "YY_NO_SCAN_BYTES", "1");
1362 buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_BYTES",0);
1364 case OPT_NO_YY_SCAN_STRING:
1365 //buf_strdefine (&userdef_buf, "YY_NO_SCAN_STRING", "1");
1366 buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_STRING",0);
1368 case OPT_NO_YYGET_EXTRA:
1369 //buf_strdefine (&userdef_buf, "YY_NO_GET_EXTRA", "1");
1370 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_EXTRA",0);
1372 case OPT_NO_YYSET_EXTRA:
1373 //buf_strdefine (&userdef_buf, "YY_NO_SET_EXTRA", "1");
1374 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_EXTRA",0);
1376 case OPT_NO_YYGET_LENG:
1377 //buf_strdefine (&userdef_buf, "YY_NO_GET_LENG", "1");
1378 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LENG",0);
1380 case OPT_NO_YYGET_TEXT:
1381 //buf_strdefine (&userdef_buf, "YY_NO_GET_TEXT", "1");
1382 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_TEXT",0);
1384 case OPT_NO_YYGET_LINENO:
1385 //buf_strdefine (&userdef_buf, "YY_NO_GET_LINENO", "1");
1386 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LINENO",0);
1388 case OPT_NO_YYSET_LINENO:
1389 //buf_strdefine (&userdef_buf, "YY_NO_SET_LINENO", "1");
1390 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LINENO",0);
1392 case OPT_NO_YYGET_IN:
1393 //buf_strdefine (&userdef_buf, "YY_NO_GET_IN", "1");
1394 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_IN",0);
1396 case OPT_NO_YYSET_IN:
1397 //buf_strdefine (&userdef_buf, "YY_NO_SET_IN", "1");
1398 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_IN",0);
1400 case OPT_NO_YYGET_OUT:
1401 //buf_strdefine (&userdef_buf, "YY_NO_GET_OUT", "1");
1402 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_OUT",0);
1404 case OPT_NO_YYSET_OUT:
1405 //buf_strdefine (&userdef_buf, "YY_NO_SET_OUT", "1");
1406 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_OUT",0);
1408 case OPT_NO_YYGET_LVAL:
1409 //buf_strdefine (&userdef_buf, "YY_NO_GET_LVAL", "1");
1410 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LVAL",0);
1412 case OPT_NO_YYSET_LVAL:
1413 //buf_strdefine (&userdef_buf, "YY_NO_SET_LVAL", "1");
1414 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LVAL",0);
1416 case OPT_NO_YYGET_LLOC:
1417 //buf_strdefine (&userdef_buf, "YY_NO_GET_LLOC", "1");
1418 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LLOC",0);
1420 case OPT_NO_YYSET_LLOC:
1421 //buf_strdefine (&userdef_buf, "YY_NO_SET_LLOC", "1");
1422 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LLOC",0);
1426 } /* while scanopt() */
1428 scanopt_destroy (sopt);
1430 num_input_files = argc - optind;
1431 input_files = argv + optind;
1432 set_input_file (num_input_files > 0 ? input_files[0] : NULL);
1434 lastccl = lastsc = lastdfa = lastnfa = 0;
1435 num_rules = num_eof_rules = default_rule = 0;
1436 numas = numsnpairs = tmpuses = 0;
1437 numecs = numeps = eps2 = num_reallocs = hshcol = dfaeql = totnst =
1439 numuniq = numdup = hshsave = eofseen = datapos = dataline = 0;
1440 num_backing_up = onesp = numprots = 0;
1441 variable_trailing_context_rules = bol_needed = false;
1443 linenum = sectnum = 1;
1446 /* Used in mkprot() so that the first proto goes in slot 1
1447 * of the proto queue.
1451 set_up_initial_allocations ();
1455 /* readin - read in the rules section of the input file(s) */
1459 static char yy_stdinit[] = "FILE *yyin = stdin, *yyout = stdout;";
1460 static char yy_nostdinit[] =
1461 "FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0;";
1463 line_directive_out ((FILE *) 0, 1);
1466 pinpoint_message (_("fatal parse error"));
1473 /* If the user explicitly requested posix compatibility by specifing the
1474 * posix-compat option, then we check for conflicting options. However, if
1475 * the POSIXLY_CORRECT variable is set, then we quietly make flex as
1476 * posix-compatible as possible. This is the recommended behavior
1477 * according to the GNU Coding Standards.
1479 * Note: The posix option was added to flex to provide the posix behavior
1480 * of the repeat operator in regular expressions, e.g., `ab{3}'
1483 /* TODO: This is where we try to make flex behave according to
1484 * posiz, AND check for conflicting options. How far should we go
1485 * with this? Should we disable all the neat-o flex features?
1487 /* Update: Estes says no, since other flex features don't violate posix. */
1490 if (getenv ("POSIXLY_CORRECT")) {
1491 posix_compat = true;
1494 if (backing_up_report) {
1495 backing_up_file = fopen (backing_name, "w");
1496 if (backing_up_file == NULL)
1498 ("could not create backing-up info file %s"),
1503 backing_up_file = NULL;
1505 if (yymore_really_used == true)
1507 else if (yymore_really_used == false)
1508 yymore_used = false;
1510 if (reject_really_used == true)
1512 else if (reject_really_used == false)
1515 if (performance_report > 0) {
1519 ("-l AT&T lex compatibility option entails a large performance penalty\n"));
1522 (" and may be the actual source of other reported performance penalties\n"));
1525 else if (do_yylineno) {
1528 ("%%option yylineno entails a performance penalty ONLY on rules that can match newline characters\n"));
1531 if (performance_report > 1) {
1535 ("-I (interactive) entails a minor performance penalty\n"));
1540 ("yymore() entails a minor performance penalty\n"));
1546 ("REJECT entails a large performance penalty\n"));
1548 if (variable_trailing_context_rules)
1551 ("Variable trailing context rules entail a large performance penalty\n"));
1557 if (variable_trailing_context_rules)
1560 if ((fulltbl || fullspd) && reject) {
1563 ("REJECT cannot be used with -f or -F"));
1564 else if (do_yylineno)
1566 ("%option yylineno cannot be used with REJECT"));
1569 ("variable trailing context rules cannot be used with -f or -F"));
1573 out_m4_define( "M4_YY_USES_REJECT", NULL);
1574 //outn ("\n#define YY_USES_REJECT");
1580 outn ("\n#define yywrap(yyscanner) 1");
1582 outn ("\n#define yywrap() 1");
1584 outn ("#define YY_SKIP_YYWRAP");
1588 outn ("\n#define FLEX_DEBUG");
1592 outn ("typedef unsigned char YY_CHAR;");
1594 outn ("typedef char YY_CHAR;");
1598 outn ("#define yytext_ptr yytext");
1601 outn ("#define YY_INTERACTIVE");
1606 /* In reentrant scanner, stdinit is handled in flex.skl. */
1609 outn ("#ifdef VMS");
1610 outn ("#ifdef __VMS_POSIX");
1611 outn ("#define YY_STDINIT");
1614 outn ("#define YY_STDINIT");
1618 outn ("#ifdef VMS");
1619 outn ("#ifndef __VMS_POSIX");
1620 outn (yy_nostdinit);
1631 outn (yy_nostdinit);
1638 outn ("typedef yyconst struct yy_trans_info *yy_state_type;");
1639 else if (!C_plus_plus)
1640 outn ("typedef int yy_state_type;");
1644 outn ("#define YY_FLEX_LEX_COMPAT");
1646 if (!C_plus_plus && !reentrant) {
1647 outn ("extern int yylineno;");
1649 outn ("int yylineno = 1;");
1654 outn ("\n#include <FlexLexer.h>");
1657 outn("\nint yyFlexLexer::yywrap() { return 1; }");
1661 outn ("int yyFlexLexer::yylex()");
1663 outn ("\tLexerError( \"yyFlexLexer::yylex invoked but %option yyclass used\" );");
1664 outn ("\treturn 0;");
1667 out_str ("\n#define YY_DECL int %s::yylex()\n",
1674 /* Watch out: yytext_ptr is a variable when yytext is an array,
1675 * but it's a macro when yytext is a pointer.
1677 if (yytext_is_array) {
1679 outn ("extern char yytext[];\n");
1683 outn ("#define yytext_ptr yytext_r");
1686 outn ("extern char *yytext;");
1687 outn ("#define yytext_ptr yytext");
1693 ("%option yyclass only meaningful for C++ scanners"));
1697 numecs = cre8ecs (nextecm, ecgroup, csize);
1701 /* Now map the equivalence class for NUL to its expected place. */
1702 ecgroup[0] = ecgroup[csize];
1703 NUL_ec = ABS (ecgroup[0]);
1710 /* set_up_initial_allocations - allocate memory for internal tables */
1712 void set_up_initial_allocations ()
1714 maximum_mns = (long_align ? MAXIMUM_MNS_LONG : MAXIMUM_MNS);
1715 current_mns = INITIAL_MNS;
1716 firstst = allocate_integer_array (current_mns);
1717 lastst = allocate_integer_array (current_mns);
1718 finalst = allocate_integer_array (current_mns);
1719 transchar = allocate_integer_array (current_mns);
1720 trans1 = allocate_integer_array (current_mns);
1721 trans2 = allocate_integer_array (current_mns);
1722 accptnum = allocate_integer_array (current_mns);
1723 assoc_rule = allocate_integer_array (current_mns);
1724 state_type = allocate_integer_array (current_mns);
1726 current_max_rules = INITIAL_MAX_RULES;
1727 rule_type = allocate_integer_array (current_max_rules);
1728 rule_linenum = allocate_integer_array (current_max_rules);
1729 rule_useful = allocate_integer_array (current_max_rules);
1730 rule_has_nl = allocate_bool_array (current_max_rules);
1732 current_max_scs = INITIAL_MAX_SCS;
1733 scset = allocate_integer_array (current_max_scs);
1734 scbol = allocate_integer_array (current_max_scs);
1735 scxclu = allocate_integer_array (current_max_scs);
1736 sceof = allocate_integer_array (current_max_scs);
1737 scname = allocate_char_ptr_array (current_max_scs);
1739 current_maxccls = INITIAL_MAX_CCLS;
1740 cclmap = allocate_integer_array (current_maxccls);
1741 ccllen = allocate_integer_array (current_maxccls);
1742 cclng = allocate_integer_array (current_maxccls);
1743 ccl_has_nl = allocate_bool_array (current_maxccls);
1745 current_max_ccl_tbl_size = INITIAL_MAX_CCL_TBL_SIZE;
1746 ccltbl = allocate_Character_array (current_max_ccl_tbl_size);
1748 current_max_dfa_size = INITIAL_MAX_DFA_SIZE;
1750 current_max_xpairs = INITIAL_MAX_XPAIRS;
1751 nxt = allocate_integer_array (current_max_xpairs);
1752 chk = allocate_integer_array (current_max_xpairs);
1754 current_max_template_xpairs = INITIAL_MAX_TEMPLATE_XPAIRS;
1755 tnxt = allocate_integer_array (current_max_template_xpairs);
1757 current_max_dfas = INITIAL_MAX_DFAS;
1758 base = allocate_integer_array (current_max_dfas);
1759 def = allocate_integer_array (current_max_dfas);
1760 dfasiz = allocate_integer_array (current_max_dfas);
1761 accsiz = allocate_integer_array (current_max_dfas);
1762 dhash = allocate_integer_array (current_max_dfas);
1763 dss = allocate_int_ptr_array (current_max_dfas);
1764 dfaacc = allocate_dfaacc_union (current_max_dfas);
1766 nultrans = (int *) 0;
1770 /* extracts basename from path, optionally stripping the extension "\.*"
1771 * (same concept as /bin/sh `basename`, but different handling of extension). */
1772 static char *basename2 (path, strip_ext)
1774 int strip_ext; /* boolean */
1779 for (b = path; *path; path++)
1782 else if (*path == '.')
1785 if (strip_ext && e && e > b)
1794 if (!did_outfilename) {
1795 snprintf (outfile_path, sizeof(outfile_path), outfile_template,
1796 prefix, C_plus_plus ? "cc" : "c");
1797 outfilename = outfile_path;
1800 fprintf (f, _("Usage: %s [OPTIONS] [FILE]...\n"), program_name);
1803 ("Generates programs that perform pattern-matching on text.\n"
1804 "\n" "Table Compression:\n"
1805 " -Ca, --align trade off larger tables for better memory alignment\n"
1806 " -Ce, --ecs construct equivalence classes\n"
1807 " -Cf do not compress tables; use -f representation\n"
1808 " -CF do not compress tables; use -F representation\n"
1809 " -Cm, --meta-ecs construct meta-equivalence classes\n"
1810 " -Cr, --read use read() instead of stdio for scanner input\n"
1811 " -f, --full generate fast, large scanner. Same as -Cfr\n"
1812 " -F, --fast use alternate table representation. Same as -CFr\n"
1813 " -Cem default compression (same as --ecs --meta-ecs)\n"
1815 " -d, --debug enable debug mode in scanner\n"
1816 " -b, --backup write backing-up information to %s\n"
1817 " -p, --perf-report write performance report to stderr\n"
1818 " -s, --nodefault suppress default rule to ECHO unmatched text\n"
1819 " -T, --trace %s should run in trace mode\n"
1820 " -w, --nowarn do not generate warnings\n"
1821 " -v, --verbose write summary of scanner statistics to stdout\n"
1823 " -o, --outfile=FILE specify output filename\n"
1824 " -S, --skel=FILE specify skeleton file\n"
1825 " -t, --stdout write scanner on stdout instead of %s\n"
1826 " --yyclass=NAME name of C++ class\n"
1827 " --header-file=FILE create a C header file in addition to the scanner\n"
1828 " --tables-file[=FILE] write tables to FILE\n" "\n"
1829 "Scanner behavior:\n"
1830 " -7, --7bit generate 7-bit scanner\n"
1831 " -8, --8bit generate 8-bit scanner\n"
1832 " -B, --batch generate batch scanner (opposite of -I)\n"
1833 " -i, --case-insensitive ignore case in patterns\n"
1834 " -l, --lex-compat maximal compatibility with original lex\n"
1835 " -X, --posix-compat maximal compatibility with POSIX lex\n"
1836 " -I, --interactive generate interactive scanner (opposite of -B)\n"
1837 " --yylineno track line count in yylineno\n"
1838 "\n" "Generated code:\n"
1839 " -+, --c++ generate C++ scanner class\n"
1840 " -Dmacro[=defn] #define macro defn (default defn is '1')\n"
1841 " -L, --noline suppress #line directives in scanner\n"
1842 " -P, --prefix=STRING use STRING as prefix instead of \"yy\"\n"
1843 " -R, --reentrant generate a reentrant C scanner\n"
1844 " --bison-bridge scanner for bison pure parser.\n"
1845 " --bison-locations include yylloc support.\n"
1846 " --stdinit initialize yyin/yyout to stdin/stdout\n"
1847 " --noansi-definitions old-style function definitions\n"
1848 " --noansi-prototypes empty parameter list in prototypes\n"
1849 " --nounistd do not include <unistd.h>\n"
1850 " --noFUNCTION do not generate a particular FUNCTION\n"
1851 "\n" "Miscellaneous:\n"
1852 " -c do-nothing POSIX option\n"
1853 " -n do-nothing POSIX option\n"
1855 " -h, --help produce this help message\n"
1856 " -V, --version report %s version\n"),
1857 backing_name, program_name, outfile_path, program_name);