1130561Sobrien/* flex - tool to generate fast lexical analyzers */ 260484Sobrien 3130561Sobrien/* Copyright (c) 1990 The Regents of the University of California. */ 4130561Sobrien/* All rights reserved. */ 5130561Sobrien 660484Sobrien/* This code is derived from software contributed to Berkeley by */ 7130561Sobrien/* Vern Paxson. */ 8130561Sobrien 960484Sobrien/* The United States Government has rights in this work pursuant */ 10130561Sobrien/* to contract no. DE-AC03-76SF00098 between the United States */ 1160484Sobrien/* Department of Energy and the University of California. */ 1260484Sobrien 13130561Sobrien/* This file is part of flex. */ 1460484Sobrien 1560484Sobrien/* Redistribution and use in source and binary forms, with or without */ 16130561Sobrien/* modification, are permitted provided that the following conditions */ 1760484Sobrien/* are met: */ 1860484Sobrien 19130561Sobrien/* 1. Redistributions of source code must retain the above copyright */ 2060484Sobrien/* notice, this list of conditions and the following disclaimer. */ 2160484Sobrien/* 2. Redistributions in binary form must reproduce the above copyright */ 22130561Sobrien/* notice, this list of conditions and the following disclaimer in the */ 2360484Sobrien/* documentation and/or other materials provided with the distribution. */ 2460484Sobrien 25130561Sobrien/* Neither the name of the University nor the names of its contributors */ 2677298Sobrien/* may be used to endorse or promote products derived from this software */ 2777298Sobrien/* without specific prior written permission. */ 28130561Sobrien 2960484Sobrien/* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */ 3060484Sobrien/* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */ 31130561Sobrien/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */ 3260484Sobrien/* PURPOSE. */ 3360484Sobrien 34130561Sobrien 35130561Sobrien#include "flexdef.h" 36130561Sobrien#include "version.h" 37130561Sobrien#include "options.h" 3860484Sobrien#include "tables.h" 3960484Sobrien#include "parse.h" 40218822Sdim 41218822Sdimstatic char flex_version[] = FLEX_VERSION; 42218822Sdim 43218822Sdim/* declare functions that have forward references */ 44218822Sdim 45218822Sdimvoid flexinit(int, char **); 46218822Sdimvoid readin(void); 47218822Sdimvoid set_up_initial_allocations(void); 48218822Sdim 49218822Sdim 50218822Sdim/* these globals are all defined and commented in flexdef.h */ 51218822Sdimint printstats, syntaxerror, eofseen, ddebug, trace, nowarn, spprdflt; 52218822Sdimint interactive, lex_compat, posix_compat, do_yylineno, 53218822Sdim useecs, fulltbl, usemecs; 54218822Sdimint fullspd, gen_line_dirs, performance_report, backing_up_report; 55218822Sdimint C_plus_plus, long_align, use_read, yytext_is_array, do_yywrap, 56218822Sdim csize; 57218822Sdimint reentrant, bison_bridge_lval, bison_bridge_lloc; 58218822Sdimint yymore_used, reject, real_reject, continued_action, in_rule; 59218822Sdimint yymore_really_used, reject_really_used; 60218822Sdimint trace_hex = 0; 61218822Sdimint datapos, dataline, linenum; 62218822SdimFILE *skelfile = NULL; 63218822Sdimint skel_ind = 0; 64218822Sdimchar *action_array; 65218822Sdimint action_size, defs1_offset, prolog_offset, action_offset, 66218822Sdim action_index; 67218822Sdimchar *infilename = NULL, *outfilename = NULL, *headerfilename = NULL; 68218822Sdimint did_outfilename; 69218822Sdimchar *prefix, *yyclass, *extra_type = NULL; 70218822Sdimint do_stdinit, use_stdout; 71218822Sdimint onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE]; 72218822Sdimint onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp; 73218822Sdimint maximum_mns, current_mns, current_max_rules; 74218822Sdimint num_rules, num_eof_rules, default_rule, lastnfa; 75218822Sdimint *firstst, *lastst, *finalst, *transchar, *trans1, *trans2; 76218822Sdimint *accptnum, *assoc_rule, *state_type; 77218822Sdimint *rule_type, *rule_linenum, *rule_useful; 78218822Sdimint current_state_type; 79218822Sdimint variable_trailing_context_rules; 80218822Sdimint numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP]; 81218822Sdimint protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE]; 82218822Sdimint numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs, 83218822Sdim tecfwd[CSIZE + 1]; 84218822Sdimint tecbck[CSIZE + 1]; 85218822Sdimint lastsc, *scset, *scbol, *scxclu, *sceof; 86218822Sdimint current_max_scs; 87218822Sdimchar **scname; 88218822Sdimint current_max_dfa_size, current_max_xpairs; 89218822Sdimint current_max_template_xpairs, current_max_dfas; 90218822Sdimint lastdfa, *nxt, *chk, *tnxt; 91130561Sobrienint *base, *def, *nultrans, NUL_ec, tblend, firstfree, **dss, *dfasiz; 92130561Sobrienunion dfaacc_union *dfaacc; 93130561Sobrienint *accsiz, *dhash, numas; 94130561Sobrienint numsnpairs, jambase, jamstate; 9589857Sobrienint lastccl, *cclmap, *ccllen, *cclng, cclreuse; 9689857Sobrienint current_maxccls, current_max_ccl_tbl_size; 97130561Sobrienunsigned char *ccltbl; 98130561Sobrienchar nmstr[MAXLINE]; 99130561Sobrienint sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs; 100130561Sobrienint tmpuses, totnst, peakpairs, numuniq, numdup, hshsave; 10160484Sobrienint num_backing_up, bol_needed; 10260484SobrienFILE *backing_up_file; 103130561Sobrienint end_of_buffer_state; 10460484Sobrienchar **input_files; 10560484Sobrienint num_input_files; 106130561Sobrienjmp_buf flex_main_jmp_buf; 10760484Sobrienbool *rule_has_nl, *ccl_has_nl; 10860484Sobrienint nlch = '\n'; 109130561Sobrien 110130561Sobrienbool tablesext, tablesverify, gentables; 111130561Sobrienchar *tablesfilename=0,*tablesname=0; 112130561Sobrienstruct yytbl_writer tableswr; 11360484Sobrien 11460484Sobrien/* Make sure program_name is initialized so we don't crash if writing 115130561Sobrien * out an error message before getting the program name from argv[0]. 11660484Sobrien */ 11760484Sobrienchar *program_name = "flex"; 118130561Sobrien 11960484Sobrienstatic const char outfile_template[] = "lex.%s.%s"; 12060484Sobrienstatic const char backing_name[] = "lex.backup"; 121130561Sobrienstatic const char tablesfile_template[] = "lex.%s.tables"; 122130561Sobrien 123130561Sobrien/* From scan.l */ 124130561Sobrienextern FILE* yyout; 125130561Sobrien 126130561Sobrienstatic char outfile_path[MAXLINE]; 127130561Sobrienstatic int outfile_created = 0; 128130561Sobrienstatic char *skelname = NULL; 129130561Sobrienstatic int _stdout_closed = 0; /* flag to prevent double-fclose() on stdout. */ 130130561Sobrienconst char *escaped_qstart = "]]M4_YY_NOOP[M4_YY_NOOP[M4_YY_NOOP[["; 131130561Sobrienconst char *escaped_qend = "]]M4_YY_NOOP]M4_YY_NOOP]M4_YY_NOOP[["; 132130561Sobrien 133130561Sobrien/* For debugging. The max number of filters to apply to skeleton. */ 13460484Sobrienstatic int preproc_level = 1000; 13560484Sobrien 136130561Sobrienint flex_main (int argc, char *argv[]); 13760484Sobrien 13860484Sobrienint flex_main (int argc, char *argv[]) 139130561Sobrien{ 14060484Sobrien int i, exit_status, child_status; 14160484Sobrien 142130561Sobrien /* Set a longjmp target. Yes, I know it's a hack, but it gets worse: The 14360484Sobrien * return value of setjmp, if non-zero, is the desired exit code PLUS ONE. 14460484Sobrien * For example, if you want 'main' to return with code '2', then call 145130561Sobrien * longjmp() with an argument of 3. This is because it is invalid to 146130561Sobrien * specify a value of 0 to longjmp. FLEX_EXIT(n) should be used instead of 147130561Sobrien * exit(n); 148130561Sobrien */ 149130561Sobrien exit_status = setjmp (flex_main_jmp_buf); 150130561Sobrien if (exit_status){ 151130561Sobrien if (stdout && !_stdout_closed && !ferror(stdout)){ 15260484Sobrien fflush(stdout); 15360484Sobrien fclose(stdout); 154130561Sobrien } 15560484Sobrien while (wait(&child_status) > 0){ 15660484Sobrien if (!WIFEXITED (child_status) 157130561Sobrien || WEXITSTATUS (child_status) != 0){ 158130561Sobrien /* report an error of a child 159130561Sobrien */ 160130561Sobrien if( exit_status <= 1 ) 16160484Sobrien exit_status = 2; 16260484Sobrien 163130561Sobrien } 16460484Sobrien } 16560484Sobrien return exit_status - 1; 166130561Sobrien } 167130561Sobrien 168130561Sobrien flexinit (argc, argv); 169130561Sobrien 170130561Sobrien readin (); 171130561Sobrien 172130561Sobrien skelout (); 17360484Sobrien /* %% [1.5] DFA */ 17460484Sobrien ntod (); 175130561Sobrien 17660484Sobrien for (i = 1; i <= num_rules; ++i) 17760484Sobrien if (!rule_useful[i] && i != default_rule) 178130561Sobrien line_warning (_("rule cannot be matched"), 179130561Sobrien rule_linenum[i]); 180130561Sobrien 181130561Sobrien if (spprdflt && !reject && rule_useful[default_rule]) 18260484Sobrien line_warning (_ 18360484Sobrien ("-s option given but default rule can be matched"), 184130561Sobrien rule_linenum[default_rule]); 18560484Sobrien 18660484Sobrien /* Generate the C state transition tables from the DFA. */ 187130561Sobrien make_tables (); 18860484Sobrien 18960484Sobrien /* Note, flexend does not return. It exits with its argument 190130561Sobrien * as status. 19160484Sobrien */ 19260484Sobrien flexend (0); 193130561Sobrien 19460484Sobrien return 0; /* keep compilers/lint happy */ 19560484Sobrien} 196130561Sobrien 197130561Sobrien/* Wrapper around flex_main, so flex_main can be built as a library. */ 198130561Sobrienint main (int argc, char *argv[]) 199130561Sobrien{ 200130561Sobrien#if ENABLE_NLS 201130561Sobrien#if HAVE_LOCALE_H 202218822Sdim setlocale (LC_MESSAGES, ""); 203218822Sdim setlocale (LC_CTYPE, ""); 204218822Sdim textdomain (PACKAGE); 205130561Sobrien bindtextdomain (PACKAGE, LOCALEDIR); 206130561Sobrien#endif 207130561Sobrien#endif 208130561Sobrien 209130561Sobrien return flex_main (argc, argv); 210130561Sobrien} 211130561Sobrien 212130561Sobrien/* check_options - check user-specified options */ 213130561Sobrien 214130561Sobrienvoid check_options (void) 21560484Sobrien{ 21660484Sobrien int i; 217130561Sobrien const char * m4 = NULL; 21860484Sobrien 21960484Sobrien if (lex_compat) { 220130561Sobrien if (C_plus_plus) 22160484Sobrien flexerror (_("Can't use -+ with -l option")); 22260484Sobrien 223130561Sobrien if (fulltbl || fullspd) 22460484Sobrien flexerror (_("Can't use -f or -F with -l option")); 22560484Sobrien 226130561Sobrien if (reentrant || bison_bridge_lval) 227130561Sobrien flexerror (_ 228130561Sobrien ("Can't use --reentrant or --bison-bridge with -l option")); 229130561Sobrien 230130561Sobrien yytext_is_array = true; 231130561Sobrien do_yylineno = true; 232130561Sobrien use_read = false; 23360484Sobrien } 23460484Sobrien 235218822Sdim 236218822Sdim#if 0 237218822Sdim /* This makes no sense whatsoever. I'm removing it. */ 238130561Sobrien if (do_yylineno) 23960484Sobrien /* This should really be "maintain_backup_tables = true" */ 24060484Sobrien reject_really_used = true; 241130561Sobrien#endif 24260484Sobrien 24360484Sobrien if (csize == unspecified) { 244130561Sobrien if ((fulltbl || fullspd) && !useecs) 24560484Sobrien csize = DEFAULT_CSIZE; 24660484Sobrien else 247130561Sobrien csize = CSIZE; 24860484Sobrien } 24960484Sobrien 250130561Sobrien if (interactive == unspecified) { 25160484Sobrien if (fulltbl || fullspd) 25260484Sobrien interactive = false; 253130561Sobrien else 25460484Sobrien interactive = true; 25560484Sobrien } 256218822Sdim 257218822Sdim if (fulltbl || fullspd) { 258218822Sdim if (usemecs) 259130561Sobrien flexerror (_ 26060484Sobrien ("-Cf/-CF and -Cm don't make sense together")); 26160484Sobrien 262130561Sobrien if (interactive) 263130561Sobrien flexerror (_("-Cf/-CF and -I are incompatible")); 264130561Sobrien 265130561Sobrien if (lex_compat) 266130561Sobrien flexerror (_ 267130561Sobrien ("-Cf/-CF are incompatible with lex-compatibility mode")); 268130561Sobrien 269130561Sobrien 270130561Sobrien if (fulltbl && fullspd) 271130561Sobrien flexerror (_ 272130561Sobrien ("-Cf and -CF are mutually exclusive")); 273130561Sobrien } 274130561Sobrien 275130561Sobrien if (C_plus_plus && fullspd) 276130561Sobrien flexerror (_("Can't use -+ with -CF option")); 277130561Sobrien 278130561Sobrien if (C_plus_plus && yytext_is_array) { 279130561Sobrien lwarn (_("%array incompatible with -+ option")); 280130561Sobrien yytext_is_array = false; 281130561Sobrien } 282130561Sobrien 283130561Sobrien if (C_plus_plus && (reentrant)) 284130561Sobrien flexerror (_("Options -+ and --reentrant are mutually exclusive.")); 285130561Sobrien 286130561Sobrien if (C_plus_plus && bison_bridge_lval) 287130561Sobrien flexerror (_("bison bridge not supported for the C++ scanner.")); 288130561Sobrien 289130561Sobrien 290130561Sobrien if (useecs) { /* Set up doubly-linked equivalence classes. */ 291130561Sobrien 292130561Sobrien /* We loop all the way up to csize, since ecgroup[csize] is 293130561Sobrien * the position used for NUL characters. 294130561Sobrien */ 295130561Sobrien ecgroup[1] = NIL; 296130561Sobrien 297130561Sobrien for (i = 2; i <= csize; ++i) { 298130561Sobrien ecgroup[i] = i - 1; 299130561Sobrien nextecm[i - 1] = i; 300130561Sobrien } 301130561Sobrien 302130561Sobrien nextecm[csize] = NIL; 303130561Sobrien } 304130561Sobrien 305130561Sobrien else { 306130561Sobrien /* Put everything in its own equivalence class. */ 307130561Sobrien for (i = 1; i <= csize; ++i) { 308130561Sobrien ecgroup[i] = i; 309130561Sobrien nextecm[i] = BAD_SUBSCRIPT; /* to catch errors */ 310130561Sobrien } 311130561Sobrien } 312130561Sobrien 313130561Sobrien if (extra_type) 314130561Sobrien buf_m4_define( &m4defs_buf, "M4_EXTRA_TYPE_DEFS", extra_type); 315130561Sobrien 316130561Sobrien if (!use_stdout) { 317130561Sobrien FILE *prev_stdout; 318130561Sobrien 319130561Sobrien if (!did_outfilename) { 320130561Sobrien char *suffix; 321130561Sobrien 322130561Sobrien if (C_plus_plus) 32360484Sobrien suffix = "cc"; 32460484Sobrien else 325130561Sobrien suffix = "c"; 326130561Sobrien 327130561Sobrien snprintf (outfile_path, sizeof(outfile_path), outfile_template, 328130561Sobrien prefix, suffix); 32960484Sobrien 33060484Sobrien outfilename = outfile_path; 331130561Sobrien } 332130561Sobrien 333130561Sobrien prev_stdout = freopen (outfilename, "w+", stdout); 334130561Sobrien 335130561Sobrien if (prev_stdout == NULL) 336130561Sobrien lerr (_("could not create %s"), outfilename); 337130561Sobrien 33860484Sobrien outfile_created = 1; 33960484Sobrien } 340130561Sobrien 341130561Sobrien 342130561Sobrien /* Setup the filter chain. */ 343130561Sobrien output_chain = filter_create_int(NULL, filter_tee_header, headerfilename); 344130561Sobrien if ( !(m4 = getenv("M4"))) { 345130561Sobrien const char *slash; 346130561Sobrien m4 = M4; 34760484Sobrien if ((slash = strrchr(M4, '/')) != NULL) { 34860484Sobrien m4 = slash+1; 349130561Sobrien /* break up $PATH */ 35060484Sobrien const char *path = getenv("PATH"); 35160484Sobrien if (!path) { 352130561Sobrien m4 = M4; 353130561Sobrien } else { 354130561Sobrien int m4_length = strlen(m4); 355130561Sobrien do { 35660484Sobrien size_t length = strlen(path); 35760484Sobrien struct stat sbuf; 358218822Sdim 359218822Sdim const char *endOfDir = strchr(path, ':'); 360218822Sdim if (!endOfDir) 361218822Sdim endOfDir = path+length; 362218822Sdim 363218822Sdim { 364130561Sobrien char *m4_path = calloc(endOfDir-path + 1 + m4_length + 1, 1); 36560484Sobrien 36660484Sobrien memcpy(m4_path, path, endOfDir-path); 367130561Sobrien m4_path[endOfDir-path] = '/'; 368130561Sobrien memcpy(m4_path + (endOfDir-path) + 1, m4, m4_length + 1); 36989857Sobrien if (stat(m4_path, &sbuf) == 0 && 370130561Sobrien (S_ISREG(sbuf.st_mode)) && sbuf.st_mode & S_IXUSR) { 371130561Sobrien m4 = m4_path; 37277298Sobrien break; 373130561Sobrien } 374130561Sobrien free(m4_path); 37577298Sobrien } 376130561Sobrien path = endOfDir+1; 377130561Sobrien } while (path[0]); 37860484Sobrien if (!path[0]) 379218822Sdim m4 = M4; 380218822Sdim } 38160484Sobrien } 382130561Sobrien } 383130561Sobrien filter_create_ext(output_chain, m4, "-gP", 0); 38460484Sobrien filter_create_int(output_chain, filter_fix_linedirs, NULL); 385130561Sobrien 386130561Sobrien /* For debugging, only run the requested number of filters. */ 38760484Sobrien if (preproc_level > 0) { 388130561Sobrien filter_truncate(output_chain, preproc_level); 389130561Sobrien filter_apply_chain(output_chain); 39077298Sobrien } 391130561Sobrien yyout = stdout; 392130561Sobrien 39360484Sobrien 394130561Sobrien /* always generate the tablesverify flag. */ 395130561Sobrien buf_m4_define (&m4defs_buf, "M4_YY_TABLES_VERIFY", tablesverify ? "1" : "0"); 39660484Sobrien if (tablesext) 397130561Sobrien gentables = false; 398130561Sobrien 39960484Sobrien if (tablesverify) 400130561Sobrien /* force generation of C tables. */ 401130561Sobrien gentables = true; 40260484Sobrien 403130561Sobrien 404130561Sobrien if (tablesext) { 40577298Sobrien FILE *tablesout; 406218822Sdim struct yytbl_hdr hdr; 407218822Sdim char *pname = 0; 408218822Sdim size_t nbytes = 0; 409130561Sobrien 410130561Sobrien buf_m4_define (&m4defs_buf, "M4_YY_TABLES_EXTERNAL", NULL); 411130561Sobrien 412130561Sobrien if (!tablesfilename) { 413130561Sobrien nbytes = strlen (prefix) + strlen (tablesfile_template) + 2; 41477298Sobrien tablesfilename = pname = calloc(nbytes, 1); 415130561Sobrien snprintf (pname, nbytes, tablesfile_template, prefix); 416130561Sobrien } 41777298Sobrien 418130561Sobrien if ((tablesout = fopen (tablesfilename, "w")) == NULL) 419130561Sobrien lerr (_("could not create %s"), tablesfilename); 42089857Sobrien free(pname); 421218822Sdim tablesfilename = 0; 422218822Sdim 423218822Sdim yytbl_writer_init (&tableswr, tablesout); 424218822Sdim 425218822Sdim nbytes = strlen (prefix) + strlen ("tables") + 2; 426130561Sobrien tablesname = calloc(nbytes, 1); 42777298Sobrien snprintf (tablesname, nbytes, "%stables", prefix); 428130561Sobrien yytbl_hdr_init (&hdr, flex_version, tablesname); 429130561Sobrien 43077298Sobrien if (yytbl_hdr_fwrite (&tableswr, &hdr) <= 0) 431218822Sdim flexerror (_("could not write tables header")); 432218822Sdim } 433218822Sdim 434130561Sobrien if (skelname && (skelfile = fopen (skelname, "r")) == NULL) 435218822Sdim lerr (_("can't open skeleton file %s"), skelname); 43677298Sobrien 437130561Sobrien if (reentrant) { 438130561Sobrien buf_m4_define (&m4defs_buf, "M4_YY_REENTRANT", NULL); 43989857Sobrien if (yytext_is_array) 440130561Sobrien buf_m4_define (&m4defs_buf, "M4_YY_TEXT_IS_ARRAY", NULL); 441130561Sobrien } 44289857Sobrien 443130561Sobrien if ( bison_bridge_lval) 444130561Sobrien buf_m4_define (&m4defs_buf, "M4_YY_BISON_LVAL", NULL); 445 446 if ( bison_bridge_lloc) 447 buf_m4_define (&m4defs_buf, "<M4_YY_BISON_LLOC>", NULL); 448 449 if (strchr(prefix, '[') || strchr(prefix, ']')) 450 flexerror(_("Prefix cannot include '[' or ']'")); 451 buf_m4_define(&m4defs_buf, "M4_YY_PREFIX", prefix); 452 453 if (did_outfilename) 454 line_directive_out (stdout, 0); 455 456 if (do_yylineno) 457 buf_m4_define (&m4defs_buf, "M4_YY_USE_LINENO", NULL); 458 459 /* Create the alignment type. */ 460 buf_strdefine (&userdef_buf, "YY_INT_ALIGNED", 461 long_align ? "long int" : "short int"); 462 463 /* Define the start condition macros. */ 464 { 465 struct Buf tmpbuf; 466 buf_init(&tmpbuf, sizeof(char)); 467 for (i = 1; i <= lastsc; i++) { 468 char *str, *fmt = "#define %s %d\n"; 469 size_t strsz; 470 471 strsz = strlen(fmt) + strlen(scname[i]) + NUMCHARLINES + 2; 472 str = malloc(strsz); 473 if (!str) 474 flexfatal(_("allocation of macro definition failed")); 475 snprintf(str, strsz, fmt, scname[i], i - 1); 476 buf_strappend(&tmpbuf, str); 477 free(str); 478 } 479 buf_m4_define(&m4defs_buf, "M4_YY_SC_DEFS", tmpbuf.elts); 480 buf_destroy(&tmpbuf); 481 } 482 483 /* This is where we begin writing to the file. */ 484 485 /* Dump the %top code. */ 486 if( top_buf.elts) 487 outn((char*) top_buf.elts); 488 489 /* Dump the m4 definitions. */ 490 buf_print_strings(&m4defs_buf, stdout); 491 m4defs_buf.nelts = 0; /* memory leak here. */ 492 493 /* Place a bogus line directive, it will be fixed in the filter. */ 494 if (gen_line_dirs) 495 outn("#line 0 \"M4_YY_OUTFILE_NAME\"\n"); 496 497 /* Dump the user defined preproc directives. */ 498 if (userdef_buf.elts) 499 outn ((char *) (userdef_buf.elts)); 500 501 skelout (); 502 /* %% [1.0] */ 503} 504 505/* flexend - terminate flex 506 * 507 * note 508 * This routine does not return. 509 */ 510 511void flexend (int exit_status) 512{ 513 static int called_before = -1; /* prevent infinite recursion. */ 514 int tblsiz; 515 516 if (++called_before) 517 FLEX_EXIT (exit_status); 518 519 if (skelfile != NULL) { 520 if (ferror (skelfile)) 521 lerr (_("input error reading skeleton file %s"), 522 skelname); 523 524 else if (fclose (skelfile)) 525 lerr (_("error closing skeleton file %s"), 526 skelname); 527 } 528 529#if 0 530 fprintf (header_out, 531 "#ifdef YY_HEADER_EXPORT_START_CONDITIONS\n"); 532 fprintf (header_out, 533 "/* Beware! Start conditions are not prefixed. */\n"); 534 535 /* Special case for "INITIAL" */ 536 fprintf (header_out, 537 "#undef INITIAL\n#define INITIAL 0\n"); 538 for (i = 2; i <= lastsc; i++) 539 fprintf (header_out, "#define %s %d\n", scname[i], i - 1); 540 fprintf (header_out, 541 "#endif /* YY_HEADER_EXPORT_START_CONDITIONS */\n\n"); 542 543 /* Kill ALL flex-related macros. This is so the user 544 * can #include more than one generated header file. */ 545 fprintf (header_out, "#ifndef YY_HEADER_NO_UNDEFS\n"); 546 fprintf (header_out, 547 "/* Undefine all internal macros, etc., that do no belong in the header. */\n\n"); 548 549 { 550 const char * undef_list[] = { 551 552 "BEGIN", 553 "ECHO", 554 "EOB_ACT_CONTINUE_SCAN", 555 "EOB_ACT_END_OF_FILE", 556 "EOB_ACT_LAST_MATCH", 557 "FLEX_SCANNER", 558 "REJECT", 559 "YYFARGS0", 560 "YYFARGS1", 561 "YYFARGS2", 562 "YYFARGS3", 563 "YYLMAX", 564 "YYSTATE", 565 "YY_AT_BOL", 566 "YY_BREAK", 567 "YY_BUFFER_EOF_PENDING", 568 "YY_BUFFER_NEW", 569 "YY_BUFFER_NORMAL", 570 "YY_BUF_SIZE", 571 "M4_YY_CALL_LAST_ARG", 572 "M4_YY_CALL_ONLY_ARG", 573 "YY_CURRENT_BUFFER", 574 "YY_DECL", 575 "M4_YY_DECL_LAST_ARG", 576 "M4_YY_DEF_LAST_ARG", 577 "M4_YY_DEF_ONLY_ARG", 578 "YY_DO_BEFORE_ACTION", 579 "YY_END_OF_BUFFER", 580 "YY_END_OF_BUFFER_CHAR", 581 "YY_EXIT_FAILURE", 582 "YY_EXTRA_TYPE", 583 "YY_FATAL_ERROR", 584 "YY_FLEX_DEFINED_ECHO", 585 "YY_FLEX_LEX_COMPAT", 586 "YY_FLEX_MAJOR_VERSION", 587 "YY_FLEX_MINOR_VERSION", 588 "YY_FLEX_SUBMINOR_VERSION", 589 "YY_FLUSH_BUFFER", 590 "YY_G", 591 "YY_INPUT", 592 "YY_INTERACTIVE", 593 "YY_INT_ALIGNED", 594 "YY_LAST_ARG", 595 "YY_LESS_LINENO", 596 "YY_LEX_ARGS", 597 "YY_LEX_DECLARATION", 598 "YY_LEX_PROTO", 599 "YY_MAIN", 600 "YY_MORE_ADJ", 601 "YY_NEED_STRLEN", 602 "YY_NEW_FILE", 603 "YY_NULL", 604 "YY_NUM_RULES", 605 "YY_ONLY_ARG", 606 "YY_PARAMS", 607 "YY_PROTO", 608 "M4_YY_PROTO_LAST_ARG", 609 "M4_YY_PROTO_ONLY_ARG void", 610 "YY_READ_BUF_SIZE", 611 "YY_REENTRANT", 612 "YY_RESTORE_YY_MORE_OFFSET", 613 "YY_RULE_SETUP", 614 "YY_SC_TO_UI", 615 "YY_SKIP_YYWRAP", 616 "YY_START", 617 "YY_START_STACK_INCR", 618 "YY_STATE_EOF", 619 "YY_STDINIT", 620 "YY_TRAILING_HEAD_MASK", 621 "YY_TRAILING_MASK", 622 "YY_USER_ACTION", 623 "YY_USE_CONST", 624 "YY_USE_PROTOS", 625 "unput", 626 "yyTABLES_NAME", 627 "yy_create_buffer", 628 "yy_delete_buffer", 629 "yy_flex_debug", 630 "yy_flush_buffer", 631 "yy_init_buffer", 632 "yy_load_buffer_state", 633 "yy_new_buffer", 634 "yy_scan_buffer", 635 "yy_scan_bytes", 636 "yy_scan_string", 637 "yy_set_bol", 638 "yy_set_interactive", 639 "yy_switch_to_buffer", 640 "yypush_buffer_state", 641 "yypop_buffer_state", 642 "yyensure_buffer_stack", 643 "yyalloc", 644 "const", 645 "yyextra", 646 "yyfree", 647 "yyget_debug", 648 "yyget_extra", 649 "yyget_in", 650 "yyget_leng", 651 "yyget_lineno", 652 "yyget_lloc", 653 "yyget_lval", 654 "yyget_out", 655 "yyget_text", 656 "yyin", 657 "yyleng", 658 "yyless", 659 "yylex", 660 "yylex_destroy", 661 "yylex_init", 662 "yylex_init_extra", 663 "yylineno", 664 "yylloc", 665 "yylval", 666 "yymore", 667 "yyout", 668 "yyrealloc", 669 "yyrestart", 670 "yyset_debug", 671 "yyset_extra", 672 "yyset_in", 673 "yyset_lineno", 674 "yyset_lloc", 675 "yyset_lval", 676 "yyset_out", 677 "yytables_destroy", 678 "yytables_fload", 679 "yyterminate", 680 "yytext", 681 "yytext_ptr", 682 "yywrap", 683 684 /* must be null-terminated */ 685 NULL}; 686 687 688 for (i=0; undef_list[i] != NULL; i++) 689 fprintf (header_out, "#undef %s\n", undef_list[i]); 690 } 691 692 /* undef any of the auto-generated symbols. */ 693 for (i = 0; i < defs_buf.nelts; i++) { 694 695 /* don't undef start conditions */ 696 if (sclookup (((char **) defs_buf.elts)[i]) > 0) 697 continue; 698 fprintf (header_out, "#undef %s\n", 699 ((char **) defs_buf.elts)[i]); 700 } 701 702 fprintf (header_out, 703 "#endif /* !YY_HEADER_NO_UNDEFS */\n"); 704 fprintf (header_out, "\n"); 705 fprintf (header_out, "#undef %sIN_HEADER\n", prefix); 706 fprintf (header_out, "#endif /* %sHEADER_H */\n", prefix); 707 708 if (ferror (header_out)) 709 lerr (_("error creating header file %s"), 710 headerfilename); 711 fflush (header_out); 712 fclose (header_out); 713#endif 714 715 if (exit_status != 0 && outfile_created) { 716 if (ferror (stdout)) 717 lerr (_("error writing output file %s"), 718 outfilename); 719 720 else if ((_stdout_closed = 1) && fclose (stdout)) 721 lerr (_("error closing output file %s"), 722 outfilename); 723 724 else if (unlink (outfilename)) 725 lerr (_("error deleting output file %s"), 726 outfilename); 727 } 728 729 730 if (backing_up_report && backing_up_file) { 731 if (num_backing_up == 0) 732 fprintf (backing_up_file, _("No backing up.\n")); 733 else if (fullspd || fulltbl) 734 fprintf (backing_up_file, 735 _ 736 ("%d backing up (non-accepting) states.\n"), 737 num_backing_up); 738 else 739 fprintf (backing_up_file, 740 _("Compressed tables always back up.\n")); 741 742 if (ferror (backing_up_file)) 743 lerr (_("error writing backup file %s"), 744 backing_name); 745 746 else if (fclose (backing_up_file)) 747 lerr (_("error closing backup file %s"), 748 backing_name); 749 } 750 751 if (printstats) { 752 fprintf (stderr, _("%s version %s usage statistics:\n"), 753 program_name, flex_version); 754 755 fprintf (stderr, _(" scanner options: -")); 756 757 if (C_plus_plus) 758 putc ('+', stderr); 759 if (backing_up_report) 760 putc ('b', stderr); 761 if (ddebug) 762 putc ('d', stderr); 763 if (sf_case_ins()) 764 putc ('i', stderr); 765 if (lex_compat) 766 putc ('l', stderr); 767 if (posix_compat) 768 putc ('X', stderr); 769 if (performance_report > 0) 770 putc ('p', stderr); 771 if (performance_report > 1) 772 putc ('p', stderr); 773 if (spprdflt) 774 putc ('s', stderr); 775 if (reentrant) 776 fputs ("--reentrant", stderr); 777 if (bison_bridge_lval) 778 fputs ("--bison-bridge", stderr); 779 if (bison_bridge_lloc) 780 fputs ("--bison-locations", stderr); 781 if (use_stdout) 782 putc ('t', stderr); 783 if (printstats) 784 putc ('v', stderr); /* always true! */ 785 if (nowarn) 786 putc ('w', stderr); 787 if (interactive == false) 788 putc ('B', stderr); 789 if (interactive == true) 790 putc ('I', stderr); 791 if (!gen_line_dirs) 792 putc ('L', stderr); 793 if (trace) 794 putc ('T', stderr); 795 796 if (csize == unspecified) 797 /* We encountered an error fairly early on, so csize 798 * never got specified. Define it now, to prevent 799 * bogus table sizes being written out below. 800 */ 801 csize = 256; 802 803 if (csize == 128) 804 putc ('7', stderr); 805 else 806 putc ('8', stderr); 807 808 fprintf (stderr, " -C"); 809 810 if (long_align) 811 putc ('a', stderr); 812 if (fulltbl) 813 putc ('f', stderr); 814 if (fullspd) 815 putc ('F', stderr); 816 if (useecs) 817 putc ('e', stderr); 818 if (usemecs) 819 putc ('m', stderr); 820 if (use_read) 821 putc ('r', stderr); 822 823 if (did_outfilename) 824 fprintf (stderr, " -o%s", outfilename); 825 826 if (skelname) 827 fprintf (stderr, " -S%s", skelname); 828 829 if (strcmp (prefix, "yy")) 830 fprintf (stderr, " -P%s", prefix); 831 832 putc ('\n', stderr); 833 834 fprintf (stderr, _(" %d/%d NFA states\n"), 835 lastnfa, current_mns); 836 fprintf (stderr, _(" %d/%d DFA states (%d words)\n"), 837 lastdfa, current_max_dfas, totnst); 838 fprintf (stderr, _(" %d rules\n"), 839 num_rules + num_eof_rules - 840 1 /* - 1 for def. rule */ ); 841 842 if (num_backing_up == 0) 843 fprintf (stderr, _(" No backing up\n")); 844 else if (fullspd || fulltbl) 845 fprintf (stderr, 846 _ 847 (" %d backing-up (non-accepting) states\n"), 848 num_backing_up); 849 else 850 fprintf (stderr, 851 _ 852 (" Compressed tables always back-up\n")); 853 854 if (bol_needed) 855 fprintf (stderr, 856 _(" Beginning-of-line patterns used\n")); 857 858 fprintf (stderr, _(" %d/%d start conditions\n"), lastsc, 859 current_max_scs); 860 fprintf (stderr, 861 _ 862 (" %d epsilon states, %d double epsilon states\n"), 863 numeps, eps2); 864 865 if (lastccl == 0) 866 fprintf (stderr, _(" no character classes\n")); 867 else 868 fprintf (stderr, 869 _ 870 (" %d/%d character classes needed %d/%d words of storage, %d reused\n"), 871 lastccl, current_maxccls, 872 cclmap[lastccl] + ccllen[lastccl], 873 current_max_ccl_tbl_size, cclreuse); 874 875 fprintf (stderr, _(" %d state/nextstate pairs created\n"), 876 numsnpairs); 877 fprintf (stderr, 878 _(" %d/%d unique/duplicate transitions\n"), 879 numuniq, numdup); 880 881 if (fulltbl) { 882 tblsiz = lastdfa * numecs; 883 fprintf (stderr, _(" %d table entries\n"), 884 tblsiz); 885 } 886 887 else { 888 tblsiz = 2 * (lastdfa + numtemps) + 2 * tblend; 889 890 fprintf (stderr, 891 _(" %d/%d base-def entries created\n"), 892 lastdfa + numtemps, current_max_dfas); 893 fprintf (stderr, 894 _ 895 (" %d/%d (peak %d) nxt-chk entries created\n"), 896 tblend, current_max_xpairs, peakpairs); 897 fprintf (stderr, 898 _ 899 (" %d/%d (peak %d) template nxt-chk entries created\n"), 900 numtemps * nummecs, 901 current_max_template_xpairs, 902 numtemps * numecs); 903 fprintf (stderr, _(" %d empty table entries\n"), 904 nummt); 905 fprintf (stderr, _(" %d protos created\n"), 906 numprots); 907 fprintf (stderr, 908 _(" %d templates created, %d uses\n"), 909 numtemps, tmpuses); 910 } 911 912 if (useecs) { 913 tblsiz = tblsiz + csize; 914 fprintf (stderr, 915 _ 916 (" %d/%d equivalence classes created\n"), 917 numecs, csize); 918 } 919 920 if (usemecs) { 921 tblsiz = tblsiz + numecs; 922 fprintf (stderr, 923 _ 924 (" %d/%d meta-equivalence classes created\n"), 925 nummecs, csize); 926 } 927 928 fprintf (stderr, 929 _ 930 (" %d (%d saved) hash collisions, %d DFAs equal\n"), 931 hshcol, hshsave, dfaeql); 932 fprintf (stderr, _(" %d sets of reallocations needed\n"), 933 num_reallocs); 934 fprintf (stderr, _(" %d total table entries needed\n"), 935 tblsiz); 936 } 937 938 FLEX_EXIT (exit_status); 939} 940 941 942/* flexinit - initialize flex */ 943 944void flexinit (int argc, char **argv) 945{ 946 int i, sawcmpflag, rv, optind; 947 char *arg; 948 scanopt_t sopt; 949 950 printstats = syntaxerror = trace = spprdflt = false; 951 lex_compat = posix_compat = C_plus_plus = backing_up_report = 952 ddebug = fulltbl = false; 953 fullspd = long_align = nowarn = yymore_used = continued_action = 954 false; 955 do_yylineno = yytext_is_array = in_rule = reject = do_stdinit = 956 false; 957 yymore_really_used = reject_really_used = unspecified; 958 interactive = csize = unspecified; 959 do_yywrap = gen_line_dirs = usemecs = useecs = true; 960 reentrant = bison_bridge_lval = bison_bridge_lloc = false; 961 performance_report = 0; 962 did_outfilename = 0; 963 prefix = "yy"; 964 yyclass = 0; 965 use_read = use_stdout = false; 966 tablesext = tablesverify = false; 967 gentables = true; 968 tablesfilename = tablesname = NULL; 969 970 sawcmpflag = false; 971 972 /* Initialize dynamic array for holding the rule actions. */ 973 action_size = 2048; /* default size of action array in bytes */ 974 action_array = allocate_character_array (action_size); 975 defs1_offset = prolog_offset = action_offset = action_index = 0; 976 action_array[0] = '\0'; 977 978 /* Initialize any buffers. */ 979 buf_init (&userdef_buf, sizeof (char)); /* one long string */ 980 buf_init (&defs_buf, sizeof (char *)); /* list of strings */ 981 buf_init (&yydmap_buf, sizeof (char)); /* one long string */ 982 buf_init (&top_buf, sizeof (char)); /* one long string */ 983 984 { 985 const char * m4defs_init_str[] = {"m4_changequote\n", 986 "m4_changequote([[, ]])\n"}; 987 buf_init (&m4defs_buf, sizeof (char *)); 988 buf_append (&m4defs_buf, &m4defs_init_str, 2); 989 } 990 991 sf_init (); 992 993 /* initialize regex lib */ 994 flex_init_regex(); 995 996 /* Enable C++ if program name ends with '+'. */ 997 program_name = basename (argv[0]); 998 999 if (program_name != NULL && 1000 program_name[strlen (program_name) - 1] == '+') 1001 C_plus_plus = true; 1002 1003 /* read flags */ 1004 sopt = scanopt_init (flexopts, argc, argv, 0); 1005 if (!sopt) { 1006 /* This will only happen when flexopts array is altered. */ 1007 fprintf (stderr, 1008 _("Internal error. flexopts are malformed.\n")); 1009 FLEX_EXIT (1); 1010 } 1011 1012 while ((rv = scanopt (sopt, &arg, &optind)) != 0) { 1013 1014 if (rv < 0) { 1015 /* Scanopt has already printed an option-specific error message. */ 1016 fprintf (stderr, 1017 _ 1018 ("Try `%s --help' for more information.\n"), 1019 program_name); 1020 FLEX_EXIT (1); 1021 } 1022 1023 switch ((enum flexopt_flag_t) rv) { 1024 case OPT_CPLUSPLUS: 1025 C_plus_plus = true; 1026 break; 1027 1028 case OPT_BATCH: 1029 interactive = false; 1030 break; 1031 1032 case OPT_BACKUP: 1033 backing_up_report = true; 1034 break; 1035 1036 case OPT_DONOTHING: 1037 break; 1038 1039 case OPT_COMPRESSION: 1040 if (!sawcmpflag) { 1041 useecs = false; 1042 usemecs = false; 1043 fulltbl = false; 1044 sawcmpflag = true; 1045 } 1046 1047 for (i = 0; arg && arg[i] != '\0'; i++) 1048 switch (arg[i]) { 1049 case 'a': 1050 long_align = true; 1051 break; 1052 1053 case 'e': 1054 useecs = true; 1055 break; 1056 1057 case 'F': 1058 fullspd = true; 1059 break; 1060 1061 case 'f': 1062 fulltbl = true; 1063 break; 1064 1065 case 'm': 1066 usemecs = true; 1067 break; 1068 1069 case 'r': 1070 use_read = true; 1071 break; 1072 1073 default: 1074 lerr (_ 1075 ("unknown -C option '%c'"), 1076 arg[i]); 1077 break; 1078 } 1079 break; 1080 1081 case OPT_DEBUG: 1082 ddebug = true; 1083 break; 1084 1085 case OPT_NO_DEBUG: 1086 ddebug = false; 1087 break; 1088 1089 case OPT_FULL: 1090 useecs = usemecs = false; 1091 use_read = fulltbl = true; 1092 break; 1093 1094 case OPT_FAST: 1095 useecs = usemecs = false; 1096 use_read = fullspd = true; 1097 break; 1098 1099 case OPT_HELP: 1100 usage (); 1101 FLEX_EXIT (0); 1102 1103 case OPT_INTERACTIVE: 1104 interactive = true; 1105 break; 1106 1107 case OPT_CASE_INSENSITIVE: 1108 sf_set_case_ins(true); 1109 break; 1110 1111 case OPT_LEX_COMPAT: 1112 lex_compat = true; 1113 break; 1114 1115 case OPT_POSIX_COMPAT: 1116 posix_compat = true; 1117 break; 1118 1119 case OPT_PREPROC_LEVEL: 1120 preproc_level = (int) strtol(arg,NULL,0); 1121 break; 1122 1123 case OPT_MAIN: 1124 buf_strdefine (&userdef_buf, "YY_MAIN", "1"); 1125 do_yywrap = false; 1126 break; 1127 1128 case OPT_NO_MAIN: 1129 buf_strdefine (&userdef_buf, "YY_MAIN", "0"); 1130 break; 1131 1132 case OPT_NO_LINE: 1133 gen_line_dirs = false; 1134 break; 1135 1136 case OPT_OUTFILE: 1137 outfilename = arg; 1138 did_outfilename = 1; 1139 break; 1140 1141 case OPT_PREFIX: 1142 prefix = arg; 1143 break; 1144 1145 case OPT_PERF_REPORT: 1146 ++performance_report; 1147 break; 1148 1149 case OPT_BISON_BRIDGE: 1150 bison_bridge_lval = true; 1151 break; 1152 1153 case OPT_BISON_BRIDGE_LOCATIONS: 1154 bison_bridge_lval = bison_bridge_lloc = true; 1155 break; 1156 1157 case OPT_REENTRANT: 1158 reentrant = true; 1159 break; 1160 1161 case OPT_NO_REENTRANT: 1162 reentrant = false; 1163 break; 1164 1165 case OPT_SKEL: 1166 skelname = arg; 1167 break; 1168 1169 case OPT_DEFAULT: 1170 spprdflt = false; 1171 break; 1172 1173 case OPT_NO_DEFAULT: 1174 spprdflt = true; 1175 break; 1176 1177 case OPT_STDOUT: 1178 use_stdout = true; 1179 break; 1180 1181 case OPT_NO_UNISTD_H: 1182 //buf_strdefine (&userdef_buf, "YY_NO_UNISTD_H", "1"); 1183 buf_m4_define( &m4defs_buf, "M4_YY_NO_UNISTD_H",0); 1184 break; 1185 1186 case OPT_TABLES_FILE: 1187 tablesext = true; 1188 tablesfilename = arg; 1189 break; 1190 1191 case OPT_TABLES_VERIFY: 1192 tablesverify = true; 1193 break; 1194 1195 case OPT_TRACE: 1196 trace = true; 1197 break; 1198 1199 case OPT_VERBOSE: 1200 printstats = true; 1201 break; 1202 1203 case OPT_VERSION: 1204 printf (_("%s %s\n"), program_name, flex_version); 1205 FLEX_EXIT (0); 1206 1207 case OPT_WARN: 1208 nowarn = false; 1209 break; 1210 1211 case OPT_NO_WARN: 1212 nowarn = true; 1213 break; 1214 1215 case OPT_7BIT: 1216 csize = 128; 1217 break; 1218 1219 case OPT_8BIT: 1220 csize = CSIZE; 1221 break; 1222 1223 case OPT_ALIGN: 1224 long_align = true; 1225 break; 1226 1227 case OPT_NO_ALIGN: 1228 long_align = false; 1229 break; 1230 1231 case OPT_ALWAYS_INTERACTIVE: 1232 buf_m4_define (&m4defs_buf, "M4_YY_ALWAYS_INTERACTIVE", 0); 1233 break; 1234 1235 case OPT_NEVER_INTERACTIVE: 1236 buf_m4_define( &m4defs_buf, "M4_YY_NEVER_INTERACTIVE", 0); 1237 break; 1238 1239 case OPT_ARRAY: 1240 yytext_is_array = true; 1241 break; 1242 1243 case OPT_POINTER: 1244 yytext_is_array = false; 1245 break; 1246 1247 case OPT_ECS: 1248 useecs = true; 1249 break; 1250 1251 case OPT_NO_ECS: 1252 useecs = false; 1253 break; 1254 1255 case OPT_HEADER_FILE: 1256 headerfilename = arg; 1257 break; 1258 1259 case OPT_META_ECS: 1260 usemecs = true; 1261 break; 1262 1263 case OPT_NO_META_ECS: 1264 usemecs = false; 1265 break; 1266 1267 case OPT_PREPROCDEFINE: 1268 { 1269 /* arg is "symbol" or "symbol=definition". */ 1270 char *def; 1271 1272 for (def = arg; 1273 *def != '\0' && *def != '='; ++def) ; 1274 1275 buf_strappend (&userdef_buf, "#define "); 1276 if (*def == '\0') { 1277 buf_strappend (&userdef_buf, arg); 1278 buf_strappend (&userdef_buf, 1279 " 1\n"); 1280 } 1281 else { 1282 buf_strnappend (&userdef_buf, arg, 1283 (int) (def - arg)); 1284 buf_strappend (&userdef_buf, " "); 1285 buf_strappend (&userdef_buf, 1286 def + 1); 1287 buf_strappend (&userdef_buf, "\n"); 1288 } 1289 } 1290 break; 1291 1292 case OPT_READ: 1293 use_read = true; 1294 break; 1295 1296 case OPT_STACK: 1297 //buf_strdefine (&userdef_buf, "YY_STACK_USED", "1"); 1298 buf_m4_define( &m4defs_buf, "M4_YY_STACK_USED",0); 1299 break; 1300 1301 case OPT_STDINIT: 1302 do_stdinit = true; 1303 break; 1304 1305 case OPT_NO_STDINIT: 1306 do_stdinit = false; 1307 break; 1308 1309 case OPT_YYCLASS: 1310 yyclass = arg; 1311 break; 1312 1313 case OPT_YYLINENO: 1314 do_yylineno = true; 1315 break; 1316 1317 case OPT_NO_YYLINENO: 1318 do_yylineno = false; 1319 break; 1320 1321 case OPT_YYWRAP: 1322 do_yywrap = true; 1323 break; 1324 1325 case OPT_NO_YYWRAP: 1326 do_yywrap = false; 1327 break; 1328 1329 case OPT_YYMORE: 1330 yymore_really_used = true; 1331 break; 1332 1333 case OPT_NO_YYMORE: 1334 yymore_really_used = false; 1335 break; 1336 1337 case OPT_REJECT: 1338 reject_really_used = true; 1339 break; 1340 1341 case OPT_NO_REJECT: 1342 reject_really_used = false; 1343 break; 1344 1345 case OPT_NO_YY_PUSH_STATE: 1346 //buf_strdefine (&userdef_buf, "YY_NO_PUSH_STATE", "1"); 1347 buf_m4_define( &m4defs_buf, "M4_YY_NO_PUSH_STATE",0); 1348 break; 1349 case OPT_NO_YY_POP_STATE: 1350 //buf_strdefine (&userdef_buf, "YY_NO_POP_STATE", "1"); 1351 buf_m4_define( &m4defs_buf, "M4_YY_NO_POP_STATE",0); 1352 break; 1353 case OPT_NO_YY_TOP_STATE: 1354 //buf_strdefine (&userdef_buf, "YY_NO_TOP_STATE", "1"); 1355 buf_m4_define( &m4defs_buf, "M4_YY_NO_TOP_STATE",0); 1356 break; 1357 case OPT_NO_UNPUT: 1358 //buf_strdefine (&userdef_buf, "YY_NO_UNPUT", "1"); 1359 buf_m4_define( &m4defs_buf, "M4_YY_NO_UNPUT",0); 1360 break; 1361 case OPT_NO_YY_SCAN_BUFFER: 1362 //buf_strdefine (&userdef_buf, "YY_NO_SCAN_BUFFER", "1"); 1363 buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_BUFFER",0); 1364 break; 1365 case OPT_NO_YY_SCAN_BYTES: 1366 //buf_strdefine (&userdef_buf, "YY_NO_SCAN_BYTES", "1"); 1367 buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_BYTES",0); 1368 break; 1369 case OPT_NO_YY_SCAN_STRING: 1370 //buf_strdefine (&userdef_buf, "YY_NO_SCAN_STRING", "1"); 1371 buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_STRING",0); 1372 break; 1373 case OPT_NO_YYGET_EXTRA: 1374 //buf_strdefine (&userdef_buf, "YY_NO_GET_EXTRA", "1"); 1375 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_EXTRA",0); 1376 break; 1377 case OPT_NO_YYSET_EXTRA: 1378 //buf_strdefine (&userdef_buf, "YY_NO_SET_EXTRA", "1"); 1379 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_EXTRA",0); 1380 break; 1381 case OPT_NO_YYGET_LENG: 1382 //buf_strdefine (&userdef_buf, "YY_NO_GET_LENG", "1"); 1383 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LENG",0); 1384 break; 1385 case OPT_NO_YYGET_TEXT: 1386 //buf_strdefine (&userdef_buf, "YY_NO_GET_TEXT", "1"); 1387 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_TEXT",0); 1388 break; 1389 case OPT_NO_YYGET_LINENO: 1390 //buf_strdefine (&userdef_buf, "YY_NO_GET_LINENO", "1"); 1391 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LINENO",0); 1392 break; 1393 case OPT_NO_YYSET_LINENO: 1394 //buf_strdefine (&userdef_buf, "YY_NO_SET_LINENO", "1"); 1395 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LINENO",0); 1396 break; 1397 case OPT_NO_YYGET_IN: 1398 //buf_strdefine (&userdef_buf, "YY_NO_GET_IN", "1"); 1399 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_IN",0); 1400 break; 1401 case OPT_NO_YYSET_IN: 1402 //buf_strdefine (&userdef_buf, "YY_NO_SET_IN", "1"); 1403 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_IN",0); 1404 break; 1405 case OPT_NO_YYGET_OUT: 1406 //buf_strdefine (&userdef_buf, "YY_NO_GET_OUT", "1"); 1407 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_OUT",0); 1408 break; 1409 case OPT_NO_YYSET_OUT: 1410 //buf_strdefine (&userdef_buf, "YY_NO_SET_OUT", "1"); 1411 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_OUT",0); 1412 break; 1413 case OPT_NO_YYGET_LVAL: 1414 //buf_strdefine (&userdef_buf, "YY_NO_GET_LVAL", "1"); 1415 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LVAL",0); 1416 break; 1417 case OPT_NO_YYSET_LVAL: 1418 //buf_strdefine (&userdef_buf, "YY_NO_SET_LVAL", "1"); 1419 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LVAL",0); 1420 break; 1421 case OPT_NO_YYGET_LLOC: 1422 //buf_strdefine (&userdef_buf, "YY_NO_GET_LLOC", "1"); 1423 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LLOC",0); 1424 break; 1425 case OPT_NO_YYSET_LLOC: 1426 //buf_strdefine (&userdef_buf, "YY_NO_SET_LLOC", "1"); 1427 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LLOC",0); 1428 break; 1429 case OPT_HEX: 1430 trace_hex = 1; 1431 break; 1432 case OPT_NO_SECT3_ESCAPE: 1433 no_section3_escape = true; 1434 break; 1435 } /* switch */ 1436 } /* while scanopt() */ 1437 1438 scanopt_destroy (sopt); 1439 1440 num_input_files = argc - optind; 1441 input_files = argv + optind; 1442 set_input_file (num_input_files > 0 ? input_files[0] : NULL); 1443 1444 lastccl = lastsc = lastdfa = lastnfa = 0; 1445 num_rules = num_eof_rules = default_rule = 0; 1446 numas = numsnpairs = tmpuses = 0; 1447 numecs = numeps = eps2 = num_reallocs = hshcol = dfaeql = totnst = 1448 0; 1449 numuniq = numdup = hshsave = eofseen = datapos = dataline = 0; 1450 num_backing_up = onesp = numprots = 0; 1451 variable_trailing_context_rules = bol_needed = false; 1452 1453 linenum = sectnum = 1; 1454 firstprot = NIL; 1455 1456 /* Used in mkprot() so that the first proto goes in slot 1 1457 * of the proto queue. 1458 */ 1459 lastprot = 1; 1460 1461 set_up_initial_allocations (); 1462} 1463 1464 1465/* readin - read in the rules section of the input file(s) */ 1466 1467void readin (void) 1468{ 1469 static char yy_stdinit[] = "FILE *yyin = stdin, *yyout = stdout;"; 1470 static char yy_nostdinit[] = 1471 "FILE *yyin = NULL, *yyout = NULL;"; 1472 1473 line_directive_out(NULL, 1); 1474 1475 if (yyparse ()) { 1476 pinpoint_message (_("fatal parse error")); 1477 flexend (1); 1478 } 1479 1480 if (syntaxerror) 1481 flexend (1); 1482 1483 /* If the user explicitly requested posix compatibility by specifing the 1484 * posix-compat option, then we check for conflicting options. However, if 1485 * the POSIXLY_CORRECT variable is set, then we quietly make flex as 1486 * posix-compatible as possible. This is the recommended behavior 1487 * according to the GNU Coding Standards. 1488 * 1489 * Note: The posix option was added to flex to provide the posix behavior 1490 * of the repeat operator in regular expressions, e.g., `ab{3}' 1491 */ 1492 if (posix_compat) { 1493 /* TODO: This is where we try to make flex behave according to 1494 * posiz, AND check for conflicting options. How far should we go 1495 * with this? Should we disable all the neat-o flex features? 1496 */ 1497 /* Update: Estes says no, since other flex features don't violate posix. */ 1498 } 1499 1500 if (getenv ("POSIXLY_CORRECT")) { 1501 posix_compat = true; 1502 } 1503 1504 if (backing_up_report) { 1505 backing_up_file = fopen (backing_name, "w"); 1506 if (backing_up_file == NULL) 1507 lerr (_ 1508 ("could not create backing-up info file %s"), 1509 backing_name); 1510 } 1511 1512 else 1513 backing_up_file = NULL; 1514 1515 if (yymore_really_used == true) 1516 yymore_used = true; 1517 else if (yymore_really_used == false) 1518 yymore_used = false; 1519 1520 if (reject_really_used == true) 1521 reject = true; 1522 else if (reject_really_used == false) 1523 reject = false; 1524 1525 if (performance_report > 0) { 1526 if (lex_compat) { 1527 fprintf (stderr, 1528 _ 1529 ("-l AT&T lex compatibility option entails a large performance penalty\n")); 1530 fprintf (stderr, 1531 _ 1532 (" and may be the actual source of other reported performance penalties\n")); 1533 } 1534 1535 else if (do_yylineno) { 1536 fprintf (stderr, 1537 _ 1538 ("%%option yylineno entails a performance penalty ONLY on rules that can match newline characters\n")); 1539 } 1540 1541 if (performance_report > 1) { 1542 if (interactive) 1543 fprintf (stderr, 1544 _ 1545 ("-I (interactive) entails a minor performance penalty\n")); 1546 1547 if (yymore_used) 1548 fprintf (stderr, 1549 _ 1550 ("yymore() entails a minor performance penalty\n")); 1551 } 1552 1553 if (reject) 1554 fprintf (stderr, 1555 _ 1556 ("REJECT entails a large performance penalty\n")); 1557 1558 if (variable_trailing_context_rules) 1559 fprintf (stderr, 1560 _ 1561 ("Variable trailing context rules entail a large performance penalty\n")); 1562 } 1563 1564 if (reject) 1565 real_reject = true; 1566 1567 if (variable_trailing_context_rules) 1568 reject = true; 1569 1570 if ((fulltbl || fullspd) && reject) { 1571 if (real_reject) 1572 flexerror (_ 1573 ("REJECT cannot be used with -f or -F")); 1574 else if (do_yylineno) 1575 flexerror (_ 1576 ("%option yylineno cannot be used with REJECT")); 1577 else 1578 flexerror (_ 1579 ("variable trailing context rules cannot be used with -f or -F")); 1580 } 1581 1582 if (reject){ 1583 out_m4_define( "M4_YY_USES_REJECT", NULL); 1584 //outn ("\n#define YY_USES_REJECT"); 1585 } 1586 1587 if (!do_yywrap) { 1588 if (!C_plus_plus) { 1589 if (reentrant) 1590 out_str ("\n#define %swrap(yyscanner) (/*CONSTCOND*/1)\n", prefix); 1591 else 1592 out_str ("\n#define %swrap() (/*CONSTCOND*/1)\n", prefix); 1593 } 1594 outn ("#define YY_SKIP_YYWRAP"); 1595 } 1596 1597 if (ddebug) 1598 outn ("\n#define FLEX_DEBUG"); 1599 1600 OUT_BEGIN_CODE (); 1601 outn ("typedef flex_uint8_t YY_CHAR;"); 1602 OUT_END_CODE (); 1603 1604 if (C_plus_plus) { 1605 outn ("#define yytext_ptr yytext"); 1606 1607 if (interactive) 1608 outn ("#define YY_INTERACTIVE"); 1609 } 1610 1611 else { 1612 OUT_BEGIN_CODE (); 1613 /* In reentrant scanner, stdinit is handled in flex.skl. */ 1614 if (do_stdinit) { 1615 if (reentrant){ 1616 outn ("#ifdef VMS"); 1617 outn ("#ifdef __VMS_POSIX"); 1618 outn ("#define YY_STDINIT"); 1619 outn ("#endif"); 1620 outn ("#else"); 1621 outn ("#define YY_STDINIT"); 1622 outn ("#endif"); 1623 } 1624 1625 outn ("#ifdef VMS"); 1626 outn ("#ifndef __VMS_POSIX"); 1627 outn (yy_nostdinit); 1628 outn ("#else"); 1629 outn (yy_stdinit); 1630 outn ("#endif"); 1631 outn ("#else"); 1632 outn (yy_stdinit); 1633 outn ("#endif"); 1634 } 1635 1636 else { 1637 if(!reentrant) 1638 outn (yy_nostdinit); 1639 } 1640 OUT_END_CODE (); 1641 } 1642 1643 OUT_BEGIN_CODE (); 1644 if (fullspd) 1645 outn ("typedef const struct yy_trans_info *yy_state_type;"); 1646 else if (!C_plus_plus) 1647 outn ("typedef int yy_state_type;"); 1648 OUT_END_CODE (); 1649 1650 if (lex_compat) 1651 outn ("#define YY_FLEX_LEX_COMPAT"); 1652 1653 if (!C_plus_plus && !reentrant) { 1654 outn ("extern int yylineno;"); 1655 OUT_BEGIN_CODE (); 1656 outn ("int yylineno = 1;"); 1657 OUT_END_CODE (); 1658 } 1659 1660 if (C_plus_plus) { 1661 outn ("\n#include <FlexLexer.h>"); 1662 1663 if (!do_yywrap) { 1664 outn("\nint yyFlexLexer::yywrap() { return 1; }"); 1665 } 1666 1667 if (yyclass) { 1668 outn ("int yyFlexLexer::yylex()"); 1669 outn ("\t{"); 1670 outn ("\tLexerError( \"yyFlexLexer::yylex invoked but %option yyclass used\" );"); 1671 outn ("\treturn 0;"); 1672 outn ("\t}"); 1673 1674 out_str ("\n#define YY_DECL int %s::yylex()\n", 1675 yyclass); 1676 } 1677 } 1678 1679 else { 1680 1681 /* Watch out: yytext_ptr is a variable when yytext is an array, 1682 * but it's a macro when yytext is a pointer. 1683 */ 1684 if (yytext_is_array) { 1685 if (!reentrant) 1686 outn ("extern char yytext[];\n"); 1687 } 1688 else { 1689 if (reentrant) { 1690 outn ("#define yytext_ptr yytext_r"); 1691 } 1692 else { 1693 outn ("extern char *yytext;"); 1694 1695 outn("#ifdef yytext_ptr"); 1696 outn("#undef yytext_ptr"); 1697 outn("#endif"); 1698 outn ("#define yytext_ptr yytext"); 1699 } 1700 } 1701 1702 if (yyclass) 1703 flexerror (_ 1704 ("%option yyclass only meaningful for C++ scanners")); 1705 } 1706 1707 if (useecs) 1708 numecs = cre8ecs (nextecm, ecgroup, csize); 1709 else 1710 numecs = csize; 1711 1712 /* Now map the equivalence class for NUL to its expected place. */ 1713 ecgroup[0] = ecgroup[csize]; 1714 NUL_ec = ABS (ecgroup[0]); 1715 1716 if (useecs) 1717 ccl2ecl (); 1718} 1719 1720 1721/* set_up_initial_allocations - allocate memory for internal tables */ 1722 1723void set_up_initial_allocations (void) 1724{ 1725 maximum_mns = (long_align ? MAXIMUM_MNS_LONG : MAXIMUM_MNS); 1726 current_mns = INITIAL_MNS; 1727 firstst = allocate_integer_array (current_mns); 1728 lastst = allocate_integer_array (current_mns); 1729 finalst = allocate_integer_array (current_mns); 1730 transchar = allocate_integer_array (current_mns); 1731 trans1 = allocate_integer_array (current_mns); 1732 trans2 = allocate_integer_array (current_mns); 1733 accptnum = allocate_integer_array (current_mns); 1734 assoc_rule = allocate_integer_array (current_mns); 1735 state_type = allocate_integer_array (current_mns); 1736 1737 current_max_rules = INITIAL_MAX_RULES; 1738 rule_type = allocate_integer_array (current_max_rules); 1739 rule_linenum = allocate_integer_array (current_max_rules); 1740 rule_useful = allocate_integer_array (current_max_rules); 1741 rule_has_nl = allocate_bool_array (current_max_rules); 1742 1743 current_max_scs = INITIAL_MAX_SCS; 1744 scset = allocate_integer_array (current_max_scs); 1745 scbol = allocate_integer_array (current_max_scs); 1746 scxclu = allocate_integer_array (current_max_scs); 1747 sceof = allocate_integer_array (current_max_scs); 1748 scname = allocate_char_ptr_array (current_max_scs); 1749 1750 current_maxccls = INITIAL_MAX_CCLS; 1751 cclmap = allocate_integer_array (current_maxccls); 1752 ccllen = allocate_integer_array (current_maxccls); 1753 cclng = allocate_integer_array (current_maxccls); 1754 ccl_has_nl = allocate_bool_array (current_maxccls); 1755 1756 current_max_ccl_tbl_size = INITIAL_MAX_CCL_TBL_SIZE; 1757 ccltbl = allocate_Character_array (current_max_ccl_tbl_size); 1758 1759 current_max_dfa_size = INITIAL_MAX_DFA_SIZE; 1760 1761 current_max_xpairs = INITIAL_MAX_XPAIRS; 1762 nxt = allocate_integer_array (current_max_xpairs); 1763 chk = allocate_integer_array (current_max_xpairs); 1764 1765 current_max_template_xpairs = INITIAL_MAX_TEMPLATE_XPAIRS; 1766 tnxt = allocate_integer_array (current_max_template_xpairs); 1767 1768 current_max_dfas = INITIAL_MAX_DFAS; 1769 base = allocate_integer_array (current_max_dfas); 1770 def = allocate_integer_array (current_max_dfas); 1771 dfasiz = allocate_integer_array (current_max_dfas); 1772 accsiz = allocate_integer_array (current_max_dfas); 1773 dhash = allocate_integer_array (current_max_dfas); 1774 dss = allocate_int_ptr_array (current_max_dfas); 1775 dfaacc = allocate_dfaacc_union (current_max_dfas); 1776 1777 nultrans = NULL; 1778} 1779 1780 1781void usage (void) 1782{ 1783 FILE *f = stdout; 1784 1785 if (!did_outfilename) { 1786 snprintf (outfile_path, sizeof(outfile_path), outfile_template, 1787 prefix, C_plus_plus ? "cc" : "c"); 1788 outfilename = outfile_path; 1789 } 1790 1791 fprintf (f, _("Usage: %s [OPTIONS] [FILE]...\n"), program_name); 1792 fprintf (f, 1793 _ 1794 ("Generates programs that perform pattern-matching on text.\n" 1795 "\n" "Table Compression:\n" 1796 " -Ca, --align trade off larger tables for better memory alignment\n" 1797 " -Ce, --ecs construct equivalence classes\n" 1798 " -Cf do not compress tables; use -f representation\n" 1799 " -CF do not compress tables; use -F representation\n" 1800 " -Cm, --meta-ecs construct meta-equivalence classes\n" 1801 " -Cr, --read use read() instead of stdio for scanner input\n" 1802 " -f, --full generate fast, large scanner. Same as -Cfr\n" 1803 " -F, --fast use alternate table representation. Same as -CFr\n" 1804 " -Cem default compression (same as --ecs --meta-ecs)\n" 1805 "\n" "Debugging:\n" 1806 " -d, --debug enable debug mode in scanner\n" 1807 " -b, --backup write backing-up information to %s\n" 1808 " -p, --perf-report write performance report to stderr\n" 1809 " -s, --nodefault suppress default rule to ECHO unmatched text\n" 1810 " -T, --trace %s should run in trace mode\n" 1811 " -w, --nowarn do not generate warnings\n" 1812 " -v, --verbose write summary of scanner statistics to stdout\n" 1813 " --hex use hexadecimal numbers instead of octal in debug outputs\n" 1814 "\n" "Files:\n" 1815 " -o, --outfile=FILE specify output filename\n" 1816 " -S, --skel=FILE specify skeleton file\n" 1817 " -t, --stdout write scanner on stdout instead of %s\n" 1818 " --yyclass=NAME name of C++ class\n" 1819 " --header-file=FILE create a C header file in addition to the scanner\n" 1820 " --tables-file[=FILE] write tables to FILE\n" "\n" 1821 "Scanner behavior:\n" 1822 " -7, --7bit generate 7-bit scanner\n" 1823 " -8, --8bit generate 8-bit scanner\n" 1824 " -B, --batch generate batch scanner (opposite of -I)\n" 1825 " -i, --case-insensitive ignore case in patterns\n" 1826 " -l, --lex-compat maximal compatibility with original lex\n" 1827 " -X, --posix-compat maximal compatibility with POSIX lex\n" 1828 " -I, --interactive generate interactive scanner (opposite of -B)\n" 1829 " --yylineno track line count in yylineno\n" 1830 "\n" "Generated code:\n" 1831 " -+, --c++ generate C++ scanner class\n" 1832 " -Dmacro[=defn] #define macro defn (default defn is '1')\n" 1833 " -L, --noline suppress #line directives in scanner\n" 1834 " -P, --prefix=STRING use STRING as prefix instead of \"yy\"\n" 1835 " -R, --reentrant generate a reentrant C scanner\n" 1836 " --bison-bridge scanner for bison pure parser.\n" 1837 " --bison-locations include yylloc support.\n" 1838 " --stdinit initialize yyin/yyout to stdin/stdout\n" 1839 " --nounistd do not include <unistd.h>\n" 1840 " --noFUNCTION do not generate a particular FUNCTION\n" 1841 "\n" "Miscellaneous:\n" 1842 " -c do-nothing POSIX option\n" 1843 " -n do-nothing POSIX option\n" 1844 " -?\n" 1845 " -h, --help produce this help message\n" 1846 " -V, --version report %s version\n"), 1847 backing_name, program_name, outfile_path, program_name); 1848 1849} 1850