1 /* $NetBSD: main.c,v 1.2 2016/01/09 17:38:57 christos Exp $ */ 2 3 /* flex - tool to generate fast lexical analyzers */ 4 5 /* Copyright (c) 1990 The Regents of the University of California. */ 6 /* All rights reserved. */ 7 8 /* This code is derived from software contributed to Berkeley by */ 9 /* Vern Paxson. */ 10 11 /* The United States Government has rights in this work pursuant */ 12 /* to contract no. DE-AC03-76SF00098 between the United States */ 13 /* Department of Energy and the University of California. */ 14 15 /* This file is part of flex. */ 16 17 /* Redistribution and use in source and binary forms, with or without */ 18 /* modification, are permitted provided that the following conditions */ 19 /* are met: */ 20 21 /* 1. Redistributions of source code must retain the above copyright */ 22 /* notice, this list of conditions and the following disclaimer. */ 23 /* 2. Redistributions in binary form must reproduce the above copyright */ 24 /* notice, this list of conditions and the following disclaimer in the */ 25 /* documentation and/or other materials provided with the distribution. */ 26 27 /* Neither the name of the University nor the names of its contributors */ 28 /* may be used to endorse or promote products derived from this software */ 29 /* without specific prior written permission. */ 30 31 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */ 32 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */ 33 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */ 34 /* PURPOSE. */ 35 #include "flexdef.h" 36 __RCSID("$NetBSD: main.c,v 1.2 2016/01/09 17:38:57 christos Exp $"); 37 38 39 #include "version.h" 40 #include "options.h" 41 #include "tables.h" 42 43 static char flex_version[] = FLEX_VERSION; 44 45 /* declare functions that have forward references */ 46 47 void flexinit PROTO ((int, char **)); 48 void readin PROTO ((void)); 49 void set_up_initial_allocations PROTO ((void)); 50 static char *basename2 PROTO ((char *path, int should_strip_ext)); 51 52 53 /* these globals are all defined and commented in flexdef.h */ 54 int printstats, syntaxerror, eofseen, ddebug, trace, nowarn, spprdflt; 55 int interactive, lex_compat, posix_compat, do_yylineno, 56 useecs, fulltbl, usemecs; 57 int fullspd, gen_line_dirs, performance_report, backing_up_report; 58 int C_plus_plus, long_align, use_read, yytext_is_array, do_yywrap, 59 csize; 60 int reentrant, bison_bridge_lval, bison_bridge_lloc; 61 int yymore_used, reject, real_reject, continued_action, in_rule; 62 int yymore_really_used, reject_really_used; 63 int trace_hex = 0; 64 int datapos, dataline, linenum; 65 FILE *skelfile = NULL; 66 int skel_ind = 0; 67 char *action_array; 68 int action_size, defs1_offset, prolog_offset, action_offset, 69 action_index; 70 char *infilename = NULL, *outfilename = NULL, *headerfilename = NULL; 71 int did_outfilename; 72 char *prefix, *yyclass, *extra_type = NULL; 73 int do_stdinit, use_stdout; 74 int onestate[ONE_STACK_SIZE], onesym[ONE_STACK_SIZE]; 75 int onenext[ONE_STACK_SIZE], onedef[ONE_STACK_SIZE], onesp; 76 int maximum_mns, current_mns, current_max_rules; 77 int num_rules, num_eof_rules, default_rule, lastnfa; 78 int *firstst, *lastst, *finalst, *transchar, *trans1, *trans2; 79 int *accptnum, *assoc_rule, *state_type; 80 int *rule_type, *rule_linenum, *rule_useful; 81 int current_state_type; 82 int variable_trailing_context_rules; 83 int numtemps, numprots, protprev[MSP], protnext[MSP], prottbl[MSP]; 84 int protcomst[MSP], firstprot, lastprot, protsave[PROT_SAVE_SIZE]; 85 int numecs, nextecm[CSIZE + 1], ecgroup[CSIZE + 1], nummecs, 86 tecfwd[CSIZE + 1]; 87 int tecbck[CSIZE + 1]; 88 int lastsc, *scset, *scbol, *scxclu, *sceof; 89 int current_max_scs; 90 char **scname; 91 int current_max_dfa_size, current_max_xpairs; 92 int current_max_template_xpairs, current_max_dfas; 93 int lastdfa, *nxt, *chk, *tnxt; 94 int *base, *def, *nultrans, NUL_ec, tblend, firstfree, **dss, *dfasiz; 95 union dfaacc_union *dfaacc; 96 int *accsiz, *dhash, numas; 97 int numsnpairs, jambase, jamstate; 98 int lastccl, *cclmap, *ccllen, *cclng, cclreuse; 99 int current_maxccls, current_max_ccl_tbl_size; 100 Char *ccltbl; 101 char nmstr[MAXLINE]; 102 int sectnum, nummt, hshcol, dfaeql, numeps, eps2, num_reallocs; 103 int tmpuses, totnst, peakpairs, numuniq, numdup, hshsave; 104 int num_backing_up, bol_needed; 105 FILE *backing_up_file; 106 int end_of_buffer_state; 107 char **input_files; 108 int num_input_files; 109 jmp_buf flex_main_jmp_buf; 110 bool *rule_has_nl, *ccl_has_nl; 111 int nlch = '\n'; 112 bool ansi_func_defs, ansi_func_protos; 113 114 bool tablesext, tablesverify, gentables; 115 char *tablesfilename=0,*tablesname=0; 116 struct yytbl_writer tableswr; 117 118 /* Make sure program_name is initialized so we don't crash if writing 119 * out an error message before getting the program name from argv[0]. 120 */ 121 char *program_name = "flex"; 122 123 #ifndef SHORT_FILE_NAMES 124 static const char outfile_template[] = "lex.%s.%s"; 125 static const char backing_name[] = "lex.backup"; 126 static const char tablesfile_template[] = "lex.%s.tables"; 127 #else 128 static const char outfile_template[] = "lex%s.%s"; 129 static const char backing_name[] = "lex.bck"; 130 static const char tablesfile_template[] = "lex%s.tbl"; 131 #endif 132 133 #ifdef MS_DOS 134 extern unsigned _stklen = 16384; 135 #endif 136 137 /* From scan.l */ 138 extern FILE* yyout; 139 140 static char outfile_path[MAXLINE]; 141 static int outfile_created = 0; 142 static char *skelname = NULL; 143 static int _stdout_closed = 0; /* flag to prevent double-fclose() on stdout. */ 144 const char *escaped_qstart = "[[]]M4_YY_NOOP[M4_YY_NOOP[M4_YY_NOOP[[]]"; 145 const char *escaped_qend = "[[]]M4_YY_NOOP]M4_YY_NOOP]M4_YY_NOOP[[]]"; 146 147 /* For debugging. The max number of filters to apply to skeleton. */ 148 static int preproc_level = 1000; 149 150 int flex_main PROTO ((int argc, char *argv[])); 151 int main PROTO ((int argc, char *argv[])); 152 153 int flex_main (argc, argv) 154 int argc; 155 char *argv[]; 156 { 157 int i, exit_status, child_status; 158 159 /* Set a longjmp target. Yes, I know it's a hack, but it gets worse: The 160 * return value of setjmp, if non-zero, is the desired exit code PLUS ONE. 161 * For example, if you want 'main' to return with code '2', then call 162 * longjmp() with an argument of 3. This is because it is invalid to 163 * specify a value of 0 to longjmp. FLEX_EXIT(n) should be used instead of 164 * exit(n); 165 */ 166 exit_status = setjmp (flex_main_jmp_buf); 167 if (exit_status){ 168 if (stdout && !_stdout_closed && !ferror(stdout)){ 169 fflush(stdout); 170 fclose(stdout); 171 } 172 while (wait(&child_status) > 0){ 173 if (!WIFEXITED (child_status) 174 || WEXITSTATUS (child_status) != 0){ 175 /* report an error of a child 176 */ 177 if( exit_status <= 1 ) 178 exit_status = 2; 179 180 } 181 } 182 return exit_status - 1; 183 } 184 185 flexinit (argc, argv); 186 187 readin (); 188 189 skelout (); 190 /* %% [1.5] DFA */ 191 ntod (); 192 193 for (i = 1; i <= num_rules; ++i) 194 if (!rule_useful[i] && i != default_rule) 195 line_warning (_("rule cannot be matched"), 196 rule_linenum[i]); 197 198 if (spprdflt && !reject && rule_useful[default_rule]) 199 line_warning (_ 200 ("-s option given but default rule can be matched"), 201 rule_linenum[default_rule]); 202 203 /* Generate the C state transition tables from the DFA. */ 204 make_tables (); 205 206 /* Note, flexend does not return. It exits with its argument 207 * as status. 208 */ 209 flexend (0); 210 211 return 0; /* keep compilers/lint happy */ 212 } 213 214 /* Wrapper around flex_main, so flex_main can be built as a library. */ 215 int main (argc, argv) 216 int argc; 217 char *argv[]; 218 { 219 #if ENABLE_NLS 220 #if HAVE_LOCALE_H 221 setlocale (LC_MESSAGES, ""); 222 setlocale (LC_CTYPE, ""); 223 textdomain (PACKAGE); 224 bindtextdomain (PACKAGE, LOCALEDIR); 225 #endif 226 #endif 227 228 return flex_main (argc, argv); 229 } 230 231 /* check_options - check user-specified options */ 232 233 void check_options () 234 { 235 int i; 236 const char * m4 = NULL; 237 238 if (lex_compat) { 239 if (C_plus_plus) 240 flexerror (_("Can't use -+ with -l option")); 241 242 if (fulltbl || fullspd) 243 flexerror (_("Can't use -f or -F with -l option")); 244 245 if (reentrant || bison_bridge_lval) 246 flexerror (_ 247 ("Can't use --reentrant or --bison-bridge with -l option")); 248 249 yytext_is_array = true; 250 do_yylineno = true; 251 use_read = false; 252 } 253 254 255 #if 0 256 /* This makes no sense whatsoever. I'm removing it. */ 257 if (do_yylineno) 258 /* This should really be "maintain_backup_tables = true" */ 259 reject_really_used = true; 260 #endif 261 262 if (csize == unspecified) { 263 if ((fulltbl || fullspd) && !useecs) 264 csize = DEFAULT_CSIZE; 265 else 266 csize = CSIZE; 267 } 268 269 if (interactive == unspecified) { 270 if (fulltbl || fullspd) 271 interactive = false; 272 else 273 interactive = true; 274 } 275 276 if (fulltbl || fullspd) { 277 if (usemecs) 278 flexerror (_ 279 ("-Cf/-CF and -Cm don't make sense together")); 280 281 if (interactive) 282 flexerror (_("-Cf/-CF and -I are incompatible")); 283 284 if (lex_compat) 285 flexerror (_ 286 ("-Cf/-CF are incompatible with lex-compatibility mode")); 287 288 289 if (fulltbl && fullspd) 290 flexerror (_ 291 ("-Cf and -CF are mutually exclusive")); 292 } 293 294 if (C_plus_plus && fullspd) 295 flexerror (_("Can't use -+ with -CF option")); 296 297 if (C_plus_plus && yytext_is_array) { 298 lwarn (_("%array incompatible with -+ option")); 299 yytext_is_array = false; 300 } 301 302 if (C_plus_plus && (reentrant)) 303 flexerror (_("Options -+ and --reentrant are mutually exclusive.")); 304 305 if (C_plus_plus && bison_bridge_lval) 306 flexerror (_("bison bridge not supported for the C++ scanner.")); 307 308 309 if (useecs) { /* Set up doubly-linked equivalence classes. */ 310 311 /* We loop all the way up to csize, since ecgroup[csize] is 312 * the position used for NUL characters. 313 */ 314 ecgroup[1] = NIL; 315 316 for (i = 2; i <= csize; ++i) { 317 ecgroup[i] = i - 1; 318 nextecm[i - 1] = i; 319 } 320 321 nextecm[csize] = NIL; 322 } 323 324 else { 325 /* Put everything in its own equivalence class. */ 326 for (i = 1; i <= csize; ++i) { 327 ecgroup[i] = i; 328 nextecm[i] = BAD_SUBSCRIPT; /* to catch errors */ 329 } 330 } 331 332 if (!ansi_func_defs) 333 buf_m4_define( &m4defs_buf, "M4_YY_NO_ANSI_FUNC_DEFS", NULL); 334 335 if (!ansi_func_protos) 336 buf_m4_define( &m4defs_buf, "M4_YY_NO_ANSI_FUNC_PROTOS", NULL); 337 338 if (extra_type) 339 buf_m4_define( &m4defs_buf, "M4_EXTRA_TYPE_DEFS", extra_type); 340 341 if (!use_stdout) { 342 FILE *prev_stdout; 343 344 if (!did_outfilename) { 345 char *suffix; 346 347 if (C_plus_plus) 348 suffix = "cc"; 349 else 350 suffix = "c"; 351 352 snprintf (outfile_path, sizeof(outfile_path), outfile_template, 353 prefix, suffix); 354 355 outfilename = outfile_path; 356 } 357 358 prev_stdout = freopen (outfilename, "w+", stdout); 359 360 if (prev_stdout == NULL) 361 lerr (_("could not create %s"), outfilename); 362 363 outfile_created = 1; 364 } 365 366 367 /* Setup the filter chain. */ 368 output_chain = filter_create_int(NULL, filter_tee_header, headerfilename); 369 if ( !(m4 = getenv("M4"))) 370 m4 = M4; 371 filter_create_ext(output_chain, m4, "-P", 0); 372 filter_create_int(output_chain, filter_fix_linedirs, NULL); 373 374 /* For debugging, only run the requested number of filters. */ 375 if (preproc_level > 0) { 376 filter_truncate(output_chain, preproc_level); 377 filter_apply_chain(output_chain); 378 } 379 yyout = stdout; 380 381 382 /* always generate the tablesverify flag. */ 383 buf_m4_define (&m4defs_buf, "M4_YY_TABLES_VERIFY", tablesverify ? "1" : "0"); 384 if (tablesext) 385 gentables = false; 386 387 if (tablesverify) 388 /* force generation of C tables. */ 389 gentables = true; 390 391 392 if (tablesext) { 393 FILE *tablesout; 394 struct yytbl_hdr hdr; 395 char *pname = 0; 396 int nbytes = 0; 397 398 buf_m4_define (&m4defs_buf, "M4_YY_TABLES_EXTERNAL", NULL); 399 400 if (!tablesfilename) { 401 nbytes = strlen (prefix) + strlen (tablesfile_template) + 2; 402 tablesfilename = pname = (char *) calloc (nbytes, 1); 403 snprintf (pname, nbytes, tablesfile_template, prefix); 404 } 405 406 if ((tablesout = fopen (tablesfilename, "w")) == NULL) 407 lerr (_("could not create %s"), tablesfilename); 408 if (pname) 409 free (pname); 410 tablesfilename = 0; 411 412 yytbl_writer_init (&tableswr, tablesout); 413 414 nbytes = strlen (prefix) + strlen ("tables") + 2; 415 tablesname = (char *) calloc (nbytes, 1); 416 snprintf (tablesname, nbytes, "%stables", prefix); 417 yytbl_hdr_init (&hdr, flex_version, tablesname); 418 419 if (yytbl_hdr_fwrite (&tableswr, &hdr) <= 0) 420 flexerror (_("could not write tables header")); 421 } 422 423 if (skelname && (skelfile = fopen (skelname, "r")) == NULL) 424 lerr (_("can't open skeleton file %s"), skelname); 425 426 if (reentrant) { 427 buf_m4_define (&m4defs_buf, "M4_YY_REENTRANT", NULL); 428 if (yytext_is_array) 429 buf_m4_define (&m4defs_buf, "M4_YY_TEXT_IS_ARRAY", NULL); 430 } 431 432 if ( bison_bridge_lval) 433 buf_m4_define (&m4defs_buf, "M4_YY_BISON_LVAL", NULL); 434 435 if ( bison_bridge_lloc) 436 buf_m4_define (&m4defs_buf, "<M4_YY_BISON_LLOC>", NULL); 437 438 buf_m4_define(&m4defs_buf, "M4_YY_PREFIX", prefix); 439 440 if (did_outfilename) 441 line_directive_out (stdout, 0); 442 443 if (do_yylineno) 444 buf_m4_define (&m4defs_buf, "M4_YY_USE_LINENO", NULL); 445 446 /* Create the alignment type. */ 447 buf_strdefine (&userdef_buf, "YY_INT_ALIGNED", 448 long_align ? "long int" : "short int"); 449 450 /* Define the start condition macros. */ 451 { 452 struct Buf tmpbuf; 453 buf_init(&tmpbuf, sizeof(char)); 454 for (i = 1; i <= lastsc; i++) { 455 char *str, *fmt = "#define %s %d\n"; 456 size_t strsz; 457 458 str = (char*)flex_alloc(strsz = strlen(fmt) + strlen(scname[i]) + (int)(1 + log10(i)) + 2); 459 if (!str) 460 flexfatal(_("allocation of macro definition failed")); 461 snprintf(str, strsz, fmt, scname[i], i - 1); 462 buf_strappend(&tmpbuf, str); 463 free(str); 464 } 465 buf_m4_define(&m4defs_buf, "M4_YY_SC_DEFS", tmpbuf.elts); 466 buf_destroy(&tmpbuf); 467 } 468 469 /* This is where we begin writing to the file. */ 470 471 /* Dump the %top code. */ 472 if( top_buf.elts) 473 outn((char*) top_buf.elts); 474 475 /* Dump the m4 definitions. */ 476 buf_print_strings(&m4defs_buf, stdout); 477 m4defs_buf.nelts = 0; /* memory leak here. */ 478 479 /* Place a bogus line directive, it will be fixed in the filter. */ 480 outn("#line 0 \"M4_YY_OUTFILE_NAME\"\n"); 481 482 /* Dump the user defined preproc directives. */ 483 if (userdef_buf.elts) 484 outn ((char *) (userdef_buf.elts)); 485 486 skelout (); 487 /* %% [1.0] */ 488 } 489 490 /* flexend - terminate flex 491 * 492 * note 493 * This routine does not return. 494 */ 495 496 void flexend (exit_status) 497 int exit_status; 498 499 { 500 static int called_before = -1; /* prevent infinite recursion. */ 501 int tblsiz; 502 503 if (++called_before) 504 FLEX_EXIT (exit_status); 505 506 if (skelfile != NULL) { 507 if (ferror (skelfile)) 508 lerr (_("input error reading skeleton file %s"), 509 skelname); 510 511 else if (fclose (skelfile)) 512 lerr (_("error closing skeleton file %s"), 513 skelname); 514 } 515 516 #if 0 517 fprintf (header_out, 518 "#ifdef YY_HEADER_EXPORT_START_CONDITIONS\n"); 519 fprintf (header_out, 520 "/* Beware! Start conditions are not prefixed. */\n"); 521 522 /* Special case for "INITIAL" */ 523 fprintf (header_out, 524 "#undef INITIAL\n#define INITIAL 0\n"); 525 for (i = 2; i <= lastsc; i++) 526 fprintf (header_out, "#define %s %d\n", scname[i], i - 1); 527 fprintf (header_out, 528 "#endif /* YY_HEADER_EXPORT_START_CONDITIONS */\n\n"); 529 530 /* Kill ALL flex-related macros. This is so the user 531 * can #include more than one generated header file. */ 532 fprintf (header_out, "#ifndef YY_HEADER_NO_UNDEFS\n"); 533 fprintf (header_out, 534 "/* Undefine all internal macros, etc., that do no belong in the header. */\n\n"); 535 536 { 537 const char * undef_list[] = { 538 539 "BEGIN", 540 "ECHO", 541 "EOB_ACT_CONTINUE_SCAN", 542 "EOB_ACT_END_OF_FILE", 543 "EOB_ACT_LAST_MATCH", 544 "FLEX_SCANNER", 545 "FLEX_STD", 546 "REJECT", 547 "YYFARGS0", 548 "YYFARGS1", 549 "YYFARGS2", 550 "YYFARGS3", 551 "YYLMAX", 552 "YYSTATE", 553 "YY_AT_BOL", 554 "YY_BREAK", 555 "YY_BUFFER_EOF_PENDING", 556 "YY_BUFFER_NEW", 557 "YY_BUFFER_NORMAL", 558 "YY_BUF_SIZE", 559 "M4_YY_CALL_LAST_ARG", 560 "M4_YY_CALL_ONLY_ARG", 561 "YY_CURRENT_BUFFER", 562 "YY_DECL", 563 "M4_YY_DECL_LAST_ARG", 564 "M4_YY_DEF_LAST_ARG", 565 "M4_YY_DEF_ONLY_ARG", 566 "YY_DO_BEFORE_ACTION", 567 "YY_END_OF_BUFFER", 568 "YY_END_OF_BUFFER_CHAR", 569 "YY_EXIT_FAILURE", 570 "YY_EXTRA_TYPE", 571 "YY_FATAL_ERROR", 572 "YY_FLEX_DEFINED_ECHO", 573 "YY_FLEX_LEX_COMPAT", 574 "YY_FLEX_MAJOR_VERSION", 575 "YY_FLEX_MINOR_VERSION", 576 "YY_FLEX_SUBMINOR_VERSION", 577 "YY_FLUSH_BUFFER", 578 "YY_G", 579 "YY_INPUT", 580 "YY_INTERACTIVE", 581 "YY_INT_ALIGNED", 582 "YY_LAST_ARG", 583 "YY_LESS_LINENO", 584 "YY_LEX_ARGS", 585 "YY_LEX_DECLARATION", 586 "YY_LEX_PROTO", 587 "YY_MAIN", 588 "YY_MORE_ADJ", 589 "YY_NEED_STRLEN", 590 "YY_NEW_FILE", 591 "YY_NULL", 592 "YY_NUM_RULES", 593 "YY_ONLY_ARG", 594 "YY_PARAMS", 595 "YY_PROTO", 596 "M4_YY_PROTO_LAST_ARG", 597 "M4_YY_PROTO_ONLY_ARG void", 598 "YY_READ_BUF_SIZE", 599 "YY_REENTRANT", 600 "YY_RESTORE_YY_MORE_OFFSET", 601 "YY_RULE_SETUP", 602 "YY_SC_TO_UI", 603 "YY_SKIP_YYWRAP", 604 "YY_START", 605 "YY_START_STACK_INCR", 606 "YY_STATE_EOF", 607 "YY_STDINIT", 608 "YY_TRAILING_HEAD_MASK", 609 "YY_TRAILING_MASK", 610 "YY_USER_ACTION", 611 "YY_USE_CONST", 612 "YY_USE_PROTOS", 613 "unput", 614 "yyTABLES_NAME", 615 "yy_create_buffer", 616 "yy_delete_buffer", 617 "yy_flex_debug", 618 "yy_flush_buffer", 619 "yy_init_buffer", 620 "yy_load_buffer_state", 621 "yy_new_buffer", 622 "yy_scan_buffer", 623 "yy_scan_bytes", 624 "yy_scan_string", 625 "yy_set_bol", 626 "yy_set_interactive", 627 "yy_switch_to_buffer", 628 "yypush_buffer_state", 629 "yypop_buffer_state", 630 "yyensure_buffer_stack", 631 "yyalloc", 632 "yyconst", 633 "yyextra", 634 "yyfree", 635 "yyget_debug", 636 "yyget_extra", 637 "yyget_in", 638 "yyget_leng", 639 "yyget_lineno", 640 "yyget_lloc", 641 "yyget_lval", 642 "yyget_out", 643 "yyget_text", 644 "yyin", 645 "yyleng", 646 "yyless", 647 "yylex", 648 "yylex_destroy", 649 "yylex_init", 650 "yylex_init_extra", 651 "yylineno", 652 "yylloc", 653 "yylval", 654 "yymore", 655 "yyout", 656 "yyrealloc", 657 "yyrestart", 658 "yyset_debug", 659 "yyset_extra", 660 "yyset_in", 661 "yyset_lineno", 662 "yyset_lloc", 663 "yyset_lval", 664 "yyset_out", 665 "yytables_destroy", 666 "yytables_fload", 667 "yyterminate", 668 "yytext", 669 "yytext_ptr", 670 "yywrap", 671 672 /* must be null-terminated */ 673 NULL}; 674 675 676 for (i=0; undef_list[i] != NULL; i++) 677 fprintf (header_out, "#undef %s\n", undef_list[i]); 678 } 679 680 /* undef any of the auto-generated symbols. */ 681 for (i = 0; i < defs_buf.nelts; i++) { 682 683 /* don't undef start conditions */ 684 if (sclookup (((char **) defs_buf.elts)[i]) > 0) 685 continue; 686 fprintf (header_out, "#undef %s\n", 687 ((char **) defs_buf.elts)[i]); 688 } 689 690 fprintf (header_out, 691 "#endif /* !YY_HEADER_NO_UNDEFS */\n"); 692 fprintf (header_out, "\n"); 693 fprintf (header_out, "#undef %sIN_HEADER\n", prefix); 694 fprintf (header_out, "#endif /* %sHEADER_H */\n", prefix); 695 696 if (ferror (header_out)) 697 lerr (_("error creating header file %s"), 698 headerfilename); 699 fflush (header_out); 700 fclose (header_out); 701 #endif 702 703 if (exit_status != 0 && outfile_created) { 704 if (ferror (stdout)) 705 lerr (_("error writing output file %s"), 706 outfilename); 707 708 else if ((_stdout_closed = 1) && fclose (stdout)) 709 lerr (_("error closing output file %s"), 710 outfilename); 711 712 else if (unlink (outfilename)) 713 lerr (_("error deleting output file %s"), 714 outfilename); 715 } 716 717 718 if (backing_up_report && backing_up_file) { 719 if (num_backing_up == 0) 720 fprintf (backing_up_file, _("No backing up.\n")); 721 else if (fullspd || fulltbl) 722 fprintf (backing_up_file, 723 _ 724 ("%d backing up (non-accepting) states.\n"), 725 num_backing_up); 726 else 727 fprintf (backing_up_file, 728 _("Compressed tables always back up.\n")); 729 730 if (ferror (backing_up_file)) 731 lerr (_("error writing backup file %s"), 732 backing_name); 733 734 else if (fclose (backing_up_file)) 735 lerr (_("error closing backup file %s"), 736 backing_name); 737 } 738 739 if (printstats) { 740 fprintf (stderr, _("%s version %s usage statistics:\n"), 741 program_name, flex_version); 742 743 fprintf (stderr, _(" scanner options: -")); 744 745 if (C_plus_plus) 746 putc ('+', stderr); 747 if (backing_up_report) 748 putc ('b', stderr); 749 if (ddebug) 750 putc ('d', stderr); 751 if (sf_case_ins()) 752 putc ('i', stderr); 753 if (lex_compat) 754 putc ('l', stderr); 755 if (posix_compat) 756 putc ('X', stderr); 757 if (performance_report > 0) 758 putc ('p', stderr); 759 if (performance_report > 1) 760 putc ('p', stderr); 761 if (spprdflt) 762 putc ('s', stderr); 763 if (reentrant) 764 fputs ("--reentrant", stderr); 765 if (bison_bridge_lval) 766 fputs ("--bison-bridge", stderr); 767 if (bison_bridge_lloc) 768 fputs ("--bison-locations", stderr); 769 if (use_stdout) 770 putc ('t', stderr); 771 if (printstats) 772 putc ('v', stderr); /* always true! */ 773 if (nowarn) 774 putc ('w', stderr); 775 if (interactive == false) 776 putc ('B', stderr); 777 if (interactive == true) 778 putc ('I', stderr); 779 if (!gen_line_dirs) 780 putc ('L', stderr); 781 if (trace) 782 putc ('T', stderr); 783 784 if (csize == unspecified) 785 /* We encountered an error fairly early on, so csize 786 * never got specified. Define it now, to prevent 787 * bogus table sizes being written out below. 788 */ 789 csize = 256; 790 791 if (csize == 128) 792 putc ('7', stderr); 793 else 794 putc ('8', stderr); 795 796 fprintf (stderr, " -C"); 797 798 if (long_align) 799 putc ('a', stderr); 800 if (fulltbl) 801 putc ('f', stderr); 802 if (fullspd) 803 putc ('F', stderr); 804 if (useecs) 805 putc ('e', stderr); 806 if (usemecs) 807 putc ('m', stderr); 808 if (use_read) 809 putc ('r', stderr); 810 811 if (did_outfilename) 812 fprintf (stderr, " -o%s", outfilename); 813 814 if (skelname) 815 fprintf (stderr, " -S%s", skelname); 816 817 if (strcmp (prefix, "yy")) 818 fprintf (stderr, " -P%s", prefix); 819 820 putc ('\n', stderr); 821 822 fprintf (stderr, _(" %d/%d NFA states\n"), 823 lastnfa, current_mns); 824 fprintf (stderr, _(" %d/%d DFA states (%d words)\n"), 825 lastdfa, current_max_dfas, totnst); 826 fprintf (stderr, _(" %d rules\n"), 827 num_rules + num_eof_rules - 828 1 /* - 1 for def. rule */ ); 829 830 if (num_backing_up == 0) 831 fprintf (stderr, _(" No backing up\n")); 832 else if (fullspd || fulltbl) 833 fprintf (stderr, 834 _ 835 (" %d backing-up (non-accepting) states\n"), 836 num_backing_up); 837 else 838 fprintf (stderr, 839 _ 840 (" Compressed tables always back-up\n")); 841 842 if (bol_needed) 843 fprintf (stderr, 844 _(" Beginning-of-line patterns used\n")); 845 846 fprintf (stderr, _(" %d/%d start conditions\n"), lastsc, 847 current_max_scs); 848 fprintf (stderr, 849 _ 850 (" %d epsilon states, %d double epsilon states\n"), 851 numeps, eps2); 852 853 if (lastccl == 0) 854 fprintf (stderr, _(" no character classes\n")); 855 else 856 fprintf (stderr, 857 _ 858 (" %d/%d character classes needed %d/%d words of storage, %d reused\n"), 859 lastccl, current_maxccls, 860 cclmap[lastccl] + ccllen[lastccl], 861 current_max_ccl_tbl_size, cclreuse); 862 863 fprintf (stderr, _(" %d state/nextstate pairs created\n"), 864 numsnpairs); 865 fprintf (stderr, 866 _(" %d/%d unique/duplicate transitions\n"), 867 numuniq, numdup); 868 869 if (fulltbl) { 870 tblsiz = lastdfa * numecs; 871 fprintf (stderr, _(" %d table entries\n"), 872 tblsiz); 873 } 874 875 else { 876 tblsiz = 2 * (lastdfa + numtemps) + 2 * tblend; 877 878 fprintf (stderr, 879 _(" %d/%d base-def entries created\n"), 880 lastdfa + numtemps, current_max_dfas); 881 fprintf (stderr, 882 _ 883 (" %d/%d (peak %d) nxt-chk entries created\n"), 884 tblend, current_max_xpairs, peakpairs); 885 fprintf (stderr, 886 _ 887 (" %d/%d (peak %d) template nxt-chk entries created\n"), 888 numtemps * nummecs, 889 current_max_template_xpairs, 890 numtemps * numecs); 891 fprintf (stderr, _(" %d empty table entries\n"), 892 nummt); 893 fprintf (stderr, _(" %d protos created\n"), 894 numprots); 895 fprintf (stderr, 896 _(" %d templates created, %d uses\n"), 897 numtemps, tmpuses); 898 } 899 900 if (useecs) { 901 tblsiz = tblsiz + csize; 902 fprintf (stderr, 903 _ 904 (" %d/%d equivalence classes created\n"), 905 numecs, csize); 906 } 907 908 if (usemecs) { 909 tblsiz = tblsiz + numecs; 910 fprintf (stderr, 911 _ 912 (" %d/%d meta-equivalence classes created\n"), 913 nummecs, csize); 914 } 915 916 fprintf (stderr, 917 _ 918 (" %d (%d saved) hash collisions, %d DFAs equal\n"), 919 hshcol, hshsave, dfaeql); 920 fprintf (stderr, _(" %d sets of reallocations needed\n"), 921 num_reallocs); 922 fprintf (stderr, _(" %d total table entries needed\n"), 923 tblsiz); 924 } 925 926 FLEX_EXIT (exit_status); 927 } 928 929 930 /* flexinit - initialize flex */ 931 932 void flexinit (argc, argv) 933 int argc; 934 char **argv; 935 { 936 int i, sawcmpflag, rv, optind; 937 char *arg; 938 scanopt_t sopt; 939 940 printstats = syntaxerror = trace = spprdflt = false; 941 lex_compat = posix_compat = C_plus_plus = backing_up_report = 942 ddebug = fulltbl = false; 943 fullspd = long_align = nowarn = yymore_used = continued_action = 944 false; 945 do_yylineno = yytext_is_array = in_rule = reject = do_stdinit = 946 false; 947 yymore_really_used = reject_really_used = unspecified; 948 interactive = csize = unspecified; 949 do_yywrap = gen_line_dirs = usemecs = useecs = true; 950 reentrant = bison_bridge_lval = bison_bridge_lloc = false; 951 performance_report = 0; 952 did_outfilename = 0; 953 prefix = "yy"; 954 yyclass = 0; 955 use_read = use_stdout = false; 956 tablesext = tablesverify = false; 957 gentables = true; 958 tablesfilename = tablesname = NULL; 959 ansi_func_defs = ansi_func_protos = true; 960 961 sawcmpflag = false; 962 963 /* Initialize dynamic array for holding the rule actions. */ 964 action_size = 2048; /* default size of action array in bytes */ 965 action_array = allocate_character_array (action_size); 966 defs1_offset = prolog_offset = action_offset = action_index = 0; 967 action_array[0] = '\0'; 968 969 /* Initialize any buffers. */ 970 buf_init (&userdef_buf, sizeof (char)); /* one long string */ 971 buf_init (&defs_buf, sizeof (char *)); /* list of strings */ 972 buf_init (&yydmap_buf, sizeof (char)); /* one long string */ 973 buf_init (&top_buf, sizeof (char)); /* one long string */ 974 975 { 976 const char * m4defs_init_str[] = {"m4_changequote\n", 977 "m4_changequote([[, ]])\n"}; 978 buf_init (&m4defs_buf, sizeof (char *)); 979 buf_append (&m4defs_buf, &m4defs_init_str, 2); 980 } 981 982 sf_init (); 983 984 /* initialize regex lib */ 985 flex_init_regex(); 986 987 /* Enable C++ if program name ends with '+'. */ 988 program_name = basename2 (argv[0], 0); 989 990 if (program_name[0] != '\0' && 991 program_name[strlen (program_name) - 1] == '+') 992 C_plus_plus = true; 993 994 /* read flags */ 995 sopt = scanopt_init (flexopts, argc, argv, 0); 996 if (!sopt) { 997 /* This will only happen when flexopts array is altered. */ 998 fprintf (stderr, 999 _("Internal error. flexopts are malformed.\n")); 1000 FLEX_EXIT (1); 1001 } 1002 1003 while ((rv = scanopt (sopt, &arg, &optind)) != 0) { 1004 1005 if (rv < 0) { 1006 /* Scanopt has already printed an option-specific error message. */ 1007 fprintf (stderr, 1008 _ 1009 ("Try `%s --help' for more information.\n"), 1010 program_name); 1011 FLEX_EXIT (1); 1012 } 1013 1014 switch ((enum flexopt_flag_t) rv) { 1015 case OPT_CPLUSPLUS: 1016 C_plus_plus = true; 1017 break; 1018 1019 case OPT_BATCH: 1020 interactive = false; 1021 break; 1022 1023 case OPT_BACKUP: 1024 backing_up_report = true; 1025 break; 1026 1027 case OPT_DONOTHING: 1028 break; 1029 1030 case OPT_COMPRESSION: 1031 if (!sawcmpflag) { 1032 useecs = false; 1033 usemecs = false; 1034 fulltbl = false; 1035 sawcmpflag = true; 1036 } 1037 1038 for (i = 0; arg && arg[i] != '\0'; i++) 1039 switch (arg[i]) { 1040 case 'a': 1041 long_align = true; 1042 break; 1043 1044 case 'e': 1045 useecs = true; 1046 break; 1047 1048 case 'F': 1049 fullspd = true; 1050 break; 1051 1052 case 'f': 1053 fulltbl = true; 1054 break; 1055 1056 case 'm': 1057 usemecs = true; 1058 break; 1059 1060 case 'r': 1061 use_read = true; 1062 break; 1063 1064 default: 1065 lerr (_ 1066 ("unknown -C option '%c'"), 1067 arg[i]); 1068 break; 1069 } 1070 break; 1071 1072 case OPT_DEBUG: 1073 ddebug = true; 1074 break; 1075 1076 case OPT_NO_DEBUG: 1077 ddebug = false; 1078 break; 1079 1080 case OPT_FULL: 1081 useecs = usemecs = false; 1082 use_read = fulltbl = true; 1083 break; 1084 1085 case OPT_FAST: 1086 useecs = usemecs = false; 1087 use_read = fullspd = true; 1088 break; 1089 1090 case OPT_HELP: 1091 usage (); 1092 FLEX_EXIT (0); 1093 1094 case OPT_INTERACTIVE: 1095 interactive = true; 1096 break; 1097 1098 case OPT_CASE_INSENSITIVE: 1099 sf_set_case_ins(true); 1100 break; 1101 1102 case OPT_LEX_COMPAT: 1103 lex_compat = true; 1104 break; 1105 1106 case OPT_POSIX_COMPAT: 1107 posix_compat = true; 1108 break; 1109 1110 case OPT_PREPROC_LEVEL: 1111 preproc_level = strtol(arg,NULL,0); 1112 break; 1113 1114 case OPT_MAIN: 1115 buf_strdefine (&userdef_buf, "YY_MAIN", "1"); 1116 do_yywrap = false; 1117 break; 1118 1119 case OPT_NO_MAIN: 1120 buf_strdefine (&userdef_buf, "YY_MAIN", "0"); 1121 break; 1122 1123 case OPT_NO_LINE: 1124 gen_line_dirs = false; 1125 break; 1126 1127 case OPT_OUTFILE: 1128 outfilename = arg; 1129 did_outfilename = 1; 1130 break; 1131 1132 case OPT_PREFIX: 1133 prefix = arg; 1134 break; 1135 1136 case OPT_PERF_REPORT: 1137 ++performance_report; 1138 break; 1139 1140 case OPT_BISON_BRIDGE: 1141 bison_bridge_lval = true; 1142 break; 1143 1144 case OPT_BISON_BRIDGE_LOCATIONS: 1145 bison_bridge_lval = bison_bridge_lloc = true; 1146 break; 1147 1148 case OPT_REENTRANT: 1149 reentrant = true; 1150 break; 1151 1152 case OPT_NO_REENTRANT: 1153 reentrant = false; 1154 break; 1155 1156 case OPT_SKEL: 1157 skelname = arg; 1158 break; 1159 1160 case OPT_DEFAULT: 1161 spprdflt = false; 1162 break; 1163 1164 case OPT_NO_DEFAULT: 1165 spprdflt = true; 1166 break; 1167 1168 case OPT_STDOUT: 1169 use_stdout = true; 1170 break; 1171 1172 case OPT_NO_UNISTD_H: 1173 //buf_strdefine (&userdef_buf, "YY_NO_UNISTD_H", "1"); 1174 buf_m4_define( &m4defs_buf, "M4_YY_NO_UNISTD_H",0); 1175 break; 1176 1177 case OPT_TABLES_FILE: 1178 tablesext = true; 1179 tablesfilename = arg; 1180 break; 1181 1182 case OPT_TABLES_VERIFY: 1183 tablesverify = true; 1184 break; 1185 1186 case OPT_TRACE: 1187 trace = true; 1188 break; 1189 1190 case OPT_VERBOSE: 1191 printstats = true; 1192 break; 1193 1194 case OPT_VERSION: 1195 printf (_("%s %s\n"), program_name, flex_version); 1196 FLEX_EXIT (0); 1197 1198 case OPT_WARN: 1199 nowarn = false; 1200 break; 1201 1202 case OPT_NO_WARN: 1203 nowarn = true; 1204 break; 1205 1206 case OPT_7BIT: 1207 csize = 128; 1208 break; 1209 1210 case OPT_8BIT: 1211 csize = CSIZE; 1212 break; 1213 1214 case OPT_ALIGN: 1215 long_align = true; 1216 break; 1217 1218 case OPT_NO_ALIGN: 1219 long_align = false; 1220 break; 1221 1222 case OPT_ALWAYS_INTERACTIVE: 1223 buf_m4_define (&m4defs_buf, "M4_YY_ALWAYS_INTERACTIVE", 0); 1224 break; 1225 1226 case OPT_NEVER_INTERACTIVE: 1227 buf_m4_define( &m4defs_buf, "M4_YY_NEVER_INTERACTIVE", 0); 1228 break; 1229 1230 case OPT_ARRAY: 1231 yytext_is_array = true; 1232 break; 1233 1234 case OPT_POINTER: 1235 yytext_is_array = false; 1236 break; 1237 1238 case OPT_ECS: 1239 useecs = true; 1240 break; 1241 1242 case OPT_NO_ECS: 1243 useecs = false; 1244 break; 1245 1246 case OPT_HEADER_FILE: 1247 headerfilename = arg; 1248 break; 1249 1250 case OPT_META_ECS: 1251 usemecs = true; 1252 break; 1253 1254 case OPT_NO_META_ECS: 1255 usemecs = false; 1256 break; 1257 1258 case OPT_PREPROCDEFINE: 1259 { 1260 /* arg is "symbol" or "symbol=definition". */ 1261 char *def; 1262 1263 for (def = arg; 1264 *def != '\0' && *def != '='; ++def) ; 1265 1266 buf_strappend (&userdef_buf, "#define "); 1267 if (*def == '\0') { 1268 buf_strappend (&userdef_buf, arg); 1269 buf_strappend (&userdef_buf, 1270 " 1\n"); 1271 } 1272 else { 1273 buf_strnappend (&userdef_buf, arg, 1274 def - arg); 1275 buf_strappend (&userdef_buf, " "); 1276 buf_strappend (&userdef_buf, 1277 def + 1); 1278 buf_strappend (&userdef_buf, "\n"); 1279 } 1280 } 1281 break; 1282 1283 case OPT_READ: 1284 use_read = true; 1285 break; 1286 1287 case OPT_STACK: 1288 //buf_strdefine (&userdef_buf, "YY_STACK_USED", "1"); 1289 buf_m4_define( &m4defs_buf, "M4_YY_STACK_USED",0); 1290 break; 1291 1292 case OPT_STDINIT: 1293 do_stdinit = true; 1294 break; 1295 1296 case OPT_NO_STDINIT: 1297 do_stdinit = false; 1298 break; 1299 1300 case OPT_YYCLASS: 1301 yyclass = arg; 1302 break; 1303 1304 case OPT_YYLINENO: 1305 do_yylineno = true; 1306 break; 1307 1308 case OPT_NO_YYLINENO: 1309 do_yylineno = false; 1310 break; 1311 1312 case OPT_YYWRAP: 1313 do_yywrap = true; 1314 break; 1315 1316 case OPT_NO_YYWRAP: 1317 do_yywrap = false; 1318 break; 1319 1320 case OPT_YYMORE: 1321 yymore_really_used = true; 1322 break; 1323 1324 case OPT_NO_YYMORE: 1325 yymore_really_used = false; 1326 break; 1327 1328 case OPT_REJECT: 1329 reject_really_used = true; 1330 break; 1331 1332 case OPT_NO_REJECT: 1333 reject_really_used = false; 1334 break; 1335 1336 case OPT_NO_ANSI_FUNC_DEFS: 1337 ansi_func_defs = false; 1338 break; 1339 1340 case OPT_NO_ANSI_FUNC_PROTOS: 1341 ansi_func_protos = false; 1342 break; 1343 1344 case OPT_NO_YY_PUSH_STATE: 1345 //buf_strdefine (&userdef_buf, "YY_NO_PUSH_STATE", "1"); 1346 buf_m4_define( &m4defs_buf, "M4_YY_NO_PUSH_STATE",0); 1347 break; 1348 case OPT_NO_YY_POP_STATE: 1349 //buf_strdefine (&userdef_buf, "YY_NO_POP_STATE", "1"); 1350 buf_m4_define( &m4defs_buf, "M4_YY_NO_POP_STATE",0); 1351 break; 1352 case OPT_NO_YY_TOP_STATE: 1353 //buf_strdefine (&userdef_buf, "YY_NO_TOP_STATE", "1"); 1354 buf_m4_define( &m4defs_buf, "M4_YY_NO_TOP_STATE",0); 1355 break; 1356 case OPT_NO_UNPUT: 1357 //buf_strdefine (&userdef_buf, "YY_NO_UNPUT", "1"); 1358 buf_m4_define( &m4defs_buf, "M4_YY_NO_UNPUT",0); 1359 break; 1360 case OPT_NO_YY_SCAN_BUFFER: 1361 //buf_strdefine (&userdef_buf, "YY_NO_SCAN_BUFFER", "1"); 1362 buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_BUFFER",0); 1363 break; 1364 case OPT_NO_YY_SCAN_BYTES: 1365 //buf_strdefine (&userdef_buf, "YY_NO_SCAN_BYTES", "1"); 1366 buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_BYTES",0); 1367 break; 1368 case OPT_NO_YY_SCAN_STRING: 1369 //buf_strdefine (&userdef_buf, "YY_NO_SCAN_STRING", "1"); 1370 buf_m4_define( &m4defs_buf, "M4_YY_NO_SCAN_STRING",0); 1371 break; 1372 case OPT_NO_YYGET_EXTRA: 1373 //buf_strdefine (&userdef_buf, "YY_NO_GET_EXTRA", "1"); 1374 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_EXTRA",0); 1375 break; 1376 case OPT_NO_YYSET_EXTRA: 1377 //buf_strdefine (&userdef_buf, "YY_NO_SET_EXTRA", "1"); 1378 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_EXTRA",0); 1379 break; 1380 case OPT_NO_YYGET_LENG: 1381 //buf_strdefine (&userdef_buf, "YY_NO_GET_LENG", "1"); 1382 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LENG",0); 1383 break; 1384 case OPT_NO_YYGET_TEXT: 1385 //buf_strdefine (&userdef_buf, "YY_NO_GET_TEXT", "1"); 1386 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_TEXT",0); 1387 break; 1388 case OPT_NO_YYGET_LINENO: 1389 //buf_strdefine (&userdef_buf, "YY_NO_GET_LINENO", "1"); 1390 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LINENO",0); 1391 break; 1392 case OPT_NO_YYSET_LINENO: 1393 //buf_strdefine (&userdef_buf, "YY_NO_SET_LINENO", "1"); 1394 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LINENO",0); 1395 break; 1396 case OPT_NO_YYGET_IN: 1397 //buf_strdefine (&userdef_buf, "YY_NO_GET_IN", "1"); 1398 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_IN",0); 1399 break; 1400 case OPT_NO_YYSET_IN: 1401 //buf_strdefine (&userdef_buf, "YY_NO_SET_IN", "1"); 1402 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_IN",0); 1403 break; 1404 case OPT_NO_YYGET_OUT: 1405 //buf_strdefine (&userdef_buf, "YY_NO_GET_OUT", "1"); 1406 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_OUT",0); 1407 break; 1408 case OPT_NO_YYSET_OUT: 1409 //buf_strdefine (&userdef_buf, "YY_NO_SET_OUT", "1"); 1410 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_OUT",0); 1411 break; 1412 case OPT_NO_YYGET_LVAL: 1413 //buf_strdefine (&userdef_buf, "YY_NO_GET_LVAL", "1"); 1414 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LVAL",0); 1415 break; 1416 case OPT_NO_YYSET_LVAL: 1417 //buf_strdefine (&userdef_buf, "YY_NO_SET_LVAL", "1"); 1418 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LVAL",0); 1419 break; 1420 case OPT_NO_YYGET_LLOC: 1421 //buf_strdefine (&userdef_buf, "YY_NO_GET_LLOC", "1"); 1422 buf_m4_define( &m4defs_buf, "M4_YY_NO_GET_LLOC",0); 1423 break; 1424 case OPT_NO_YYSET_LLOC: 1425 //buf_strdefine (&userdef_buf, "YY_NO_SET_LLOC", "1"); 1426 buf_m4_define( &m4defs_buf, "M4_YY_NO_SET_LLOC",0); 1427 break; 1428 case OPT_HEX: 1429 trace_hex = 1; 1430 } /* switch */ 1431 } /* while scanopt() */ 1432 1433 scanopt_destroy (sopt); 1434 1435 num_input_files = argc - optind; 1436 input_files = argv + optind; 1437 set_input_file (num_input_files > 0 ? input_files[0] : NULL); 1438 1439 lastccl = lastsc = lastdfa = lastnfa = 0; 1440 num_rules = num_eof_rules = default_rule = 0; 1441 numas = numsnpairs = tmpuses = 0; 1442 numecs = numeps = eps2 = num_reallocs = hshcol = dfaeql = totnst = 1443 0; 1444 numuniq = numdup = hshsave = eofseen = datapos = dataline = 0; 1445 num_backing_up = onesp = numprots = 0; 1446 variable_trailing_context_rules = bol_needed = false; 1447 1448 linenum = sectnum = 1; 1449 firstprot = NIL; 1450 1451 /* Used in mkprot() so that the first proto goes in slot 1 1452 * of the proto queue. 1453 */ 1454 lastprot = 1; 1455 1456 set_up_initial_allocations (); 1457 } 1458 1459 1460 /* readin - read in the rules section of the input file(s) */ 1461 1462 void readin () 1463 { 1464 static char yy_stdinit[] = "FILE *yyin = stdin, *yyout = stdout;"; 1465 static char yy_nostdinit[] = 1466 "FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0;"; 1467 1468 line_directive_out ((FILE *) 0, 1); 1469 1470 if (yyparse ()) { 1471 pinpoint_message (_("fatal parse error")); 1472 flexend (1); 1473 } 1474 1475 if (syntaxerror) 1476 flexend (1); 1477 1478 /* If the user explicitly requested posix compatibility by specifing the 1479 * posix-compat option, then we check for conflicting options. However, if 1480 * the POSIXLY_CORRECT variable is set, then we quietly make flex as 1481 * posix-compatible as possible. This is the recommended behavior 1482 * according to the GNU Coding Standards. 1483 * 1484 * Note: The posix option was added to flex to provide the posix behavior 1485 * of the repeat operator in regular expressions, e.g., `ab{3}' 1486 */ 1487 if (posix_compat) { 1488 /* TODO: This is where we try to make flex behave according to 1489 * posiz, AND check for conflicting options. How far should we go 1490 * with this? Should we disable all the neat-o flex features? 1491 */ 1492 /* Update: Estes says no, since other flex features don't violate posix. */ 1493 } 1494 1495 if (getenv ("POSIXLY_CORRECT")) { 1496 posix_compat = true; 1497 } 1498 1499 if (backing_up_report) { 1500 backing_up_file = fopen (backing_name, "w"); 1501 if (backing_up_file == NULL) 1502 lerr (_ 1503 ("could not create backing-up info file %s"), 1504 backing_name); 1505 } 1506 1507 else 1508 backing_up_file = NULL; 1509 1510 if (yymore_really_used == true) 1511 yymore_used = true; 1512 else if (yymore_really_used == false) 1513 yymore_used = false; 1514 1515 if (reject_really_used == true) 1516 reject = true; 1517 else if (reject_really_used == false) 1518 reject = false; 1519 1520 if (performance_report > 0) { 1521 if (lex_compat) { 1522 fprintf (stderr, 1523 _ 1524 ("-l AT&T lex compatibility option entails a large performance penalty\n")); 1525 fprintf (stderr, 1526 _ 1527 (" and may be the actual source of other reported performance penalties\n")); 1528 } 1529 1530 else if (do_yylineno) { 1531 fprintf (stderr, 1532 _ 1533 ("%%option yylineno entails a performance penalty ONLY on rules that can match newline characters\n")); 1534 } 1535 1536 if (performance_report > 1) { 1537 if (interactive) 1538 fprintf (stderr, 1539 _ 1540 ("-I (interactive) entails a minor performance penalty\n")); 1541 1542 if (yymore_used) 1543 fprintf (stderr, 1544 _ 1545 ("yymore() entails a minor performance penalty\n")); 1546 } 1547 1548 if (reject) 1549 fprintf (stderr, 1550 _ 1551 ("REJECT entails a large performance penalty\n")); 1552 1553 if (variable_trailing_context_rules) 1554 fprintf (stderr, 1555 _ 1556 ("Variable trailing context rules entail a large performance penalty\n")); 1557 } 1558 1559 if (reject) 1560 real_reject = true; 1561 1562 if (variable_trailing_context_rules) 1563 reject = true; 1564 1565 if ((fulltbl || fullspd) && reject) { 1566 if (real_reject) 1567 flexerror (_ 1568 ("REJECT cannot be used with -f or -F")); 1569 else if (do_yylineno) 1570 flexerror (_ 1571 ("%option yylineno cannot be used with REJECT")); 1572 else 1573 flexerror (_ 1574 ("variable trailing context rules cannot be used with -f or -F")); 1575 } 1576 1577 if (reject){ 1578 out_m4_define( "M4_YY_USES_REJECT", NULL); 1579 //outn ("\n#define YY_USES_REJECT"); 1580 } 1581 1582 if (!do_yywrap) { 1583 if (!C_plus_plus) { 1584 if (reentrant) 1585 outn ("\n#define yywrap(yyscanner) (/*CONSTCOND*/1)"); 1586 else 1587 outn ("\n#define yywrap() (/*CONSTCOND*/1)"); 1588 } 1589 outn ("#define YY_SKIP_YYWRAP"); 1590 } 1591 1592 if (ddebug) 1593 outn ("\n#define FLEX_DEBUG"); 1594 1595 OUT_BEGIN_CODE (); 1596 if (csize == 256) 1597 outn ("typedef unsigned char YY_CHAR;"); 1598 else 1599 outn ("typedef char YY_CHAR;"); 1600 OUT_END_CODE (); 1601 1602 if (C_plus_plus) { 1603 outn ("#define yytext_ptr yytext"); 1604 1605 if (interactive) 1606 outn ("#define YY_INTERACTIVE"); 1607 } 1608 1609 else { 1610 OUT_BEGIN_CODE (); 1611 /* In reentrant scanner, stdinit is handled in flex.skl. */ 1612 if (do_stdinit) { 1613 if (reentrant){ 1614 outn ("#ifdef VMS"); 1615 outn ("#ifdef __VMS_POSIX"); 1616 outn ("#define YY_STDINIT"); 1617 outn ("#endif"); 1618 outn ("#else"); 1619 outn ("#define YY_STDINIT"); 1620 outn ("#endif"); 1621 } 1622 1623 outn ("#ifdef VMS"); 1624 outn ("#ifndef __VMS_POSIX"); 1625 outn (yy_nostdinit); 1626 outn ("#else"); 1627 outn (yy_stdinit); 1628 outn ("#endif"); 1629 outn ("#else"); 1630 outn (yy_stdinit); 1631 outn ("#endif"); 1632 } 1633 1634 else { 1635 if(!reentrant) 1636 outn (yy_nostdinit); 1637 } 1638 OUT_END_CODE (); 1639 } 1640 1641 OUT_BEGIN_CODE (); 1642 if (fullspd) 1643 outn ("typedef yyconst struct yy_trans_info *yy_state_type;"); 1644 else if (!C_plus_plus) 1645 outn ("typedef int yy_state_type;"); 1646 OUT_END_CODE (); 1647 1648 if (lex_compat) 1649 outn ("#define YY_FLEX_LEX_COMPAT"); 1650 1651 if (!C_plus_plus && !reentrant) { 1652 outn ("extern int yylineno;"); 1653 OUT_BEGIN_CODE (); 1654 outn ("int yylineno = 1;"); 1655 OUT_END_CODE (); 1656 } 1657 1658 if (C_plus_plus) { 1659 outn ("\n#include <FlexLexer.h>"); 1660 1661 if (!do_yywrap) { 1662 outn("\nint yyFlexLexer::yywrap() { return 1; }"); 1663 } 1664 1665 if (yyclass) { 1666 outn ("int yyFlexLexer::yylex()"); 1667 outn ("\t{"); 1668 outn ("\tLexerError( \"yyFlexLexer::yylex invoked but %option yyclass used\" );"); 1669 outn ("\treturn 0;"); 1670 outn ("\t}"); 1671 1672 out_str ("\n#define YY_DECL int %s::yylex()\n", 1673 yyclass); 1674 } 1675 } 1676 1677 else { 1678 1679 /* Watch out: yytext_ptr is a variable when yytext is an array, 1680 * but it's a macro when yytext is a pointer. 1681 */ 1682 if (yytext_is_array) { 1683 if (!reentrant) 1684 outn ("extern char yytext[];\n"); 1685 } 1686 else { 1687 if (reentrant) { 1688 outn ("#define yytext_ptr yytext_r"); 1689 } 1690 else { 1691 outn ("extern char *yytext;"); 1692 1693 outn("#ifdef yytext_ptr"); 1694 outn("#undef yytext_ptr"); 1695 outn("#endif"); 1696 outn ("#define yytext_ptr yytext"); 1697 } 1698 } 1699 1700 if (yyclass) 1701 flexerror (_ 1702 ("%option yyclass only meaningful for C++ scanners")); 1703 } 1704 1705 if (useecs) 1706 numecs = cre8ecs (nextecm, ecgroup, csize); 1707 else 1708 numecs = csize; 1709 1710 /* Now map the equivalence class for NUL to its expected place. */ 1711 ecgroup[0] = ecgroup[csize]; 1712 NUL_ec = ABS (ecgroup[0]); 1713 1714 if (useecs) 1715 ccl2ecl (); 1716 } 1717 1718 1719 /* set_up_initial_allocations - allocate memory for internal tables */ 1720 1721 void set_up_initial_allocations () 1722 { 1723 maximum_mns = (long_align ? MAXIMUM_MNS_LONG : MAXIMUM_MNS); 1724 current_mns = INITIAL_MNS; 1725 firstst = allocate_integer_array (current_mns); 1726 lastst = allocate_integer_array (current_mns); 1727 finalst = allocate_integer_array (current_mns); 1728 transchar = allocate_integer_array (current_mns); 1729 trans1 = allocate_integer_array (current_mns); 1730 trans2 = allocate_integer_array (current_mns); 1731 accptnum = allocate_integer_array (current_mns); 1732 assoc_rule = allocate_integer_array (current_mns); 1733 state_type = allocate_integer_array (current_mns); 1734 1735 current_max_rules = INITIAL_MAX_RULES; 1736 rule_type = allocate_integer_array (current_max_rules); 1737 rule_linenum = allocate_integer_array (current_max_rules); 1738 rule_useful = allocate_integer_array (current_max_rules); 1739 rule_has_nl = allocate_bool_array (current_max_rules); 1740 1741 current_max_scs = INITIAL_MAX_SCS; 1742 scset = allocate_integer_array (current_max_scs); 1743 scbol = allocate_integer_array (current_max_scs); 1744 scxclu = allocate_integer_array (current_max_scs); 1745 sceof = allocate_integer_array (current_max_scs); 1746 scname = allocate_char_ptr_array (current_max_scs); 1747 1748 current_maxccls = INITIAL_MAX_CCLS; 1749 cclmap = allocate_integer_array (current_maxccls); 1750 ccllen = allocate_integer_array (current_maxccls); 1751 cclng = allocate_integer_array (current_maxccls); 1752 ccl_has_nl = allocate_bool_array (current_maxccls); 1753 1754 current_max_ccl_tbl_size = INITIAL_MAX_CCL_TBL_SIZE; 1755 ccltbl = allocate_Character_array (current_max_ccl_tbl_size); 1756 1757 current_max_dfa_size = INITIAL_MAX_DFA_SIZE; 1758 1759 current_max_xpairs = INITIAL_MAX_XPAIRS; 1760 nxt = allocate_integer_array (current_max_xpairs); 1761 chk = allocate_integer_array (current_max_xpairs); 1762 1763 current_max_template_xpairs = INITIAL_MAX_TEMPLATE_XPAIRS; 1764 tnxt = allocate_integer_array (current_max_template_xpairs); 1765 1766 current_max_dfas = INITIAL_MAX_DFAS; 1767 base = allocate_integer_array (current_max_dfas); 1768 def = allocate_integer_array (current_max_dfas); 1769 dfasiz = allocate_integer_array (current_max_dfas); 1770 accsiz = allocate_integer_array (current_max_dfas); 1771 dhash = allocate_integer_array (current_max_dfas); 1772 dss = allocate_int_ptr_array (current_max_dfas); 1773 dfaacc = allocate_dfaacc_union (current_max_dfas); 1774 1775 nultrans = (int *) 0; 1776 } 1777 1778 1779 /* extracts basename from path, optionally stripping the extension "\.*" 1780 * (same concept as /bin/sh `basename`, but different handling of extension). */ 1781 static char *basename2 (path, strip_ext) 1782 char *path; 1783 int strip_ext; /* boolean */ 1784 { 1785 char *b, *e = 0; 1786 1787 b = path; 1788 for (b = path; *path; path++) 1789 if (*path == '/') 1790 b = path + 1; 1791 else if (*path == '.') 1792 e = path; 1793 1794 if (strip_ext && e && e > b) 1795 *e = '\0'; 1796 return b; 1797 } 1798 1799 void usage () 1800 { 1801 FILE *f = stdout; 1802 1803 if (!did_outfilename) { 1804 snprintf (outfile_path, sizeof(outfile_path), outfile_template, 1805 prefix, C_plus_plus ? "cc" : "c"); 1806 outfilename = outfile_path; 1807 } 1808 1809 fprintf (f, _("Usage: %s [OPTIONS] [FILE]...\n"), program_name); 1810 fprintf (f, 1811 _ 1812 ("Generates programs that perform pattern-matching on text.\n" 1813 "\n" "Table Compression:\n" 1814 " -Ca, --align trade off larger tables for better memory alignment\n" 1815 " -Ce, --ecs construct equivalence classes\n" 1816 " -Cf do not compress tables; use -f representation\n" 1817 " -CF do not compress tables; use -F representation\n" 1818 " -Cm, --meta-ecs construct meta-equivalence classes\n" 1819 " -Cr, --read use read() instead of stdio for scanner input\n" 1820 " -f, --full generate fast, large scanner. Same as -Cfr\n" 1821 " -F, --fast use alternate table representation. Same as -CFr\n" 1822 " -Cem default compression (same as --ecs --meta-ecs)\n" 1823 "\n" "Debugging:\n" 1824 " -d, --debug enable debug mode in scanner\n" 1825 " -b, --backup write backing-up information to %s\n" 1826 " -p, --perf-report write performance report to stderr\n" 1827 " -s, --nodefault suppress default rule to ECHO unmatched text\n" 1828 " -T, --trace %s should run in trace mode\n" 1829 " -w, --nowarn do not generate warnings\n" 1830 " -v, --verbose write summary of scanner statistics to stdout\n" 1831 " --hex use hexadecimal numbers instead of octal in debug outputs\n" 1832 "\n" "Files:\n" 1833 " -o, --outfile=FILE specify output filename\n" 1834 " -S, --skel=FILE specify skeleton file\n" 1835 " -t, --stdout write scanner on stdout instead of %s\n" 1836 " --yyclass=NAME name of C++ class\n" 1837 " --header-file=FILE create a C header file in addition to the scanner\n" 1838 " --tables-file[=FILE] write tables to FILE\n" "\n" 1839 "Scanner behavior:\n" 1840 " -7, --7bit generate 7-bit scanner\n" 1841 " -8, --8bit generate 8-bit scanner\n" 1842 " -B, --batch generate batch scanner (opposite of -I)\n" 1843 " -i, --case-insensitive ignore case in patterns\n" 1844 " -l, --lex-compat maximal compatibility with original lex\n" 1845 " -X, --posix-compat maximal compatibility with POSIX lex\n" 1846 " -I, --interactive generate interactive scanner (opposite of -B)\n" 1847 " --yylineno track line count in yylineno\n" 1848 "\n" "Generated code:\n" 1849 " -+, --c++ generate C++ scanner class\n" 1850 " -Dmacro[=defn] #define macro defn (default defn is '1')\n" 1851 " -L, --noline suppress #line directives in scanner\n" 1852 " -P, --prefix=STRING use STRING as prefix instead of \"yy\"\n" 1853 " -R, --reentrant generate a reentrant C scanner\n" 1854 " --bison-bridge scanner for bison pure parser.\n" 1855 " --bison-locations include yylloc support.\n" 1856 " --stdinit initialize yyin/yyout to stdin/stdout\n" 1857 " --noansi-definitions old-style function definitions\n" 1858 " --noansi-prototypes empty parameter list in prototypes\n" 1859 " --nounistd do not include <unistd.h>\n" 1860 " --noFUNCTION do not generate a particular FUNCTION\n" 1861 "\n" "Miscellaneous:\n" 1862 " -c do-nothing POSIX option\n" 1863 " -n do-nothing POSIX option\n" 1864 " -?\n" 1865 " -h, --help produce this help message\n" 1866 " -V, --version report %s version\n"), 1867 backing_name, program_name, outfile_path, program_name); 1868 1869 } 1870