xref: /freebsd/contrib/flex/src/main.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /* flex - tool to generate fast lexical analyzers */
2 
3 /*  Copyright (c) 1990 The Regents of the University of California. */
4 /*  All rights reserved. */
5 
6 /*  This code is derived from software contributed to Berkeley by */
7 /*  Vern Paxson. */
8 
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. */
12 
13 /*  This file is part of flex. */
14 
15 /*  Redistribution and use in source and binary forms, with or without */
16 /*  modification, are permitted provided that the following conditions */
17 /*  are met: */
18 
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. */
24 
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. */
28 
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 */
32 /*  PURPOSE. */
33 
34 
35 #include "flexdef.h"
36 #include "version.h"
37 #include "options.h"
38 #include "tables.h"
39 #include "parse.h"
40 
41 static char flex_version[] = FLEX_VERSION;
42 
43 /* declare functions that have forward references */
44 
45 void flexinit(int, char **);
46 void readin(void);
47 void set_up_initial_allocations(void);
48 
49 
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,
56 	csize;
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     trace_hex = 0;
61 int     datapos, dataline, linenum;
62 FILE   *skelfile = NULL;
63 int     skel_ind = 0;
64 char   *action_array;
65 int     action_size, defs1_offset, prolog_offset, action_offset,
66 	action_index;
67 char   *infilename = NULL, *outfilename = NULL, *headerfilename = NULL;
68 int     did_outfilename;
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,
83 	tecfwd[CSIZE + 1];
84 int     tecbck[CSIZE + 1];
85 int     lastsc, *scset, *scbol, *scxclu, *sceof;
86 int     current_max_scs;
87 char  **scname;
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;
98 char    nmstr[MAXLINE];
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;
104 char  **input_files;
105 int     num_input_files;
106 jmp_buf flex_main_jmp_buf;
107 bool   *rule_has_nl, *ccl_has_nl;
108 int     nlch = '\n';
109 
110 bool    tablesext, tablesverify, gentables;
111 char   *tablesfilename=0,*tablesname=0;
112 struct yytbl_writer tableswr;
113 
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].
116  */
117 char   *program_name = "flex";
118 
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";
122 
123 /* From scan.l */
124 extern FILE* yyout;
125 
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[[";
132 
133 /* For debugging. The max number of filters to apply to skeleton. */
134 static int preproc_level = 1000;
135 
136 int flex_main (int argc, char *argv[]);
137 
138 int flex_main (int argc, char *argv[])
139 {
140 	int     i, exit_status, child_status;
141 
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
147 	 * exit(n);
148 	 */
149 	exit_status = setjmp (flex_main_jmp_buf);
150 	if (exit_status){
151         if (stdout && !_stdout_closed && !ferror(stdout)){
152             fflush(stdout);
153             fclose(stdout);
154         }
155         while (wait(&child_status) > 0){
156             if (!WIFEXITED (child_status)
157                 || WEXITSTATUS (child_status) != 0){
158                 /* report an error of a child
159                  */
160                 if( exit_status <= 1 )
161                     exit_status = 2;
162 
163             }
164         }
165         return exit_status - 1;
166     }
167 
168 	flexinit (argc, argv);
169 
170 	readin ();
171 
172 	skelout ();
173 	/* %% [1.5] DFA */
174 	ntod ();
175 
176 	for (i = 1; i <= num_rules; ++i)
177 		if (!rule_useful[i] && i != default_rule)
178 			line_warning (_("rule cannot be matched"),
179 				      rule_linenum[i]);
180 
181 	if (spprdflt && !reject && rule_useful[default_rule])
182 		line_warning (_
183 			      ("-s option given but default rule can be matched"),
184 			      rule_linenum[default_rule]);
185 
186 	/* Generate the C state transition tables from the DFA. */
187 	make_tables ();
188 
189 	/* Note, flexend does not return.  It exits with its argument
190 	 * as status.
191 	 */
192 	flexend (0);
193 
194 	return 0;		/* keep compilers/lint happy */
195 }
196 
197 /* Wrapper around flex_main, so flex_main can be built as a library. */
198 int main (int argc, char *argv[])
199 {
200 #if ENABLE_NLS
201 #if HAVE_LOCALE_H
202 	setlocale (LC_MESSAGES, "");
203         setlocale (LC_CTYPE, "");
204 	textdomain (PACKAGE);
205 	bindtextdomain (PACKAGE, LOCALEDIR);
206 #endif
207 #endif
208 
209 	return flex_main (argc, argv);
210 }
211 
212 /* check_options - check user-specified options */
213 
214 void check_options (void)
215 {
216 	int     i;
217     const char * m4 = NULL;
218 
219 	if (lex_compat) {
220 		if (C_plus_plus)
221 			flexerror (_("Can't use -+ with -l option"));
222 
223 		if (fulltbl || fullspd)
224 			flexerror (_("Can't use -f or -F with -l option"));
225 
226 		if (reentrant || bison_bridge_lval)
227 			flexerror (_
228 				   ("Can't use --reentrant or --bison-bridge with -l option"));
229 
230 		yytext_is_array = true;
231 		do_yylineno = true;
232 		use_read = false;
233 	}
234 
235 
236 #if 0
237 	/* This makes no sense whatsoever. I'm removing it. */
238 	if (do_yylineno)
239 		/* This should really be "maintain_backup_tables = true" */
240 		reject_really_used = true;
241 #endif
242 
243 	if (csize == unspecified) {
244 		if ((fulltbl || fullspd) && !useecs)
245 			csize = DEFAULT_CSIZE;
246 		else
247 			csize = CSIZE;
248 	}
249 
250 	if (interactive == unspecified) {
251 		if (fulltbl || fullspd)
252 			interactive = false;
253 		else
254 			interactive = true;
255 	}
256 
257 	if (fulltbl || fullspd) {
258 		if (usemecs)
259 			flexerror (_
260 				   ("-Cf/-CF and -Cm don't make sense together"));
261 
262 		if (interactive)
263 			flexerror (_("-Cf/-CF and -I are incompatible"));
264 
265 		if (lex_compat)
266 			flexerror (_
267 				   ("-Cf/-CF are incompatible with lex-compatibility mode"));
268 
269 
270 		if (fulltbl && fullspd)
271 			flexerror (_
272 				   ("-Cf and -CF are mutually exclusive"));
273 	}
274 
275 	if (C_plus_plus && fullspd)
276 		flexerror (_("Can't use -+ with -CF option"));
277 
278 	if (C_plus_plus && yytext_is_array) {
279 		lwarn (_("%array incompatible with -+ option"));
280 		yytext_is_array = false;
281 	}
282 
283 	if (C_plus_plus && (reentrant))
284 		flexerror (_("Options -+ and --reentrant are mutually exclusive."));
285 
286 	if (C_plus_plus && bison_bridge_lval)
287 		flexerror (_("bison bridge not supported for the C++ scanner."));
288 
289 
290 	if (useecs) {		/* Set up doubly-linked equivalence classes. */
291 
292 		/* We loop all the way up to csize, since ecgroup[csize] is
293 		 * the position used for NUL characters.
294 		 */
295 		ecgroup[1] = NIL;
296 
297 		for (i = 2; i <= csize; ++i) {
298 			ecgroup[i] = i - 1;
299 			nextecm[i - 1] = i;
300 		}
301 
302 		nextecm[csize] = NIL;
303 	}
304 
305 	else {
306 		/* Put everything in its own equivalence class. */
307 		for (i = 1; i <= csize; ++i) {
308 			ecgroup[i] = i;
309 			nextecm[i] = BAD_SUBSCRIPT;	/* to catch errors */
310 		}
311 	}
312 
313 	if (extra_type)
314 		buf_m4_define( &m4defs_buf, "M4_EXTRA_TYPE_DEFS", extra_type);
315 
316 	if (!use_stdout) {
317 		FILE   *prev_stdout;
318 
319 		if (!did_outfilename) {
320 			char   *suffix;
321 
322 			if (C_plus_plus)
323 				suffix = "cc";
324 			else
325 				suffix = "c";
326 
327 			snprintf (outfile_path, sizeof(outfile_path), outfile_template,
328 				 prefix, suffix);
329 
330 			outfilename = outfile_path;
331 		}
332 
333 		prev_stdout = freopen (outfilename, "w+", stdout);
334 
335 		if (prev_stdout == NULL)
336 			lerr (_("could not create %s"), outfilename);
337 
338 		outfile_created = 1;
339 	}
340 
341 
342     /* Setup the filter chain. */
343     output_chain = filter_create_int(NULL, filter_tee_header, headerfilename);
344     if ( !(m4 = getenv("M4"))) {
345 		const char *slash;
346 		m4 = M4;
347 		if ((slash = strrchr(M4, '/')) != NULL) {
348 			m4 = slash+1;
349 			/* break up $PATH */
350 			const char *path = getenv("PATH");
351 			if (!path) {
352 				m4 = M4;
353 			} else {
354 				int m4_length = strlen(m4);
355 				do {
356 					size_t length = strlen(path);
357 					struct stat sbuf;
358 
359 					const char *endOfDir = strchr(path, ':');
360 					if (!endOfDir)
361 						endOfDir = path+length;
362 
363 					{
364 						char *m4_path = calloc(endOfDir-path + 1 + m4_length + 1, 1);
365 
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) {
371 							m4 = m4_path;
372 							break;
373 						}
374 						free(m4_path);
375 					}
376 					path = endOfDir+1;
377 				} while (path[0]);
378 				if (!path[0])
379 				    m4 = M4;
380 			}
381 		}
382 	}
383     filter_create_ext(output_chain, m4, "-gP", 0);
384     filter_create_int(output_chain, filter_fix_linedirs, NULL);
385 
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);
390     }
391     yyout = stdout;
392 
393 
394 	/* always generate the tablesverify flag. */
395 	buf_m4_define (&m4defs_buf, "M4_YY_TABLES_VERIFY", tablesverify ? "1" : "0");
396 	if (tablesext)
397 		gentables = false;
398 
399 	if (tablesverify)
400 		/* force generation of C tables. */
401 		gentables = true;
402 
403 
404 	if (tablesext) {
405 		FILE   *tablesout;
406 		struct yytbl_hdr hdr;
407 		char   *pname = 0;
408 		size_t  nbytes = 0;
409 
410 		buf_m4_define (&m4defs_buf, "M4_YY_TABLES_EXTERNAL", NULL);
411 
412 		if (!tablesfilename) {
413 			nbytes = strlen (prefix) + strlen (tablesfile_template) + 2;
414 			tablesfilename = pname = calloc(nbytes, 1);
415 			snprintf (pname, nbytes, tablesfile_template, prefix);
416 		}
417 
418 		if ((tablesout = fopen (tablesfilename, "w")) == NULL)
419 			lerr (_("could not create %s"), tablesfilename);
420 		free(pname);
421 		tablesfilename = 0;
422 
423 		yytbl_writer_init (&tableswr, tablesout);
424 
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);
429 
430 		if (yytbl_hdr_fwrite (&tableswr, &hdr) <= 0)
431 			flexerror (_("could not write tables header"));
432 	}
433 
434 	if (skelname && (skelfile = fopen (skelname, "r")) == NULL)
435 		lerr (_("can't open skeleton file %s"), skelname);
436 
437 	if (reentrant) {
438         buf_m4_define (&m4defs_buf, "M4_YY_REENTRANT", NULL);
439 		if (yytext_is_array)
440 			buf_m4_define (&m4defs_buf, "M4_YY_TEXT_IS_ARRAY", NULL);
441 	}
442 
443 	if ( bison_bridge_lval)
444 		buf_m4_define (&m4defs_buf, "M4_YY_BISON_LVAL", NULL);
445 
446 	if ( bison_bridge_lloc)
447         buf_m4_define (&m4defs_buf, "<M4_YY_BISON_LLOC>", NULL);
448 
449     if (strchr(prefix, '[') || strchr(prefix, ']'))
450         flexerror(_("Prefix cannot include '[' or ']'"));
451     buf_m4_define(&m4defs_buf, "M4_YY_PREFIX", prefix);
452 
453 	if (did_outfilename)
454 		line_directive_out (stdout, 0);
455 
456 	if (do_yylineno)
457 		buf_m4_define (&m4defs_buf, "M4_YY_USE_LINENO", NULL);
458 
459 	/* Create the alignment type. */
460 	buf_strdefine (&userdef_buf, "YY_INT_ALIGNED",
461 		       long_align ? "long int" : "short int");
462 
463     /* Define the start condition macros. */
464     {
465         struct Buf tmpbuf;
466         buf_init(&tmpbuf, sizeof(char));
467         for (i = 1; i <= lastsc; i++) {
468              char *str, *fmt = "#define %s %d\n";
469              size_t strsz;
470 
471              strsz = strlen(fmt) + strlen(scname[i]) + NUMCHARLINES + 2;
472              str = malloc(strsz);
473              if (!str)
474                flexfatal(_("allocation of macro definition failed"));
475              snprintf(str, strsz, fmt,      scname[i], i - 1);
476              buf_strappend(&tmpbuf, str);
477              free(str);
478         }
479         buf_m4_define(&m4defs_buf, "M4_YY_SC_DEFS", tmpbuf.elts);
480         buf_destroy(&tmpbuf);
481     }
482 
483     /* This is where we begin writing to the file. */
484 
485     /* Dump the %top code. */
486     if( top_buf.elts)
487         outn((char*) top_buf.elts);
488 
489     /* Dump the m4 definitions. */
490     buf_print_strings(&m4defs_buf, stdout);
491     m4defs_buf.nelts = 0; /* memory leak here. */
492 
493     /* Place a bogus line directive, it will be fixed in the filter. */
494     if (gen_line_dirs)
495         outn("#line 0 \"M4_YY_OUTFILE_NAME\"\n");
496 
497 	/* Dump the user defined preproc directives. */
498 	if (userdef_buf.elts)
499 		outn ((char *) (userdef_buf.elts));
500 
501 	skelout ();
502 	/* %% [1.0] */
503 }
504 
505 /* flexend - terminate flex
506  *
507  * note
508  *    This routine does not return.
509  */
510 
511 void flexend (int exit_status)
512 {
513 	static int called_before = -1;	/* prevent infinite recursion. */
514 	int     tblsiz;
515 
516 	if (++called_before)
517 		FLEX_EXIT (exit_status);
518 
519 	if (skelfile != NULL) {
520 		if (ferror (skelfile))
521 			lerr (_("input error reading skeleton file %s"),
522 				skelname);
523 
524 		else if (fclose (skelfile))
525 			lerr (_("error closing skeleton file %s"),
526 				skelname);
527 	}
528 
529 #if 0
530 		fprintf (header_out,
531 			 "#ifdef YY_HEADER_EXPORT_START_CONDITIONS\n");
532 		fprintf (header_out,
533 			 "/* Beware! Start conditions are not prefixed. */\n");
534 
535 		/* Special case for "INITIAL" */
536 		fprintf (header_out,
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);
540 		fprintf (header_out,
541 			 "#endif /* YY_HEADER_EXPORT_START_CONDITIONS */\n\n");
542 
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");
546 		fprintf (header_out,
547 			 "/* Undefine all internal macros, etc., that do no belong in the header. */\n\n");
548 
549         {
550             const char * undef_list[] = {
551 
552                 "BEGIN",
553                 "ECHO",
554                 "EOB_ACT_CONTINUE_SCAN",
555                 "EOB_ACT_END_OF_FILE",
556                 "EOB_ACT_LAST_MATCH",
557                 "FLEX_SCANNER",
558                 "REJECT",
559                 "YYFARGS0",
560                 "YYFARGS1",
561                 "YYFARGS2",
562                 "YYFARGS3",
563                 "YYLMAX",
564                 "YYSTATE",
565                 "YY_AT_BOL",
566                 "YY_BREAK",
567                 "YY_BUFFER_EOF_PENDING",
568                 "YY_BUFFER_NEW",
569                 "YY_BUFFER_NORMAL",
570                 "YY_BUF_SIZE",
571                 "M4_YY_CALL_LAST_ARG",
572                 "M4_YY_CALL_ONLY_ARG",
573                 "YY_CURRENT_BUFFER",
574                 "YY_DECL",
575                 "M4_YY_DECL_LAST_ARG",
576                 "M4_YY_DEF_LAST_ARG",
577                 "M4_YY_DEF_ONLY_ARG",
578                 "YY_DO_BEFORE_ACTION",
579                 "YY_END_OF_BUFFER",
580                 "YY_END_OF_BUFFER_CHAR",
581                 "YY_EXIT_FAILURE",
582                 "YY_EXTRA_TYPE",
583                 "YY_FATAL_ERROR",
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",
589                 "YY_FLUSH_BUFFER",
590                 "YY_G",
591                 "YY_INPUT",
592                 "YY_INTERACTIVE",
593                 "YY_INT_ALIGNED",
594                 "YY_LAST_ARG",
595                 "YY_LESS_LINENO",
596                 "YY_LEX_ARGS",
597                 "YY_LEX_DECLARATION",
598                 "YY_LEX_PROTO",
599                 "YY_MAIN",
600                 "YY_MORE_ADJ",
601                 "YY_NEED_STRLEN",
602                 "YY_NEW_FILE",
603                 "YY_NULL",
604                 "YY_NUM_RULES",
605                 "YY_ONLY_ARG",
606                 "YY_PARAMS",
607                 "YY_PROTO",
608                 "M4_YY_PROTO_LAST_ARG",
609                 "M4_YY_PROTO_ONLY_ARG void",
610                 "YY_READ_BUF_SIZE",
611                 "YY_REENTRANT",
612                 "YY_RESTORE_YY_MORE_OFFSET",
613                 "YY_RULE_SETUP",
614                 "YY_SC_TO_UI",
615                 "YY_SKIP_YYWRAP",
616                 "YY_START",
617                 "YY_START_STACK_INCR",
618                 "YY_STATE_EOF",
619                 "YY_STDINIT",
620                 "YY_TRAILING_HEAD_MASK",
621                 "YY_TRAILING_MASK",
622                 "YY_USER_ACTION",
623                 "YY_USE_CONST",
624                 "YY_USE_PROTOS",
625                 "unput",
626                 "yyTABLES_NAME",
627                 "yy_create_buffer",
628                 "yy_delete_buffer",
629                 "yy_flex_debug",
630                 "yy_flush_buffer",
631                 "yy_init_buffer",
632                 "yy_load_buffer_state",
633                 "yy_new_buffer",
634                 "yy_scan_buffer",
635                 "yy_scan_bytes",
636                 "yy_scan_string",
637                 "yy_set_bol",
638                 "yy_set_interactive",
639                 "yy_switch_to_buffer",
640 				"yypush_buffer_state",
641 				"yypop_buffer_state",
642 				"yyensure_buffer_stack",
643                 "yyalloc",
644                 "const",
645                 "yyextra",
646                 "yyfree",
647                 "yyget_debug",
648                 "yyget_extra",
649                 "yyget_in",
650                 "yyget_leng",
651                 "yyget_lineno",
652                 "yyget_lloc",
653                 "yyget_lval",
654                 "yyget_out",
655                 "yyget_text",
656                 "yyin",
657                 "yyleng",
658                 "yyless",
659                 "yylex",
660                 "yylex_destroy",
661                 "yylex_init",
662                 "yylex_init_extra",
663                 "yylineno",
664                 "yylloc",
665                 "yylval",
666                 "yymore",
667                 "yyout",
668                 "yyrealloc",
669                 "yyrestart",
670                 "yyset_debug",
671                 "yyset_extra",
672                 "yyset_in",
673                 "yyset_lineno",
674                 "yyset_lloc",
675                 "yyset_lval",
676                 "yyset_out",
677                 "yytables_destroy",
678                 "yytables_fload",
679                 "yyterminate",
680                 "yytext",
681                 "yytext_ptr",
682                 "yywrap",
683 
684                 /* must be null-terminated */
685                 NULL};
686 
687 
688                 for (i=0; undef_list[i] != NULL; i++)
689                     fprintf (header_out, "#undef %s\n", undef_list[i]);
690         }
691 
692 		/* undef any of the auto-generated symbols. */
693 		for (i = 0; i < defs_buf.nelts; i++) {
694 
695 			/* don't undef start conditions */
696 			if (sclookup (((char **) defs_buf.elts)[i]) > 0)
697 				continue;
698 			fprintf (header_out, "#undef %s\n",
699 				 ((char **) defs_buf.elts)[i]);
700 		}
701 
702 		fprintf (header_out,
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);
707 
708 		if (ferror (header_out))
709 			lerr (_("error creating header file %s"),
710 				headerfilename);
711 		fflush (header_out);
712 		fclose (header_out);
713 #endif
714 
715 	if (exit_status != 0 && outfile_created) {
716 		if (ferror (stdout))
717 			lerr (_("error writing output file %s"),
718 				outfilename);
719 
720 		else if ((_stdout_closed = 1) && fclose (stdout))
721 			lerr (_("error closing output file %s"),
722 				outfilename);
723 
724 		else if (unlink (outfilename))
725 			lerr (_("error deleting output file %s"),
726 				outfilename);
727 	}
728 
729 
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,
735 				 _
736 				 ("%d backing up (non-accepting) states.\n"),
737 				 num_backing_up);
738 		else
739 			fprintf (backing_up_file,
740 				 _("Compressed tables always back up.\n"));
741 
742 		if (ferror (backing_up_file))
743 			lerr (_("error writing backup file %s"),
744 				backing_name);
745 
746 		else if (fclose (backing_up_file))
747 			lerr (_("error closing backup file %s"),
748 				backing_name);
749 	}
750 
751 	if (printstats) {
752 		fprintf (stderr, _("%s version %s usage statistics:\n"),
753 			 program_name, flex_version);
754 
755 		fprintf (stderr, _("  scanner options: -"));
756 
757 		if (C_plus_plus)
758 			putc ('+', stderr);
759 		if (backing_up_report)
760 			putc ('b', stderr);
761 		if (ddebug)
762 			putc ('d', stderr);
763 		if (sf_case_ins())
764 			putc ('i', stderr);
765 		if (lex_compat)
766 			putc ('l', stderr);
767 		if (posix_compat)
768 			putc ('X', stderr);
769 		if (performance_report > 0)
770 			putc ('p', stderr);
771 		if (performance_report > 1)
772 			putc ('p', stderr);
773 		if (spprdflt)
774 			putc ('s', stderr);
775 		if (reentrant)
776 			fputs ("--reentrant", stderr);
777         if (bison_bridge_lval)
778             fputs ("--bison-bridge", stderr);
779         if (bison_bridge_lloc)
780             fputs ("--bison-locations", stderr);
781 		if (use_stdout)
782 			putc ('t', stderr);
783 		if (printstats)
784 			putc ('v', stderr);	/* always true! */
785 		if (nowarn)
786 			putc ('w', stderr);
787 		if (interactive == false)
788 			putc ('B', stderr);
789 		if (interactive == true)
790 			putc ('I', stderr);
791 		if (!gen_line_dirs)
792 			putc ('L', stderr);
793 		if (trace)
794 			putc ('T', stderr);
795 
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.
800 			 */
801 			csize = 256;
802 
803 		if (csize == 128)
804 			putc ('7', stderr);
805 		else
806 			putc ('8', stderr);
807 
808 		fprintf (stderr, " -C");
809 
810 		if (long_align)
811 			putc ('a', stderr);
812 		if (fulltbl)
813 			putc ('f', stderr);
814 		if (fullspd)
815 			putc ('F', stderr);
816 		if (useecs)
817 			putc ('e', stderr);
818 		if (usemecs)
819 			putc ('m', stderr);
820 		if (use_read)
821 			putc ('r', stderr);
822 
823 		if (did_outfilename)
824 			fprintf (stderr, " -o%s", outfilename);
825 
826 		if (skelname)
827 			fprintf (stderr, " -S%s", skelname);
828 
829 		if (strcmp (prefix, "yy"))
830 			fprintf (stderr, " -P%s", prefix);
831 
832 		putc ('\n', stderr);
833 
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 */ );
841 
842 		if (num_backing_up == 0)
843 			fprintf (stderr, _("  No backing up\n"));
844 		else if (fullspd || fulltbl)
845 			fprintf (stderr,
846 				 _
847 				 ("  %d backing-up (non-accepting) states\n"),
848 				 num_backing_up);
849 		else
850 			fprintf (stderr,
851 				 _
852 				 ("  Compressed tables always back-up\n"));
853 
854 		if (bol_needed)
855 			fprintf (stderr,
856 				 _("  Beginning-of-line patterns used\n"));
857 
858 		fprintf (stderr, _("  %d/%d start conditions\n"), lastsc,
859 			 current_max_scs);
860 		fprintf (stderr,
861 			 _
862 			 ("  %d epsilon states, %d double epsilon states\n"),
863 			 numeps, eps2);
864 
865 		if (lastccl == 0)
866 			fprintf (stderr, _("  no character classes\n"));
867 		else
868 			fprintf (stderr,
869 				 _
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);
874 
875 		fprintf (stderr, _("  %d state/nextstate pairs created\n"),
876 			 numsnpairs);
877 		fprintf (stderr,
878 			 _("  %d/%d unique/duplicate transitions\n"),
879 			 numuniq, numdup);
880 
881 		if (fulltbl) {
882 			tblsiz = lastdfa * numecs;
883 			fprintf (stderr, _("  %d table entries\n"),
884 				 tblsiz);
885 		}
886 
887 		else {
888 			tblsiz = 2 * (lastdfa + numtemps) + 2 * tblend;
889 
890 			fprintf (stderr,
891 				 _("  %d/%d base-def entries created\n"),
892 				 lastdfa + numtemps, current_max_dfas);
893 			fprintf (stderr,
894 				 _
895 				 ("  %d/%d (peak %d) nxt-chk entries created\n"),
896 				 tblend, current_max_xpairs, peakpairs);
897 			fprintf (stderr,
898 				 _
899 				 ("  %d/%d (peak %d) template nxt-chk entries created\n"),
900 				 numtemps * nummecs,
901 				 current_max_template_xpairs,
902 				 numtemps * numecs);
903 			fprintf (stderr, _("  %d empty table entries\n"),
904 				 nummt);
905 			fprintf (stderr, _("  %d protos created\n"),
906 				 numprots);
907 			fprintf (stderr,
908 				 _("  %d templates created, %d uses\n"),
909 				 numtemps, tmpuses);
910 		}
911 
912 		if (useecs) {
913 			tblsiz = tblsiz + csize;
914 			fprintf (stderr,
915 				 _
916 				 ("  %d/%d equivalence classes created\n"),
917 				 numecs, csize);
918 		}
919 
920 		if (usemecs) {
921 			tblsiz = tblsiz + numecs;
922 			fprintf (stderr,
923 				 _
924 				 ("  %d/%d meta-equivalence classes created\n"),
925 				 nummecs, csize);
926 		}
927 
928 		fprintf (stderr,
929 			 _
930 			 ("  %d (%d saved) hash collisions, %d DFAs equal\n"),
931 			 hshcol, hshsave, dfaeql);
932 		fprintf (stderr, _("  %d sets of reallocations needed\n"),
933 			 num_reallocs);
934 		fprintf (stderr, _("  %d total table entries needed\n"),
935 			 tblsiz);
936 	}
937 
938 	FLEX_EXIT (exit_status);
939 }
940 
941 
942 /* flexinit - initialize flex */
943 
944 void flexinit (int argc, char **argv)
945 {
946 	int     i, sawcmpflag, rv, optind;
947 	char   *arg;
948 	scanopt_t sopt;
949 
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 =
954 		false;
955 	do_yylineno = yytext_is_array = in_rule = reject = do_stdinit =
956 		false;
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;
962 	did_outfilename = 0;
963 	prefix = "yy";
964 	yyclass = 0;
965 	use_read = use_stdout = false;
966 	tablesext = tablesverify = false;
967 	gentables = true;
968 	tablesfilename = tablesname = NULL;
969 
970 	sawcmpflag = false;
971 
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';
977 
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 */
983 
984     {
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);
989     }
990 
991     sf_init ();
992 
993     /* initialize regex lib */
994     flex_init_regex();
995 
996 	/* Enable C++ if program name ends with '+'. */
997 	program_name = basename (argv[0]);
998 
999 	if (program_name != NULL &&
1000 	    program_name[strlen (program_name) - 1] == '+')
1001 		C_plus_plus = true;
1002 
1003 	/* read flags */
1004 	sopt = scanopt_init (flexopts, argc, argv, 0);
1005 	if (!sopt) {
1006 		/* This will only happen when flexopts array is altered. */
1007 		fprintf (stderr,
1008 			 _("Internal error. flexopts are malformed.\n"));
1009 		FLEX_EXIT (1);
1010 	}
1011 
1012 	while ((rv = scanopt (sopt, &arg, &optind)) != 0) {
1013 
1014 		if (rv < 0) {
1015 			/* Scanopt has already printed an option-specific error message. */
1016 			fprintf (stderr,
1017 				 _
1018 				 ("Try `%s --help' for more information.\n"),
1019 				 program_name);
1020 			FLEX_EXIT (1);
1021 		}
1022 
1023 		switch ((enum flexopt_flag_t) rv) {
1024 		case OPT_CPLUSPLUS:
1025 			C_plus_plus = true;
1026 			break;
1027 
1028 		case OPT_BATCH:
1029 			interactive = false;
1030 			break;
1031 
1032 		case OPT_BACKUP:
1033 			backing_up_report = true;
1034 			break;
1035 
1036 		case OPT_DONOTHING:
1037 			break;
1038 
1039 		case OPT_COMPRESSION:
1040 			if (!sawcmpflag) {
1041 				useecs = false;
1042 				usemecs = false;
1043 				fulltbl = false;
1044 				sawcmpflag = true;
1045 			}
1046 
1047 			for (i = 0; arg && arg[i] != '\0'; i++)
1048 				switch (arg[i]) {
1049 				case 'a':
1050 					long_align = true;
1051 					break;
1052 
1053 				case 'e':
1054 					useecs = true;
1055 					break;
1056 
1057 				case 'F':
1058 					fullspd = true;
1059 					break;
1060 
1061 				case 'f':
1062 					fulltbl = true;
1063 					break;
1064 
1065 				case 'm':
1066 					usemecs = true;
1067 					break;
1068 
1069 				case 'r':
1070 					use_read = true;
1071 					break;
1072 
1073 				default:
1074 					lerr (_
1075 						("unknown -C option '%c'"),
1076 						arg[i]);
1077 					break;
1078 				}
1079 			break;
1080 
1081 		case OPT_DEBUG:
1082 			ddebug = true;
1083 			break;
1084 
1085 		case OPT_NO_DEBUG:
1086 			ddebug = false;
1087 			break;
1088 
1089 		case OPT_FULL:
1090 			useecs = usemecs = false;
1091 			use_read = fulltbl = true;
1092 			break;
1093 
1094 		case OPT_FAST:
1095 			useecs = usemecs = false;
1096 			use_read = fullspd = true;
1097 			break;
1098 
1099 		case OPT_HELP:
1100 			usage ();
1101 			FLEX_EXIT (0);
1102 
1103 		case OPT_INTERACTIVE:
1104 			interactive = true;
1105 			break;
1106 
1107 		case OPT_CASE_INSENSITIVE:
1108 			sf_set_case_ins(true);
1109 			break;
1110 
1111 		case OPT_LEX_COMPAT:
1112 			lex_compat = true;
1113 			break;
1114 
1115 		case OPT_POSIX_COMPAT:
1116 			posix_compat = true;
1117 			break;
1118 
1119         case OPT_PREPROC_LEVEL:
1120             preproc_level = (int) strtol(arg,NULL,0);
1121             break;
1122 
1123 		case OPT_MAIN:
1124 			buf_strdefine (&userdef_buf, "YY_MAIN", "1");
1125 			do_yywrap = false;
1126 			break;
1127 
1128 		case OPT_NO_MAIN:
1129 			buf_strdefine (&userdef_buf, "YY_MAIN", "0");
1130 			break;
1131 
1132 		case OPT_NO_LINE:
1133 			gen_line_dirs = false;
1134 			break;
1135 
1136 		case OPT_OUTFILE:
1137 			outfilename = arg;
1138 			did_outfilename = 1;
1139 			break;
1140 
1141 		case OPT_PREFIX:
1142 			prefix = arg;
1143 			break;
1144 
1145 		case OPT_PERF_REPORT:
1146 			++performance_report;
1147 			break;
1148 
1149 		case OPT_BISON_BRIDGE:
1150 			bison_bridge_lval = true;
1151 			break;
1152 
1153 		case OPT_BISON_BRIDGE_LOCATIONS:
1154 			bison_bridge_lval = bison_bridge_lloc = true;
1155 			break;
1156 
1157 		case OPT_REENTRANT:
1158 			reentrant = true;
1159 			break;
1160 
1161 		case OPT_NO_REENTRANT:
1162 			reentrant = false;
1163 			break;
1164 
1165 		case OPT_SKEL:
1166 			skelname = arg;
1167 			break;
1168 
1169 		case OPT_DEFAULT:
1170 			spprdflt = false;
1171 			break;
1172 
1173 		case OPT_NO_DEFAULT:
1174 			spprdflt = true;
1175 			break;
1176 
1177 		case OPT_STDOUT:
1178 			use_stdout = true;
1179 			break;
1180 
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);
1184 			break;
1185 
1186 		case OPT_TABLES_FILE:
1187 			tablesext = true;
1188 			tablesfilename = arg;
1189 			break;
1190 
1191 		case OPT_TABLES_VERIFY:
1192 			tablesverify = true;
1193 			break;
1194 
1195 		case OPT_TRACE:
1196 			trace = true;
1197 			break;
1198 
1199 		case OPT_VERBOSE:
1200 			printstats = true;
1201 			break;
1202 
1203 		case OPT_VERSION:
1204 			printf (_("%s %s\n"), program_name, flex_version);
1205 			FLEX_EXIT (0);
1206 
1207 		case OPT_WARN:
1208 			nowarn = false;
1209 			break;
1210 
1211 		case OPT_NO_WARN:
1212 			nowarn = true;
1213 			break;
1214 
1215 		case OPT_7BIT:
1216 			csize = 128;
1217 			break;
1218 
1219 		case OPT_8BIT:
1220 			csize = CSIZE;
1221 			break;
1222 
1223 		case OPT_ALIGN:
1224 			long_align = true;
1225 			break;
1226 
1227 		case OPT_NO_ALIGN:
1228 			long_align = false;
1229 			break;
1230 
1231 		case OPT_ALWAYS_INTERACTIVE:
1232 			buf_m4_define (&m4defs_buf, "M4_YY_ALWAYS_INTERACTIVE", 0);
1233 			break;
1234 
1235 		case OPT_NEVER_INTERACTIVE:
1236             buf_m4_define( &m4defs_buf, "M4_YY_NEVER_INTERACTIVE", 0);
1237 			break;
1238 
1239 		case OPT_ARRAY:
1240 			yytext_is_array = true;
1241 			break;
1242 
1243 		case OPT_POINTER:
1244 			yytext_is_array = false;
1245 			break;
1246 
1247 		case OPT_ECS:
1248 			useecs = true;
1249 			break;
1250 
1251 		case OPT_NO_ECS:
1252 			useecs = false;
1253 			break;
1254 
1255 		case OPT_HEADER_FILE:
1256 			headerfilename = arg;
1257 			break;
1258 
1259 		case OPT_META_ECS:
1260 			usemecs = true;
1261 			break;
1262 
1263 		case OPT_NO_META_ECS:
1264 			usemecs = false;
1265 			break;
1266 
1267 		case OPT_PREPROCDEFINE:
1268 			{
1269 				/* arg is "symbol" or "symbol=definition". */
1270 				char   *def;
1271 
1272 				for (def = arg;
1273 				     *def != '\0' && *def != '='; ++def) ;
1274 
1275 				buf_strappend (&userdef_buf, "#define ");
1276 				if (*def == '\0') {
1277 					buf_strappend (&userdef_buf, arg);
1278 					buf_strappend (&userdef_buf,
1279 						       " 1\n");
1280 				}
1281 				else {
1282 					buf_strnappend (&userdef_buf, arg,
1283 							(int) (def - arg));
1284 					buf_strappend (&userdef_buf, " ");
1285 					buf_strappend (&userdef_buf,
1286 						       def + 1);
1287 					buf_strappend (&userdef_buf, "\n");
1288 				}
1289 			}
1290 			break;
1291 
1292 		case OPT_READ:
1293 			use_read = true;
1294 			break;
1295 
1296 		case OPT_STACK:
1297 			//buf_strdefine (&userdef_buf, "YY_STACK_USED", "1");
1298             buf_m4_define( &m4defs_buf, "M4_YY_STACK_USED",0);
1299 			break;
1300 
1301 		case OPT_STDINIT:
1302 			do_stdinit = true;
1303 			break;
1304 
1305 		case OPT_NO_STDINIT:
1306 			do_stdinit = false;
1307 			break;
1308 
1309 		case OPT_YYCLASS:
1310 			yyclass = arg;
1311 			break;
1312 
1313 		case OPT_YYLINENO:
1314 			do_yylineno = true;
1315 			break;
1316 
1317 		case OPT_NO_YYLINENO:
1318 			do_yylineno = false;
1319 			break;
1320 
1321 		case OPT_YYWRAP:
1322 			do_yywrap = true;
1323 			break;
1324 
1325 		case OPT_NO_YYWRAP:
1326 			do_yywrap = false;
1327 			break;
1328 
1329 		case OPT_YYMORE:
1330 			yymore_really_used = true;
1331 			break;
1332 
1333 		case OPT_NO_YYMORE:
1334 			yymore_really_used = false;
1335 			break;
1336 
1337 		case OPT_REJECT:
1338 			reject_really_used = true;
1339 			break;
1340 
1341 		case OPT_NO_REJECT:
1342 			reject_really_used = false;
1343 			break;
1344 
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);
1348 			break;
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);
1352 			break;
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);
1356 			break;
1357 		case OPT_NO_UNPUT:
1358 			//buf_strdefine (&userdef_buf, "YY_NO_UNPUT", "1");
1359             buf_m4_define( &m4defs_buf, "M4_YY_NO_UNPUT",0);
1360 			break;
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);
1364 			break;
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);
1368 			break;
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);
1372 			break;
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);
1376 			break;
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);
1380 			break;
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);
1384 			break;
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);
1388 			break;
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);
1392 			break;
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);
1396 			break;
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);
1400 			break;
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);
1404 			break;
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);
1408 			break;
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);
1412 			break;
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);
1416 			break;
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);
1420 			break;
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);
1424 			break;
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);
1428 			break;
1429 		case OPT_HEX:
1430 			trace_hex = 1;
1431                         break;
1432                 case OPT_NO_SECT3_ESCAPE:
1433                         no_section3_escape = true;
1434                         break;
1435 		}		/* switch */
1436 	}			/* while scanopt() */
1437 
1438 	scanopt_destroy (sopt);
1439 
1440 	num_input_files = argc - optind;
1441 	input_files = argv + optind;
1442 	set_input_file (num_input_files > 0 ? input_files[0] : NULL);
1443 
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 =
1448 		0;
1449 	numuniq = numdup = hshsave = eofseen = datapos = dataline = 0;
1450 	num_backing_up = onesp = numprots = 0;
1451 	variable_trailing_context_rules = bol_needed = false;
1452 
1453 	linenum = sectnum = 1;
1454 	firstprot = NIL;
1455 
1456 	/* Used in mkprot() so that the first proto goes in slot 1
1457 	 * of the proto queue.
1458 	 */
1459 	lastprot = 1;
1460 
1461 	set_up_initial_allocations ();
1462 }
1463 
1464 
1465 /* readin - read in the rules section of the input file(s) */
1466 
1467 void readin (void)
1468 {
1469 	static char yy_stdinit[] = "FILE *yyin = stdin, *yyout = stdout;";
1470 	static char yy_nostdinit[] =
1471 		"FILE *yyin = NULL, *yyout = NULL;";
1472 
1473 	line_directive_out(NULL, 1);
1474 
1475 	if (yyparse ()) {
1476 		pinpoint_message (_("fatal parse error"));
1477 		flexend (1);
1478 	}
1479 
1480 	if (syntaxerror)
1481 		flexend (1);
1482 
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.
1488 	 *
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}'
1491 	 */
1492 	if (posix_compat) {
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?
1496 		 */
1497 		/* Update: Estes says no, since other flex features don't violate posix. */
1498 	}
1499 
1500 	if (getenv ("POSIXLY_CORRECT")) {
1501 		posix_compat = true;
1502 	}
1503 
1504 	if (backing_up_report) {
1505 		backing_up_file = fopen (backing_name, "w");
1506 		if (backing_up_file == NULL)
1507 			lerr (_
1508 				("could not create backing-up info file %s"),
1509 				backing_name);
1510 	}
1511 
1512 	else
1513 		backing_up_file = NULL;
1514 
1515 	if (yymore_really_used == true)
1516 		yymore_used = true;
1517 	else if (yymore_really_used == false)
1518 		yymore_used = false;
1519 
1520 	if (reject_really_used == true)
1521 		reject = true;
1522 	else if (reject_really_used == false)
1523 		reject = false;
1524 
1525 	if (performance_report > 0) {
1526 		if (lex_compat) {
1527 			fprintf (stderr,
1528 				 _
1529 				 ("-l AT&T lex compatibility option entails a large performance penalty\n"));
1530 			fprintf (stderr,
1531 				 _
1532 				 (" and may be the actual source of other reported performance penalties\n"));
1533 		}
1534 
1535 		else if (do_yylineno) {
1536 			fprintf (stderr,
1537 				 _
1538 				 ("%%option yylineno entails a performance penalty ONLY on rules that can match newline characters\n"));
1539 		}
1540 
1541 		if (performance_report > 1) {
1542 			if (interactive)
1543 				fprintf (stderr,
1544 					 _
1545 					 ("-I (interactive) entails a minor performance penalty\n"));
1546 
1547 			if (yymore_used)
1548 				fprintf (stderr,
1549 					 _
1550 					 ("yymore() entails a minor performance penalty\n"));
1551 		}
1552 
1553 		if (reject)
1554 			fprintf (stderr,
1555 				 _
1556 				 ("REJECT entails a large performance penalty\n"));
1557 
1558 		if (variable_trailing_context_rules)
1559 			fprintf (stderr,
1560 				 _
1561 				 ("Variable trailing context rules entail a large performance penalty\n"));
1562 	}
1563 
1564 	if (reject)
1565 		real_reject = true;
1566 
1567 	if (variable_trailing_context_rules)
1568 		reject = true;
1569 
1570 	if ((fulltbl || fullspd) && reject) {
1571 		if (real_reject)
1572 			flexerror (_
1573 				   ("REJECT cannot be used with -f or -F"));
1574 		else if (do_yylineno)
1575 			flexerror (_
1576 				   ("%option yylineno cannot be used with REJECT"));
1577 		else
1578 			flexerror (_
1579 				   ("variable trailing context rules cannot be used with -f or -F"));
1580 	}
1581 
1582 	if (reject){
1583         out_m4_define( "M4_YY_USES_REJECT", NULL);
1584 		//outn ("\n#define YY_USES_REJECT");
1585     }
1586 
1587 	if (!do_yywrap) {
1588 		if (!C_plus_plus) {
1589 			 if (reentrant)
1590 				out_str ("\n#define %swrap(yyscanner) (/*CONSTCOND*/1)\n", prefix);
1591 			 else
1592 				out_str ("\n#define %swrap() (/*CONSTCOND*/1)\n", prefix);
1593 		}
1594 		outn ("#define YY_SKIP_YYWRAP");
1595 	}
1596 
1597 	if (ddebug)
1598 		outn ("\n#define FLEX_DEBUG");
1599 
1600 	OUT_BEGIN_CODE ();
1601 	outn ("typedef flex_uint8_t YY_CHAR;");
1602 	OUT_END_CODE ();
1603 
1604 	if (C_plus_plus) {
1605 		outn ("#define yytext_ptr yytext");
1606 
1607 		if (interactive)
1608 			outn ("#define YY_INTERACTIVE");
1609 	}
1610 
1611 	else {
1612 		OUT_BEGIN_CODE ();
1613 		/* In reentrant scanner, stdinit is handled in flex.skl. */
1614 		if (do_stdinit) {
1615 			if (reentrant){
1616                 outn ("#ifdef VMS");
1617                 outn ("#ifdef __VMS_POSIX");
1618                 outn ("#define YY_STDINIT");
1619                 outn ("#endif");
1620                 outn ("#else");
1621                 outn ("#define YY_STDINIT");
1622                 outn ("#endif");
1623             }
1624 
1625 			outn ("#ifdef VMS");
1626 			outn ("#ifndef __VMS_POSIX");
1627 			outn (yy_nostdinit);
1628 			outn ("#else");
1629 			outn (yy_stdinit);
1630 			outn ("#endif");
1631 			outn ("#else");
1632 			outn (yy_stdinit);
1633 			outn ("#endif");
1634 		}
1635 
1636 		else {
1637 			if(!reentrant)
1638                 outn (yy_nostdinit);
1639 		}
1640 		OUT_END_CODE ();
1641 	}
1642 
1643 	OUT_BEGIN_CODE ();
1644 	if (fullspd)
1645 		outn ("typedef const struct yy_trans_info *yy_state_type;");
1646 	else if (!C_plus_plus)
1647 		outn ("typedef int yy_state_type;");
1648 	OUT_END_CODE ();
1649 
1650 	if (lex_compat)
1651 		outn ("#define YY_FLEX_LEX_COMPAT");
1652 
1653 	if (!C_plus_plus && !reentrant) {
1654 		outn ("extern int yylineno;");
1655 		OUT_BEGIN_CODE ();
1656 		outn ("int yylineno = 1;");
1657 		OUT_END_CODE ();
1658 	}
1659 
1660 	if (C_plus_plus) {
1661 		outn ("\n#include <FlexLexer.h>");
1662 
1663  		if (!do_yywrap) {
1664 			outn("\nint yyFlexLexer::yywrap() { return 1; }");
1665 		}
1666 
1667 		if (yyclass) {
1668 			outn ("int yyFlexLexer::yylex()");
1669 			outn ("\t{");
1670 			outn ("\tLexerError( \"yyFlexLexer::yylex invoked but %option yyclass used\" );");
1671 			outn ("\treturn 0;");
1672 			outn ("\t}");
1673 
1674 			out_str ("\n#define YY_DECL int %s::yylex()\n",
1675 				 yyclass);
1676 		}
1677 	}
1678 
1679 	else {
1680 
1681 		/* Watch out: yytext_ptr is a variable when yytext is an array,
1682 		 * but it's a macro when yytext is a pointer.
1683 		 */
1684 		if (yytext_is_array) {
1685 			if (!reentrant)
1686 				outn ("extern char yytext[];\n");
1687 		}
1688 		else {
1689 			if (reentrant) {
1690 				outn ("#define yytext_ptr yytext_r");
1691 			}
1692 			else {
1693 				outn ("extern char *yytext;");
1694 
1695 				outn("#ifdef yytext_ptr");
1696 				outn("#undef yytext_ptr");
1697 				outn("#endif");
1698 				outn ("#define yytext_ptr yytext");
1699 			}
1700 		}
1701 
1702 		if (yyclass)
1703 			flexerror (_
1704 				   ("%option yyclass only meaningful for C++ scanners"));
1705 	}
1706 
1707 	if (useecs)
1708 		numecs = cre8ecs (nextecm, ecgroup, csize);
1709 	else
1710 		numecs = csize;
1711 
1712 	/* Now map the equivalence class for NUL to its expected place. */
1713 	ecgroup[0] = ecgroup[csize];
1714 	NUL_ec = ABS (ecgroup[0]);
1715 
1716 	if (useecs)
1717 		ccl2ecl ();
1718 }
1719 
1720 
1721 /* set_up_initial_allocations - allocate memory for internal tables */
1722 
1723 void set_up_initial_allocations (void)
1724 {
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);
1736 
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);
1742 
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);
1749 
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);
1755 
1756 	current_max_ccl_tbl_size = INITIAL_MAX_CCL_TBL_SIZE;
1757 	ccltbl = allocate_Character_array (current_max_ccl_tbl_size);
1758 
1759 	current_max_dfa_size = INITIAL_MAX_DFA_SIZE;
1760 
1761 	current_max_xpairs = INITIAL_MAX_XPAIRS;
1762 	nxt = allocate_integer_array (current_max_xpairs);
1763 	chk = allocate_integer_array (current_max_xpairs);
1764 
1765 	current_max_template_xpairs = INITIAL_MAX_TEMPLATE_XPAIRS;
1766 	tnxt = allocate_integer_array (current_max_template_xpairs);
1767 
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);
1776 
1777 	nultrans = NULL;
1778 }
1779 
1780 
1781 void usage (void)
1782 {
1783 	FILE   *f = stdout;
1784 
1785 	if (!did_outfilename) {
1786 		snprintf (outfile_path, sizeof(outfile_path), outfile_template,
1787 			 prefix, C_plus_plus ? "cc" : "c");
1788 		outfilename = outfile_path;
1789 	}
1790 
1791 	fprintf (f, _("Usage: %s [OPTIONS] [FILE]...\n"), program_name);
1792 	fprintf (f,
1793 		 _
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"
1805 		  "\n" "Debugging:\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"
1814 		  "\n" "Files:\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"
1844 		  "  -?\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);
1848 
1849 }
1850