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 */
41 static char flex_version[] = FLEX_VERSION;
43 /* declare functions that have forward references */
45 void flexinit(int, char **);
47 void set_up_initial_allocations(void);
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;
61 int datapos, dataline, linenum;
62 FILE *skelfile = NULL;
65 int action_size, defs1_offset, prolog_offset, action_offset,
67 char *infilename = NULL, *outfilename = NULL, *headerfilename = NULL;
69 char *prefix, *yyclass, *extra_type = NULL;
70 int do_stdinit, use_stdout;
71 int onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE];
72 int onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp;
73 int maximum_mns, current_mns, current_max_rules;
74 int num_rules, num_eof_rules, default_rule, lastnfa;
75 int *firstst, *lastst, *finalst, *transchar, *trans1, *trans2;
76 int *accptnum, *assoc_rule, *state_type;
77 int *rule_type, *rule_linenum, *rule_useful;
78 int current_state_type;
79 int variable_trailing_context_rules;
80 int numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP];
81 int protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE];
82 int numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs,
84 int tecbck[CSIZE + 1];
85 int lastsc, *scset, *scbol, *scxclu, *sceof;
88 int current_max_dfa_size, current_max_xpairs;
89 int current_max_template_xpairs, current_max_dfas;
90 int lastdfa, *nxt, *chk, *tnxt;
91 int *base, *def, *nultrans, NUL_ec, tblend, firstfree, **dss, *dfasiz;
92 union dfaacc_union *dfaacc;
93 int *accsiz, *dhash, numas;
94 int numsnpairs, jambase, jamstate;
95 int lastccl, *cclmap, *ccllen, *cclng, cclreuse;
96 int current_maxccls, current_max_ccl_tbl_size;
97 unsigned char *ccltbl;
99 int sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs;
100 int tmpuses, totnst, peakpairs, numuniq, numdup, hshsave;
101 int num_backing_up, bol_needed;
102 FILE *backing_up_file;
103 int end_of_buffer_state;
106 jmp_buf flex_main_jmp_buf;
107 bool *rule_has_nl, *ccl_has_nl;
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 static const char outfile_template[] = "lex.%s.%s";
120 static const char backing_name[] = "lex.backup";
121 static const char tablesfile_template[] = "lex.%s.tables";
126 static char outfile_path[MAXLINE];
127 static int outfile_created = 0;
128 static char *skelname = NULL;
129 static int _stdout_closed = 0; /* flag to prevent double-fclose() on stdout. */
130 const char *escaped_qstart = "]]M4_YY_NOOP[M4_YY_NOOP[M4_YY_NOOP[[";
131 const char *escaped_qend = "]]M4_YY_NOOP]M4_YY_NOOP]M4_YY_NOOP[[";
133 /* For debugging. The max number of filters to apply to skeleton. */
134 static int preproc_level = 1000;
136 int flex_main (int argc, char *argv[]);
138 int flex_main (int argc, char *argv[])
140 int i, exit_status, child_status;
142 /* Set a longjmp target. Yes, I know it's a hack, but it gets worse: The
143 * return value of setjmp, if non-zero, is the desired exit code PLUS ONE.
144 * For example, if you want 'main' to return with code '2', then call
145 * longjmp() with an argument of 3. This is because it is invalid to
146 * specify a value of 0 to longjmp. FLEX_EXIT(n) should be used instead of
149 exit_status = setjmp (flex_main_jmp_buf);
151 if (stdout && !_stdout_closed && !ferror(stdout)){
155 while (wait(&child_status) > 0){
156 if (!WIFEXITED (child_status)
157 || WEXITSTATUS (child_status) != 0){
158 /* report an error of a child
160 if( exit_status <= 1 )
165 return exit_status - 1;
168 flexinit (argc, argv);
176 for (i = 1; i <= num_rules; ++i)
177 if (!rule_useful[i] && i != default_rule)
178 line_warning (_("rule cannot be matched"),
181 if (spprdflt && !reject && rule_useful[default_rule])
183 ("-s option given but default rule can be matched"),
184 rule_linenum[default_rule]);
186 /* Generate the C state transition tables from the DFA. */
189 /* Note, flexend does not return. It exits with its argument
194 return 0; /* keep compilers/lint happy */
197 /* Wrapper around flex_main, so flex_main can be built as a library. */
198 int main (int argc, char *argv[])
202 setlocale (LC_MESSAGES, "");
203 setlocale (LC_CTYPE, "");
204 textdomain (PACKAGE);
205 bindtextdomain (PACKAGE, LOCALEDIR);
209 return flex_main (argc, argv);
212 /* check_options - check user-specified options */
214 void check_options (void)
217 const char * m4 = NULL;
221 flexerror (_("Can't use -+ with -l option"));
223 if (fulltbl || fullspd)
224 flexerror (_("Can't use -f or -F with -l option"));
226 if (reentrant || bison_bridge_lval)
228 ("Can't use --reentrant or --bison-bridge with -l option"));
230 yytext_is_array = true;
237 /* This makes no sense whatsoever. I'm removing it. */
239 /* This should really be "maintain_backup_tables = true" */
240 reject_really_used = true;
243 if (csize == unspecified) {
244 if ((fulltbl || fullspd) && !useecs)
245 csize = DEFAULT_CSIZE;
250 if (interactive == unspecified) {
251 if (fulltbl || fullspd)
257 if (fulltbl || fullspd) {
260 ("-Cf/-CF and -Cm don't make sense together"));
263 flexerror (_("-Cf/-CF and -I are incompatible"));
267 ("-Cf/-CF are incompatible with lex-compatibility mode"));
270 if (fulltbl && fullspd)
272 ("-Cf and -CF are mutually exclusive"));
275 if (C_plus_plus && fullspd)
276 flexerror (_("Can't use -+ with -CF option"));
278 if (C_plus_plus && yytext_is_array) {
279 lwarn (_("%array incompatible with -+ option"));
280 yytext_is_array = false;
283 if (C_plus_plus && (reentrant))
284 flexerror (_("Options -+ and --reentrant are mutually exclusive."));
286 if (C_plus_plus && bison_bridge_lval)
287 flexerror (_("bison bridge not supported for the C++ scanner."));
290 if (useecs) { /* Set up doubly-linked equivalence classes. */
292 /* We loop all the way up to csize, since ecgroup[csize] is
293 * the position used for NUL characters.
297 for (i = 2; i <= csize; ++i) {
302 nextecm[csize] = NIL;
306 /* Put everything in its own equivalence class. */
307 for (i = 1; i <= csize; ++i) {
309 nextecm[i] = BAD_SUBSCRIPT; /* to catch errors */
314 buf_m4_define( &m4defs_buf, "M4_EXTRA_TYPE_DEFS", extra_type);
319 if (!did_outfilename) {
327 snprintf (outfile_path, sizeof(outfile_path), outfile_template,
330 outfilename = outfile_path;
333 prev_stdout = freopen (outfilename, "w+", stdout);
335 if (prev_stdout == NULL)
336 lerr (_("could not create %s"), outfilename);
342 /* Setup the filter chain. */
343 output_chain = filter_create_int(NULL, filter_tee_header, headerfilename);
344 if ( !(m4 = getenv("M4"))) {
347 if ((slash = strrchr(M4, '/')) != NULL) {
350 const char *path = getenv("PATH");
354 int m4_length = strlen(m4);
356 size_t length = strlen(path);
359 const char *endOfDir = strchr(path, ':');
361 endOfDir = path+length;
364 char *m4_path = calloc(endOfDir-path + 1 + m4_length + 1, 1);
366 memcpy(m4_path, path, endOfDir-path);
367 m4_path[endOfDir-path] = '/';
368 memcpy(m4_path + (endOfDir-path) + 1, m4, m4_length + 1);
369 if (stat(m4_path, &sbuf) == 0 &&
370 (S_ISREG(sbuf.st_mode)) && sbuf.st_mode & S_IXUSR) {
383 filter_create_ext(output_chain, m4, "-gP", 0);
384 filter_create_int(output_chain, filter_fix_linedirs, NULL);
386 /* For debugging, only run the requested number of filters. */
387 if (preproc_level > 0) {
388 filter_truncate(output_chain, preproc_level);
389 filter_apply_chain(output_chain);
394 /* always generate the tablesverify flag. */
395 buf_m4_define (&m4defs_buf, "M4_YY_TABLES_VERIFY", tablesverify ? "1" : "0");
400 /* force generation of C tables. */
406 struct yytbl_hdr hdr;
410 buf_m4_define (&m4defs_buf, "M4_YY_TABLES_EXTERNAL", NULL);
412 if (!tablesfilename) {
413 nbytes = strlen (prefix) + strlen (tablesfile_template) + 2;
414 tablesfilename = pname = calloc(nbytes, 1);
415 snprintf (pname, nbytes, tablesfile_template, prefix);
418 if ((tablesout = fopen (tablesfilename, "w")) == NULL)
419 lerr (_("could not create %s"), tablesfilename);
423 yytbl_writer_init (&tableswr, tablesout);
425 nbytes = strlen (prefix) + strlen ("tables") + 2;
426 tablesname = calloc(nbytes, 1);
427 snprintf (tablesname, nbytes, "%stables", prefix);
428 yytbl_hdr_init (&hdr, flex_version, tablesname);
430 if (yytbl_hdr_fwrite (&tableswr, &hdr) <= 0)
431 flexerror (_("could not write tables header"));
434 if (skelname && (skelfile = fopen (skelname, "r")) == NULL)
435 lerr (_("can't open skeleton file %s"), skelname);
438 buf_m4_define (&m4defs_buf, "M4_YY_REENTRANT", NULL);
440 buf_m4_define (&m4defs_buf, "M4_YY_TEXT_IS_ARRAY", NULL);
443 if ( bison_bridge_lval)
444 buf_m4_define (&m4defs_buf, "M4_YY_BISON_LVAL", NULL);
446 if ( bison_bridge_lloc)
447 buf_m4_define (&m4defs_buf, "<M4_YY_BISON_LLOC>", NULL);
449 if (strchr(prefix, '[') || strchr(prefix, ']'))
450 flexerror(_("Prefix cannot include '[' or ']'"));
451 buf_m4_define(&m4defs_buf, "M4_YY_PREFIX", prefix);
454 line_directive_out (stdout, 0);
457 buf_m4_define (&m4defs_buf, "M4_YY_USE_LINENO", NULL);
459 /* Create the alignment type. */
460 buf_strdefine (&userdef_buf, "YY_INT_ALIGNED",
461 long_align ? "long int" : "short int");
463 /* Define the start condition macros. */
466 buf_init(&tmpbuf, sizeof(char));
467 for (i = 1; i <= lastsc; i++) {
468 char *str, *fmt = "#define %s %d\n";
471 strsz = strlen(fmt) + strlen(scname[i]) + NUMCHARLINES + 2;
474 flexfatal(_("allocation of macro definition failed"));
475 snprintf(str, strsz, fmt, scname[i], i - 1);
476 buf_strappend(&tmpbuf, str);
479 buf_m4_define(&m4defs_buf, "M4_YY_SC_DEFS", tmpbuf.elts);
480 buf_destroy(&tmpbuf);
483 /* This is where we begin writing to the file. */
485 /* Dump the %top code. */
487 outn((char*) top_buf.elts);
489 /* Dump the m4 definitions. */
490 buf_print_strings(&m4defs_buf, stdout);
491 m4defs_buf.nelts = 0; /* memory leak here. */
493 /* Place a bogus line directive, it will be fixed in the filter. */
495 outn("#line 0 \"M4_YY_OUTFILE_NAME\"\n");
497 /* Dump the user defined preproc directives. */
498 if (userdef_buf.elts)
499 outn ((char *) (userdef_buf.elts));
505 /* flexend - terminate flex
508 * This routine does not return.
511 void flexend (int exit_status)
513 static int called_before = -1; /* prevent infinite recursion. */
517 FLEX_EXIT (exit_status);
519 if (skelfile != NULL) {
520 if (ferror (skelfile))
521 lerr (_("input error reading skeleton file %s"),
524 else if (fclose (skelfile))
525 lerr (_("error closing skeleton file %s"),
531 "#ifdef YY_HEADER_EXPORT_START_CONDITIONS\n");
533 "/* Beware! Start conditions are not prefixed. */\n");
535 /* Special case for "INITIAL" */
537 "#undef INITIAL\n#define INITIAL 0\n");
538 for (i = 2; i <= lastsc; i++)
539 fprintf (header_out, "#define %s %d\n", scname[i], i - 1);
541 "#endif /* YY_HEADER_EXPORT_START_CONDITIONS */\n\n");
543 /* Kill ALL flex-related macros. This is so the user
544 * can #include more than one generated header file. */
545 fprintf (header_out, "#ifndef YY_HEADER_NO_UNDEFS\n");
547 "/* Undefine all internal macros, etc., that do no belong in the header. */\n\n");
550 const char * undef_list[] = {
554 "EOB_ACT_CONTINUE_SCAN",
555 "EOB_ACT_END_OF_FILE",
556 "EOB_ACT_LAST_MATCH",
567 "YY_BUFFER_EOF_PENDING",
571 "M4_YY_CALL_LAST_ARG",
572 "M4_YY_CALL_ONLY_ARG",
575 "M4_YY_DECL_LAST_ARG",
576 "M4_YY_DEF_LAST_ARG",
577 "M4_YY_DEF_ONLY_ARG",
578 "YY_DO_BEFORE_ACTION",
580 "YY_END_OF_BUFFER_CHAR",
584 "YY_FLEX_DEFINED_ECHO",
585 "YY_FLEX_LEX_COMPAT",
586 "YY_FLEX_MAJOR_VERSION",
587 "YY_FLEX_MINOR_VERSION",
588 "YY_FLEX_SUBMINOR_VERSION",
597 "YY_LEX_DECLARATION",
608 "M4_YY_PROTO_LAST_ARG",
609 "M4_YY_PROTO_ONLY_ARG void",
612 "YY_RESTORE_YY_MORE_OFFSET",
617 "YY_START_STACK_INCR",
620 "YY_TRAILING_HEAD_MASK",
632 "yy_load_buffer_state",
638 "yy_set_interactive",
639 "yy_switch_to_buffer",
640 "yypush_buffer_state",
641 "yypop_buffer_state",
642 "yyensure_buffer_stack",
684 /* must be null-terminated */
688 for (i=0; undef_list[i] != NULL; i++)
689 fprintf (header_out, "#undef %s\n", undef_list[i]);
692 /* undef any of the auto-generated symbols. */
693 for (i = 0; i < defs_buf.nelts; i++) {
695 /* don't undef start conditions */
696 if (sclookup (((char **) defs_buf.elts)[i]) > 0)
698 fprintf (header_out, "#undef %s\n",
699 ((char **) defs_buf.elts)[i]);
703 "#endif /* !YY_HEADER_NO_UNDEFS */\n");
704 fprintf (header_out, "\n");
705 fprintf (header_out, "#undef %sIN_HEADER\n", prefix);
706 fprintf (header_out, "#endif /* %sHEADER_H */\n", prefix);
708 if (ferror (header_out))
709 lerr (_("error creating header file %s"),
715 if (exit_status != 0 && outfile_created) {
717 lerr (_("error writing output file %s"),
720 else if ((_stdout_closed = 1) && fclose (stdout))
721 lerr (_("error closing output file %s"),
724 else if (unlink (outfilename))
725 lerr (_("error deleting output file %s"),
730 if (backing_up_report && backing_up_file) {
731 if (num_backing_up == 0)
732 fprintf (backing_up_file, _("No backing up.\n"));
733 else if (fullspd || fulltbl)
734 fprintf (backing_up_file,
736 ("%d backing up (non-accepting) states.\n"),
739 fprintf (backing_up_file,
740 _("Compressed tables always back up.\n"));
742 if (ferror (backing_up_file))
743 lerr (_("error writing backup file %s"),
746 else if (fclose (backing_up_file))
747 lerr (_("error closing backup file %s"),
752 fprintf (stderr, _("%s version %s usage statistics:\n"),
753 program_name, flex_version);
755 fprintf (stderr, _(" scanner options: -"));
759 if (backing_up_report)
769 if (performance_report > 0)
771 if (performance_report > 1)
776 fputs ("--reentrant", stderr);
777 if (bison_bridge_lval)
778 fputs ("--bison-bridge", stderr);
779 if (bison_bridge_lloc)
780 fputs ("--bison-locations", stderr);
784 putc ('v', stderr); /* always true! */
787 if (interactive == false)
789 if (interactive == true)
796 if (csize == unspecified)
797 /* We encountered an error fairly early on, so csize
798 * never got specified. Define it now, to prevent
799 * bogus table sizes being written out below.
808 fprintf (stderr, " -C");
824 fprintf (stderr, " -o%s", outfilename);
827 fprintf (stderr, " -S%s", skelname);
829 if (strcmp (prefix, "yy"))
830 fprintf (stderr, " -P%s", prefix);
834 fprintf (stderr, _(" %d/%d NFA states\n"),
835 lastnfa, current_mns);
836 fprintf (stderr, _(" %d/%d DFA states (%d words)\n"),
837 lastdfa, current_max_dfas, totnst);
838 fprintf (stderr, _(" %d rules\n"),
839 num_rules + num_eof_rules -
840 1 /* - 1 for def. rule */ );
842 if (num_backing_up == 0)
843 fprintf (stderr, _(" No backing up\n"));
844 else if (fullspd || fulltbl)
847 (" %d backing-up (non-accepting) states\n"),
852 (" Compressed tables always back-up\n"));
856 _(" Beginning-of-line patterns used\n"));
858 fprintf (stderr, _(" %d/%d start conditions\n"), lastsc,
862 (" %d epsilon states, %d double epsilon states\n"),
866 fprintf (stderr, _(" no character classes\n"));
870 (" %d/%d character classes needed %d/%d words of storage, %d reused\n"),
871 lastccl, current_maxccls,
872 cclmap[lastccl] + ccllen[lastccl],
873 current_max_ccl_tbl_size, cclreuse);
875 fprintf (stderr, _(" %d state/nextstate pairs created\n"),
878 _(" %d/%d unique/duplicate transitions\n"),
882 tblsiz = lastdfa * numecs;
883 fprintf (stderr, _(" %d table entries\n"),
888 tblsiz = 2 * (lastdfa + numtemps) + 2 * tblend;
891 _(" %d/%d base-def entries created\n"),
892 lastdfa + numtemps, current_max_dfas);
895 (" %d/%d (peak %d) nxt-chk entries created\n"),
896 tblend, current_max_xpairs, peakpairs);
899 (" %d/%d (peak %d) template nxt-chk entries created\n"),
901 current_max_template_xpairs,
903 fprintf (stderr, _(" %d empty table entries\n"),
905 fprintf (stderr, _(" %d protos created\n"),
908 _(" %d templates created, %d uses\n"),
913 tblsiz = tblsiz + csize;
916 (" %d/%d equivalence classes created\n"),
921 tblsiz = tblsiz + numecs;
924 (" %d/%d meta-equivalence classes created\n"),
930 (" %d (%d saved) hash collisions, %d DFAs equal\n"),
931 hshcol, hshsave, dfaeql);
932 fprintf (stderr, _(" %d sets of reallocations needed\n"),
934 fprintf (stderr, _(" %d total table entries needed\n"),
938 FLEX_EXIT (exit_status);
942 /* flexinit - initialize flex */
944 void flexinit (int argc, char **argv)
946 int i, sawcmpflag, rv, optind;
950 printstats = syntaxerror = trace = spprdflt = false;
951 lex_compat = posix_compat = C_plus_plus = backing_up_report =
952 ddebug = fulltbl = false;
953 fullspd = long_align = nowarn = yymore_used = continued_action =
955 do_yylineno = yytext_is_array = in_rule = reject = do_stdinit =
957 yymore_really_used = reject_really_used = unspecified;
958 interactive = csize = unspecified;
959 do_yywrap = gen_line_dirs = usemecs = useecs = true;
960 reentrant = bison_bridge_lval = bison_bridge_lloc = false;
961 performance_report = 0;
965 use_read = use_stdout = false;
966 tablesext = tablesverify = false;
968 tablesfilename = tablesname = NULL;
972 /* Initialize dynamic array for holding the rule actions. */
973 action_size = 2048; /* default size of action array in bytes */
974 action_array = allocate_character_array (action_size);
975 defs1_offset = prolog_offset = action_offset = action_index = 0;
976 action_array[0] = '\0';
978 /* Initialize any buffers. */
979 buf_init (&userdef_buf, sizeof (char)); /* one long string */
980 buf_init (&defs_buf, sizeof (char *)); /* list of strings */
981 buf_init (&yydmap_buf, sizeof (char)); /* one long string */
982 buf_init (&top_buf, sizeof (char)); /* one long string */
985 const char * m4defs_init_str[] = {"m4_changequote\n",
986 "m4_changequote([[, ]])\n"};
987 buf_init (&m4defs_buf, sizeof (char *));
988 buf_append (&m4defs_buf, &m4defs_init_str, 2);
993 /* initialize regex lib */
996 /* Enable C++ if program name ends with '+'. */
997 program_name = basename (argv[0]);
999 if (program_name != NULL &&
1000 program_name[strlen (program_name) - 1] == '+')
1004 sopt = scanopt_init (flexopts, argc, argv, 0);
1006 /* This will only happen when flexopts array is altered. */
1008 _("Internal error. flexopts are malformed.\n"));
1012 while ((rv = scanopt (sopt, &arg, &optind)) != 0) {
1015 /* Scanopt has already printed an option-specific error message. */
1018 ("Try `%s --help' for more information.\n"),
1023 switch ((enum flexopt_flag_t) rv) {
1029 interactive = false;
1033 backing_up_report = true;
1039 case OPT_COMPRESSION:
1047 for (i = 0; arg && arg[i] != '\0'; i++)
1075 ("unknown -C option '%c'"),
1090 useecs = usemecs = false;
1091 use_read = fulltbl = true;
1095 useecs = usemecs = false;
1096 use_read = fullspd = true;
1103 case OPT_INTERACTIVE:
1107 case OPT_CASE_INSENSITIVE:
1108 sf_set_case_ins(true);
1111 case OPT_LEX_COMPAT:
1115 case OPT_POSIX_COMPAT:
1116 posix_compat = true;
1119 case OPT_PREPROC_LEVEL:
1120 preproc_level = (int) strtol(arg,NULL,0);
1124 buf_strdefine (&userdef_buf, "YY_MAIN", "1");
1129 buf_strdefine (&userdef_buf, "YY_MAIN", "0");
1133 gen_line_dirs = false;
1138 did_outfilename = 1;
1145 case OPT_PERF_REPORT:
1146 ++performance_report;
1149 case OPT_BISON_BRIDGE:
1150 bison_bridge_lval = true;
1153 case OPT_BISON_BRIDGE_LOCATIONS:
1154 bison_bridge_lval = bison_bridge_lloc = true;
1161 case OPT_NO_REENTRANT:
1173 case OPT_NO_DEFAULT:
1181 case OPT_NO_UNISTD_H:
1182 //buf_strdefine (&userdef_buf, "YY_NO_UNISTD_H", "1");
1183 buf_m4_define( &m4defs_buf, "M4_YY_NO_UNISTD_H",0);
1186 case OPT_TABLES_FILE:
1188 tablesfilename = arg;
1191 case OPT_TABLES_VERIFY:
1192 tablesverify = true;
1204 printf (_("%s %s\n"), program_name, flex_version);
1231 case OPT_ALWAYS_INTERACTIVE:
1232 buf_m4_define (&m4defs_buf, "M4_YY_ALWAYS_INTERACTIVE", 0);
1235 case OPT_NEVER_INTERACTIVE:
1236 buf_m4_define( &m4defs_buf, "M4_YY_NEVER_INTERACTIVE", 0);
1240 yytext_is_array = true;
1244 yytext_is_array = false;
1255 case OPT_HEADER_FILE:
1256 headerfilename = arg;
1263 case OPT_NO_META_ECS:
1267 case OPT_PREPROCDEFINE:
1269 /* arg is "symbol" or "symbol=definition". */
1273 *def != '\0' && *def != '='; ++def) ;
1275 buf_strappend (&userdef_buf, "#define ");
1277 buf_strappend (&userdef_buf, arg);
1278 buf_strappend (&userdef_buf,
1282 buf_strnappend (&userdef_buf, arg,
1284 buf_strappend (&userdef_buf, " ");
1285 buf_strappend (&userdef_buf,
1287 buf_strappend (&userdef_buf, "\n");
1297 //buf_strdefine (&userdef_buf, "YY_STACK_USED", "1");
1298 buf_m4_define( &m4defs_buf, "M4_YY_STACK_USED",0);
1305 case OPT_NO_STDINIT:
1317 case OPT_NO_YYLINENO:
1318 do_yylineno = false;
1330 yymore_really_used = true;
1334 yymore_really_used = false;
1338 reject_really_used = true;
1342 reject_really_used = false;
1345 case OPT_NO_YY_PUSH_STATE:
1346 //buf_strdefine (&userdef_buf, "YY_NO_PUSH_STATE", "1");
1347 buf_m4_define( &m4defs_buf, "M4_YY_NO_PUSH_STATE",0);
1349 case OPT_NO_YY_POP_STATE:
1350 //buf_strdefine (&userdef_buf, "YY_NO_POP_STATE", "1");
1351 buf_m4_define( &m4defs_buf, "M4_YY_NO_POP_STATE",0);
1353 case OPT_NO_YY_TOP_STATE:
1354 //buf_strdefine (&userdef_buf, "YY_NO_TOP_STATE", "1");
1355 buf_m4_define( &m4defs_buf, "M4_YY_NO_TOP_STATE",0);
1358 //buf_strdefine (&userdef_buf, "YY_NO_UNPUT", "1");
1359 buf_m4_define( &m4defs_buf, "M4_YY_NO_UNPUT",0);
1361 case OPT_NO_YY_SCAN_BUFFER:
1362 //buf_strdefine (&userdef_buf, "YY_NO_SCAN_BUFFER", "1");
1363 buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_BUFFER",0);
1365 case OPT_NO_YY_SCAN_BYTES:
1366 //buf_strdefine (&userdef_buf, "YY_NO_SCAN_BYTES", "1");
1367 buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_BYTES",0);
1369 case OPT_NO_YY_SCAN_STRING:
1370 //buf_strdefine (&userdef_buf, "YY_NO_SCAN_STRING", "1");
1371 buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_STRING",0);
1373 case OPT_NO_YYGET_EXTRA:
1374 //buf_strdefine (&userdef_buf, "YY_NO_GET_EXTRA", "1");
1375 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_EXTRA",0);
1377 case OPT_NO_YYSET_EXTRA:
1378 //buf_strdefine (&userdef_buf, "YY_NO_SET_EXTRA", "1");
1379 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_EXTRA",0);
1381 case OPT_NO_YYGET_LENG:
1382 //buf_strdefine (&userdef_buf, "YY_NO_GET_LENG", "1");
1383 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LENG",0);
1385 case OPT_NO_YYGET_TEXT:
1386 //buf_strdefine (&userdef_buf, "YY_NO_GET_TEXT", "1");
1387 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_TEXT",0);
1389 case OPT_NO_YYGET_LINENO:
1390 //buf_strdefine (&userdef_buf, "YY_NO_GET_LINENO", "1");
1391 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LINENO",0);
1393 case OPT_NO_YYSET_LINENO:
1394 //buf_strdefine (&userdef_buf, "YY_NO_SET_LINENO", "1");
1395 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LINENO",0);
1397 case OPT_NO_YYGET_IN:
1398 //buf_strdefine (&userdef_buf, "YY_NO_GET_IN", "1");
1399 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_IN",0);
1401 case OPT_NO_YYSET_IN:
1402 //buf_strdefine (&userdef_buf, "YY_NO_SET_IN", "1");
1403 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_IN",0);
1405 case OPT_NO_YYGET_OUT:
1406 //buf_strdefine (&userdef_buf, "YY_NO_GET_OUT", "1");
1407 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_OUT",0);
1409 case OPT_NO_YYSET_OUT:
1410 //buf_strdefine (&userdef_buf, "YY_NO_SET_OUT", "1");
1411 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_OUT",0);
1413 case OPT_NO_YYGET_LVAL:
1414 //buf_strdefine (&userdef_buf, "YY_NO_GET_LVAL", "1");
1415 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LVAL",0);
1417 case OPT_NO_YYSET_LVAL:
1418 //buf_strdefine (&userdef_buf, "YY_NO_SET_LVAL", "1");
1419 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LVAL",0);
1421 case OPT_NO_YYGET_LLOC:
1422 //buf_strdefine (&userdef_buf, "YY_NO_GET_LLOC", "1");
1423 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LLOC",0);
1425 case OPT_NO_YYSET_LLOC:
1426 //buf_strdefine (&userdef_buf, "YY_NO_SET_LLOC", "1");
1427 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LLOC",0);
1432 case OPT_NO_SECT3_ESCAPE:
1433 no_section3_escape = true;
1436 } /* while scanopt() */
1438 scanopt_destroy (sopt);
1440 num_input_files = argc - optind;
1441 input_files = argv + optind;
1442 set_input_file (num_input_files > 0 ? input_files[0] : NULL);
1444 lastccl = lastsc = lastdfa = lastnfa = 0;
1445 num_rules = num_eof_rules = default_rule = 0;
1446 numas = numsnpairs = tmpuses = 0;
1447 numecs = numeps = eps2 = num_reallocs = hshcol = dfaeql = totnst =
1449 numuniq = numdup = hshsave = eofseen = datapos = dataline = 0;
1450 num_backing_up = onesp = numprots = 0;
1451 variable_trailing_context_rules = bol_needed = false;
1453 linenum = sectnum = 1;
1456 /* Used in mkprot() so that the first proto goes in slot 1
1457 * of the proto queue.
1461 set_up_initial_allocations ();
1465 /* readin - read in the rules section of the input file(s) */
1469 static char yy_stdinit[] = "FILE *yyin = stdin, *yyout = stdout;";
1470 static char yy_nostdinit[] =
1471 "FILE *yyin = NULL, *yyout = NULL;";
1473 line_directive_out(NULL, 1);
1476 pinpoint_message (_("fatal parse error"));
1483 /* If the user explicitly requested posix compatibility by specifing the
1484 * posix-compat option, then we check for conflicting options. However, if
1485 * the POSIXLY_CORRECT variable is set, then we quietly make flex as
1486 * posix-compatible as possible. This is the recommended behavior
1487 * according to the GNU Coding Standards.
1489 * Note: The posix option was added to flex to provide the posix behavior
1490 * of the repeat operator in regular expressions, e.g., `ab{3}'
1493 /* TODO: This is where we try to make flex behave according to
1494 * posiz, AND check for conflicting options. How far should we go
1495 * with this? Should we disable all the neat-o flex features?
1497 /* Update: Estes says no, since other flex features don't violate posix. */
1500 if (getenv ("POSIXLY_CORRECT")) {
1501 posix_compat = true;
1504 if (backing_up_report) {
1505 backing_up_file = fopen (backing_name, "w");
1506 if (backing_up_file == NULL)
1508 ("could not create backing-up info file %s"),
1513 backing_up_file = NULL;
1515 if (yymore_really_used == true)
1517 else if (yymore_really_used == false)
1518 yymore_used = false;
1520 if (reject_really_used == true)
1522 else if (reject_really_used == false)
1525 if (performance_report > 0) {
1529 ("-l AT&T lex compatibility option entails a large performance penalty\n"));
1532 (" and may be the actual source of other reported performance penalties\n"));
1535 else if (do_yylineno) {
1538 ("%%option yylineno entails a performance penalty ONLY on rules that can match newline characters\n"));
1541 if (performance_report > 1) {
1545 ("-I (interactive) entails a minor performance penalty\n"));
1550 ("yymore() entails a minor performance penalty\n"));
1556 ("REJECT entails a large performance penalty\n"));
1558 if (variable_trailing_context_rules)
1561 ("Variable trailing context rules entail a large performance penalty\n"));
1567 if (variable_trailing_context_rules)
1570 if ((fulltbl || fullspd) && reject) {
1573 ("REJECT cannot be used with -f or -F"));
1574 else if (do_yylineno)
1576 ("%option yylineno cannot be used with REJECT"));
1579 ("variable trailing context rules cannot be used with -f or -F"));
1583 out_m4_define( "M4_YY_USES_REJECT", NULL);
1584 //outn ("\n#define YY_USES_REJECT");
1590 out_str ("\n#define %swrap(yyscanner) (/*CONSTCOND*/1)\n", prefix);
1592 out_str ("\n#define %swrap() (/*CONSTCOND*/1)\n", prefix);
1594 outn ("#define YY_SKIP_YYWRAP");
1598 outn ("\n#define FLEX_DEBUG");
1601 outn ("typedef flex_uint8_t YY_CHAR;");
1605 outn ("#define yytext_ptr yytext");
1608 outn ("#define YY_INTERACTIVE");
1613 /* In reentrant scanner, stdinit is handled in flex.skl. */
1616 outn ("#ifdef VMS");
1617 outn ("#ifdef __VMS_POSIX");
1618 outn ("#define YY_STDINIT");
1621 outn ("#define YY_STDINIT");
1625 outn ("#ifdef VMS");
1626 outn ("#ifndef __VMS_POSIX");
1627 outn (yy_nostdinit);
1638 outn (yy_nostdinit);
1645 outn ("typedef const struct yy_trans_info *yy_state_type;");
1646 else if (!C_plus_plus)
1647 outn ("typedef int yy_state_type;");
1651 outn ("#define YY_FLEX_LEX_COMPAT");
1653 if (!C_plus_plus && !reentrant) {
1654 outn ("extern int yylineno;");
1656 outn ("int yylineno = 1;");
1661 outn ("\n#include <FlexLexer.h>");
1664 outn("\nint yyFlexLexer::yywrap() { return 1; }");
1668 outn ("int yyFlexLexer::yylex()");
1670 outn ("\tLexerError( \"yyFlexLexer::yylex invoked but %option yyclass used\" );");
1671 outn ("\treturn 0;");
1674 out_str ("\n#define YY_DECL int %s::yylex()\n",
1681 /* Watch out: yytext_ptr is a variable when yytext is an array,
1682 * but it's a macro when yytext is a pointer.
1684 if (yytext_is_array) {
1686 outn ("extern char yytext[];\n");
1690 outn ("#define yytext_ptr yytext_r");
1693 outn ("extern char *yytext;");
1695 outn("#ifdef yytext_ptr");
1696 outn("#undef yytext_ptr");
1698 outn ("#define yytext_ptr yytext");
1704 ("%option yyclass only meaningful for C++ scanners"));
1708 numecs = cre8ecs (nextecm, ecgroup, csize);
1712 /* Now map the equivalence class for NUL to its expected place. */
1713 ecgroup[0] = ecgroup[csize];
1714 NUL_ec = ABS (ecgroup[0]);
1721 /* set_up_initial_allocations - allocate memory for internal tables */
1723 void set_up_initial_allocations (void)
1725 maximum_mns = (long_align ? MAXIMUM_MNS_LONG : MAXIMUM_MNS);
1726 current_mns = INITIAL_MNS;
1727 firstst = allocate_integer_array (current_mns);
1728 lastst = allocate_integer_array (current_mns);
1729 finalst = allocate_integer_array (current_mns);
1730 transchar = allocate_integer_array (current_mns);
1731 trans1 = allocate_integer_array (current_mns);
1732 trans2 = allocate_integer_array (current_mns);
1733 accptnum = allocate_integer_array (current_mns);
1734 assoc_rule = allocate_integer_array (current_mns);
1735 state_type = allocate_integer_array (current_mns);
1737 current_max_rules = INITIAL_MAX_RULES;
1738 rule_type = allocate_integer_array (current_max_rules);
1739 rule_linenum = allocate_integer_array (current_max_rules);
1740 rule_useful = allocate_integer_array (current_max_rules);
1741 rule_has_nl = allocate_bool_array (current_max_rules);
1743 current_max_scs = INITIAL_MAX_SCS;
1744 scset = allocate_integer_array (current_max_scs);
1745 scbol = allocate_integer_array (current_max_scs);
1746 scxclu = allocate_integer_array (current_max_scs);
1747 sceof = allocate_integer_array (current_max_scs);
1748 scname = allocate_char_ptr_array (current_max_scs);
1750 current_maxccls = INITIAL_MAX_CCLS;
1751 cclmap = allocate_integer_array (current_maxccls);
1752 ccllen = allocate_integer_array (current_maxccls);
1753 cclng = allocate_integer_array (current_maxccls);
1754 ccl_has_nl = allocate_bool_array (current_maxccls);
1756 current_max_ccl_tbl_size = INITIAL_MAX_CCL_TBL_SIZE;
1757 ccltbl = allocate_Character_array (current_max_ccl_tbl_size);
1759 current_max_dfa_size = INITIAL_MAX_DFA_SIZE;
1761 current_max_xpairs = INITIAL_MAX_XPAIRS;
1762 nxt = allocate_integer_array (current_max_xpairs);
1763 chk = allocate_integer_array (current_max_xpairs);
1765 current_max_template_xpairs = INITIAL_MAX_TEMPLATE_XPAIRS;
1766 tnxt = allocate_integer_array (current_max_template_xpairs);
1768 current_max_dfas = INITIAL_MAX_DFAS;
1769 base = allocate_integer_array (current_max_dfas);
1770 def = allocate_integer_array (current_max_dfas);
1771 dfasiz = allocate_integer_array (current_max_dfas);
1772 accsiz = allocate_integer_array (current_max_dfas);
1773 dhash = allocate_integer_array (current_max_dfas);
1774 dss = allocate_int_ptr_array (current_max_dfas);
1775 dfaacc = allocate_dfaacc_union (current_max_dfas);
1785 if (!did_outfilename) {
1786 snprintf (outfile_path, sizeof(outfile_path), outfile_template,
1787 prefix, C_plus_plus ? "cc" : "c");
1788 outfilename = outfile_path;
1791 fprintf (f, _("Usage: %s [OPTIONS] [FILE]...\n"), program_name);
1794 ("Generates programs that perform pattern-matching on text.\n"
1795 "\n" "Table Compression:\n"
1796 " -Ca, --align trade off larger tables for better memory alignment\n"
1797 " -Ce, --ecs construct equivalence classes\n"
1798 " -Cf do not compress tables; use -f representation\n"
1799 " -CF do not compress tables; use -F representation\n"
1800 " -Cm, --meta-ecs construct meta-equivalence classes\n"
1801 " -Cr, --read use read() instead of stdio for scanner input\n"
1802 " -f, --full generate fast, large scanner. Same as -Cfr\n"
1803 " -F, --fast use alternate table representation. Same as -CFr\n"
1804 " -Cem default compression (same as --ecs --meta-ecs)\n"
1806 " -d, --debug enable debug mode in scanner\n"
1807 " -b, --backup write backing-up information to %s\n"
1808 " -p, --perf-report write performance report to stderr\n"
1809 " -s, --nodefault suppress default rule to ECHO unmatched text\n"
1810 " -T, --trace %s should run in trace mode\n"
1811 " -w, --nowarn do not generate warnings\n"
1812 " -v, --verbose write summary of scanner statistics to stdout\n"
1813 " --hex use hexadecimal numbers instead of octal in debug outputs\n"
1815 " -o, --outfile=FILE specify output filename\n"
1816 " -S, --skel=FILE specify skeleton file\n"
1817 " -t, --stdout write scanner on stdout instead of %s\n"
1818 " --yyclass=NAME name of C++ class\n"
1819 " --header-file=FILE create a C header file in addition to the scanner\n"
1820 " --tables-file[=FILE] write tables to FILE\n" "\n"
1821 "Scanner behavior:\n"
1822 " -7, --7bit generate 7-bit scanner\n"
1823 " -8, --8bit generate 8-bit scanner\n"
1824 " -B, --batch generate batch scanner (opposite of -I)\n"
1825 " -i, --case-insensitive ignore case in patterns\n"
1826 " -l, --lex-compat maximal compatibility with original lex\n"
1827 " -X, --posix-compat maximal compatibility with POSIX lex\n"
1828 " -I, --interactive generate interactive scanner (opposite of -B)\n"
1829 " --yylineno track line count in yylineno\n"
1830 "\n" "Generated code:\n"
1831 " -+, --c++ generate C++ scanner class\n"
1832 " -Dmacro[=defn] #define macro defn (default defn is '1')\n"
1833 " -L, --noline suppress #line directives in scanner\n"
1834 " -P, --prefix=STRING use STRING as prefix instead of \"yy\"\n"
1835 " -R, --reentrant generate a reentrant C scanner\n"
1836 " --bison-bridge scanner for bison pure parser.\n"
1837 " --bison-locations include yylloc support.\n"
1838 " --stdinit initialize yyin/yyout to stdin/stdout\n"
1839 " --nounistd do not include <unistd.h>\n"
1840 " --noFUNCTION do not generate a particular FUNCTION\n"
1841 "\n" "Miscellaneous:\n"
1842 " -c do-nothing POSIX option\n"
1843 " -n do-nothing POSIX option\n"
1845 " -h, --help produce this help message\n"
1846 " -V, --version report %s version\n"),
1847 backing_name, program_name, outfile_path, program_name);