main.c revision 250125
11573Srgrimes/* flex - tool to generate fast lexical analyzers */ 21573Srgrimes 31573Srgrimes/* Copyright (c) 1990 The Regents of the University of California. */ 41573Srgrimes/* All rights reserved. */ 51573Srgrimes 61573Srgrimes/* This code is derived from software contributed to Berkeley by */ 71573Srgrimes/* Vern Paxson. */ 81573Srgrimes 91573Srgrimes/* The United States Government has rights in this work pursuant */ 101573Srgrimes/* to contract no. DE-AC03-76SF00098 between the United States */ 111573Srgrimes/* Department of Energy and the University of California. */ 121573Srgrimes 131573Srgrimes/* This file is part of flex. */ 141573Srgrimes 151573Srgrimes/* Redistribution and use in source and binary forms, with or without */ 161573Srgrimes/* modification, are permitted provided that the following conditions */ 171573Srgrimes/* are met: */ 181573Srgrimes 191573Srgrimes/* 1. Redistributions of source code must retain the above copyright */ 201573Srgrimes/* notice, this list of conditions and the following disclaimer. */ 211573Srgrimes/* 2. Redistributions in binary form must reproduce the above copyright */ 221573Srgrimes/* notice, this list of conditions and the following disclaimer in the */ 231573Srgrimes/* documentation and/or other materials provided with the distribution. */ 241573Srgrimes 251573Srgrimes/* Neither the name of the University nor the names of its contributors */ 261573Srgrimes/* may be used to endorse or promote products derived from this software */ 271573Srgrimes/* without specific prior written permission. */ 281573Srgrimes 291573Srgrimes/* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */ 301573Srgrimes/* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */ 311573Srgrimes/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */ 321573Srgrimes/* PURPOSE. */ 331573Srgrimes 341573Srgrimes 351573Srgrimes#include "flexdef.h" 361573Srgrimes#include "version.h" 371573Srgrimes#include "options.h" 381573Srgrimes#include "tables.h" 391573Srgrimes 401573Srgrimesstatic char flex_version[] = FLEX_VERSION; 411573Srgrimes 421573Srgrimes/* declare functions that have forward references */ 431573Srgrimes 441573Srgrimesvoid flexinit PROTO ((int, char **)); 451573Srgrimesvoid readin PROTO ((void)); 461573Srgrimesvoid set_up_initial_allocations PROTO ((void)); 471573Srgrimesstatic char *basename2 PROTO ((char *path, int should_strip_ext)); 481573Srgrimes 491573Srgrimes 501573Srgrimes/* these globals are all defined and commented in flexdef.h */ 511573Srgrimesint printstats, syntaxerror, eofseen, ddebug, trace, nowarn, spprdflt; 521573Srgrimesint interactive, lex_compat, posix_compat, do_yylineno, 531573Srgrimes useecs, fulltbl, usemecs; 549978Swpaulint fullspd, gen_line_dirs, performance_report, backing_up_report; 559978Swpaulint C_plus_plus, long_align, use_read, yytext_is_array, do_yywrap, 569978Swpaul csize; 579978Swpaulint reentrant, bison_bridge_lval, bison_bridge_lloc; 589978Swpaulint yymore_used, reject, real_reject, continued_action, in_rule; 591573Srgrimesint yymore_really_used, reject_really_used; 602592Scsgrint datapos, dataline, linenum; 612592ScsgrFILE *skelfile = NULL; 621573Srgrimesint skel_ind = 0; 631573Srgrimeschar *action_array; 641573Srgrimesint action_size, defs1_offset, prolog_offset, action_offset, 651573Srgrimes action_index; 661573Srgrimeschar *infilename = NULL, *outfilename = NULL, *headerfilename = NULL; 671573Srgrimesint did_outfilename; 681573Srgrimeschar *prefix, *yyclass, *extra_type = NULL; 691573Srgrimesint do_stdinit, use_stdout; 701573Srgrimesint onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE]; 711573Srgrimesint onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp; 721573Srgrimesint maximum_mns, current_mns, current_max_rules; 731573Srgrimesint num_rules, num_eof_rules, default_rule, lastnfa; 741573Srgrimesint *firstst, *lastst, *finalst, *transchar, *trans1, *trans2; 751573Srgrimesint *accptnum, *assoc_rule, *state_type; 761573Srgrimesint *rule_type, *rule_linenum, *rule_useful; 771573Srgrimesint current_state_type; 781573Srgrimesint variable_trailing_context_rules; 791573Srgrimesint numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP]; 801573Srgrimesint protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE]; 811573Srgrimesint numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs, 821573Srgrimes tecfwd[CSIZE + 1]; 831573Srgrimesint tecbck[CSIZE + 1]; 841573Srgrimesint lastsc, *scset, *scbol, *scxclu, *sceof; 851573Srgrimesint current_max_scs; 861573Srgrimeschar **scname; 871573Srgrimesint current_max_dfa_size, current_max_xpairs; 881573Srgrimesint current_max_template_xpairs, current_max_dfas; 891573Srgrimesint lastdfa, *nxt, *chk, *tnxt; 901573Srgrimesint *base, *def, *nultrans, NUL_ec, tblend, firstfree, **dss, *dfasiz; 911573Srgrimesunion dfaacc_union *dfaacc; 921573Srgrimesint *accsiz, *dhash, numas; 931573Srgrimesint numsnpairs, jambase, jamstate; 941573Srgrimesint lastccl, *cclmap, *ccllen, *cclng, cclreuse; 951573Srgrimesint current_maxccls, current_max_ccl_tbl_size; 961573SrgrimesChar *ccltbl; 971573Srgrimeschar nmstr[MAXLINE]; 981573Srgrimesint sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs; 991573Srgrimesint tmpuses, totnst, peakpairs, numuniq, numdup, hshsave; 1001573Srgrimesint num_backing_up, bol_needed; 1011573SrgrimesFILE *backing_up_file; 1021573Srgrimesint end_of_buffer_state; 1031573Srgrimeschar **input_files; 1041573Srgrimesint num_input_files; 1051573Srgrimesjmp_buf flex_main_jmp_buf; 1061573Srgrimesbool *rule_has_nl, *ccl_has_nl; 1071573Srgrimesint nlch = '\n'; 1081573Srgrimesbool ansi_func_defs, ansi_func_protos; 1091573Srgrimes 1101573Srgrimesbool tablesext, tablesverify, gentables; 1111573Srgrimeschar *tablesfilename=0,*tablesname=0; 1121573Srgrimesstruct yytbl_writer tableswr; 1131573Srgrimes 1141573Srgrimes/* Make sure program_name is initialized so we don't crash if writing 1151573Srgrimes * out an error message before getting the program name from argv[0]. 1161573Srgrimes */ 1171573Srgrimeschar *program_name = "flex"; 1181573Srgrimes 1191573Srgrimes#ifndef SHORT_FILE_NAMES 1201573Srgrimesstatic char *outfile_template = "lex.%s.%s"; 1211573Srgrimesstatic char *backing_name = "lex.backup"; 1221573Srgrimesstatic char *tablesfile_template = "lex.%s.tables"; 1231573Srgrimes#else 1241573Srgrimesstatic char *outfile_template = "lex%s.%s"; 1251573Srgrimesstatic char *backing_name = "lex.bck"; 1261573Srgrimesstatic char *tablesfile_template = "lex%s.tbl"; 1271573Srgrimes#endif 1281573Srgrimes 1291573Srgrimes#ifdef MS_DOS 1301573Srgrimesextern unsigned _stklen = 16384; 1311573Srgrimes#endif 1321573Srgrimes 1331573Srgrimes/* From scan.l */ 1341573Srgrimesextern FILE* yyout; 1351573Srgrimes 1361573Srgrimesstatic char outfile_path[MAXLINE]; 1371573Srgrimesstatic int outfile_created = 0; 1381573Srgrimesstatic char *skelname = NULL; 1391573Srgrimesstatic int _stdout_closed = 0; /* flag to prevent double-fclose() on stdout. */ 1401573Srgrimesconst char *escaped_qstart = "[[]]M4_YY_NOOP[M4_YY_NOOP[M4_YY_NOOP[[]]"; 1412592Scsgrconst char *escaped_qend = "[[]]M4_YY_NOOP]M4_YY_NOOP]M4_YY_NOOP[[]]"; 1421573Srgrimes 1431573Srgrimes/* For debugging. The max number of filters to apply to skeleton. */ 1441573Srgrimesstatic int preproc_level = 1000; 1451573Srgrimes 1461573Srgrimesint flex_main PROTO ((int argc, char *argv[])); 1471573Srgrimesint main PROTO ((int argc, char *argv[])); 1481573Srgrimes 1491573Srgrimesint flex_main (argc, argv) 1501573Srgrimes int argc; 1511573Srgrimes char *argv[]; 1521573Srgrimes{ 1531573Srgrimes int i, exit_status, child_status; 1542592Scsgr 1552592Scsgr /* Set a longjmp target. Yes, I know it's a hack, but it gets worse: The 1562592Scsgr * return value of setjmp, if non-zero, is the desired exit code PLUS ONE. 1572592Scsgr * For example, if you want 'main' to return with code '2', then call 1582592Scsgr * longjmp() with an argument of 3. This is because it is invalid to 1592592Scsgr * specify a value of 0 to longjmp. FLEX_EXIT(n) should be used instead of 1601573Srgrimes * exit(n); 1611573Srgrimes */ 1621573Srgrimes exit_status = setjmp (flex_main_jmp_buf); 1631573Srgrimes if (exit_status){ 1642592Scsgr if (stdout && !_stdout_closed && !ferror(stdout)){ 1651573Srgrimes fflush(stdout); 1661573Srgrimes fclose(stdout); 1671573Srgrimes } 1681573Srgrimes while (wait(&child_status) > 0){ 1691573Srgrimes if (!WIFEXITED (child_status) 1701573Srgrimes || WEXITSTATUS (child_status) != 0){ 1711573Srgrimes /* report an error of a child 1721573Srgrimes */ 1731573Srgrimes if( exit_status <= 1 ) 1741573Srgrimes exit_status = 2; 1751573Srgrimes 1761573Srgrimes } 1771573Srgrimes } 1781573Srgrimes return exit_status - 1; 1791573Srgrimes } 1801573Srgrimes 1811573Srgrimes flexinit (argc, argv); 1821573Srgrimes 1831573Srgrimes readin (); 1841573Srgrimes 1851573Srgrimes ntod (); 1861573Srgrimes 1871573Srgrimes for (i = 1; i <= num_rules; ++i) 1881573Srgrimes if (!rule_useful[i] && i != default_rule) 1891573Srgrimes line_warning (_("rule cannot be matched"), 1901573Srgrimes rule_linenum[i]); 1911573Srgrimes 1921573Srgrimes if (spprdflt && !reject && rule_useful[default_rule]) 1931573Srgrimes line_warning (_ 1941573Srgrimes ("-s option given but default rule can be matched"), 1951573Srgrimes rule_linenum[default_rule]); 1961573Srgrimes 1971573Srgrimes /* Generate the C state transition tables from the DFA. */ 1981573Srgrimes make_tables (); 1991573Srgrimes 2001573Srgrimes /* Note, flexend does not return. It exits with its argument 2011573Srgrimes * as status. 2021573Srgrimes */ 2031573Srgrimes flexend (0); 2041573Srgrimes 2051573Srgrimes return 0; /* keep compilers/lint happy */ 2061573Srgrimes} 2071573Srgrimes 2081573Srgrimes/* Wrapper around flex_main, so flex_main can be built as a library. */ 2091573Srgrimesint main (argc, argv) 2101573Srgrimes int argc; 2111573Srgrimes char *argv[]; 2121573Srgrimes{ 2131573Srgrimes#if ENABLE_NLS 2141573Srgrimes#if HAVE_LOCALE_H 2151573Srgrimes setlocale (LC_MESSAGES, ""); 2161573Srgrimes setlocale (LC_CTYPE, ""); 2171573Srgrimes textdomain (PACKAGE); 2181573Srgrimes bindtextdomain (PACKAGE, LOCALEDIR); 2191573Srgrimes#endif 2201573Srgrimes#endif 2211573Srgrimes 2221573Srgrimes return flex_main (argc, argv); 2231573Srgrimes} 2241573Srgrimes 2251573Srgrimes/* check_options - check user-specified options */ 2261573Srgrimes 2271573Srgrimesvoid check_options () 2281573Srgrimes{ 2291573Srgrimes int i; 2301573Srgrimes const char * m4 = NULL; 2311573Srgrimes 2321573Srgrimes if (lex_compat) { 2331573Srgrimes if (C_plus_plus) 2341573Srgrimes flexerror (_("Can't use -+ with -l option")); 2351573Srgrimes 2361573Srgrimes if (fulltbl || fullspd) 2371573Srgrimes flexerror (_("Can't use -f or -F with -l option")); 2381573Srgrimes 2391573Srgrimes if (reentrant || bison_bridge_lval) 2401573Srgrimes flexerror (_ 2411573Srgrimes ("Can't use --reentrant or --bison-bridge with -l option")); 2421573Srgrimes 2431573Srgrimes /* Don't rely on detecting use of yymore() and REJECT, 2441573Srgrimes * just assume they'll be used. 2451573Srgrimes */ 2461573Srgrimes yymore_really_used = reject_really_used = true; 2471573Srgrimes 2481573Srgrimes yytext_is_array = true; 2491573Srgrimes do_yylineno = true; 2501573Srgrimes use_read = false; 2511573Srgrimes } 2521573Srgrimes 2531573Srgrimes 2541573Srgrimes#if 0 2551573Srgrimes /* This makes no sense whatsoever. I'm removing it. */ 2561573Srgrimes if (do_yylineno) 2571573Srgrimes /* This should really be "maintain_backup_tables = true" */ 2581573Srgrimes reject_really_used = true; 2591573Srgrimes#endif 2601573Srgrimes 2611573Srgrimes if (csize == unspecified) { 2621573Srgrimes if ((fulltbl || fullspd) && !useecs) 2631573Srgrimes csize = DEFAULT_CSIZE; 2641573Srgrimes else 2651573Srgrimes csize = CSIZE; 2661573Srgrimes } 2671573Srgrimes 2681573Srgrimes if (interactive == unspecified) { 2691573Srgrimes if (fulltbl || fullspd) 2701573Srgrimes interactive = false; 2711573Srgrimes else 2721573Srgrimes interactive = true; 2731573Srgrimes } 2741573Srgrimes 2751573Srgrimes if (fulltbl || fullspd) { 2761573Srgrimes if (usemecs) 2771573Srgrimes flexerror (_ 2781573Srgrimes ("-Cf/-CF and -Cm don't make sense together")); 2791573Srgrimes 2801573Srgrimes if (interactive) 2811573Srgrimes flexerror (_("-Cf/-CF and -I are incompatible")); 2821573Srgrimes 2831573Srgrimes if (lex_compat) 2841573Srgrimes flexerror (_ 2851573Srgrimes ("-Cf/-CF are incompatible with lex-compatibility mode")); 2861573Srgrimes 2871573Srgrimes 2881573Srgrimes if (fulltbl && fullspd) 2891573Srgrimes flexerror (_ 2901573Srgrimes ("-Cf and -CF are mutually exclusive")); 2911573Srgrimes } 2921573Srgrimes 2931573Srgrimes if (C_plus_plus && fullspd) 2941573Srgrimes flexerror (_("Can't use -+ with -CF option")); 2951573Srgrimes 2961573Srgrimes if (C_plus_plus && yytext_is_array) { 2971573Srgrimes warn (_("%array incompatible with -+ option")); 2981573Srgrimes yytext_is_array = false; 2991573Srgrimes } 3001573Srgrimes 3011573Srgrimes if (C_plus_plus && (reentrant)) 3021573Srgrimes flexerror (_("Options -+ and --reentrant are mutually exclusive.")); 3031573Srgrimes 3041573Srgrimes if (C_plus_plus && bison_bridge_lval) 3051573Srgrimes flexerror (_("bison bridge not supported for the C++ scanner.")); 3061573Srgrimes 3071573Srgrimes 3081573Srgrimes if (useecs) { /* Set up doubly-linked equivalence classes. */ 3091573Srgrimes 3101573Srgrimes /* We loop all the way up to csize, since ecgroup[csize] is 3111573Srgrimes * the position used for NUL characters. 3121573Srgrimes */ 3131573Srgrimes ecgroup[1] = NIL; 3141573Srgrimes 3151573Srgrimes for (i = 2; i <= csize; ++i) { 3161573Srgrimes ecgroup[i] = i - 1; 3171573Srgrimes nextecm[i - 1] = i; 3181573Srgrimes } 3191573Srgrimes 3201573Srgrimes nextecm[csize] = NIL; 3211573Srgrimes } 3221573Srgrimes 3231573Srgrimes else { 3241573Srgrimes /* Put everything in its own equivalence class. */ 3251573Srgrimes for (i = 1; i <= csize; ++i) { 3261573Srgrimes ecgroup[i] = i; 3271573Srgrimes nextecm[i] = BAD_SUBSCRIPT; /* to catch errors */ 3281573Srgrimes } 3291573Srgrimes } 3301573Srgrimes 3311573Srgrimes if (!ansi_func_defs) 3321573Srgrimes buf_m4_define( &m4defs_buf, "M4_YY_NO_ANSI_FUNC_DEFS", NULL); 3331573Srgrimes 3341573Srgrimes if (!ansi_func_protos) 3351573Srgrimes buf_m4_define( &m4defs_buf, "M4_YY_NO_ANSI_FUNC_PROTOS", NULL); 3361573Srgrimes 3371573Srgrimes if (extra_type) 3381573Srgrimes buf_m4_define( &m4defs_buf, "M4_EXTRA_TYPE_DEFS", extra_type); 3391573Srgrimes 3401573Srgrimes if (!use_stdout) { 3411573Srgrimes FILE *prev_stdout; 3421573Srgrimes 3431573Srgrimes if (!did_outfilename) { 3441573Srgrimes char *suffix; 3451573Srgrimes 3461573Srgrimes if (C_plus_plus) 3471573Srgrimes suffix = "cc"; 3481573Srgrimes else 3491573Srgrimes suffix = "c"; 3501573Srgrimes 3511573Srgrimes snprintf (outfile_path, sizeof(outfile_path), outfile_template, 3521573Srgrimes prefix, suffix); 3531573Srgrimes 3541573Srgrimes outfilename = outfile_path; 3551573Srgrimes } 3561573Srgrimes 3571573Srgrimes prev_stdout = freopen (outfilename, "w+", stdout); 3581573Srgrimes 3591573Srgrimes if (prev_stdout == NULL) 3601573Srgrimes lerrsf (_("could not create %s"), outfilename); 3611573Srgrimes 3621573Srgrimes outfile_created = 1; 3631573Srgrimes } 36410059Swpaul 3657183Swpaul 3667183Swpaul /* Setup the filter chain. */ 3677183Swpaul output_chain = filter_create_int(NULL, filter_tee_header, headerfilename); 3689978Swpaul if ( !(m4 = getenv("M4"))) 3699978Swpaul m4 = M4; 3701573Srgrimes filter_create_ext(output_chain, m4, "-P", 0); 3719978Swpaul filter_create_int(output_chain, filter_fix_linedirs, NULL); 3729978Swpaul 3739978Swpaul /* For debugging, only run the requested number of filters. */ 3749978Swpaul if (preproc_level > 0) { 3759978Swpaul filter_truncate(output_chain, preproc_level); 3767183Swpaul filter_apply_chain(output_chain); 3777183Swpaul } 3787183Swpaul yyout = stdout; 3797183Swpaul 38010059Swpaul 3817183Swpaul /* always generate the tablesverify flag. */ 3821573Srgrimes buf_m4_define (&m4defs_buf, "M4_YY_TABLES_VERIFY", tablesverify ? "1" : "0"); 3831573Srgrimes if (tablesext) 3841573Srgrimes gentables = false; 3851573Srgrimes 3861573Srgrimes if (tablesverify) 3871573Srgrimes /* force generation of C tables. */ 3881573Srgrimes gentables = true; 3899552Speter 3909552Speter 3919552Speter if (tablesext) { 3929552Speter FILE *tablesout; 3931573Srgrimes struct yytbl_hdr hdr; 3941573Srgrimes char *pname = 0; 3951573Srgrimes int nbytes = 0; 3961573Srgrimes 3971573Srgrimes buf_m4_define (&m4defs_buf, "M4_YY_TABLES_EXTERNAL", NULL); 3981573Srgrimes 3991573Srgrimes if (!tablesfilename) { 4001573Srgrimes nbytes = strlen (prefix) + strlen (tablesfile_template) + 2; 4011573Srgrimes tablesfilename = pname = (char *) calloc (nbytes, 1); 4021573Srgrimes snprintf (pname, nbytes, tablesfile_template, prefix); 4031573Srgrimes } 4041573Srgrimes 4051573Srgrimes if ((tablesout = fopen (tablesfilename, "w")) == NULL) 4061573Srgrimes lerrsf (_("could not create %s"), tablesfilename); 4071573Srgrimes if (pname) 4087183Swpaul free (pname); 4097183Swpaul tablesfilename = 0; 4107183Swpaul 4117183Swpaul yytbl_writer_init (&tableswr, tablesout); 4127183Swpaul 4137183Swpaul nbytes = strlen (prefix) + strlen ("tables") + 2; 4147183Swpaul tablesname = (char *) calloc (nbytes, 1); 4157183Swpaul snprintf (tablesname, nbytes, "%stables", prefix); 4167183Swpaul yytbl_hdr_init (&hdr, flex_version, tablesname); 4177183Swpaul 4187183Swpaul if (yytbl_hdr_fwrite (&tableswr, &hdr) <= 0) 41910059Swpaul flexerror (_("could not write tables header")); 42010059Swpaul } 4217183Swpaul 4227183Swpaul if (skelname && (skelfile = fopen (skelname, "r")) == NULL) 4237183Swpaul lerrsf (_("can't open skeleton file %s"), skelname); 4247183Swpaul 4257183Swpaul if (reentrant) { 4267183Swpaul buf_m4_define (&m4defs_buf, "M4_YY_REENTRANT", NULL); 42710059Swpaul if (yytext_is_array) 4287183Swpaul buf_m4_define (&m4defs_buf, "M4_YY_TEXT_IS_ARRAY", NULL); 4297183Swpaul } 4307183Swpaul 4317183Swpaul if ( bison_bridge_lval) 4327183Swpaul buf_m4_define (&m4defs_buf, "M4_YY_BISON_LVAL", NULL); 4337183Swpaul 4347183Swpaul if ( bison_bridge_lloc) 4357183Swpaul buf_m4_define (&m4defs_buf, "<M4_YY_BISON_LLOC>", NULL); 4367183Swpaul 4371573Srgrimes buf_m4_define(&m4defs_buf, "M4_YY_PREFIX", prefix); 4387183Swpaul 4397183Swpaul if (did_outfilename) 4407183Swpaul line_directive_out (stdout, 0); 4417183Swpaul 4427183Swpaul if (do_yylineno) 4437183Swpaul buf_m4_define (&m4defs_buf, "M4_YY_USE_LINENO", NULL); 4447183Swpaul 4459978Swpaul /* Create the alignment type. */ 4467183Swpaul buf_strdefine (&userdef_buf, "YY_INT_ALIGNED", 4477183Swpaul long_align ? "long int" : "short int"); 4487183Swpaul 4497183Swpaul /* Define the start condition macros. */ 4507183Swpaul { 4517183Swpaul struct Buf tmpbuf; 4527183Swpaul buf_init(&tmpbuf, sizeof(char)); 4537183Swpaul for (i = 1; i <= lastsc; i++) { 4547183Swpaul char *str, *fmt = "#define %s %d\n"; 4559978Swpaul size_t strsz; 4567183Swpaul 4577183Swpaul str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(scname[i]) + (int)(1 + log10(i)) + 2); 4587183Swpaul if (!str) 4597183Swpaul flexfatal(_("allocation of macro definition failed")); 4607183Swpaul snprintf(str, strsz, fmt, scname[i], i - 1); 4617183Swpaul buf_strappend(&tmpbuf, str); 4627183Swpaul free(str); 4637183Swpaul } 4647183Swpaul buf_m4_define(&m4defs_buf, "M4_YY_SC_DEFS", tmpbuf.elts); 4657183Swpaul buf_destroy(&tmpbuf); 4667183Swpaul } 4677183Swpaul 4687183Swpaul /* This is where we begin writing to the file. */ 4697183Swpaul 4701573Srgrimes /* Dump the %top code. */ 4711573Srgrimes if( top_buf.elts) 4721573Srgrimes outn((char*) top_buf.elts); 4731573Srgrimes 4741573Srgrimes /* Dump the m4 definitions. */ 4751573Srgrimes buf_print_strings(&m4defs_buf, stdout); 4761573Srgrimes m4defs_buf.nelts = 0; /* memory leak here. */ 4771573Srgrimes 4781573Srgrimes /* Place a bogus line directive, it will be fixed in the filter. */ 4791573Srgrimes outn("#line 0 \"M4_YY_OUTFILE_NAME\"\n"); 4801573Srgrimes 4811573Srgrimes /* Dump the user defined preproc directives. */ 4821573Srgrimes if (userdef_buf.elts) 4831573Srgrimes outn ((char *) (userdef_buf.elts)); 4841573Srgrimes 4851573Srgrimes skelout (); 4861573Srgrimes /* %% [1.0] */ 4871573Srgrimes} 4881573Srgrimes 4891573Srgrimes/* flexend - terminate flex 4901573Srgrimes * 4911573Srgrimes * note 4921573Srgrimes * This routine does not return. 4931573Srgrimes */ 4941573Srgrimes 4951573Srgrimesvoid flexend (exit_status) 4961573Srgrimes int exit_status; 4971573Srgrimes 4981573Srgrimes{ 4991573Srgrimes static int called_before = -1; /* prevent infinite recursion. */ 5001573Srgrimes int tblsiz; 5011573Srgrimes 502 if (++called_before) 503 FLEX_EXIT (exit_status); 504 505 if (skelfile != NULL) { 506 if (ferror (skelfile)) 507 lerrsf (_("input error reading skeleton file %s"), 508 skelname); 509 510 else if (fclose (skelfile)) 511 lerrsf (_("error closing skeleton file %s"), 512 skelname); 513 } 514 515#if 0 516 fprintf (header_out, 517 "#ifdef YY_HEADER_EXPORT_START_CONDITIONS\n"); 518 fprintf (header_out, 519 "/* Beware! Start conditions are not prefixed. */\n"); 520 521 /* Special case for "INITIAL" */ 522 fprintf (header_out, 523 "#undef INITIAL\n#define INITIAL 0\n"); 524 for (i = 2; i <= lastsc; i++) 525 fprintf (header_out, "#define %s %d\n", scname[i], i - 1); 526 fprintf (header_out, 527 "#endif /* YY_HEADER_EXPORT_START_CONDITIONS */\n\n"); 528 529 /* Kill ALL flex-related macros. This is so the user 530 * can #include more than one generated header file. */ 531 fprintf (header_out, "#ifndef YY_HEADER_NO_UNDEFS\n"); 532 fprintf (header_out, 533 "/* Undefine all internal macros, etc., that do no belong in the header. */\n\n"); 534 535 { 536 const char * undef_list[] = { 537 538 "BEGIN", 539 "ECHO", 540 "EOB_ACT_CONTINUE_SCAN", 541 "EOB_ACT_END_OF_FILE", 542 "EOB_ACT_LAST_MATCH", 543 "FLEX_SCANNER", 544 "FLEX_STD", 545 "REJECT", 546 "YYFARGS0", 547 "YYFARGS1", 548 "YYFARGS2", 549 "YYFARGS3", 550 "YYLMAX", 551 "YYSTATE", 552 "YY_AT_BOL", 553 "YY_BREAK", 554 "YY_BUFFER_EOF_PENDING", 555 "YY_BUFFER_NEW", 556 "YY_BUFFER_NORMAL", 557 "YY_BUF_SIZE", 558 "M4_YY_CALL_LAST_ARG", 559 "M4_YY_CALL_ONLY_ARG", 560 "YY_CURRENT_BUFFER", 561 "YY_DECL", 562 "M4_YY_DECL_LAST_ARG", 563 "M4_YY_DEF_LAST_ARG", 564 "M4_YY_DEF_ONLY_ARG", 565 "YY_DO_BEFORE_ACTION", 566 "YY_END_OF_BUFFER", 567 "YY_END_OF_BUFFER_CHAR", 568 "YY_EXIT_FAILURE", 569 "YY_EXTRA_TYPE", 570 "YY_FATAL_ERROR", 571 "YY_FLEX_DEFINED_ECHO", 572 "YY_FLEX_LEX_COMPAT", 573 "YY_FLEX_MAJOR_VERSION", 574 "YY_FLEX_MINOR_VERSION", 575 "YY_FLEX_SUBMINOR_VERSION", 576 "YY_FLUSH_BUFFER", 577 "YY_G", 578 "YY_INPUT", 579 "YY_INTERACTIVE", 580 "YY_INT_ALIGNED", 581 "YY_LAST_ARG", 582 "YY_LESS_LINENO", 583 "YY_LEX_ARGS", 584 "YY_LEX_DECLARATION", 585 "YY_LEX_PROTO", 586 "YY_MAIN", 587 "YY_MORE_ADJ", 588 "YY_NEED_STRLEN", 589 "YY_NEW_FILE", 590 "YY_NULL", 591 "YY_NUM_RULES", 592 "YY_ONLY_ARG", 593 "YY_PARAMS", 594 "YY_PROTO", 595 "M4_YY_PROTO_LAST_ARG", 596 "M4_YY_PROTO_ONLY_ARG void", 597 "YY_READ_BUF_SIZE", 598 "YY_REENTRANT", 599 "YY_RESTORE_YY_MORE_OFFSET", 600 "YY_RULE_SETUP", 601 "YY_SC_TO_UI", 602 "YY_SKIP_YYWRAP", 603 "YY_START", 604 "YY_START_STACK_INCR", 605 "YY_STATE_EOF", 606 "YY_STDINIT", 607 "YY_TRAILING_HEAD_MASK", 608 "YY_TRAILING_MASK", 609 "YY_USER_ACTION", 610 "YY_USE_CONST", 611 "YY_USE_PROTOS", 612 "unput", 613 "yyTABLES_NAME", 614 "yy_create_buffer", 615 "yy_delete_buffer", 616 "yy_flex_debug", 617 "yy_flush_buffer", 618 "yy_init_buffer", 619 "yy_load_buffer_state", 620 "yy_new_buffer", 621 "yy_scan_buffer", 622 "yy_scan_bytes", 623 "yy_scan_string", 624 "yy_set_bol", 625 "yy_set_interactive", 626 "yy_switch_to_buffer", 627 "yypush_buffer_state", 628 "yypop_buffer_state", 629 "yyensure_buffer_stack", 630 "yyalloc", 631 "yyconst", 632 "yyextra", 633 "yyfree", 634 "yyget_debug", 635 "yyget_extra", 636 "yyget_in", 637 "yyget_leng", 638 "yyget_lineno", 639 "yyget_lloc", 640 "yyget_lval", 641 "yyget_out", 642 "yyget_text", 643 "yyin", 644 "yyleng", 645 "yyless", 646 "yylex", 647 "yylex_destroy", 648 "yylex_init", 649 "yylex_init_extra", 650 "yylineno", 651 "yylloc", 652 "yylval", 653 "yymore", 654 "yyout", 655 "yyrealloc", 656 "yyrestart", 657 "yyset_debug", 658 "yyset_extra", 659 "yyset_in", 660 "yyset_lineno", 661 "yyset_lloc", 662 "yyset_lval", 663 "yyset_out", 664 "yytables_destroy", 665 "yytables_fload", 666 "yyterminate", 667 "yytext", 668 "yytext_ptr", 669 "yywrap", 670 671 /* must be null-terminated */ 672 NULL}; 673 674 675 for (i=0; undef_list[i] != NULL; i++) 676 fprintf (header_out, "#undef %s\n", undef_list[i]); 677 } 678 679 /* undef any of the auto-generated symbols. */ 680 for (i = 0; i < defs_buf.nelts; i++) { 681 682 /* don't undef start conditions */ 683 if (sclookup (((char **) defs_buf.elts)[i]) > 0) 684 continue; 685 fprintf (header_out, "#undef %s\n", 686 ((char **) defs_buf.elts)[i]); 687 } 688 689 fprintf (header_out, 690 "#endif /* !YY_HEADER_NO_UNDEFS */\n"); 691 fprintf (header_out, "\n"); 692 fprintf (header_out, "#undef %sIN_HEADER\n", prefix); 693 fprintf (header_out, "#endif /* %sHEADER_H */\n", prefix); 694 695 if (ferror (header_out)) 696 lerrsf (_("error creating header file %s"), 697 headerfilename); 698 fflush (header_out); 699 fclose (header_out); 700#endif 701 702 if (exit_status != 0 && outfile_created) { 703 if (ferror (stdout)) 704 lerrsf (_("error writing output file %s"), 705 outfilename); 706 707 else if ((_stdout_closed = 1) && fclose (stdout)) 708 lerrsf (_("error closing output file %s"), 709 outfilename); 710 711 else if (unlink (outfilename)) 712 lerrsf (_("error deleting output file %s"), 713 outfilename); 714 } 715 716 717 if (backing_up_report && backing_up_file) { 718 if (num_backing_up == 0) 719 fprintf (backing_up_file, _("No backing up.\n")); 720 else if (fullspd || fulltbl) 721 fprintf (backing_up_file, 722 _ 723 ("%d backing up (non-accepting) states.\n"), 724 num_backing_up); 725 else 726 fprintf (backing_up_file, 727 _("Compressed tables always back up.\n")); 728 729 if (ferror (backing_up_file)) 730 lerrsf (_("error writing backup file %s"), 731 backing_name); 732 733 else if (fclose (backing_up_file)) 734 lerrsf (_("error closing backup file %s"), 735 backing_name); 736 } 737 738 if (printstats) { 739 fprintf (stderr, _("%s version %s usage statistics:\n"), 740 program_name, flex_version); 741 742 fprintf (stderr, _(" scanner options: -")); 743 744 if (C_plus_plus) 745 putc ('+', stderr); 746 if (backing_up_report) 747 putc ('b', stderr); 748 if (ddebug) 749 putc ('d', stderr); 750 if (sf_case_ins()) 751 putc ('i', stderr); 752 if (lex_compat) 753 putc ('l', stderr); 754 if (posix_compat) 755 putc ('X', stderr); 756 if (performance_report > 0) 757 putc ('p', stderr); 758 if (performance_report > 1) 759 putc ('p', stderr); 760 if (spprdflt) 761 putc ('s', stderr); 762 if (reentrant) 763 fputs ("--reentrant", stderr); 764 if (bison_bridge_lval) 765 fputs ("--bison-bridge", stderr); 766 if (bison_bridge_lloc) 767 fputs ("--bison-locations", stderr); 768 if (use_stdout) 769 putc ('t', stderr); 770 if (printstats) 771 putc ('v', stderr); /* always true! */ 772 if (nowarn) 773 putc ('w', stderr); 774 if (interactive == false) 775 putc ('B', stderr); 776 if (interactive == true) 777 putc ('I', stderr); 778 if (!gen_line_dirs) 779 putc ('L', stderr); 780 if (trace) 781 putc ('T', stderr); 782 783 if (csize == unspecified) 784 /* We encountered an error fairly early on, so csize 785 * never got specified. Define it now, to prevent 786 * bogus table sizes being written out below. 787 */ 788 csize = 256; 789 790 if (csize == 128) 791 putc ('7', stderr); 792 else 793 putc ('8', stderr); 794 795 fprintf (stderr, " -C"); 796 797 if (long_align) 798 putc ('a', stderr); 799 if (fulltbl) 800 putc ('f', stderr); 801 if (fullspd) 802 putc ('F', stderr); 803 if (useecs) 804 putc ('e', stderr); 805 if (usemecs) 806 putc ('m', stderr); 807 if (use_read) 808 putc ('r', stderr); 809 810 if (did_outfilename) 811 fprintf (stderr, " -o%s", outfilename); 812 813 if (skelname) 814 fprintf (stderr, " -S%s", skelname); 815 816 if (strcmp (prefix, "yy")) 817 fprintf (stderr, " -P%s", prefix); 818 819 putc ('\n', stderr); 820 821 fprintf (stderr, _(" %d/%d NFA states\n"), 822 lastnfa, current_mns); 823 fprintf (stderr, _(" %d/%d DFA states (%d words)\n"), 824 lastdfa, current_max_dfas, totnst); 825 fprintf (stderr, _(" %d rules\n"), 826 num_rules + num_eof_rules - 827 1 /* - 1 for def. rule */ ); 828 829 if (num_backing_up == 0) 830 fprintf (stderr, _(" No backing up\n")); 831 else if (fullspd || fulltbl) 832 fprintf (stderr, 833 _ 834 (" %d backing-up (non-accepting) states\n"), 835 num_backing_up); 836 else 837 fprintf (stderr, 838 _ 839 (" Compressed tables always back-up\n")); 840 841 if (bol_needed) 842 fprintf (stderr, 843 _(" Beginning-of-line patterns used\n")); 844 845 fprintf (stderr, _(" %d/%d start conditions\n"), lastsc, 846 current_max_scs); 847 fprintf (stderr, 848 _ 849 (" %d epsilon states, %d double epsilon states\n"), 850 numeps, eps2); 851 852 if (lastccl == 0) 853 fprintf (stderr, _(" no character classes\n")); 854 else 855 fprintf (stderr, 856 _ 857 (" %d/%d character classes needed %d/%d words of storage, %d reused\n"), 858 lastccl, current_maxccls, 859 cclmap[lastccl] + ccllen[lastccl], 860 current_max_ccl_tbl_size, cclreuse); 861 862 fprintf (stderr, _(" %d state/nextstate pairs created\n"), 863 numsnpairs); 864 fprintf (stderr, 865 _(" %d/%d unique/duplicate transitions\n"), 866 numuniq, numdup); 867 868 if (fulltbl) { 869 tblsiz = lastdfa * numecs; 870 fprintf (stderr, _(" %d table entries\n"), 871 tblsiz); 872 } 873 874 else { 875 tblsiz = 2 * (lastdfa + numtemps) + 2 * tblend; 876 877 fprintf (stderr, 878 _(" %d/%d base-def entries created\n"), 879 lastdfa + numtemps, current_max_dfas); 880 fprintf (stderr, 881 _ 882 (" %d/%d (peak %d) nxt-chk entries created\n"), 883 tblend, current_max_xpairs, peakpairs); 884 fprintf (stderr, 885 _ 886 (" %d/%d (peak %d) template nxt-chk entries created\n"), 887 numtemps * nummecs, 888 current_max_template_xpairs, 889 numtemps * numecs); 890 fprintf (stderr, _(" %d empty table entries\n"), 891 nummt); 892 fprintf (stderr, _(" %d protos created\n"), 893 numprots); 894 fprintf (stderr, 895 _(" %d templates created, %d uses\n"), 896 numtemps, tmpuses); 897 } 898 899 if (useecs) { 900 tblsiz = tblsiz + csize; 901 fprintf (stderr, 902 _ 903 (" %d/%d equivalence classes created\n"), 904 numecs, csize); 905 } 906 907 if (usemecs) { 908 tblsiz = tblsiz + numecs; 909 fprintf (stderr, 910 _ 911 (" %d/%d meta-equivalence classes created\n"), 912 nummecs, csize); 913 } 914 915 fprintf (stderr, 916 _ 917 (" %d (%d saved) hash collisions, %d DFAs equal\n"), 918 hshcol, hshsave, dfaeql); 919 fprintf (stderr, _(" %d sets of reallocations needed\n"), 920 num_reallocs); 921 fprintf (stderr, _(" %d total table entries needed\n"), 922 tblsiz); 923 } 924 925 FLEX_EXIT (exit_status); 926} 927 928 929/* flexinit - initialize flex */ 930 931void flexinit (argc, argv) 932 int argc; 933 char **argv; 934{ 935 int i, sawcmpflag, rv, optind; 936 char *arg; 937 scanopt_t sopt; 938 939 printstats = syntaxerror = trace = spprdflt = false; 940 lex_compat = posix_compat = C_plus_plus = backing_up_report = 941 ddebug = fulltbl = false; 942 fullspd = long_align = nowarn = yymore_used = continued_action = 943 false; 944 do_yylineno = yytext_is_array = in_rule = reject = do_stdinit = 945 false; 946 yymore_really_used = reject_really_used = unspecified; 947 interactive = csize = unspecified; 948 do_yywrap = gen_line_dirs = usemecs = useecs = true; 949 reentrant = bison_bridge_lval = bison_bridge_lloc = false; 950 performance_report = 0; 951 did_outfilename = 0; 952 prefix = "yy"; 953 yyclass = 0; 954 use_read = use_stdout = false; 955 tablesext = tablesverify = false; 956 gentables = true; 957 tablesfilename = tablesname = NULL; 958 ansi_func_defs = ansi_func_protos = true; 959 960 sawcmpflag = false; 961 962 /* Initialize dynamic array for holding the rule actions. */ 963 action_size = 2048; /* default size of action array in bytes */ 964 action_array = allocate_character_array (action_size); 965 defs1_offset = prolog_offset = action_offset = action_index = 0; 966 action_array[0] = '\0'; 967 968 /* Initialize any buffers. */ 969 buf_init (&userdef_buf, sizeof (char)); /* one long string */ 970 buf_init (&defs_buf, sizeof (char *)); /* list of strings */ 971 buf_init (&yydmap_buf, sizeof (char)); /* one long string */ 972 buf_init (&top_buf, sizeof (char)); /* one long string */ 973 974 { 975 const char * m4defs_init_str[] = {"m4_changequote\n", 976 "m4_changequote([[, ]])\n"}; 977 buf_init (&m4defs_buf, sizeof (char *)); 978 buf_append (&m4defs_buf, &m4defs_init_str, 2); 979 } 980 981 sf_init (); 982 983 /* initialize regex lib */ 984 flex_init_regex(); 985 986 /* Enable C++ if program name ends with '+'. */ 987 program_name = basename2 (argv[0], 0); 988 989 if (program_name[0] != '\0' && 990 program_name[strlen (program_name) - 1] == '+') 991 C_plus_plus = true; 992 993 /* read flags */ 994 sopt = scanopt_init (flexopts, argc, argv, 0); 995 if (!sopt) { 996 /* This will only happen when flexopts array is altered. */ 997 fprintf (stderr, 998 _("Internal error. flexopts are malformed.\n")); 999 FLEX_EXIT (1); 1000 } 1001 1002 while ((rv = scanopt (sopt, &arg, &optind)) != 0) { 1003 1004 if (rv < 0) { 1005 /* Scanopt has already printed an option-specific error message. */ 1006 fprintf (stderr, 1007 _ 1008 ("Try `%s --help' for more information.\n"), 1009 program_name); 1010 FLEX_EXIT (1); 1011 } 1012 1013 switch ((enum flexopt_flag_t) rv) { 1014 case OPT_CPLUSPLUS: 1015 C_plus_plus = true; 1016 break; 1017 1018 case OPT_BATCH: 1019 interactive = false; 1020 break; 1021 1022 case OPT_BACKUP: 1023 backing_up_report = true; 1024 break; 1025 1026 case OPT_DONOTHING: 1027 break; 1028 1029 case OPT_COMPRESSION: 1030 if (!sawcmpflag) { 1031 useecs = false; 1032 usemecs = false; 1033 fulltbl = false; 1034 sawcmpflag = true; 1035 } 1036 1037 for (i = 0; arg && arg[i] != '\0'; i++) 1038 switch (arg[i]) { 1039 case 'a': 1040 long_align = true; 1041 break; 1042 1043 case 'e': 1044 useecs = true; 1045 break; 1046 1047 case 'F': 1048 fullspd = true; 1049 break; 1050 1051 case 'f': 1052 fulltbl = true; 1053 break; 1054 1055 case 'm': 1056 usemecs = true; 1057 break; 1058 1059 case 'r': 1060 use_read = true; 1061 break; 1062 1063 default: 1064 lerrif (_ 1065 ("unknown -C option '%c'"), 1066 (int) arg[i]); 1067 break; 1068 } 1069 break; 1070 1071 case OPT_DEBUG: 1072 ddebug = true; 1073 break; 1074 1075 case OPT_NO_DEBUG: 1076 ddebug = false; 1077 break; 1078 1079 case OPT_FULL: 1080 useecs = usemecs = false; 1081 use_read = fulltbl = true; 1082 break; 1083 1084 case OPT_FAST: 1085 useecs = usemecs = false; 1086 use_read = fullspd = true; 1087 break; 1088 1089 case OPT_HELP: 1090 usage (); 1091 FLEX_EXIT (0); 1092 1093 case OPT_INTERACTIVE: 1094 interactive = true; 1095 break; 1096 1097 case OPT_CASE_INSENSITIVE: 1098 sf_set_case_ins(true); 1099 break; 1100 1101 case OPT_LEX_COMPAT: 1102 lex_compat = true; 1103 break; 1104 1105 case OPT_POSIX_COMPAT: 1106 posix_compat = true; 1107 break; 1108 1109 case OPT_PREPROC_LEVEL: 1110 preproc_level = strtol(arg,NULL,0); 1111 break; 1112 1113 case OPT_MAIN: 1114 buf_strdefine (&userdef_buf, "YY_MAIN", "1"); 1115 do_yywrap = false; 1116 break; 1117 1118 case OPT_NO_MAIN: 1119 buf_strdefine (&userdef_buf, "YY_MAIN", "0"); 1120 break; 1121 1122 case OPT_NO_LINE: 1123 gen_line_dirs = false; 1124 break; 1125 1126 case OPT_OUTFILE: 1127 outfilename = arg; 1128 did_outfilename = 1; 1129 break; 1130 1131 case OPT_PREFIX: 1132 prefix = arg; 1133 break; 1134 1135 case OPT_PERF_REPORT: 1136 ++performance_report; 1137 break; 1138 1139 case OPT_BISON_BRIDGE: 1140 bison_bridge_lval = true; 1141 break; 1142 1143 case OPT_BISON_BRIDGE_LOCATIONS: 1144 bison_bridge_lval = bison_bridge_lloc = true; 1145 break; 1146 1147 case OPT_REENTRANT: 1148 reentrant = true; 1149 break; 1150 1151 case OPT_NO_REENTRANT: 1152 reentrant = false; 1153 break; 1154 1155 case OPT_SKEL: 1156 skelname = arg; 1157 break; 1158 1159 case OPT_DEFAULT: 1160 spprdflt = false; 1161 break; 1162 1163 case OPT_NO_DEFAULT: 1164 spprdflt = true; 1165 break; 1166 1167 case OPT_STDOUT: 1168 use_stdout = true; 1169 break; 1170 1171 case OPT_NO_UNISTD_H: 1172 //buf_strdefine (&userdef_buf, "YY_NO_UNISTD_H", "1"); 1173 buf_m4_define( &m4defs_buf, "M4_YY_NO_UNISTD_H",0); 1174 break; 1175 1176 case OPT_TABLES_FILE: 1177 tablesext = true; 1178 tablesfilename = arg; 1179 break; 1180 1181 case OPT_TABLES_VERIFY: 1182 tablesverify = true; 1183 break; 1184 1185 case OPT_TRACE: 1186 trace = true; 1187 break; 1188 1189 case OPT_VERBOSE: 1190 printstats = true; 1191 break; 1192 1193 case OPT_VERSION: 1194 printf (_("%s %s\n"), program_name, flex_version); 1195 FLEX_EXIT (0); 1196 1197 case OPT_WARN: 1198 nowarn = false; 1199 break; 1200 1201 case OPT_NO_WARN: 1202 nowarn = true; 1203 break; 1204 1205 case OPT_7BIT: 1206 csize = 128; 1207 break; 1208 1209 case OPT_8BIT: 1210 csize = CSIZE; 1211 break; 1212 1213 case OPT_ALIGN: 1214 long_align = true; 1215 break; 1216 1217 case OPT_NO_ALIGN: 1218 long_align = false; 1219 break; 1220 1221 case OPT_ALWAYS_INTERACTIVE: 1222 buf_m4_define (&m4defs_buf, "M4_YY_ALWAYS_INTERACTIVE", 0); 1223 break; 1224 1225 case OPT_NEVER_INTERACTIVE: 1226 buf_m4_define( &m4defs_buf, "M4_YY_NEVER_INTERACTIVE", 0); 1227 break; 1228 1229 case OPT_ARRAY: 1230 yytext_is_array = true; 1231 break; 1232 1233 case OPT_POINTER: 1234 yytext_is_array = false; 1235 break; 1236 1237 case OPT_ECS: 1238 useecs = true; 1239 break; 1240 1241 case OPT_NO_ECS: 1242 useecs = false; 1243 break; 1244 1245 case OPT_HEADER_FILE: 1246 headerfilename = arg; 1247 break; 1248 1249 case OPT_META_ECS: 1250 usemecs = true; 1251 break; 1252 1253 case OPT_NO_META_ECS: 1254 usemecs = false; 1255 break; 1256 1257 case OPT_PREPROCDEFINE: 1258 { 1259 /* arg is "symbol" or "symbol=definition". */ 1260 char *def; 1261 1262 for (def = arg; 1263 *def != '\0' && *def != '='; ++def) ; 1264 1265 buf_strappend (&userdef_buf, "#define "); 1266 if (*def == '\0') { 1267 buf_strappend (&userdef_buf, arg); 1268 buf_strappend (&userdef_buf, 1269 " 1\n"); 1270 } 1271 else { 1272 buf_strnappend (&userdef_buf, arg, 1273 def - arg); 1274 buf_strappend (&userdef_buf, " "); 1275 buf_strappend (&userdef_buf, 1276 def + 1); 1277 buf_strappend (&userdef_buf, "\n"); 1278 } 1279 } 1280 break; 1281 1282 case OPT_READ: 1283 use_read = true; 1284 break; 1285 1286 case OPT_STACK: 1287 //buf_strdefine (&userdef_buf, "YY_STACK_USED", "1"); 1288 buf_m4_define( &m4defs_buf, "M4_YY_STACK_USED",0); 1289 break; 1290 1291 case OPT_STDINIT: 1292 do_stdinit = true; 1293 break; 1294 1295 case OPT_NO_STDINIT: 1296 do_stdinit = false; 1297 break; 1298 1299 case OPT_YYCLASS: 1300 yyclass = arg; 1301 break; 1302 1303 case OPT_YYLINENO: 1304 do_yylineno = true; 1305 break; 1306 1307 case OPT_NO_YYLINENO: 1308 do_yylineno = false; 1309 break; 1310 1311 case OPT_YYWRAP: 1312 do_yywrap = true; 1313 break; 1314 1315 case OPT_NO_YYWRAP: 1316 do_yywrap = false; 1317 break; 1318 1319 case OPT_YYMORE: 1320 yymore_really_used = true; 1321 break; 1322 1323 case OPT_NO_YYMORE: 1324 yymore_really_used = false; 1325 break; 1326 1327 case OPT_REJECT: 1328 reject_really_used = true; 1329 break; 1330 1331 case OPT_NO_REJECT: 1332 reject_really_used = false; 1333 break; 1334 1335 case OPT_NO_ANSI_FUNC_DEFS: 1336 ansi_func_defs = false; 1337 break; 1338 1339 case OPT_NO_ANSI_FUNC_PROTOS: 1340 ansi_func_protos = false; 1341 break; 1342 1343 case OPT_NO_YY_PUSH_STATE: 1344 //buf_strdefine (&userdef_buf, "YY_NO_PUSH_STATE", "1"); 1345 buf_m4_define( &m4defs_buf, "M4_YY_NO_PUSH_STATE",0); 1346 break; 1347 case OPT_NO_YY_POP_STATE: 1348 //buf_strdefine (&userdef_buf, "YY_NO_POP_STATE", "1"); 1349 buf_m4_define( &m4defs_buf, "M4_YY_NO_POP_STATE",0); 1350 break; 1351 case OPT_NO_YY_TOP_STATE: 1352 //buf_strdefine (&userdef_buf, "YY_NO_TOP_STATE", "1"); 1353 buf_m4_define( &m4defs_buf, "M4_YY_NO_TOP_STATE",0); 1354 break; 1355 case OPT_NO_UNPUT: 1356 //buf_strdefine (&userdef_buf, "YY_NO_UNPUT", "1"); 1357 buf_m4_define( &m4defs_buf, "M4_YY_NO_UNPUT",0); 1358 break; 1359 case OPT_NO_YY_SCAN_BUFFER: 1360 //buf_strdefine (&userdef_buf, "YY_NO_SCAN_BUFFER", "1"); 1361 buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_BUFFER",0); 1362 break; 1363 case OPT_NO_YY_SCAN_BYTES: 1364 //buf_strdefine (&userdef_buf, "YY_NO_SCAN_BYTES", "1"); 1365 buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_BYTES",0); 1366 break; 1367 case OPT_NO_YY_SCAN_STRING: 1368 //buf_strdefine (&userdef_buf, "YY_NO_SCAN_STRING", "1"); 1369 buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_STRING",0); 1370 break; 1371 case OPT_NO_YYGET_EXTRA: 1372 //buf_strdefine (&userdef_buf, "YY_NO_GET_EXTRA", "1"); 1373 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_EXTRA",0); 1374 break; 1375 case OPT_NO_YYSET_EXTRA: 1376 //buf_strdefine (&userdef_buf, "YY_NO_SET_EXTRA", "1"); 1377 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_EXTRA",0); 1378 break; 1379 case OPT_NO_YYGET_LENG: 1380 //buf_strdefine (&userdef_buf, "YY_NO_GET_LENG", "1"); 1381 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LENG",0); 1382 break; 1383 case OPT_NO_YYGET_TEXT: 1384 //buf_strdefine (&userdef_buf, "YY_NO_GET_TEXT", "1"); 1385 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_TEXT",0); 1386 break; 1387 case OPT_NO_YYGET_LINENO: 1388 //buf_strdefine (&userdef_buf, "YY_NO_GET_LINENO", "1"); 1389 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LINENO",0); 1390 break; 1391 case OPT_NO_YYSET_LINENO: 1392 //buf_strdefine (&userdef_buf, "YY_NO_SET_LINENO", "1"); 1393 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LINENO",0); 1394 break; 1395 case OPT_NO_YYGET_IN: 1396 //buf_strdefine (&userdef_buf, "YY_NO_GET_IN", "1"); 1397 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_IN",0); 1398 break; 1399 case OPT_NO_YYSET_IN: 1400 //buf_strdefine (&userdef_buf, "YY_NO_SET_IN", "1"); 1401 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_IN",0); 1402 break; 1403 case OPT_NO_YYGET_OUT: 1404 //buf_strdefine (&userdef_buf, "YY_NO_GET_OUT", "1"); 1405 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_OUT",0); 1406 break; 1407 case OPT_NO_YYSET_OUT: 1408 //buf_strdefine (&userdef_buf, "YY_NO_SET_OUT", "1"); 1409 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_OUT",0); 1410 break; 1411 case OPT_NO_YYGET_LVAL: 1412 //buf_strdefine (&userdef_buf, "YY_NO_GET_LVAL", "1"); 1413 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LVAL",0); 1414 break; 1415 case OPT_NO_YYSET_LVAL: 1416 //buf_strdefine (&userdef_buf, "YY_NO_SET_LVAL", "1"); 1417 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LVAL",0); 1418 break; 1419 case OPT_NO_YYGET_LLOC: 1420 //buf_strdefine (&userdef_buf, "YY_NO_GET_LLOC", "1"); 1421 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LLOC",0); 1422 break; 1423 case OPT_NO_YYSET_LLOC: 1424 //buf_strdefine (&userdef_buf, "YY_NO_SET_LLOC", "1"); 1425 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LLOC",0); 1426 break; 1427 1428 } /* switch */ 1429 } /* while scanopt() */ 1430 1431 scanopt_destroy (sopt); 1432 1433 num_input_files = argc - optind; 1434 input_files = argv + optind; 1435 set_input_file (num_input_files > 0 ? input_files[0] : NULL); 1436 1437 lastccl = lastsc = lastdfa = lastnfa = 0; 1438 num_rules = num_eof_rules = default_rule = 0; 1439 numas = numsnpairs = tmpuses = 0; 1440 numecs = numeps = eps2 = num_reallocs = hshcol = dfaeql = totnst = 1441 0; 1442 numuniq = numdup = hshsave = eofseen = datapos = dataline = 0; 1443 num_backing_up = onesp = numprots = 0; 1444 variable_trailing_context_rules = bol_needed = false; 1445 1446 linenum = sectnum = 1; 1447 firstprot = NIL; 1448 1449 /* Used in mkprot() so that the first proto goes in slot 1 1450 * of the proto queue. 1451 */ 1452 lastprot = 1; 1453 1454 set_up_initial_allocations (); 1455} 1456 1457 1458/* readin - read in the rules section of the input file(s) */ 1459 1460void readin () 1461{ 1462 static char yy_stdinit[] = "FILE *yyin = stdin, *yyout = stdout;"; 1463 static char yy_nostdinit[] = 1464 "FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0;"; 1465 1466 line_directive_out ((FILE *) 0, 1); 1467 1468 if (yyparse ()) { 1469 pinpoint_message (_("fatal parse error")); 1470 flexend (1); 1471 } 1472 1473 if (syntaxerror) 1474 flexend (1); 1475 1476 /* If the user explicitly requested posix compatibility by specifing the 1477 * posix-compat option, then we check for conflicting options. However, if 1478 * the POSIXLY_CORRECT variable is set, then we quietly make flex as 1479 * posix-compatible as possible. This is the recommended behavior 1480 * according to the GNU Coding Standards. 1481 * 1482 * Note: The posix option was added to flex to provide the posix behavior 1483 * of the repeat operator in regular expressions, e.g., `ab{3}' 1484 */ 1485 if (posix_compat) { 1486 /* TODO: This is where we try to make flex behave according to 1487 * posiz, AND check for conflicting options. How far should we go 1488 * with this? Should we disable all the neat-o flex features? 1489 */ 1490 /* Update: Estes says no, since other flex features don't violate posix. */ 1491 } 1492 1493 if (getenv ("POSIXLY_CORRECT")) { 1494 posix_compat = true; 1495 } 1496 1497 if (backing_up_report) { 1498 backing_up_file = fopen (backing_name, "w"); 1499 if (backing_up_file == NULL) 1500 lerrsf (_ 1501 ("could not create backing-up info file %s"), 1502 backing_name); 1503 } 1504 1505 else 1506 backing_up_file = NULL; 1507 1508 if (yymore_really_used == true) 1509 yymore_used = true; 1510 else if (yymore_really_used == false) 1511 yymore_used = false; 1512 1513 if (reject_really_used == true) 1514 reject = true; 1515 else if (reject_really_used == false) 1516 reject = false; 1517 1518 if (performance_report > 0) { 1519 if (lex_compat) { 1520 fprintf (stderr, 1521 _ 1522 ("-l AT&T lex compatibility option entails a large performance penalty\n")); 1523 fprintf (stderr, 1524 _ 1525 (" and may be the actual source of other reported performance penalties\n")); 1526 } 1527 1528 else if (do_yylineno) { 1529 fprintf (stderr, 1530 _ 1531 ("%%option yylineno entails a performance penalty ONLY on rules that can match newline characters\n")); 1532 } 1533 1534 if (performance_report > 1) { 1535 if (interactive) 1536 fprintf (stderr, 1537 _ 1538 ("-I (interactive) entails a minor performance penalty\n")); 1539 1540 if (yymore_used) 1541 fprintf (stderr, 1542 _ 1543 ("yymore() entails a minor performance penalty\n")); 1544 } 1545 1546 if (reject) 1547 fprintf (stderr, 1548 _ 1549 ("REJECT entails a large performance penalty\n")); 1550 1551 if (variable_trailing_context_rules) 1552 fprintf (stderr, 1553 _ 1554 ("Variable trailing context rules entail a large performance penalty\n")); 1555 } 1556 1557 if (reject) 1558 real_reject = true; 1559 1560 if (variable_trailing_context_rules) 1561 reject = true; 1562 1563 if ((fulltbl || fullspd) && reject) { 1564 if (real_reject) 1565 flexerror (_ 1566 ("REJECT cannot be used with -f or -F")); 1567 else if (do_yylineno) 1568 flexerror (_ 1569 ("%option yylineno cannot be used with REJECT")); 1570 else 1571 flexerror (_ 1572 ("variable trailing context rules cannot be used with -f or -F")); 1573 } 1574 1575 if (reject){ 1576 out_m4_define( "M4_YY_USES_REJECT", NULL); 1577 //outn ("\n#define YY_USES_REJECT"); 1578 } 1579 1580 if (!do_yywrap) { 1581 if (!C_plus_plus) 1582 if (reentrant) 1583 outn ("\n#define yywrap(yyscanner) 1"); 1584 else 1585 outn ("\n#define yywrap() 1"); 1586 outn ("#define YY_SKIP_YYWRAP"); 1587 } 1588 1589 if (ddebug) 1590 outn ("\n#define FLEX_DEBUG"); 1591 1592 OUT_BEGIN_CODE (); 1593 if (csize == 256) 1594 outn ("typedef unsigned char YY_CHAR;"); 1595 else 1596 outn ("typedef char YY_CHAR;"); 1597 OUT_END_CODE (); 1598 1599 if (C_plus_plus) { 1600 outn ("#define yytext_ptr yytext"); 1601 1602 if (interactive) 1603 outn ("#define YY_INTERACTIVE"); 1604 } 1605 1606 else { 1607 OUT_BEGIN_CODE (); 1608 /* In reentrant scanner, stdinit is handled in flex.skl. */ 1609 if (do_stdinit) { 1610 if (reentrant){ 1611 outn ("#ifdef VMS"); 1612 outn ("#ifdef __VMS_POSIX"); 1613 outn ("#define YY_STDINIT"); 1614 outn ("#endif"); 1615 outn ("#else"); 1616 outn ("#define YY_STDINIT"); 1617 outn ("#endif"); 1618 } 1619 1620 outn ("#ifdef VMS"); 1621 outn ("#ifndef __VMS_POSIX"); 1622 outn (yy_nostdinit); 1623 outn ("#else"); 1624 outn (yy_stdinit); 1625 outn ("#endif"); 1626 outn ("#else"); 1627 outn (yy_stdinit); 1628 outn ("#endif"); 1629 } 1630 1631 else { 1632 if(!reentrant) 1633 outn (yy_nostdinit); 1634 } 1635 OUT_END_CODE (); 1636 } 1637 1638 OUT_BEGIN_CODE (); 1639 if (fullspd) 1640 outn ("typedef yyconst struct yy_trans_info *yy_state_type;"); 1641 else if (!C_plus_plus) 1642 outn ("typedef int yy_state_type;"); 1643 OUT_END_CODE (); 1644 1645 if (lex_compat) 1646 outn ("#define YY_FLEX_LEX_COMPAT"); 1647 1648 if (!C_plus_plus && !reentrant) { 1649 outn ("extern int yylineno;"); 1650 OUT_BEGIN_CODE (); 1651 outn ("int yylineno = 1;"); 1652 OUT_END_CODE (); 1653 } 1654 1655 if (C_plus_plus) { 1656 outn ("\n#include <FlexLexer.h>"); 1657 1658 if (!do_yywrap) { 1659 outn("\nint yyFlexLexer::yywrap() { return 1; }"); 1660 } 1661 1662 if (yyclass) { 1663 outn ("int yyFlexLexer::yylex()"); 1664 outn ("\t{"); 1665 outn ("\tLexerError( \"yyFlexLexer::yylex invoked but %option yyclass used\" );"); 1666 outn ("\treturn 0;"); 1667 outn ("\t}"); 1668 1669 out_str ("\n#define YY_DECL int %s::yylex()\n", 1670 yyclass); 1671 } 1672 } 1673 1674 else { 1675 1676 /* Watch out: yytext_ptr is a variable when yytext is an array, 1677 * but it's a macro when yytext is a pointer. 1678 */ 1679 if (yytext_is_array) { 1680 if (!reentrant) 1681 outn ("extern char yytext[];\n"); 1682 } 1683 else { 1684 if (reentrant) { 1685 outn ("#define yytext_ptr yytext_r"); 1686 } 1687 else { 1688 outn ("extern char *yytext;"); 1689 outn ("#define yytext_ptr yytext"); 1690 } 1691 } 1692 1693 if (yyclass) 1694 flexerror (_ 1695 ("%option yyclass only meaningful for C++ scanners")); 1696 } 1697 1698 if (useecs) 1699 numecs = cre8ecs (nextecm, ecgroup, csize); 1700 else 1701 numecs = csize; 1702 1703 /* Now map the equivalence class for NUL to its expected place. */ 1704 ecgroup[0] = ecgroup[csize]; 1705 NUL_ec = ABS (ecgroup[0]); 1706 1707 if (useecs) 1708 ccl2ecl (); 1709} 1710 1711 1712/* set_up_initial_allocations - allocate memory for internal tables */ 1713 1714void set_up_initial_allocations () 1715{ 1716 maximum_mns = (long_align ? MAXIMUM_MNS_LONG : MAXIMUM_MNS); 1717 current_mns = INITIAL_MNS; 1718 firstst = allocate_integer_array (current_mns); 1719 lastst = allocate_integer_array (current_mns); 1720 finalst = allocate_integer_array (current_mns); 1721 transchar = allocate_integer_array (current_mns); 1722 trans1 = allocate_integer_array (current_mns); 1723 trans2 = allocate_integer_array (current_mns); 1724 accptnum = allocate_integer_array (current_mns); 1725 assoc_rule = allocate_integer_array (current_mns); 1726 state_type = allocate_integer_array (current_mns); 1727 1728 current_max_rules = INITIAL_MAX_RULES; 1729 rule_type = allocate_integer_array (current_max_rules); 1730 rule_linenum = allocate_integer_array (current_max_rules); 1731 rule_useful = allocate_integer_array (current_max_rules); 1732 rule_has_nl = allocate_bool_array (current_max_rules); 1733 1734 current_max_scs = INITIAL_MAX_SCS; 1735 scset = allocate_integer_array (current_max_scs); 1736 scbol = allocate_integer_array (current_max_scs); 1737 scxclu = allocate_integer_array (current_max_scs); 1738 sceof = allocate_integer_array (current_max_scs); 1739 scname = allocate_char_ptr_array (current_max_scs); 1740 1741 current_maxccls = INITIAL_MAX_CCLS; 1742 cclmap = allocate_integer_array (current_maxccls); 1743 ccllen = allocate_integer_array (current_maxccls); 1744 cclng = allocate_integer_array (current_maxccls); 1745 ccl_has_nl = allocate_bool_array (current_maxccls); 1746 1747 current_max_ccl_tbl_size = INITIAL_MAX_CCL_TBL_SIZE; 1748 ccltbl = allocate_Character_array (current_max_ccl_tbl_size); 1749 1750 current_max_dfa_size = INITIAL_MAX_DFA_SIZE; 1751 1752 current_max_xpairs = INITIAL_MAX_XPAIRS; 1753 nxt = allocate_integer_array (current_max_xpairs); 1754 chk = allocate_integer_array (current_max_xpairs); 1755 1756 current_max_template_xpairs = INITIAL_MAX_TEMPLATE_XPAIRS; 1757 tnxt = allocate_integer_array (current_max_template_xpairs); 1758 1759 current_max_dfas = INITIAL_MAX_DFAS; 1760 base = allocate_integer_array (current_max_dfas); 1761 def = allocate_integer_array (current_max_dfas); 1762 dfasiz = allocate_integer_array (current_max_dfas); 1763 accsiz = allocate_integer_array (current_max_dfas); 1764 dhash = allocate_integer_array (current_max_dfas); 1765 dss = allocate_int_ptr_array (current_max_dfas); 1766 dfaacc = allocate_dfaacc_union (current_max_dfas); 1767 1768 nultrans = (int *) 0; 1769} 1770 1771 1772/* extracts basename from path, optionally stripping the extension "\.*" 1773 * (same concept as /bin/sh `basename`, but different handling of extension). */ 1774static char *basename2 (path, strip_ext) 1775 char *path; 1776 int strip_ext; /* boolean */ 1777{ 1778 char *b, *e = 0; 1779 1780 b = path; 1781 for (b = path; *path; path++) 1782 if (*path == '/') 1783 b = path + 1; 1784 else if (*path == '.') 1785 e = path; 1786 1787 if (strip_ext && e && e > b) 1788 *e = '\0'; 1789 return b; 1790} 1791 1792void usage () 1793{ 1794 FILE *f = stdout; 1795 1796 if (!did_outfilename) { 1797 snprintf (outfile_path, sizeof(outfile_path), outfile_template, 1798 prefix, C_plus_plus ? "cc" : "c"); 1799 outfilename = outfile_path; 1800 } 1801 1802 fprintf (f, _("Usage: %s [OPTIONS] [FILE]...\n"), program_name); 1803 fprintf (f, 1804 _ 1805 ("Generates programs that perform pattern-matching on text.\n" 1806 "\n" "Table Compression:\n" 1807 " -Ca, --align trade off larger tables for better memory alignment\n" 1808 " -Ce, --ecs construct equivalence classes\n" 1809 " -Cf do not compress tables; use -f representation\n" 1810 " -CF do not compress tables; use -F representation\n" 1811 " -Cm, --meta-ecs construct meta-equivalence classes\n" 1812 " -Cr, --read use read() instead of stdio for scanner input\n" 1813 " -f, --full generate fast, large scanner. Same as -Cfr\n" 1814 " -F, --fast use alternate table representation. Same as -CFr\n" 1815 " -Cem default compression (same as --ecs --meta-ecs)\n" 1816 "\n" "Debugging:\n" 1817 " -d, --debug enable debug mode in scanner\n" 1818 " -b, --backup write backing-up information to %s\n" 1819 " -p, --perf-report write performance report to stderr\n" 1820 " -s, --nodefault suppress default rule to ECHO unmatched text\n" 1821 " -T, --trace %s should run in trace mode\n" 1822 " -w, --nowarn do not generate warnings\n" 1823 " -v, --verbose write summary of scanner statistics to stdout\n" 1824 "\n" "Files:\n" 1825 " -o, --outfile=FILE specify output filename\n" 1826 " -S, --skel=FILE specify skeleton file\n" 1827 " -t, --stdout write scanner on stdout instead of %s\n" 1828 " --yyclass=NAME name of C++ class\n" 1829 " --header-file=FILE create a C header file in addition to the scanner\n" 1830 " --tables-file[=FILE] write tables to FILE\n" "\n" 1831 "Scanner behavior:\n" 1832 " -7, --7bit generate 7-bit scanner\n" 1833 " -8, --8bit generate 8-bit scanner\n" 1834 " -B, --batch generate batch scanner (opposite of -I)\n" 1835 " -i, --case-insensitive ignore case in patterns\n" 1836 " -l, --lex-compat maximal compatibility with original lex\n" 1837 " -X, --posix-compat maximal compatibility with POSIX lex\n" 1838 " -I, --interactive generate interactive scanner (opposite of -B)\n" 1839 " --yylineno track line count in yylineno\n" 1840 "\n" "Generated code:\n" 1841 " -+, --c++ generate C++ scanner class\n" 1842 " -Dmacro[=defn] #define macro defn (default defn is '1')\n" 1843 " -L, --noline suppress #line directives in scanner\n" 1844 " -P, --prefix=STRING use STRING as prefix instead of \"yy\"\n" 1845 " -R, --reentrant generate a reentrant C scanner\n" 1846 " --bison-bridge scanner for bison pure parser.\n" 1847 " --bison-locations include yylloc support.\n" 1848 " --stdinit initialize yyin/yyout to stdin/stdout\n" 1849 " --noansi-definitions old-style function definitions\n" 1850 " --noansi-prototypes empty parameter list in prototypes\n" 1851 " --nounistd do not include <unistd.h>\n" 1852 " --noFUNCTION do not generate a particular FUNCTION\n" 1853 "\n" "Miscellaneous:\n" 1854 " -c do-nothing POSIX option\n" 1855 " -n do-nothing POSIX option\n" 1856 " -?\n" 1857 " -h, --help produce this help message\n" 1858 " -V, --version report %s version\n"), 1859 backing_name, program_name, outfile_path, program_name); 1860 1861} 1862