1228072Sbapt/* flex - tool to generate fast lexical analyzers */
2228072Sbapt
3228072Sbapt/*  Copyright (c) 1990 The Regents of the University of California. */
4228072Sbapt/*  All rights reserved. */
5228072Sbapt
6228072Sbapt/*  This code is derived from software contributed to Berkeley by */
7228072Sbapt/*  Vern Paxson. */
8228072Sbapt
9228072Sbapt/*  The United States Government has rights in this work pursuant */
10228072Sbapt/*  to contract no. DE-AC03-76SF00098 between the United States */
11228072Sbapt/*  Department of Energy and the University of California. */
12228072Sbapt
13228072Sbapt/*  This file is part of flex. */
14228072Sbapt
15228072Sbapt/*  Redistribution and use in source and binary forms, with or without */
16228072Sbapt/*  modification, are permitted provided that the following conditions */
17228072Sbapt/*  are met: */
18228072Sbapt
19228072Sbapt/*  1. Redistributions of source code must retain the above copyright */
20228072Sbapt/*     notice, this list of conditions and the following disclaimer. */
21228072Sbapt/*  2. Redistributions in binary form must reproduce the above copyright */
22228072Sbapt/*     notice, this list of conditions and the following disclaimer in the */
23228072Sbapt/*     documentation and/or other materials provided with the distribution. */
24228072Sbapt
25228072Sbapt/*  Neither the name of the University nor the names of its contributors */
26228072Sbapt/*  may be used to endorse or promote products derived from this software */
27228072Sbapt/*  without specific prior written permission. */
28228072Sbapt
29228072Sbapt/*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
30228072Sbapt/*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
31228072Sbapt/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
32228072Sbapt/*  PURPOSE. */
33228072Sbapt
34228072Sbapt
35228072Sbapt#include "flexdef.h"
36228072Sbapt#include "version.h"
37228072Sbapt#include "options.h"
38228072Sbapt#include "tables.h"
39228072Sbapt
40228072Sbaptstatic char flex_version[] = FLEX_VERSION;
41228072Sbapt
42228072Sbapt/* declare functions that have forward references */
43228072Sbapt
44228072Sbaptvoid flexinit PROTO ((int, char **));
45228072Sbaptvoid readin PROTO ((void));
46228072Sbaptvoid set_up_initial_allocations PROTO ((void));
47228072Sbaptstatic char *basename2 PROTO ((char *path, int should_strip_ext));
48228072Sbapt
49228072Sbapt
50228072Sbapt/* these globals are all defined and commented in flexdef.h */
51228072Sbaptint     printstats, syntaxerror, eofseen, ddebug, trace, nowarn, spprdflt;
52228072Sbaptint     interactive, lex_compat, posix_compat, do_yylineno,
53228072Sbapt	useecs, fulltbl, usemecs;
54228072Sbaptint     fullspd, gen_line_dirs, performance_report, backing_up_report;
55228072Sbaptint     C_plus_plus, long_align, use_read, yytext_is_array, do_yywrap,
56228072Sbapt	csize;
57228072Sbaptint     reentrant, bison_bridge_lval, bison_bridge_lloc;
58228072Sbaptint     yymore_used, reject, real_reject, continued_action, in_rule;
59228072Sbaptint     yymore_really_used, reject_really_used;
60250125Sjkimint     datapos, dataline, linenum;
61228072SbaptFILE   *skelfile = NULL;
62228072Sbaptint     skel_ind = 0;
63228072Sbaptchar   *action_array;
64228072Sbaptint     action_size, defs1_offset, prolog_offset, action_offset,
65228072Sbapt	action_index;
66228072Sbaptchar   *infilename = NULL, *outfilename = NULL, *headerfilename = NULL;
67228072Sbaptint     did_outfilename;
68228072Sbaptchar   *prefix, *yyclass, *extra_type = NULL;
69228072Sbaptint     do_stdinit, use_stdout;
70228072Sbaptint     onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE];
71228072Sbaptint     onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp;
72228072Sbaptint     maximum_mns, current_mns, current_max_rules;
73228072Sbaptint     num_rules, num_eof_rules, default_rule, lastnfa;
74228072Sbaptint    *firstst, *lastst, *finalst, *transchar, *trans1, *trans2;
75228072Sbaptint    *accptnum, *assoc_rule, *state_type;
76228072Sbaptint    *rule_type, *rule_linenum, *rule_useful;
77228072Sbaptint     current_state_type;
78228072Sbaptint     variable_trailing_context_rules;
79228072Sbaptint     numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP];
80228072Sbaptint     protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE];
81228072Sbaptint     numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs,
82228072Sbapt	tecfwd[CSIZE + 1];
83228072Sbaptint     tecbck[CSIZE + 1];
84228072Sbaptint     lastsc, *scset, *scbol, *scxclu, *sceof;
85228072Sbaptint     current_max_scs;
86228072Sbaptchar  **scname;
87228072Sbaptint     current_max_dfa_size, current_max_xpairs;
88228072Sbaptint     current_max_template_xpairs, current_max_dfas;
89228072Sbaptint     lastdfa, *nxt, *chk, *tnxt;
90228072Sbaptint    *base, *def, *nultrans, NUL_ec, tblend, firstfree, **dss, *dfasiz;
91228072Sbaptunion dfaacc_union *dfaacc;
92228072Sbaptint    *accsiz, *dhash, numas;
93228072Sbaptint     numsnpairs, jambase, jamstate;
94228072Sbaptint     lastccl, *cclmap, *ccllen, *cclng, cclreuse;
95228072Sbaptint     current_maxccls, current_max_ccl_tbl_size;
96228072SbaptChar   *ccltbl;
97228072Sbaptchar    nmstr[MAXLINE];
98228072Sbaptint     sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs;
99228072Sbaptint     tmpuses, totnst, peakpairs, numuniq, numdup, hshsave;
100228072Sbaptint     num_backing_up, bol_needed;
101228072SbaptFILE   *backing_up_file;
102228072Sbaptint     end_of_buffer_state;
103228072Sbaptchar  **input_files;
104228072Sbaptint     num_input_files;
105228072Sbaptjmp_buf flex_main_jmp_buf;
106228072Sbaptbool   *rule_has_nl, *ccl_has_nl;
107228072Sbaptint     nlch = '\n';
108228072Sbaptbool    ansi_func_defs, ansi_func_protos;
109228072Sbapt
110228072Sbaptbool    tablesext, tablesverify, gentables;
111228072Sbaptchar   *tablesfilename=0,*tablesname=0;
112228072Sbaptstruct yytbl_writer tableswr;
113228072Sbapt
114228072Sbapt/* Make sure program_name is initialized so we don't crash if writing
115228072Sbapt * out an error message before getting the program name from argv[0].
116228072Sbapt */
117228072Sbaptchar   *program_name = "flex";
118228072Sbapt
119228072Sbapt#ifndef SHORT_FILE_NAMES
120228072Sbaptstatic char *outfile_template = "lex.%s.%s";
121228072Sbaptstatic char *backing_name = "lex.backup";
122228072Sbaptstatic char *tablesfile_template = "lex.%s.tables";
123228072Sbapt#else
124228072Sbaptstatic char *outfile_template = "lex%s.%s";
125228072Sbaptstatic char *backing_name = "lex.bck";
126228072Sbaptstatic char *tablesfile_template = "lex%s.tbl";
127228072Sbapt#endif
128228072Sbapt
129228072Sbapt#ifdef MS_DOS
130228072Sbaptextern unsigned _stklen = 16384;
131228072Sbapt#endif
132228072Sbapt
133228072Sbapt/* From scan.l */
134228072Sbaptextern FILE* yyout;
135228072Sbapt
136228072Sbaptstatic char outfile_path[MAXLINE];
137228072Sbaptstatic int outfile_created = 0;
138228072Sbaptstatic char *skelname = NULL;
139228072Sbaptstatic int _stdout_closed = 0; /* flag to prevent double-fclose() on stdout. */
140228072Sbaptconst char *escaped_qstart = "[[]]M4_YY_NOOP[M4_YY_NOOP[M4_YY_NOOP[[]]";
141228072Sbaptconst char *escaped_qend   = "[[]]M4_YY_NOOP]M4_YY_NOOP]M4_YY_NOOP[[]]";
142228072Sbapt
143228072Sbapt/* For debugging. The max number of filters to apply to skeleton. */
144228072Sbaptstatic int preproc_level = 1000;
145228072Sbapt
146228072Sbaptint flex_main PROTO ((int argc, char *argv[]));
147228072Sbaptint main PROTO ((int argc, char *argv[]));
148228072Sbapt
149228072Sbaptint flex_main (argc, argv)
150228072Sbapt     int argc;
151228072Sbapt     char   *argv[];
152228072Sbapt{
153228072Sbapt	int     i, exit_status, child_status;
154228072Sbapt
155228072Sbapt	/* Set a longjmp target. Yes, I know it's a hack, but it gets worse: The
156228072Sbapt	 * return value of setjmp, if non-zero, is the desired exit code PLUS ONE.
157228072Sbapt	 * For example, if you want 'main' to return with code '2', then call
158228072Sbapt	 * longjmp() with an argument of 3. This is because it is invalid to
159228072Sbapt	 * specify a value of 0 to longjmp. FLEX_EXIT(n) should be used instead of
160228072Sbapt	 * exit(n);
161228072Sbapt	 */
162228072Sbapt	exit_status = setjmp (flex_main_jmp_buf);
163228072Sbapt	if (exit_status){
164228072Sbapt        if (stdout && !_stdout_closed && !ferror(stdout)){
165228072Sbapt            fflush(stdout);
166228072Sbapt            fclose(stdout);
167228072Sbapt        }
168228072Sbapt        while (wait(&child_status) > 0){
169228072Sbapt            if (!WIFEXITED (child_status)
170228072Sbapt                || WEXITSTATUS (child_status) != 0){
171228072Sbapt                /* report an error of a child
172228072Sbapt                 */
173228072Sbapt                if( exit_status <= 1 )
174228072Sbapt                    exit_status = 2;
175228072Sbapt
176228072Sbapt            }
177228072Sbapt        }
178228072Sbapt        return exit_status - 1;
179228072Sbapt    }
180228072Sbapt
181228072Sbapt	flexinit (argc, argv);
182228072Sbapt
183228072Sbapt	readin ();
184228072Sbapt
185250128Sjkim	skelout ();
186250128Sjkim	/* %% [1.5] DFA */
187228072Sbapt	ntod ();
188228072Sbapt
189228072Sbapt	for (i = 1; i <= num_rules; ++i)
190228072Sbapt		if (!rule_useful[i] && i != default_rule)
191228072Sbapt			line_warning (_("rule cannot be matched"),
192228072Sbapt				      rule_linenum[i]);
193228072Sbapt
194228072Sbapt	if (spprdflt && !reject && rule_useful[default_rule])
195228072Sbapt		line_warning (_
196228072Sbapt			      ("-s option given but default rule can be matched"),
197228072Sbapt			      rule_linenum[default_rule]);
198228072Sbapt
199228072Sbapt	/* Generate the C state transition tables from the DFA. */
200228072Sbapt	make_tables ();
201228072Sbapt
202228072Sbapt	/* Note, flexend does not return.  It exits with its argument
203228072Sbapt	 * as status.
204228072Sbapt	 */
205228072Sbapt	flexend (0);
206228072Sbapt
207228072Sbapt	return 0;		/* keep compilers/lint happy */
208228072Sbapt}
209228072Sbapt
210228072Sbapt/* Wrapper around flex_main, so flex_main can be built as a library. */
211228072Sbaptint main (argc, argv)
212228072Sbapt     int argc;
213228072Sbapt     char   *argv[];
214228072Sbapt{
215228072Sbapt#if ENABLE_NLS
216228072Sbapt#if HAVE_LOCALE_H
217228072Sbapt	setlocale (LC_MESSAGES, "");
218228072Sbapt        setlocale (LC_CTYPE, "");
219228072Sbapt	textdomain (PACKAGE);
220228072Sbapt	bindtextdomain (PACKAGE, LOCALEDIR);
221228072Sbapt#endif
222228072Sbapt#endif
223228072Sbapt
224228072Sbapt	return flex_main (argc, argv);
225228072Sbapt}
226228072Sbapt
227228072Sbapt/* check_options - check user-specified options */
228228072Sbapt
229228072Sbaptvoid check_options ()
230228072Sbapt{
231228072Sbapt	int     i;
232228072Sbapt    const char * m4 = NULL;
233228072Sbapt
234228072Sbapt	if (lex_compat) {
235228072Sbapt		if (C_plus_plus)
236228072Sbapt			flexerror (_("Can't use -+ with -l option"));
237228072Sbapt
238228072Sbapt		if (fulltbl || fullspd)
239228072Sbapt			flexerror (_("Can't use -f or -F with -l option"));
240228072Sbapt
241228072Sbapt		if (reentrant || bison_bridge_lval)
242228072Sbapt			flexerror (_
243228072Sbapt				   ("Can't use --reentrant or --bison-bridge with -l option"));
244228072Sbapt
245228072Sbapt		yytext_is_array = true;
246228072Sbapt		do_yylineno = true;
247228072Sbapt		use_read = false;
248228072Sbapt	}
249228072Sbapt
250228072Sbapt
251228072Sbapt#if 0
252228072Sbapt	/* This makes no sense whatsoever. I'm removing it. */
253228072Sbapt	if (do_yylineno)
254228072Sbapt		/* This should really be "maintain_backup_tables = true" */
255228072Sbapt		reject_really_used = true;
256228072Sbapt#endif
257228072Sbapt
258228072Sbapt	if (csize == unspecified) {
259228072Sbapt		if ((fulltbl || fullspd) && !useecs)
260228072Sbapt			csize = DEFAULT_CSIZE;
261228072Sbapt		else
262228072Sbapt			csize = CSIZE;
263228072Sbapt	}
264228072Sbapt
265228072Sbapt	if (interactive == unspecified) {
266228072Sbapt		if (fulltbl || fullspd)
267228072Sbapt			interactive = false;
268228072Sbapt		else
269228072Sbapt			interactive = true;
270228072Sbapt	}
271228072Sbapt
272228072Sbapt	if (fulltbl || fullspd) {
273228072Sbapt		if (usemecs)
274228072Sbapt			flexerror (_
275228072Sbapt				   ("-Cf/-CF and -Cm don't make sense together"));
276228072Sbapt
277228072Sbapt		if (interactive)
278228072Sbapt			flexerror (_("-Cf/-CF and -I are incompatible"));
279228072Sbapt
280228072Sbapt		if (lex_compat)
281228072Sbapt			flexerror (_
282228072Sbapt				   ("-Cf/-CF are incompatible with lex-compatibility mode"));
283228072Sbapt
284228072Sbapt
285228072Sbapt		if (fulltbl && fullspd)
286228072Sbapt			flexerror (_
287228072Sbapt				   ("-Cf and -CF are mutually exclusive"));
288228072Sbapt	}
289228072Sbapt
290228072Sbapt	if (C_plus_plus && fullspd)
291228072Sbapt		flexerror (_("Can't use -+ with -CF option"));
292228072Sbapt
293228072Sbapt	if (C_plus_plus && yytext_is_array) {
294228072Sbapt		warn (_("%array incompatible with -+ option"));
295228072Sbapt		yytext_is_array = false;
296228072Sbapt	}
297228072Sbapt
298228072Sbapt	if (C_plus_plus && (reentrant))
299228072Sbapt		flexerror (_("Options -+ and --reentrant are mutually exclusive."));
300228072Sbapt
301228072Sbapt	if (C_plus_plus && bison_bridge_lval)
302228072Sbapt		flexerror (_("bison bridge not supported for the C++ scanner."));
303228072Sbapt
304228072Sbapt
305228072Sbapt	if (useecs) {		/* Set up doubly-linked equivalence classes. */
306228072Sbapt
307228072Sbapt		/* We loop all the way up to csize, since ecgroup[csize] is
308228072Sbapt		 * the position used for NUL characters.
309228072Sbapt		 */
310228072Sbapt		ecgroup[1] = NIL;
311228072Sbapt
312228072Sbapt		for (i = 2; i <= csize; ++i) {
313228072Sbapt			ecgroup[i] = i - 1;
314228072Sbapt			nextecm[i - 1] = i;
315228072Sbapt		}
316228072Sbapt
317228072Sbapt		nextecm[csize] = NIL;
318228072Sbapt	}
319228072Sbapt
320228072Sbapt	else {
321228072Sbapt		/* Put everything in its own equivalence class. */
322228072Sbapt		for (i = 1; i <= csize; ++i) {
323228072Sbapt			ecgroup[i] = i;
324228072Sbapt			nextecm[i] = BAD_SUBSCRIPT;	/* to catch errors */
325228072Sbapt		}
326228072Sbapt	}
327228072Sbapt
328228072Sbapt    if (!ansi_func_defs)
329228072Sbapt        buf_m4_define( &m4defs_buf, "M4_YY_NO_ANSI_FUNC_DEFS", NULL);
330228072Sbapt
331228072Sbapt    if (!ansi_func_protos)
332228072Sbapt        buf_m4_define( &m4defs_buf, "M4_YY_NO_ANSI_FUNC_PROTOS", NULL);
333228072Sbapt
334228072Sbapt    if (extra_type)
335228072Sbapt        buf_m4_define( &m4defs_buf, "M4_EXTRA_TYPE_DEFS", extra_type);
336228072Sbapt
337228072Sbapt	if (!use_stdout) {
338228072Sbapt		FILE   *prev_stdout;
339228072Sbapt
340228072Sbapt		if (!did_outfilename) {
341228072Sbapt			char   *suffix;
342228072Sbapt
343228072Sbapt			if (C_plus_plus)
344228072Sbapt				suffix = "cc";
345228072Sbapt			else
346228072Sbapt				suffix = "c";
347228072Sbapt
348228072Sbapt			snprintf (outfile_path, sizeof(outfile_path), outfile_template,
349228072Sbapt				 prefix, suffix);
350228072Sbapt
351228072Sbapt			outfilename = outfile_path;
352228072Sbapt		}
353228072Sbapt
354228072Sbapt		prev_stdout = freopen (outfilename, "w+", stdout);
355228072Sbapt
356228072Sbapt		if (prev_stdout == NULL)
357228072Sbapt			lerrsf (_("could not create %s"), outfilename);
358228072Sbapt
359228072Sbapt		outfile_created = 1;
360228072Sbapt	}
361228072Sbapt
362228072Sbapt
363228072Sbapt    /* Setup the filter chain. */
364228072Sbapt    output_chain = filter_create_int(NULL, filter_tee_header, headerfilename);
365228072Sbapt    if ( !(m4 = getenv("M4")))
366228072Sbapt        m4 = M4;
367250879Sjkim    filter_create_ext(output_chain, m4, "-gP", 0);
368228072Sbapt    filter_create_int(output_chain, filter_fix_linedirs, NULL);
369228072Sbapt
370228072Sbapt    /* For debugging, only run the requested number of filters. */
371228072Sbapt    if (preproc_level > 0) {
372228072Sbapt        filter_truncate(output_chain, preproc_level);
373228072Sbapt        filter_apply_chain(output_chain);
374228072Sbapt    }
375228072Sbapt    yyout = stdout;
376228072Sbapt
377228072Sbapt
378228072Sbapt	/* always generate the tablesverify flag. */
379228072Sbapt	buf_m4_define (&m4defs_buf, "M4_YY_TABLES_VERIFY", tablesverify ? "1" : "0");
380228072Sbapt	if (tablesext)
381228072Sbapt		gentables = false;
382228072Sbapt
383228072Sbapt	if (tablesverify)
384228072Sbapt		/* force generation of C tables. */
385228072Sbapt		gentables = true;
386228072Sbapt
387228072Sbapt
388228072Sbapt	if (tablesext) {
389228072Sbapt		FILE   *tablesout;
390228072Sbapt		struct yytbl_hdr hdr;
391228072Sbapt		char   *pname = 0;
392228072Sbapt		int     nbytes = 0;
393228072Sbapt
394228072Sbapt		buf_m4_define (&m4defs_buf, "M4_YY_TABLES_EXTERNAL", NULL);
395228072Sbapt
396228072Sbapt		if (!tablesfilename) {
397228072Sbapt			nbytes = strlen (prefix) + strlen (tablesfile_template) + 2;
398228072Sbapt			tablesfilename = pname = (char *) calloc (nbytes, 1);
399228072Sbapt			snprintf (pname, nbytes, tablesfile_template, prefix);
400228072Sbapt		}
401228072Sbapt
402228072Sbapt		if ((tablesout = fopen (tablesfilename, "w")) == NULL)
403228072Sbapt			lerrsf (_("could not create %s"), tablesfilename);
404228072Sbapt		if (pname)
405228072Sbapt			free (pname);
406228072Sbapt		tablesfilename = 0;
407228072Sbapt
408228072Sbapt		yytbl_writer_init (&tableswr, tablesout);
409228072Sbapt
410228072Sbapt		nbytes = strlen (prefix) + strlen ("tables") + 2;
411228072Sbapt		tablesname = (char *) calloc (nbytes, 1);
412228072Sbapt		snprintf (tablesname, nbytes, "%stables", prefix);
413228072Sbapt		yytbl_hdr_init (&hdr, flex_version, tablesname);
414228072Sbapt
415228072Sbapt		if (yytbl_hdr_fwrite (&tableswr, &hdr) <= 0)
416228072Sbapt			flexerror (_("could not write tables header"));
417228072Sbapt	}
418228072Sbapt
419228072Sbapt	if (skelname && (skelfile = fopen (skelname, "r")) == NULL)
420228072Sbapt		lerrsf (_("can't open skeleton file %s"), skelname);
421228072Sbapt
422228072Sbapt	if (reentrant) {
423228072Sbapt        buf_m4_define (&m4defs_buf, "M4_YY_REENTRANT", NULL);
424228072Sbapt		if (yytext_is_array)
425228072Sbapt			buf_m4_define (&m4defs_buf, "M4_YY_TEXT_IS_ARRAY", NULL);
426228072Sbapt	}
427228072Sbapt
428228072Sbapt	if ( bison_bridge_lval)
429228072Sbapt		buf_m4_define (&m4defs_buf, "M4_YY_BISON_LVAL", NULL);
430228072Sbapt
431228072Sbapt	if ( bison_bridge_lloc)
432228072Sbapt        buf_m4_define (&m4defs_buf, "<M4_YY_BISON_LLOC>", NULL);
433228072Sbapt
434228072Sbapt    buf_m4_define(&m4defs_buf, "M4_YY_PREFIX", prefix);
435228072Sbapt
436228072Sbapt	if (did_outfilename)
437228072Sbapt		line_directive_out (stdout, 0);
438228072Sbapt
439228072Sbapt	if (do_yylineno)
440228072Sbapt		buf_m4_define (&m4defs_buf, "M4_YY_USE_LINENO", NULL);
441228072Sbapt
442228072Sbapt	/* Create the alignment type. */
443228072Sbapt	buf_strdefine (&userdef_buf, "YY_INT_ALIGNED",
444228072Sbapt		       long_align ? "long int" : "short int");
445228072Sbapt
446228072Sbapt    /* Define the start condition macros. */
447228072Sbapt    {
448228072Sbapt        struct Buf tmpbuf;
449228072Sbapt        buf_init(&tmpbuf, sizeof(char));
450228072Sbapt        for (i = 1; i <= lastsc; i++) {
451228072Sbapt             char *str, *fmt = "#define %s %d\n";
452228072Sbapt             size_t strsz;
453228072Sbapt
454250876Sjkim             str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(scname[i]) + NUMCHARLINES + 2);
455250125Sjkim             if (!str)
456250125Sjkim               flexfatal(_("allocation of macro definition failed"));
457228072Sbapt             snprintf(str, strsz, fmt,      scname[i], i - 1);
458228072Sbapt             buf_strappend(&tmpbuf, str);
459228072Sbapt             free(str);
460228072Sbapt        }
461228072Sbapt        buf_m4_define(&m4defs_buf, "M4_YY_SC_DEFS", tmpbuf.elts);
462228072Sbapt        buf_destroy(&tmpbuf);
463228072Sbapt    }
464228072Sbapt
465228072Sbapt    /* This is where we begin writing to the file. */
466228072Sbapt
467228072Sbapt    /* Dump the %top code. */
468228072Sbapt    if( top_buf.elts)
469228072Sbapt        outn((char*) top_buf.elts);
470228072Sbapt
471228072Sbapt    /* Dump the m4 definitions. */
472228072Sbapt    buf_print_strings(&m4defs_buf, stdout);
473228072Sbapt    m4defs_buf.nelts = 0; /* memory leak here. */
474228072Sbapt
475228072Sbapt    /* Place a bogus line directive, it will be fixed in the filter. */
476228072Sbapt    outn("#line 0 \"M4_YY_OUTFILE_NAME\"\n");
477228072Sbapt
478228072Sbapt	/* Dump the user defined preproc directives. */
479228072Sbapt	if (userdef_buf.elts)
480228072Sbapt		outn ((char *) (userdef_buf.elts));
481228072Sbapt
482228072Sbapt	skelout ();
483228072Sbapt	/* %% [1.0] */
484228072Sbapt}
485228072Sbapt
486228072Sbapt/* flexend - terminate flex
487228072Sbapt *
488228072Sbapt * note
489228072Sbapt *    This routine does not return.
490228072Sbapt */
491228072Sbapt
492228072Sbaptvoid flexend (exit_status)
493228072Sbapt     int exit_status;
494228072Sbapt
495228072Sbapt{
496228072Sbapt	static int called_before = -1;	/* prevent infinite recursion. */
497228072Sbapt	int     tblsiz;
498228072Sbapt
499228072Sbapt	if (++called_before)
500228072Sbapt		FLEX_EXIT (exit_status);
501228072Sbapt
502228072Sbapt	if (skelfile != NULL) {
503228072Sbapt		if (ferror (skelfile))
504228072Sbapt			lerrsf (_("input error reading skeleton file %s"),
505228072Sbapt				skelname);
506228072Sbapt
507228072Sbapt		else if (fclose (skelfile))
508228072Sbapt			lerrsf (_("error closing skeleton file %s"),
509228072Sbapt				skelname);
510228072Sbapt	}
511228072Sbapt
512228072Sbapt#if 0
513228072Sbapt		fprintf (header_out,
514228072Sbapt			 "#ifdef YY_HEADER_EXPORT_START_CONDITIONS\n");
515228072Sbapt		fprintf (header_out,
516228072Sbapt			 "/* Beware! Start conditions are not prefixed. */\n");
517228072Sbapt
518228072Sbapt		/* Special case for "INITIAL" */
519228072Sbapt		fprintf (header_out,
520228072Sbapt			 "#undef INITIAL\n#define INITIAL 0\n");
521228072Sbapt		for (i = 2; i <= lastsc; i++)
522228072Sbapt			fprintf (header_out, "#define %s %d\n", scname[i], i - 1);
523228072Sbapt		fprintf (header_out,
524228072Sbapt			 "#endif /* YY_HEADER_EXPORT_START_CONDITIONS */\n\n");
525228072Sbapt
526228072Sbapt		/* Kill ALL flex-related macros. This is so the user
527228072Sbapt		 * can #include more than one generated header file. */
528228072Sbapt		fprintf (header_out, "#ifndef YY_HEADER_NO_UNDEFS\n");
529228072Sbapt		fprintf (header_out,
530228072Sbapt			 "/* Undefine all internal macros, etc., that do no belong in the header. */\n\n");
531228072Sbapt
532228072Sbapt        {
533228072Sbapt            const char * undef_list[] = {
534228072Sbapt
535228072Sbapt                "BEGIN",
536228072Sbapt                "ECHO",
537228072Sbapt                "EOB_ACT_CONTINUE_SCAN",
538228072Sbapt                "EOB_ACT_END_OF_FILE",
539228072Sbapt                "EOB_ACT_LAST_MATCH",
540228072Sbapt                "FLEX_SCANNER",
541228072Sbapt                "FLEX_STD",
542228072Sbapt                "REJECT",
543228072Sbapt                "YYFARGS0",
544228072Sbapt                "YYFARGS1",
545228072Sbapt                "YYFARGS2",
546228072Sbapt                "YYFARGS3",
547228072Sbapt                "YYLMAX",
548228072Sbapt                "YYSTATE",
549228072Sbapt                "YY_AT_BOL",
550228072Sbapt                "YY_BREAK",
551228072Sbapt                "YY_BUFFER_EOF_PENDING",
552228072Sbapt                "YY_BUFFER_NEW",
553228072Sbapt                "YY_BUFFER_NORMAL",
554228072Sbapt                "YY_BUF_SIZE",
555228072Sbapt                "M4_YY_CALL_LAST_ARG",
556228072Sbapt                "M4_YY_CALL_ONLY_ARG",
557228072Sbapt                "YY_CURRENT_BUFFER",
558228072Sbapt                "YY_DECL",
559228072Sbapt                "M4_YY_DECL_LAST_ARG",
560228072Sbapt                "M4_YY_DEF_LAST_ARG",
561228072Sbapt                "M4_YY_DEF_ONLY_ARG",
562228072Sbapt                "YY_DO_BEFORE_ACTION",
563228072Sbapt                "YY_END_OF_BUFFER",
564228072Sbapt                "YY_END_OF_BUFFER_CHAR",
565228072Sbapt                "YY_EXIT_FAILURE",
566228072Sbapt                "YY_EXTRA_TYPE",
567228072Sbapt                "YY_FATAL_ERROR",
568228072Sbapt                "YY_FLEX_DEFINED_ECHO",
569228072Sbapt                "YY_FLEX_LEX_COMPAT",
570228072Sbapt                "YY_FLEX_MAJOR_VERSION",
571228072Sbapt                "YY_FLEX_MINOR_VERSION",
572228072Sbapt                "YY_FLEX_SUBMINOR_VERSION",
573228072Sbapt                "YY_FLUSH_BUFFER",
574228072Sbapt                "YY_G",
575228072Sbapt                "YY_INPUT",
576228072Sbapt                "YY_INTERACTIVE",
577228072Sbapt                "YY_INT_ALIGNED",
578228072Sbapt                "YY_LAST_ARG",
579228072Sbapt                "YY_LESS_LINENO",
580228072Sbapt                "YY_LEX_ARGS",
581228072Sbapt                "YY_LEX_DECLARATION",
582228072Sbapt                "YY_LEX_PROTO",
583228072Sbapt                "YY_MAIN",
584228072Sbapt                "YY_MORE_ADJ",
585228072Sbapt                "YY_NEED_STRLEN",
586228072Sbapt                "YY_NEW_FILE",
587228072Sbapt                "YY_NULL",
588228072Sbapt                "YY_NUM_RULES",
589228072Sbapt                "YY_ONLY_ARG",
590228072Sbapt                "YY_PARAMS",
591228072Sbapt                "YY_PROTO",
592228072Sbapt                "M4_YY_PROTO_LAST_ARG",
593228072Sbapt                "M4_YY_PROTO_ONLY_ARG void",
594228072Sbapt                "YY_READ_BUF_SIZE",
595228072Sbapt                "YY_REENTRANT",
596228072Sbapt                "YY_RESTORE_YY_MORE_OFFSET",
597228072Sbapt                "YY_RULE_SETUP",
598228072Sbapt                "YY_SC_TO_UI",
599228072Sbapt                "YY_SKIP_YYWRAP",
600228072Sbapt                "YY_START",
601228072Sbapt                "YY_START_STACK_INCR",
602228072Sbapt                "YY_STATE_EOF",
603228072Sbapt                "YY_STDINIT",
604228072Sbapt                "YY_TRAILING_HEAD_MASK",
605228072Sbapt                "YY_TRAILING_MASK",
606228072Sbapt                "YY_USER_ACTION",
607228072Sbapt                "YY_USE_CONST",
608228072Sbapt                "YY_USE_PROTOS",
609228072Sbapt                "unput",
610228072Sbapt                "yyTABLES_NAME",
611228072Sbapt                "yy_create_buffer",
612228072Sbapt                "yy_delete_buffer",
613228072Sbapt                "yy_flex_debug",
614228072Sbapt                "yy_flush_buffer",
615228072Sbapt                "yy_init_buffer",
616228072Sbapt                "yy_load_buffer_state",
617228072Sbapt                "yy_new_buffer",
618228072Sbapt                "yy_scan_buffer",
619228072Sbapt                "yy_scan_bytes",
620228072Sbapt                "yy_scan_string",
621228072Sbapt                "yy_set_bol",
622228072Sbapt                "yy_set_interactive",
623228072Sbapt                "yy_switch_to_buffer",
624228072Sbapt				"yypush_buffer_state",
625228072Sbapt				"yypop_buffer_state",
626228072Sbapt				"yyensure_buffer_stack",
627228072Sbapt                "yyalloc",
628228072Sbapt                "yyconst",
629228072Sbapt                "yyextra",
630228072Sbapt                "yyfree",
631228072Sbapt                "yyget_debug",
632228072Sbapt                "yyget_extra",
633228072Sbapt                "yyget_in",
634228072Sbapt                "yyget_leng",
635228072Sbapt                "yyget_lineno",
636228072Sbapt                "yyget_lloc",
637228072Sbapt                "yyget_lval",
638228072Sbapt                "yyget_out",
639228072Sbapt                "yyget_text",
640228072Sbapt                "yyin",
641228072Sbapt                "yyleng",
642228072Sbapt                "yyless",
643228072Sbapt                "yylex",
644228072Sbapt                "yylex_destroy",
645228072Sbapt                "yylex_init",
646228072Sbapt                "yylex_init_extra",
647228072Sbapt                "yylineno",
648228072Sbapt                "yylloc",
649228072Sbapt                "yylval",
650228072Sbapt                "yymore",
651228072Sbapt                "yyout",
652228072Sbapt                "yyrealloc",
653228072Sbapt                "yyrestart",
654228072Sbapt                "yyset_debug",
655228072Sbapt                "yyset_extra",
656228072Sbapt                "yyset_in",
657228072Sbapt                "yyset_lineno",
658228072Sbapt                "yyset_lloc",
659228072Sbapt                "yyset_lval",
660228072Sbapt                "yyset_out",
661228072Sbapt                "yytables_destroy",
662228072Sbapt                "yytables_fload",
663228072Sbapt                "yyterminate",
664228072Sbapt                "yytext",
665228072Sbapt                "yytext_ptr",
666228072Sbapt                "yywrap",
667228072Sbapt
668228072Sbapt                /* must be null-terminated */
669228072Sbapt                NULL};
670228072Sbapt
671228072Sbapt
672228072Sbapt                for (i=0; undef_list[i] != NULL; i++)
673228072Sbapt                    fprintf (header_out, "#undef %s\n", undef_list[i]);
674228072Sbapt        }
675228072Sbapt
676228072Sbapt		/* undef any of the auto-generated symbols. */
677228072Sbapt		for (i = 0; i < defs_buf.nelts; i++) {
678228072Sbapt
679228072Sbapt			/* don't undef start conditions */
680228072Sbapt			if (sclookup (((char **) defs_buf.elts)[i]) > 0)
681228072Sbapt				continue;
682228072Sbapt			fprintf (header_out, "#undef %s\n",
683228072Sbapt				 ((char **) defs_buf.elts)[i]);
684228072Sbapt		}
685228072Sbapt
686228072Sbapt		fprintf (header_out,
687228072Sbapt			 "#endif /* !YY_HEADER_NO_UNDEFS */\n");
688228072Sbapt		fprintf (header_out, "\n");
689228072Sbapt		fprintf (header_out, "#undef %sIN_HEADER\n", prefix);
690228072Sbapt		fprintf (header_out, "#endif /* %sHEADER_H */\n", prefix);
691228072Sbapt
692228072Sbapt		if (ferror (header_out))
693228072Sbapt			lerrsf (_("error creating header file %s"),
694228072Sbapt				headerfilename);
695228072Sbapt		fflush (header_out);
696228072Sbapt		fclose (header_out);
697228072Sbapt#endif
698228072Sbapt
699228072Sbapt	if (exit_status != 0 && outfile_created) {
700228072Sbapt		if (ferror (stdout))
701228072Sbapt			lerrsf (_("error writing output file %s"),
702228072Sbapt				outfilename);
703228072Sbapt
704228072Sbapt		else if ((_stdout_closed = 1) && fclose (stdout))
705228072Sbapt			lerrsf (_("error closing output file %s"),
706228072Sbapt				outfilename);
707228072Sbapt
708228072Sbapt		else if (unlink (outfilename))
709228072Sbapt			lerrsf (_("error deleting output file %s"),
710228072Sbapt				outfilename);
711228072Sbapt	}
712228072Sbapt
713228072Sbapt
714228072Sbapt	if (backing_up_report && backing_up_file) {
715228072Sbapt		if (num_backing_up == 0)
716228072Sbapt			fprintf (backing_up_file, _("No backing up.\n"));
717228072Sbapt		else if (fullspd || fulltbl)
718228072Sbapt			fprintf (backing_up_file,
719228072Sbapt				 _
720228072Sbapt				 ("%d backing up (non-accepting) states.\n"),
721228072Sbapt				 num_backing_up);
722228072Sbapt		else
723228072Sbapt			fprintf (backing_up_file,
724228072Sbapt				 _("Compressed tables always back up.\n"));
725228072Sbapt
726228072Sbapt		if (ferror (backing_up_file))
727228072Sbapt			lerrsf (_("error writing backup file %s"),
728228072Sbapt				backing_name);
729228072Sbapt
730228072Sbapt		else if (fclose (backing_up_file))
731228072Sbapt			lerrsf (_("error closing backup file %s"),
732228072Sbapt				backing_name);
733228072Sbapt	}
734228072Sbapt
735228072Sbapt	if (printstats) {
736228072Sbapt		fprintf (stderr, _("%s version %s usage statistics:\n"),
737228072Sbapt			 program_name, flex_version);
738228072Sbapt
739228072Sbapt		fprintf (stderr, _("  scanner options: -"));
740228072Sbapt
741228072Sbapt		if (C_plus_plus)
742228072Sbapt			putc ('+', stderr);
743228072Sbapt		if (backing_up_report)
744228072Sbapt			putc ('b', stderr);
745228072Sbapt		if (ddebug)
746228072Sbapt			putc ('d', stderr);
747228072Sbapt		if (sf_case_ins())
748228072Sbapt			putc ('i', stderr);
749228072Sbapt		if (lex_compat)
750228072Sbapt			putc ('l', stderr);
751228072Sbapt		if (posix_compat)
752228072Sbapt			putc ('X', stderr);
753228072Sbapt		if (performance_report > 0)
754228072Sbapt			putc ('p', stderr);
755228072Sbapt		if (performance_report > 1)
756228072Sbapt			putc ('p', stderr);
757228072Sbapt		if (spprdflt)
758228072Sbapt			putc ('s', stderr);
759228072Sbapt		if (reentrant)
760228072Sbapt			fputs ("--reentrant", stderr);
761228072Sbapt        if (bison_bridge_lval)
762228072Sbapt            fputs ("--bison-bridge", stderr);
763228072Sbapt        if (bison_bridge_lloc)
764228072Sbapt            fputs ("--bison-locations", stderr);
765228072Sbapt		if (use_stdout)
766228072Sbapt			putc ('t', stderr);
767228072Sbapt		if (printstats)
768228072Sbapt			putc ('v', stderr);	/* always true! */
769228072Sbapt		if (nowarn)
770228072Sbapt			putc ('w', stderr);
771228072Sbapt		if (interactive == false)
772228072Sbapt			putc ('B', stderr);
773228072Sbapt		if (interactive == true)
774228072Sbapt			putc ('I', stderr);
775228072Sbapt		if (!gen_line_dirs)
776228072Sbapt			putc ('L', stderr);
777228072Sbapt		if (trace)
778228072Sbapt			putc ('T', stderr);
779228072Sbapt
780228072Sbapt		if (csize == unspecified)
781228072Sbapt			/* We encountered an error fairly early on, so csize
782228072Sbapt			 * never got specified.  Define it now, to prevent
783228072Sbapt			 * bogus table sizes being written out below.
784228072Sbapt			 */
785228072Sbapt			csize = 256;
786228072Sbapt
787228072Sbapt		if (csize == 128)
788228072Sbapt			putc ('7', stderr);
789228072Sbapt		else
790228072Sbapt			putc ('8', stderr);
791228072Sbapt
792228072Sbapt		fprintf (stderr, " -C");
793228072Sbapt
794228072Sbapt		if (long_align)
795228072Sbapt			putc ('a', stderr);
796228072Sbapt		if (fulltbl)
797228072Sbapt			putc ('f', stderr);
798228072Sbapt		if (fullspd)
799228072Sbapt			putc ('F', stderr);
800228072Sbapt		if (useecs)
801228072Sbapt			putc ('e', stderr);
802228072Sbapt		if (usemecs)
803228072Sbapt			putc ('m', stderr);
804228072Sbapt		if (use_read)
805228072Sbapt			putc ('r', stderr);
806228072Sbapt
807228072Sbapt		if (did_outfilename)
808228072Sbapt			fprintf (stderr, " -o%s", outfilename);
809228072Sbapt
810228072Sbapt		if (skelname)
811228072Sbapt			fprintf (stderr, " -S%s", skelname);
812228072Sbapt
813228072Sbapt		if (strcmp (prefix, "yy"))
814228072Sbapt			fprintf (stderr, " -P%s", prefix);
815228072Sbapt
816228072Sbapt		putc ('\n', stderr);
817228072Sbapt
818228072Sbapt		fprintf (stderr, _("  %d/%d NFA states\n"),
819228072Sbapt			 lastnfa, current_mns);
820228072Sbapt		fprintf (stderr, _("  %d/%d DFA states (%d words)\n"),
821228072Sbapt			 lastdfa, current_max_dfas, totnst);
822228072Sbapt		fprintf (stderr, _("  %d rules\n"),
823228072Sbapt			 num_rules + num_eof_rules -
824228072Sbapt			 1 /* - 1 for def. rule */ );
825228072Sbapt
826228072Sbapt		if (num_backing_up == 0)
827228072Sbapt			fprintf (stderr, _("  No backing up\n"));
828228072Sbapt		else if (fullspd || fulltbl)
829228072Sbapt			fprintf (stderr,
830228072Sbapt				 _
831228072Sbapt				 ("  %d backing-up (non-accepting) states\n"),
832228072Sbapt				 num_backing_up);
833228072Sbapt		else
834228072Sbapt			fprintf (stderr,
835228072Sbapt				 _
836228072Sbapt				 ("  Compressed tables always back-up\n"));
837228072Sbapt
838228072Sbapt		if (bol_needed)
839228072Sbapt			fprintf (stderr,
840228072Sbapt				 _("  Beginning-of-line patterns used\n"));
841228072Sbapt
842228072Sbapt		fprintf (stderr, _("  %d/%d start conditions\n"), lastsc,
843228072Sbapt			 current_max_scs);
844228072Sbapt		fprintf (stderr,
845228072Sbapt			 _
846228072Sbapt			 ("  %d epsilon states, %d double epsilon states\n"),
847228072Sbapt			 numeps, eps2);
848228072Sbapt
849228072Sbapt		if (lastccl == 0)
850228072Sbapt			fprintf (stderr, _("  no character classes\n"));
851228072Sbapt		else
852228072Sbapt			fprintf (stderr,
853228072Sbapt				 _
854228072Sbapt				 ("  %d/%d character classes needed %d/%d words of storage, %d reused\n"),
855228072Sbapt				 lastccl, current_maxccls,
856228072Sbapt				 cclmap[lastccl] + ccllen[lastccl],
857228072Sbapt				 current_max_ccl_tbl_size, cclreuse);
858228072Sbapt
859228072Sbapt		fprintf (stderr, _("  %d state/nextstate pairs created\n"),
860228072Sbapt			 numsnpairs);
861228072Sbapt		fprintf (stderr,
862228072Sbapt			 _("  %d/%d unique/duplicate transitions\n"),
863228072Sbapt			 numuniq, numdup);
864228072Sbapt
865228072Sbapt		if (fulltbl) {
866228072Sbapt			tblsiz = lastdfa * numecs;
867228072Sbapt			fprintf (stderr, _("  %d table entries\n"),
868228072Sbapt				 tblsiz);
869228072Sbapt		}
870228072Sbapt
871228072Sbapt		else {
872228072Sbapt			tblsiz = 2 * (lastdfa + numtemps) + 2 * tblend;
873228072Sbapt
874228072Sbapt			fprintf (stderr,
875228072Sbapt				 _("  %d/%d base-def entries created\n"),
876228072Sbapt				 lastdfa + numtemps, current_max_dfas);
877228072Sbapt			fprintf (stderr,
878228072Sbapt				 _
879228072Sbapt				 ("  %d/%d (peak %d) nxt-chk entries created\n"),
880228072Sbapt				 tblend, current_max_xpairs, peakpairs);
881228072Sbapt			fprintf (stderr,
882228072Sbapt				 _
883228072Sbapt				 ("  %d/%d (peak %d) template nxt-chk entries created\n"),
884228072Sbapt				 numtemps * nummecs,
885228072Sbapt				 current_max_template_xpairs,
886228072Sbapt				 numtemps * numecs);
887228072Sbapt			fprintf (stderr, _("  %d empty table entries\n"),
888228072Sbapt				 nummt);
889228072Sbapt			fprintf (stderr, _("  %d protos created\n"),
890228072Sbapt				 numprots);
891228072Sbapt			fprintf (stderr,
892228072Sbapt				 _("  %d templates created, %d uses\n"),
893228072Sbapt				 numtemps, tmpuses);
894228072Sbapt		}
895228072Sbapt
896228072Sbapt		if (useecs) {
897228072Sbapt			tblsiz = tblsiz + csize;
898228072Sbapt			fprintf (stderr,
899228072Sbapt				 _
900228072Sbapt				 ("  %d/%d equivalence classes created\n"),
901228072Sbapt				 numecs, csize);
902228072Sbapt		}
903228072Sbapt
904228072Sbapt		if (usemecs) {
905228072Sbapt			tblsiz = tblsiz + numecs;
906228072Sbapt			fprintf (stderr,
907228072Sbapt				 _
908228072Sbapt				 ("  %d/%d meta-equivalence classes created\n"),
909228072Sbapt				 nummecs, csize);
910228072Sbapt		}
911228072Sbapt
912228072Sbapt		fprintf (stderr,
913228072Sbapt			 _
914228072Sbapt			 ("  %d (%d saved) hash collisions, %d DFAs equal\n"),
915228072Sbapt			 hshcol, hshsave, dfaeql);
916228072Sbapt		fprintf (stderr, _("  %d sets of reallocations needed\n"),
917228072Sbapt			 num_reallocs);
918228072Sbapt		fprintf (stderr, _("  %d total table entries needed\n"),
919228072Sbapt			 tblsiz);
920228072Sbapt	}
921228072Sbapt
922228072Sbapt	FLEX_EXIT (exit_status);
923228072Sbapt}
924228072Sbapt
925228072Sbapt
926228072Sbapt/* flexinit - initialize flex */
927228072Sbapt
928228072Sbaptvoid flexinit (argc, argv)
929228072Sbapt     int argc;
930228072Sbapt     char  **argv;
931228072Sbapt{
932228072Sbapt	int     i, sawcmpflag, rv, optind;
933228072Sbapt	char   *arg;
934228072Sbapt	scanopt_t sopt;
935228072Sbapt
936228072Sbapt	printstats = syntaxerror = trace = spprdflt = false;
937228072Sbapt	lex_compat = posix_compat = C_plus_plus = backing_up_report =
938228072Sbapt		ddebug = fulltbl = false;
939228072Sbapt	fullspd = long_align = nowarn = yymore_used = continued_action =
940228072Sbapt		false;
941228072Sbapt	do_yylineno = yytext_is_array = in_rule = reject = do_stdinit =
942228072Sbapt		false;
943228072Sbapt	yymore_really_used = reject_really_used = unspecified;
944228072Sbapt	interactive = csize = unspecified;
945228072Sbapt	do_yywrap = gen_line_dirs = usemecs = useecs = true;
946228072Sbapt	reentrant = bison_bridge_lval = bison_bridge_lloc = false;
947228072Sbapt	performance_report = 0;
948228072Sbapt	did_outfilename = 0;
949228072Sbapt	prefix = "yy";
950228072Sbapt	yyclass = 0;
951228072Sbapt	use_read = use_stdout = false;
952228072Sbapt	tablesext = tablesverify = false;
953228072Sbapt	gentables = true;
954228072Sbapt	tablesfilename = tablesname = NULL;
955228072Sbapt    ansi_func_defs = ansi_func_protos = true;
956228072Sbapt
957228072Sbapt	sawcmpflag = false;
958228072Sbapt
959228072Sbapt	/* Initialize dynamic array for holding the rule actions. */
960228072Sbapt	action_size = 2048;	/* default size of action array in bytes */
961228072Sbapt	action_array = allocate_character_array (action_size);
962228072Sbapt	defs1_offset = prolog_offset = action_offset = action_index = 0;
963228072Sbapt	action_array[0] = '\0';
964228072Sbapt
965228072Sbapt	/* Initialize any buffers. */
966228072Sbapt	buf_init (&userdef_buf, sizeof (char));	/* one long string */
967228072Sbapt	buf_init (&defs_buf, sizeof (char *));	/* list of strings */
968228072Sbapt	buf_init (&yydmap_buf, sizeof (char));	/* one long string */
969228072Sbapt	buf_init (&top_buf, sizeof (char));	    /* one long string */
970228072Sbapt
971228072Sbapt    {
972228072Sbapt        const char * m4defs_init_str[] = {"m4_changequote\n",
973228072Sbapt                                          "m4_changequote([[, ]])\n"};
974228072Sbapt        buf_init (&m4defs_buf, sizeof (char *));
975228072Sbapt        buf_append (&m4defs_buf, &m4defs_init_str, 2);
976228072Sbapt    }
977228072Sbapt
978228072Sbapt    sf_init ();
979228072Sbapt
980228072Sbapt    /* initialize regex lib */
981228072Sbapt    flex_init_regex();
982228072Sbapt
983228072Sbapt	/* Enable C++ if program name ends with '+'. */
984228072Sbapt	program_name = basename2 (argv[0], 0);
985228072Sbapt
986228072Sbapt	if (program_name[0] != '\0' &&
987228072Sbapt	    program_name[strlen (program_name) - 1] == '+')
988228072Sbapt		C_plus_plus = true;
989228072Sbapt
990228072Sbapt	/* read flags */
991228072Sbapt	sopt = scanopt_init (flexopts, argc, argv, 0);
992228072Sbapt	if (!sopt) {
993228072Sbapt		/* This will only happen when flexopts array is altered. */
994228072Sbapt		fprintf (stderr,
995228072Sbapt			 _("Internal error. flexopts are malformed.\n"));
996228072Sbapt		FLEX_EXIT (1);
997228072Sbapt	}
998228072Sbapt
999228072Sbapt	while ((rv = scanopt (sopt, &arg, &optind)) != 0) {
1000228072Sbapt
1001228072Sbapt		if (rv < 0) {
1002228072Sbapt			/* Scanopt has already printed an option-specific error message. */
1003228072Sbapt			fprintf (stderr,
1004228072Sbapt				 _
1005228072Sbapt				 ("Try `%s --help' for more information.\n"),
1006228072Sbapt				 program_name);
1007228072Sbapt			FLEX_EXIT (1);
1008228072Sbapt		}
1009228072Sbapt
1010228072Sbapt		switch ((enum flexopt_flag_t) rv) {
1011228072Sbapt		case OPT_CPLUSPLUS:
1012228072Sbapt			C_plus_plus = true;
1013228072Sbapt			break;
1014228072Sbapt
1015228072Sbapt		case OPT_BATCH:
1016228072Sbapt			interactive = false;
1017228072Sbapt			break;
1018228072Sbapt
1019228072Sbapt		case OPT_BACKUP:
1020228072Sbapt			backing_up_report = true;
1021228072Sbapt			break;
1022228072Sbapt
1023228072Sbapt		case OPT_DONOTHING:
1024228072Sbapt			break;
1025228072Sbapt
1026228072Sbapt		case OPT_COMPRESSION:
1027228072Sbapt			if (!sawcmpflag) {
1028228072Sbapt				useecs = false;
1029228072Sbapt				usemecs = false;
1030228072Sbapt				fulltbl = false;
1031228072Sbapt				sawcmpflag = true;
1032228072Sbapt			}
1033228072Sbapt
1034228072Sbapt			for (i = 0; arg && arg[i] != '\0'; i++)
1035228072Sbapt				switch (arg[i]) {
1036228072Sbapt				case 'a':
1037228072Sbapt					long_align = true;
1038228072Sbapt					break;
1039228072Sbapt
1040228072Sbapt				case 'e':
1041228072Sbapt					useecs = true;
1042228072Sbapt					break;
1043228072Sbapt
1044228072Sbapt				case 'F':
1045228072Sbapt					fullspd = true;
1046228072Sbapt					break;
1047228072Sbapt
1048228072Sbapt				case 'f':
1049228072Sbapt					fulltbl = true;
1050228072Sbapt					break;
1051228072Sbapt
1052228072Sbapt				case 'm':
1053228072Sbapt					usemecs = true;
1054228072Sbapt					break;
1055228072Sbapt
1056228072Sbapt				case 'r':
1057228072Sbapt					use_read = true;
1058228072Sbapt					break;
1059228072Sbapt
1060228072Sbapt				default:
1061228072Sbapt					lerrif (_
1062228072Sbapt						("unknown -C option '%c'"),
1063228072Sbapt						(int) arg[i]);
1064228072Sbapt					break;
1065228072Sbapt				}
1066228072Sbapt			break;
1067228072Sbapt
1068228072Sbapt		case OPT_DEBUG:
1069228072Sbapt			ddebug = true;
1070228072Sbapt			break;
1071228072Sbapt
1072228072Sbapt		case OPT_NO_DEBUG:
1073228072Sbapt			ddebug = false;
1074228072Sbapt			break;
1075228072Sbapt
1076228072Sbapt		case OPT_FULL:
1077228072Sbapt			useecs = usemecs = false;
1078228072Sbapt			use_read = fulltbl = true;
1079228072Sbapt			break;
1080228072Sbapt
1081228072Sbapt		case OPT_FAST:
1082228072Sbapt			useecs = usemecs = false;
1083228072Sbapt			use_read = fullspd = true;
1084228072Sbapt			break;
1085228072Sbapt
1086228072Sbapt		case OPT_HELP:
1087228072Sbapt			usage ();
1088228072Sbapt			FLEX_EXIT (0);
1089228072Sbapt
1090228072Sbapt		case OPT_INTERACTIVE:
1091228072Sbapt			interactive = true;
1092228072Sbapt			break;
1093228072Sbapt
1094228072Sbapt		case OPT_CASE_INSENSITIVE:
1095228072Sbapt			sf_set_case_ins(true);
1096228072Sbapt			break;
1097228072Sbapt
1098228072Sbapt		case OPT_LEX_COMPAT:
1099228072Sbapt			lex_compat = true;
1100228072Sbapt			break;
1101228072Sbapt
1102228072Sbapt		case OPT_POSIX_COMPAT:
1103228072Sbapt			posix_compat = true;
1104228072Sbapt			break;
1105228072Sbapt
1106228072Sbapt        case OPT_PREPROC_LEVEL:
1107228072Sbapt            preproc_level = strtol(arg,NULL,0);
1108228072Sbapt            break;
1109228072Sbapt
1110228072Sbapt		case OPT_MAIN:
1111228072Sbapt			buf_strdefine (&userdef_buf, "YY_MAIN", "1");
1112228072Sbapt			do_yywrap = false;
1113228072Sbapt			break;
1114228072Sbapt
1115228072Sbapt		case OPT_NO_MAIN:
1116228072Sbapt			buf_strdefine (&userdef_buf, "YY_MAIN", "0");
1117228072Sbapt			break;
1118228072Sbapt
1119228072Sbapt		case OPT_NO_LINE:
1120228072Sbapt			gen_line_dirs = false;
1121228072Sbapt			break;
1122228072Sbapt
1123228072Sbapt		case OPT_OUTFILE:
1124228072Sbapt			outfilename = arg;
1125228072Sbapt			did_outfilename = 1;
1126228072Sbapt			break;
1127228072Sbapt
1128228072Sbapt		case OPT_PREFIX:
1129228072Sbapt			prefix = arg;
1130228072Sbapt			break;
1131228072Sbapt
1132228072Sbapt		case OPT_PERF_REPORT:
1133228072Sbapt			++performance_report;
1134228072Sbapt			break;
1135228072Sbapt
1136228072Sbapt		case OPT_BISON_BRIDGE:
1137228072Sbapt			bison_bridge_lval = true;
1138228072Sbapt			break;
1139228072Sbapt
1140228072Sbapt		case OPT_BISON_BRIDGE_LOCATIONS:
1141228072Sbapt			bison_bridge_lval = bison_bridge_lloc = true;
1142228072Sbapt			break;
1143228072Sbapt
1144228072Sbapt		case OPT_REENTRANT:
1145228072Sbapt			reentrant = true;
1146228072Sbapt			break;
1147228072Sbapt
1148228072Sbapt		case OPT_NO_REENTRANT:
1149228072Sbapt			reentrant = false;
1150228072Sbapt			break;
1151228072Sbapt
1152228072Sbapt		case OPT_SKEL:
1153228072Sbapt			skelname = arg;
1154228072Sbapt			break;
1155228072Sbapt
1156228072Sbapt		case OPT_DEFAULT:
1157228072Sbapt			spprdflt = false;
1158228072Sbapt			break;
1159228072Sbapt
1160228072Sbapt		case OPT_NO_DEFAULT:
1161228072Sbapt			spprdflt = true;
1162228072Sbapt			break;
1163228072Sbapt
1164228072Sbapt		case OPT_STDOUT:
1165228072Sbapt			use_stdout = true;
1166228072Sbapt			break;
1167228072Sbapt
1168228072Sbapt		case OPT_NO_UNISTD_H:
1169228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_UNISTD_H", "1");
1170228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_UNISTD_H",0);
1171228072Sbapt			break;
1172228072Sbapt
1173228072Sbapt		case OPT_TABLES_FILE:
1174228072Sbapt			tablesext = true;
1175228072Sbapt			tablesfilename = arg;
1176228072Sbapt			break;
1177228072Sbapt
1178228072Sbapt		case OPT_TABLES_VERIFY:
1179228072Sbapt			tablesverify = true;
1180228072Sbapt			break;
1181228072Sbapt
1182228072Sbapt		case OPT_TRACE:
1183228072Sbapt			trace = true;
1184228072Sbapt			break;
1185228072Sbapt
1186228072Sbapt		case OPT_VERBOSE:
1187228072Sbapt			printstats = true;
1188228072Sbapt			break;
1189228072Sbapt
1190228072Sbapt		case OPT_VERSION:
1191228072Sbapt			printf (_("%s %s\n"), program_name, flex_version);
1192228072Sbapt			FLEX_EXIT (0);
1193228072Sbapt
1194228072Sbapt		case OPT_WARN:
1195228072Sbapt			nowarn = false;
1196228072Sbapt			break;
1197228072Sbapt
1198228072Sbapt		case OPT_NO_WARN:
1199228072Sbapt			nowarn = true;
1200228072Sbapt			break;
1201228072Sbapt
1202228072Sbapt		case OPT_7BIT:
1203228072Sbapt			csize = 128;
1204228072Sbapt			break;
1205228072Sbapt
1206228072Sbapt		case OPT_8BIT:
1207228072Sbapt			csize = CSIZE;
1208228072Sbapt			break;
1209228072Sbapt
1210228072Sbapt		case OPT_ALIGN:
1211228072Sbapt			long_align = true;
1212228072Sbapt			break;
1213228072Sbapt
1214228072Sbapt		case OPT_NO_ALIGN:
1215228072Sbapt			long_align = false;
1216228072Sbapt			break;
1217228072Sbapt
1218228072Sbapt		case OPT_ALWAYS_INTERACTIVE:
1219228072Sbapt			buf_m4_define (&m4defs_buf, "M4_YY_ALWAYS_INTERACTIVE", 0);
1220228072Sbapt			break;
1221228072Sbapt
1222228072Sbapt		case OPT_NEVER_INTERACTIVE:
1223228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NEVER_INTERACTIVE", 0);
1224228072Sbapt			break;
1225228072Sbapt
1226228072Sbapt		case OPT_ARRAY:
1227228072Sbapt			yytext_is_array = true;
1228228072Sbapt			break;
1229228072Sbapt
1230228072Sbapt		case OPT_POINTER:
1231228072Sbapt			yytext_is_array = false;
1232228072Sbapt			break;
1233228072Sbapt
1234228072Sbapt		case OPT_ECS:
1235228072Sbapt			useecs = true;
1236228072Sbapt			break;
1237228072Sbapt
1238228072Sbapt		case OPT_NO_ECS:
1239228072Sbapt			useecs = false;
1240228072Sbapt			break;
1241228072Sbapt
1242228072Sbapt		case OPT_HEADER_FILE:
1243228072Sbapt			headerfilename = arg;
1244228072Sbapt			break;
1245228072Sbapt
1246228072Sbapt		case OPT_META_ECS:
1247228072Sbapt			usemecs = true;
1248228072Sbapt			break;
1249228072Sbapt
1250228072Sbapt		case OPT_NO_META_ECS:
1251228072Sbapt			usemecs = false;
1252228072Sbapt			break;
1253228072Sbapt
1254228072Sbapt		case OPT_PREPROCDEFINE:
1255228072Sbapt			{
1256228072Sbapt				/* arg is "symbol" or "symbol=definition". */
1257228072Sbapt				char   *def;
1258228072Sbapt
1259228072Sbapt				for (def = arg;
1260228072Sbapt				     *def != '\0' && *def != '='; ++def) ;
1261228072Sbapt
1262228072Sbapt				buf_strappend (&userdef_buf, "#define ");
1263228072Sbapt				if (*def == '\0') {
1264228072Sbapt					buf_strappend (&userdef_buf, arg);
1265228072Sbapt					buf_strappend (&userdef_buf,
1266228072Sbapt						       " 1\n");
1267228072Sbapt				}
1268228072Sbapt				else {
1269228072Sbapt					buf_strnappend (&userdef_buf, arg,
1270228072Sbapt							def - arg);
1271228072Sbapt					buf_strappend (&userdef_buf, " ");
1272228072Sbapt					buf_strappend (&userdef_buf,
1273228072Sbapt						       def + 1);
1274228072Sbapt					buf_strappend (&userdef_buf, "\n");
1275228072Sbapt				}
1276228072Sbapt			}
1277228072Sbapt			break;
1278228072Sbapt
1279228072Sbapt		case OPT_READ:
1280228072Sbapt			use_read = true;
1281228072Sbapt			break;
1282228072Sbapt
1283228072Sbapt		case OPT_STACK:
1284228072Sbapt			//buf_strdefine (&userdef_buf, "YY_STACK_USED", "1");
1285228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_STACK_USED",0);
1286228072Sbapt			break;
1287228072Sbapt
1288228072Sbapt		case OPT_STDINIT:
1289228072Sbapt			do_stdinit = true;
1290228072Sbapt			break;
1291228072Sbapt
1292228072Sbapt		case OPT_NO_STDINIT:
1293228072Sbapt			do_stdinit = false;
1294228072Sbapt			break;
1295228072Sbapt
1296228072Sbapt		case OPT_YYCLASS:
1297228072Sbapt			yyclass = arg;
1298228072Sbapt			break;
1299228072Sbapt
1300228072Sbapt		case OPT_YYLINENO:
1301228072Sbapt			do_yylineno = true;
1302228072Sbapt			break;
1303228072Sbapt
1304228072Sbapt		case OPT_NO_YYLINENO:
1305228072Sbapt			do_yylineno = false;
1306228072Sbapt			break;
1307228072Sbapt
1308228072Sbapt		case OPT_YYWRAP:
1309228072Sbapt			do_yywrap = true;
1310228072Sbapt			break;
1311228072Sbapt
1312228072Sbapt		case OPT_NO_YYWRAP:
1313228072Sbapt			do_yywrap = false;
1314228072Sbapt			break;
1315228072Sbapt
1316228072Sbapt		case OPT_YYMORE:
1317228072Sbapt			yymore_really_used = true;
1318228072Sbapt			break;
1319228072Sbapt
1320228072Sbapt		case OPT_NO_YYMORE:
1321228072Sbapt			yymore_really_used = false;
1322228072Sbapt			break;
1323228072Sbapt
1324228072Sbapt		case OPT_REJECT:
1325228072Sbapt			reject_really_used = true;
1326228072Sbapt			break;
1327228072Sbapt
1328228072Sbapt		case OPT_NO_REJECT:
1329228072Sbapt			reject_really_used = false;
1330228072Sbapt			break;
1331228072Sbapt
1332228072Sbapt        case OPT_NO_ANSI_FUNC_DEFS:
1333228072Sbapt            ansi_func_defs = false;
1334228072Sbapt            break;
1335228072Sbapt
1336228072Sbapt        case OPT_NO_ANSI_FUNC_PROTOS:
1337228072Sbapt            ansi_func_protos = false;
1338228072Sbapt            break;
1339228072Sbapt
1340228072Sbapt		case OPT_NO_YY_PUSH_STATE:
1341228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_PUSH_STATE", "1");
1342228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_PUSH_STATE",0);
1343228072Sbapt			break;
1344228072Sbapt		case OPT_NO_YY_POP_STATE:
1345228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_POP_STATE", "1");
1346228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_POP_STATE",0);
1347228072Sbapt			break;
1348228072Sbapt		case OPT_NO_YY_TOP_STATE:
1349228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_TOP_STATE", "1");
1350228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_TOP_STATE",0);
1351228072Sbapt			break;
1352228072Sbapt		case OPT_NO_UNPUT:
1353228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_UNPUT", "1");
1354228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_UNPUT",0);
1355228072Sbapt			break;
1356228072Sbapt		case OPT_NO_YY_SCAN_BUFFER:
1357228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_SCAN_BUFFER", "1");
1358228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_BUFFER",0);
1359228072Sbapt			break;
1360228072Sbapt		case OPT_NO_YY_SCAN_BYTES:
1361228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_SCAN_BYTES", "1");
1362228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_BYTES",0);
1363228072Sbapt			break;
1364228072Sbapt		case OPT_NO_YY_SCAN_STRING:
1365228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_SCAN_STRING", "1");
1366228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_STRING",0);
1367228072Sbapt			break;
1368228072Sbapt		case OPT_NO_YYGET_EXTRA:
1369228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_GET_EXTRA", "1");
1370228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_EXTRA",0);
1371228072Sbapt			break;
1372228072Sbapt		case OPT_NO_YYSET_EXTRA:
1373228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_SET_EXTRA", "1");
1374228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_EXTRA",0);
1375228072Sbapt			break;
1376228072Sbapt		case OPT_NO_YYGET_LENG:
1377228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_GET_LENG", "1");
1378228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LENG",0);
1379228072Sbapt			break;
1380228072Sbapt		case OPT_NO_YYGET_TEXT:
1381228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_GET_TEXT", "1");
1382228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_TEXT",0);
1383228072Sbapt			break;
1384228072Sbapt		case OPT_NO_YYGET_LINENO:
1385228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_GET_LINENO", "1");
1386228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LINENO",0);
1387228072Sbapt			break;
1388228072Sbapt		case OPT_NO_YYSET_LINENO:
1389228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_SET_LINENO", "1");
1390228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LINENO",0);
1391228072Sbapt			break;
1392228072Sbapt		case OPT_NO_YYGET_IN:
1393228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_GET_IN", "1");
1394228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_IN",0);
1395228072Sbapt			break;
1396228072Sbapt		case OPT_NO_YYSET_IN:
1397228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_SET_IN", "1");
1398228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_IN",0);
1399228072Sbapt			break;
1400228072Sbapt		case OPT_NO_YYGET_OUT:
1401228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_GET_OUT", "1");
1402228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_OUT",0);
1403228072Sbapt			break;
1404228072Sbapt		case OPT_NO_YYSET_OUT:
1405228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_SET_OUT", "1");
1406228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_OUT",0);
1407228072Sbapt			break;
1408228072Sbapt		case OPT_NO_YYGET_LVAL:
1409228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_GET_LVAL", "1");
1410228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LVAL",0);
1411228072Sbapt			break;
1412228072Sbapt		case OPT_NO_YYSET_LVAL:
1413228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_SET_LVAL", "1");
1414228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LVAL",0);
1415228072Sbapt			break;
1416228072Sbapt		case OPT_NO_YYGET_LLOC:
1417228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_GET_LLOC", "1");
1418228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LLOC",0);
1419228072Sbapt			break;
1420228072Sbapt		case OPT_NO_YYSET_LLOC:
1421228072Sbapt			//buf_strdefine (&userdef_buf, "YY_NO_SET_LLOC", "1");
1422228072Sbapt            buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LLOC",0);
1423228072Sbapt			break;
1424228072Sbapt
1425228072Sbapt		}		/* switch */
1426228072Sbapt	}			/* while scanopt() */
1427228072Sbapt
1428228072Sbapt	scanopt_destroy (sopt);
1429228072Sbapt
1430228072Sbapt	num_input_files = argc - optind;
1431228072Sbapt	input_files = argv + optind;
1432228072Sbapt	set_input_file (num_input_files > 0 ? input_files[0] : NULL);
1433228072Sbapt
1434228072Sbapt	lastccl = lastsc = lastdfa = lastnfa = 0;
1435228072Sbapt	num_rules = num_eof_rules = default_rule = 0;
1436228072Sbapt	numas = numsnpairs = tmpuses = 0;
1437228072Sbapt	numecs = numeps = eps2 = num_reallocs = hshcol = dfaeql = totnst =
1438228072Sbapt		0;
1439228072Sbapt	numuniq = numdup = hshsave = eofseen = datapos = dataline = 0;
1440228072Sbapt	num_backing_up = onesp = numprots = 0;
1441228072Sbapt	variable_trailing_context_rules = bol_needed = false;
1442228072Sbapt
1443250125Sjkim	linenum = sectnum = 1;
1444228072Sbapt	firstprot = NIL;
1445228072Sbapt
1446228072Sbapt	/* Used in mkprot() so that the first proto goes in slot 1
1447228072Sbapt	 * of the proto queue.
1448228072Sbapt	 */
1449228072Sbapt	lastprot = 1;
1450228072Sbapt
1451228072Sbapt	set_up_initial_allocations ();
1452228072Sbapt}
1453228072Sbapt
1454228072Sbapt
1455228072Sbapt/* readin - read in the rules section of the input file(s) */
1456228072Sbapt
1457228072Sbaptvoid readin ()
1458228072Sbapt{
1459228072Sbapt	static char yy_stdinit[] = "FILE *yyin = stdin, *yyout = stdout;";
1460228072Sbapt	static char yy_nostdinit[] =
1461228072Sbapt		"FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0;";
1462228072Sbapt
1463228072Sbapt	line_directive_out ((FILE *) 0, 1);
1464228072Sbapt
1465228072Sbapt	if (yyparse ()) {
1466228072Sbapt		pinpoint_message (_("fatal parse error"));
1467228072Sbapt		flexend (1);
1468228072Sbapt	}
1469228072Sbapt
1470228072Sbapt	if (syntaxerror)
1471228072Sbapt		flexend (1);
1472228072Sbapt
1473228072Sbapt	/* If the user explicitly requested posix compatibility by specifing the
1474228072Sbapt	 * posix-compat option, then we check for conflicting options. However, if
1475228072Sbapt	 * the POSIXLY_CORRECT variable is set, then we quietly make flex as
1476228072Sbapt	 * posix-compatible as possible.  This is the recommended behavior
1477228072Sbapt	 * according to the GNU Coding Standards.
1478228072Sbapt	 *
1479228072Sbapt	 * Note: The posix option was added to flex to provide the posix behavior
1480228072Sbapt	 * of the repeat operator in regular expressions, e.g., `ab{3}'
1481228072Sbapt	 */
1482228072Sbapt	if (posix_compat) {
1483228072Sbapt		/* TODO: This is where we try to make flex behave according to
1484228072Sbapt		 * posiz, AND check for conflicting options. How far should we go
1485228072Sbapt		 * with this? Should we disable all the neat-o flex features?
1486228072Sbapt		 */
1487228072Sbapt		/* Update: Estes says no, since other flex features don't violate posix. */
1488228072Sbapt	}
1489228072Sbapt
1490228072Sbapt	if (getenv ("POSIXLY_CORRECT")) {
1491228072Sbapt		posix_compat = true;
1492228072Sbapt	}
1493228072Sbapt
1494228072Sbapt	if (backing_up_report) {
1495228072Sbapt		backing_up_file = fopen (backing_name, "w");
1496228072Sbapt		if (backing_up_file == NULL)
1497228072Sbapt			lerrsf (_
1498228072Sbapt				("could not create backing-up info file %s"),
1499228072Sbapt				backing_name);
1500228072Sbapt	}
1501228072Sbapt
1502228072Sbapt	else
1503228072Sbapt		backing_up_file = NULL;
1504228072Sbapt
1505228072Sbapt	if (yymore_really_used == true)
1506228072Sbapt		yymore_used = true;
1507228072Sbapt	else if (yymore_really_used == false)
1508228072Sbapt		yymore_used = false;
1509228072Sbapt
1510228072Sbapt	if (reject_really_used == true)
1511228072Sbapt		reject = true;
1512228072Sbapt	else if (reject_really_used == false)
1513228072Sbapt		reject = false;
1514228072Sbapt
1515228072Sbapt	if (performance_report > 0) {
1516228072Sbapt		if (lex_compat) {
1517228072Sbapt			fprintf (stderr,
1518228072Sbapt				 _
1519228072Sbapt				 ("-l AT&T lex compatibility option entails a large performance penalty\n"));
1520228072Sbapt			fprintf (stderr,
1521228072Sbapt				 _
1522228072Sbapt				 (" and may be the actual source of other reported performance penalties\n"));
1523228072Sbapt		}
1524228072Sbapt
1525228072Sbapt		else if (do_yylineno) {
1526228072Sbapt			fprintf (stderr,
1527228072Sbapt				 _
1528228072Sbapt				 ("%%option yylineno entails a performance penalty ONLY on rules that can match newline characters\n"));
1529228072Sbapt		}
1530228072Sbapt
1531228072Sbapt		if (performance_report > 1) {
1532228072Sbapt			if (interactive)
1533228072Sbapt				fprintf (stderr,
1534228072Sbapt					 _
1535228072Sbapt					 ("-I (interactive) entails a minor performance penalty\n"));
1536228072Sbapt
1537228072Sbapt			if (yymore_used)
1538228072Sbapt				fprintf (stderr,
1539228072Sbapt					 _
1540228072Sbapt					 ("yymore() entails a minor performance penalty\n"));
1541228072Sbapt		}
1542228072Sbapt
1543228072Sbapt		if (reject)
1544228072Sbapt			fprintf (stderr,
1545228072Sbapt				 _
1546228072Sbapt				 ("REJECT entails a large performance penalty\n"));
1547228072Sbapt
1548228072Sbapt		if (variable_trailing_context_rules)
1549228072Sbapt			fprintf (stderr,
1550228072Sbapt				 _
1551228072Sbapt				 ("Variable trailing context rules entail a large performance penalty\n"));
1552228072Sbapt	}
1553228072Sbapt
1554228072Sbapt	if (reject)
1555228072Sbapt		real_reject = true;
1556228072Sbapt
1557228072Sbapt	if (variable_trailing_context_rules)
1558228072Sbapt		reject = true;
1559228072Sbapt
1560228072Sbapt	if ((fulltbl || fullspd) && reject) {
1561228072Sbapt		if (real_reject)
1562228072Sbapt			flexerror (_
1563228072Sbapt				   ("REJECT cannot be used with -f or -F"));
1564228072Sbapt		else if (do_yylineno)
1565228072Sbapt			flexerror (_
1566228072Sbapt				   ("%option yylineno cannot be used with REJECT"));
1567228072Sbapt		else
1568228072Sbapt			flexerror (_
1569228072Sbapt				   ("variable trailing context rules cannot be used with -f or -F"));
1570228072Sbapt	}
1571228072Sbapt
1572228072Sbapt	if (reject){
1573228072Sbapt        out_m4_define( "M4_YY_USES_REJECT", NULL);
1574228072Sbapt		//outn ("\n#define YY_USES_REJECT");
1575228072Sbapt    }
1576228072Sbapt
1577228072Sbapt	if (!do_yywrap) {
1578250875Sjkim		if (!C_plus_plus) {
1579250125Sjkim			 if (reentrant)
1580250125Sjkim				outn ("\n#define yywrap(yyscanner) 1");
1581250125Sjkim			 else
1582250125Sjkim				outn ("\n#define yywrap() 1");
1583250875Sjkim		}
1584228072Sbapt		outn ("#define YY_SKIP_YYWRAP");
1585228072Sbapt	}
1586228072Sbapt
1587228072Sbapt	if (ddebug)
1588228072Sbapt		outn ("\n#define FLEX_DEBUG");
1589228072Sbapt
1590228072Sbapt	OUT_BEGIN_CODE ();
1591228072Sbapt	if (csize == 256)
1592228072Sbapt		outn ("typedef unsigned char YY_CHAR;");
1593228072Sbapt	else
1594228072Sbapt		outn ("typedef char YY_CHAR;");
1595228072Sbapt	OUT_END_CODE ();
1596228072Sbapt
1597228072Sbapt	if (C_plus_plus) {
1598228072Sbapt		outn ("#define yytext_ptr yytext");
1599228072Sbapt
1600228072Sbapt		if (interactive)
1601228072Sbapt			outn ("#define YY_INTERACTIVE");
1602228072Sbapt	}
1603228072Sbapt
1604228072Sbapt	else {
1605228072Sbapt		OUT_BEGIN_CODE ();
1606228072Sbapt		/* In reentrant scanner, stdinit is handled in flex.skl. */
1607228072Sbapt		if (do_stdinit) {
1608228072Sbapt			if (reentrant){
1609228072Sbapt                outn ("#ifdef VMS");
1610228072Sbapt                outn ("#ifdef __VMS_POSIX");
1611228072Sbapt                outn ("#define YY_STDINIT");
1612228072Sbapt                outn ("#endif");
1613228072Sbapt                outn ("#else");
1614228072Sbapt                outn ("#define YY_STDINIT");
1615228072Sbapt                outn ("#endif");
1616228072Sbapt            }
1617228072Sbapt
1618228072Sbapt			outn ("#ifdef VMS");
1619228072Sbapt			outn ("#ifndef __VMS_POSIX");
1620228072Sbapt			outn (yy_nostdinit);
1621228072Sbapt			outn ("#else");
1622228072Sbapt			outn (yy_stdinit);
1623228072Sbapt			outn ("#endif");
1624228072Sbapt			outn ("#else");
1625228072Sbapt			outn (yy_stdinit);
1626228072Sbapt			outn ("#endif");
1627228072Sbapt		}
1628228072Sbapt
1629228072Sbapt		else {
1630228072Sbapt			if(!reentrant)
1631228072Sbapt                outn (yy_nostdinit);
1632228072Sbapt		}
1633228072Sbapt		OUT_END_CODE ();
1634228072Sbapt	}
1635228072Sbapt
1636228072Sbapt	OUT_BEGIN_CODE ();
1637228072Sbapt	if (fullspd)
1638228072Sbapt		outn ("typedef yyconst struct yy_trans_info *yy_state_type;");
1639228072Sbapt	else if (!C_plus_plus)
1640228072Sbapt		outn ("typedef int yy_state_type;");
1641228072Sbapt	OUT_END_CODE ();
1642228072Sbapt
1643228072Sbapt	if (lex_compat)
1644228072Sbapt		outn ("#define YY_FLEX_LEX_COMPAT");
1645228072Sbapt
1646228072Sbapt	if (!C_plus_plus && !reentrant) {
1647228072Sbapt		outn ("extern int yylineno;");
1648228072Sbapt		OUT_BEGIN_CODE ();
1649228072Sbapt		outn ("int yylineno = 1;");
1650228072Sbapt		OUT_END_CODE ();
1651228072Sbapt	}
1652228072Sbapt
1653228072Sbapt	if (C_plus_plus) {
1654228072Sbapt		outn ("\n#include <FlexLexer.h>");
1655228072Sbapt
1656228072Sbapt 		if (!do_yywrap) {
1657228072Sbapt			outn("\nint yyFlexLexer::yywrap() { return 1; }");
1658228072Sbapt		}
1659228072Sbapt
1660228072Sbapt		if (yyclass) {
1661228072Sbapt			outn ("int yyFlexLexer::yylex()");
1662228072Sbapt			outn ("\t{");
1663228072Sbapt			outn ("\tLexerError( \"yyFlexLexer::yylex invoked but %option yyclass used\" );");
1664228072Sbapt			outn ("\treturn 0;");
1665228072Sbapt			outn ("\t}");
1666228072Sbapt
1667228072Sbapt			out_str ("\n#define YY_DECL int %s::yylex()\n",
1668228072Sbapt				 yyclass);
1669228072Sbapt		}
1670228072Sbapt	}
1671228072Sbapt
1672228072Sbapt	else {
1673228072Sbapt
1674228072Sbapt		/* Watch out: yytext_ptr is a variable when yytext is an array,
1675228072Sbapt		 * but it's a macro when yytext is a pointer.
1676228072Sbapt		 */
1677228072Sbapt		if (yytext_is_array) {
1678228072Sbapt			if (!reentrant)
1679228072Sbapt				outn ("extern char yytext[];\n");
1680228072Sbapt		}
1681228072Sbapt		else {
1682228072Sbapt			if (reentrant) {
1683228072Sbapt				outn ("#define yytext_ptr yytext_r");
1684228072Sbapt			}
1685228072Sbapt			else {
1686228072Sbapt				outn ("extern char *yytext;");
1687228072Sbapt				outn ("#define yytext_ptr yytext");
1688228072Sbapt			}
1689228072Sbapt		}
1690228072Sbapt
1691228072Sbapt		if (yyclass)
1692228072Sbapt			flexerror (_
1693228072Sbapt				   ("%option yyclass only meaningful for C++ scanners"));
1694228072Sbapt	}
1695228072Sbapt
1696228072Sbapt	if (useecs)
1697228072Sbapt		numecs = cre8ecs (nextecm, ecgroup, csize);
1698228072Sbapt	else
1699228072Sbapt		numecs = csize;
1700228072Sbapt
1701228072Sbapt	/* Now map the equivalence class for NUL to its expected place. */
1702228072Sbapt	ecgroup[0] = ecgroup[csize];
1703228072Sbapt	NUL_ec = ABS (ecgroup[0]);
1704228072Sbapt
1705228072Sbapt	if (useecs)
1706228072Sbapt		ccl2ecl ();
1707228072Sbapt}
1708228072Sbapt
1709228072Sbapt
1710228072Sbapt/* set_up_initial_allocations - allocate memory for internal tables */
1711228072Sbapt
1712228072Sbaptvoid set_up_initial_allocations ()
1713228072Sbapt{
1714228072Sbapt	maximum_mns = (long_align ? MAXIMUM_MNS_LONG : MAXIMUM_MNS);
1715228072Sbapt	current_mns = INITIAL_MNS;
1716228072Sbapt	firstst = allocate_integer_array (current_mns);
1717228072Sbapt	lastst = allocate_integer_array (current_mns);
1718228072Sbapt	finalst = allocate_integer_array (current_mns);
1719228072Sbapt	transchar = allocate_integer_array (current_mns);
1720228072Sbapt	trans1 = allocate_integer_array (current_mns);
1721228072Sbapt	trans2 = allocate_integer_array (current_mns);
1722228072Sbapt	accptnum = allocate_integer_array (current_mns);
1723228072Sbapt	assoc_rule = allocate_integer_array (current_mns);
1724228072Sbapt	state_type = allocate_integer_array (current_mns);
1725228072Sbapt
1726228072Sbapt	current_max_rules = INITIAL_MAX_RULES;
1727228072Sbapt	rule_type = allocate_integer_array (current_max_rules);
1728228072Sbapt	rule_linenum = allocate_integer_array (current_max_rules);
1729228072Sbapt	rule_useful = allocate_integer_array (current_max_rules);
1730228072Sbapt	rule_has_nl = allocate_bool_array (current_max_rules);
1731228072Sbapt
1732228072Sbapt	current_max_scs = INITIAL_MAX_SCS;
1733228072Sbapt	scset = allocate_integer_array (current_max_scs);
1734228072Sbapt	scbol = allocate_integer_array (current_max_scs);
1735228072Sbapt	scxclu = allocate_integer_array (current_max_scs);
1736228072Sbapt	sceof = allocate_integer_array (current_max_scs);
1737228072Sbapt	scname = allocate_char_ptr_array (current_max_scs);
1738228072Sbapt
1739228072Sbapt	current_maxccls = INITIAL_MAX_CCLS;
1740228072Sbapt	cclmap = allocate_integer_array (current_maxccls);
1741228072Sbapt	ccllen = allocate_integer_array (current_maxccls);
1742228072Sbapt	cclng = allocate_integer_array (current_maxccls);
1743228072Sbapt	ccl_has_nl = allocate_bool_array (current_maxccls);
1744228072Sbapt
1745228072Sbapt	current_max_ccl_tbl_size = INITIAL_MAX_CCL_TBL_SIZE;
1746228072Sbapt	ccltbl = allocate_Character_array (current_max_ccl_tbl_size);
1747228072Sbapt
1748228072Sbapt	current_max_dfa_size = INITIAL_MAX_DFA_SIZE;
1749228072Sbapt
1750228072Sbapt	current_max_xpairs = INITIAL_MAX_XPAIRS;
1751228072Sbapt	nxt = allocate_integer_array (current_max_xpairs);
1752228072Sbapt	chk = allocate_integer_array (current_max_xpairs);
1753228072Sbapt
1754228072Sbapt	current_max_template_xpairs = INITIAL_MAX_TEMPLATE_XPAIRS;
1755228072Sbapt	tnxt = allocate_integer_array (current_max_template_xpairs);
1756228072Sbapt
1757228072Sbapt	current_max_dfas = INITIAL_MAX_DFAS;
1758228072Sbapt	base = allocate_integer_array (current_max_dfas);
1759228072Sbapt	def = allocate_integer_array (current_max_dfas);
1760228072Sbapt	dfasiz = allocate_integer_array (current_max_dfas);
1761228072Sbapt	accsiz = allocate_integer_array (current_max_dfas);
1762228072Sbapt	dhash = allocate_integer_array (current_max_dfas);
1763228072Sbapt	dss = allocate_int_ptr_array (current_max_dfas);
1764228072Sbapt	dfaacc = allocate_dfaacc_union (current_max_dfas);
1765228072Sbapt
1766228072Sbapt	nultrans = (int *) 0;
1767228072Sbapt}
1768228072Sbapt
1769228072Sbapt
1770228072Sbapt/* extracts basename from path, optionally stripping the extension "\.*"
1771228072Sbapt * (same concept as /bin/sh `basename`, but different handling of extension). */
1772228072Sbaptstatic char *basename2 (path, strip_ext)
1773228072Sbapt     char   *path;
1774228072Sbapt     int strip_ext;		/* boolean */
1775228072Sbapt{
1776228072Sbapt	char   *b, *e = 0;
1777228072Sbapt
1778228072Sbapt	b = path;
1779228072Sbapt	for (b = path; *path; path++)
1780228072Sbapt		if (*path == '/')
1781228072Sbapt			b = path + 1;
1782228072Sbapt		else if (*path == '.')
1783228072Sbapt			e = path;
1784228072Sbapt
1785228072Sbapt	if (strip_ext && e && e > b)
1786228072Sbapt		*e = '\0';
1787228072Sbapt	return b;
1788228072Sbapt}
1789228072Sbapt
1790228072Sbaptvoid usage ()
1791228072Sbapt{
1792228072Sbapt	FILE   *f = stdout;
1793228072Sbapt
1794228072Sbapt	if (!did_outfilename) {
1795228072Sbapt		snprintf (outfile_path, sizeof(outfile_path), outfile_template,
1796228072Sbapt			 prefix, C_plus_plus ? "cc" : "c");
1797228072Sbapt		outfilename = outfile_path;
1798228072Sbapt	}
1799228072Sbapt
1800228072Sbapt	fprintf (f, _("Usage: %s [OPTIONS] [FILE]...\n"), program_name);
1801228072Sbapt	fprintf (f,
1802228072Sbapt		 _
1803228072Sbapt		 ("Generates programs that perform pattern-matching on text.\n"
1804228072Sbapt		  "\n" "Table Compression:\n"
1805228072Sbapt		  "  -Ca, --align      trade off larger tables for better memory alignment\n"
1806228072Sbapt		  "  -Ce, --ecs        construct equivalence classes\n"
1807228072Sbapt		  "  -Cf               do not compress tables; use -f representation\n"
1808228072Sbapt		  "  -CF               do not compress tables; use -F representation\n"
1809228072Sbapt		  "  -Cm, --meta-ecs   construct meta-equivalence classes\n"
1810228072Sbapt		  "  -Cr, --read       use read() instead of stdio for scanner input\n"
1811228072Sbapt		  "  -f, --full        generate fast, large scanner. Same as -Cfr\n"
1812228072Sbapt		  "  -F, --fast        use alternate table representation. Same as -CFr\n"
1813228072Sbapt		  "  -Cem              default compression (same as --ecs --meta-ecs)\n"
1814228072Sbapt		  "\n" "Debugging:\n"
1815228072Sbapt		  "  -d, --debug             enable debug mode in scanner\n"
1816228072Sbapt		  "  -b, --backup            write backing-up information to %s\n"
1817228072Sbapt		  "  -p, --perf-report       write performance report to stderr\n"
1818228072Sbapt		  "  -s, --nodefault         suppress default rule to ECHO unmatched text\n"
1819228072Sbapt		  "  -T, --trace             %s should run in trace mode\n"
1820228072Sbapt		  "  -w, --nowarn            do not generate warnings\n"
1821228072Sbapt		  "  -v, --verbose           write summary of scanner statistics to stdout\n"
1822228072Sbapt		  "\n" "Files:\n"
1823228072Sbapt		  "  -o, --outfile=FILE      specify output filename\n"
1824228072Sbapt		  "  -S, --skel=FILE         specify skeleton file\n"
1825228072Sbapt		  "  -t, --stdout            write scanner on stdout instead of %s\n"
1826228072Sbapt		  "      --yyclass=NAME      name of C++ class\n"
1827228072Sbapt		  "      --header-file=FILE   create a C header file in addition to the scanner\n"
1828228072Sbapt		  "      --tables-file[=FILE] write tables to FILE\n" "\n"
1829228072Sbapt		  "Scanner behavior:\n"
1830228072Sbapt		  "  -7, --7bit              generate 7-bit scanner\n"
1831228072Sbapt		  "  -8, --8bit              generate 8-bit scanner\n"
1832228072Sbapt		  "  -B, --batch             generate batch scanner (opposite of -I)\n"
1833228072Sbapt		  "  -i, --case-insensitive  ignore case in patterns\n"
1834228072Sbapt		  "  -l, --lex-compat        maximal compatibility with original lex\n"
1835228072Sbapt		  "  -X, --posix-compat      maximal compatibility with POSIX lex\n"
1836228072Sbapt		  "  -I, --interactive       generate interactive scanner (opposite of -B)\n"
1837228072Sbapt		  "      --yylineno          track line count in yylineno\n"
1838228072Sbapt		  "\n" "Generated code:\n"
1839228072Sbapt		  "  -+,  --c++               generate C++ scanner class\n"
1840228072Sbapt		  "  -Dmacro[=defn]           #define macro defn  (default defn is '1')\n"
1841228072Sbapt		  "  -L,  --noline            suppress #line directives in scanner\n"
1842228072Sbapt		  "  -P,  --prefix=STRING     use STRING as prefix instead of \"yy\"\n"
1843228072Sbapt		  "  -R,  --reentrant         generate a reentrant C scanner\n"
1844228072Sbapt		  "       --bison-bridge      scanner for bison pure parser.\n"
1845228072Sbapt		  "       --bison-locations   include yylloc support.\n"
1846228072Sbapt		  "       --stdinit           initialize yyin/yyout to stdin/stdout\n"
1847228072Sbapt          "       --noansi-definitions old-style function definitions\n"
1848228072Sbapt          "       --noansi-prototypes  empty parameter list in prototypes\n"
1849228072Sbapt		  "       --nounistd          do not include <unistd.h>\n"
1850228072Sbapt		  "       --noFUNCTION        do not generate a particular FUNCTION\n"
1851228072Sbapt		  "\n" "Miscellaneous:\n"
1852228072Sbapt		  "  -c                      do-nothing POSIX option\n"
1853228072Sbapt		  "  -n                      do-nothing POSIX option\n"
1854228072Sbapt		  "  -?\n"
1855228072Sbapt		  "  -h, --help              produce this help message\n"
1856228072Sbapt		  "  -V, --version           report %s version\n"),
1857228072Sbapt		 backing_name, program_name, outfile_path, program_name);
1858228072Sbapt
1859228072Sbapt}
1860