1 /* $Id: output.c,v 1.76 2016/06/07 00:14:34 tom Exp $ */ 2 3 #include "defs.h" 4 5 #define StaticOrR (rflag ? "" : "static ") 6 #define CountLine(fp) (!rflag || ((fp) == code_file)) 7 8 #if defined(YYBTYACC) 9 #define PER_STATE 3 10 #else 11 #define PER_STATE 2 12 #endif 13 14 static int nvectors; 15 static int nentries; 16 static Value_t **froms; 17 static Value_t **tos; 18 #if defined(YYBTYACC) 19 static Value_t *conflicts = NULL; 20 static Value_t nconflicts = 0; 21 #endif 22 static Value_t *tally; 23 static Value_t *width; 24 static Value_t *state_count; 25 static Value_t *order; 26 static Value_t *base; 27 static Value_t *pos; 28 static int maxtable; 29 static Value_t *table; 30 static Value_t *check; 31 static int lowzero; 32 static long high; 33 34 static void 35 putc_code(FILE * fp, int c) 36 { 37 if ((c == '\n') && (fp == code_file)) 38 ++outline; 39 putc(c, fp); 40 } 41 42 static void 43 putl_code(FILE * fp, const char *s) 44 { 45 if (fp == code_file) 46 ++outline; 47 fputs(s, fp); 48 } 49 50 static void 51 puts_code(FILE * fp, const char *s) 52 { 53 fputs(s, fp); 54 } 55 56 static void 57 puts_param_types(FILE * fp, param *list, int more) 58 { 59 param *p; 60 61 if (list != 0) 62 { 63 for (p = list; p; p = p->next) 64 { 65 size_t len_type = strlen(p->type); 66 fprintf(fp, "%s%s%s%s%s", p->type, 67 (((len_type != 0) && (p->type[len_type - 1] == '*')) 68 ? "" 69 : " "), 70 p->name, p->type2, 71 ((more || p->next) ? ", " : "")); 72 } 73 } 74 else 75 { 76 if (!more) 77 fprintf(fp, "void"); 78 } 79 } 80 81 static void 82 puts_param_names(FILE * fp, param *list, int more) 83 { 84 param *p; 85 86 for (p = list; p; p = p->next) 87 { 88 fprintf(fp, "%s%s", p->name, 89 ((more || p->next) ? ", " : "")); 90 } 91 } 92 93 static void 94 write_code_lineno(FILE * fp) 95 { 96 if (!lflag && (fp == code_file)) 97 { 98 ++outline; 99 fprintf(fp, line_format, outline + 1, code_file_name); 100 } 101 } 102 103 static void 104 write_input_lineno(void) 105 { 106 if (!lflag) 107 { 108 ++outline; 109 fprintf(code_file, line_format, lineno, input_file_name); 110 } 111 } 112 113 static void 114 define_prefixed(FILE * fp, const char *name) 115 { 116 int bump_line = CountLine(fp); 117 if (bump_line) 118 ++outline; 119 fprintf(fp, "\n"); 120 121 if (bump_line) 122 ++outline; 123 fprintf(fp, "#ifndef %s\n", name); 124 125 if (bump_line) 126 ++outline; 127 fprintf(fp, "#define %-10s %s%s\n", name, symbol_prefix, name + 2); 128 129 if (bump_line) 130 ++outline; 131 fprintf(fp, "#endif /* %s */\n", name); 132 } 133 134 static void 135 output_prefix(FILE * fp) 136 { 137 if (symbol_prefix == NULL) 138 { 139 symbol_prefix = "yy"; 140 } 141 else 142 { 143 define_prefixed(fp, "yyparse"); 144 define_prefixed(fp, "yylex"); 145 define_prefixed(fp, "yyerror"); 146 define_prefixed(fp, "yychar"); 147 define_prefixed(fp, "yyval"); 148 define_prefixed(fp, "yylval"); 149 define_prefixed(fp, "yydebug"); 150 define_prefixed(fp, "yynerrs"); 151 define_prefixed(fp, "yyerrflag"); 152 define_prefixed(fp, "yylhs"); 153 define_prefixed(fp, "yylen"); 154 define_prefixed(fp, "yydefred"); 155 #if defined(YYBTYACC) 156 define_prefixed(fp, "yystos"); 157 #endif 158 define_prefixed(fp, "yydgoto"); 159 define_prefixed(fp, "yysindex"); 160 define_prefixed(fp, "yyrindex"); 161 define_prefixed(fp, "yygindex"); 162 define_prefixed(fp, "yytable"); 163 define_prefixed(fp, "yycheck"); 164 define_prefixed(fp, "yyname"); 165 define_prefixed(fp, "yyrule"); 166 #if defined(YYBTYACC) 167 if (locations) 168 { 169 define_prefixed(fp, "yyloc"); 170 define_prefixed(fp, "yylloc"); 171 } 172 putc_code(fp, '\n'); 173 putl_code(fp, "#if YYBTYACC\n"); 174 175 define_prefixed(fp, "yycindex"); 176 define_prefixed(fp, "yyctable"); 177 178 putc_code(fp, '\n'); 179 putl_code(fp, "#endif /* YYBTYACC */\n"); 180 putc_code(fp, '\n'); 181 #endif 182 } 183 if (CountLine(fp)) 184 ++outline; 185 fprintf(fp, "#define YYPREFIX \"%s\"\n", symbol_prefix); 186 } 187 188 static void 189 output_newline(void) 190 { 191 if (!rflag) 192 ++outline; 193 putc('\n', output_file); 194 } 195 196 static void 197 output_line(const char *value) 198 { 199 fputs(value, output_file); 200 output_newline(); 201 } 202 203 static void 204 output_int(int value) 205 { 206 fprintf(output_file, "%5d,", value); 207 } 208 209 static void 210 start_int_table(const char *name, int value) 211 { 212 int need = 34 - (int)(strlen(symbol_prefix) + strlen(name)); 213 214 if (need < 6) 215 need = 6; 216 fprintf(output_file, 217 "%sconst YYINT %s%s[] = {%*d,", 218 StaticOrR, symbol_prefix, name, need, value); 219 } 220 221 static void 222 start_str_table(const char *name) 223 { 224 fprintf(output_file, 225 "%sconst char *const %s%s[] = {", 226 StaticOrR, symbol_prefix, name); 227 output_newline(); 228 } 229 230 static void 231 end_table(void) 232 { 233 output_newline(); 234 output_line("};"); 235 } 236 237 static void 238 output_YYINT_typedef(FILE * fp) 239 { 240 /* generate the type used to index the various parser tables */ 241 if (CountLine(fp)) 242 ++outline; 243 fprintf(fp, "typedef %s YYINT;\n", CONCAT1("", YYINT)); 244 } 245 246 static void 247 output_rule_data(void) 248 { 249 int i; 250 int j; 251 252 output_YYINT_typedef(output_file); 253 254 start_int_table("lhs", symbol_value[start_symbol]); 255 256 j = 10; 257 for (i = 3; i < nrules; i++) 258 { 259 if (j >= 10) 260 { 261 output_newline(); 262 j = 1; 263 } 264 else 265 ++j; 266 267 output_int(symbol_value[rlhs[i]]); 268 } 269 end_table(); 270 271 start_int_table("len", 2); 272 273 j = 10; 274 for (i = 3; i < nrules; i++) 275 { 276 if (j >= 10) 277 { 278 output_newline(); 279 j = 1; 280 } 281 else 282 j++; 283 284 output_int(rrhs[i + 1] - rrhs[i] - 1); 285 } 286 end_table(); 287 } 288 289 static void 290 output_yydefred(void) 291 { 292 int i, j; 293 294 start_int_table("defred", (defred[0] ? defred[0] - 2 : 0)); 295 296 j = 10; 297 for (i = 1; i < nstates; i++) 298 { 299 if (j < 10) 300 ++j; 301 else 302 { 303 output_newline(); 304 j = 1; 305 } 306 307 output_int((defred[i] ? defred[i] - 2 : 0)); 308 } 309 310 end_table(); 311 } 312 313 #if defined(YYBTYACC) 314 static void 315 output_accessing_symbols(void) 316 { 317 int i, j; 318 int *translate; 319 320 if (nstates != 0) 321 { 322 translate = TMALLOC(int, nstates); 323 NO_SPACE(translate); 324 325 for (i = 0; i < nstates; ++i) 326 { 327 int gsymb = accessing_symbol[i]; 328 329 translate[i] = symbol_pval[gsymb]; 330 } 331 332 /* yystos[] may be unused, depending on compile-time defines */ 333 start_int_table("stos", translate[0]); 334 335 j = 10; 336 for (i = 1; i < nstates; ++i) 337 { 338 if (j < 10) 339 ++j; 340 else 341 { 342 output_newline(); 343 j = 1; 344 } 345 346 output_int(translate[i]); 347 } 348 349 end_table(); 350 FREE(translate); 351 } 352 } 353 354 static Value_t 355 find_conflict_base(int cbase) 356 { 357 int i, j; 358 359 for (i = 0; i < cbase; i++) 360 { 361 for (j = 0; j + cbase < nconflicts; j++) 362 { 363 if (conflicts[i + j] != conflicts[cbase + j]) 364 break; 365 } 366 if (j + cbase >= nconflicts) 367 break; 368 } 369 return (Value_t)i; 370 } 371 #endif 372 373 static void 374 token_actions(void) 375 { 376 int i, j; 377 Value_t shiftcount, reducecount; 378 #if defined(YYBTYACC) 379 Value_t conflictcount = 0; 380 Value_t csym = -1; 381 Value_t cbase = 0; 382 #endif 383 int max, min; 384 Value_t *actionrow, *r, *s; 385 action *p; 386 387 actionrow = NEW2(PER_STATE * ntokens, Value_t); 388 for (i = 0; i < nstates; ++i) 389 { 390 if (parser[i]) 391 { 392 for (j = 0; j < PER_STATE * ntokens; ++j) 393 actionrow[j] = 0; 394 395 shiftcount = 0; 396 reducecount = 0; 397 #if defined(YYBTYACC) 398 if (backtrack) 399 { 400 conflictcount = 0; 401 csym = -1; 402 cbase = nconflicts; 403 } 404 #endif 405 for (p = parser[i]; p; p = p->next) 406 { 407 #if defined(YYBTYACC) 408 if (backtrack) 409 { 410 if (csym != -1 && csym != p->symbol) 411 { 412 conflictcount++; 413 conflicts[nconflicts++] = -1; 414 j = find_conflict_base(cbase); 415 actionrow[csym + 2 * ntokens] = (Value_t)(j + 1); 416 if (j == cbase) 417 { 418 cbase = nconflicts; 419 } 420 else 421 { 422 if (conflicts[cbase] == -1) 423 cbase++; 424 nconflicts = cbase; 425 } 426 csym = -1; 427 } 428 } 429 #endif 430 if (p->suppressed == 0) 431 { 432 if (p->action_code == SHIFT) 433 { 434 ++shiftcount; 435 actionrow[p->symbol] = p->number; 436 } 437 else if (p->action_code == REDUCE && p->number != defred[i]) 438 { 439 ++reducecount; 440 actionrow[p->symbol + ntokens] = p->number; 441 } 442 } 443 #if defined(YYBTYACC) 444 else if (backtrack && p->suppressed == 1) 445 { 446 csym = p->symbol; 447 if (p->action_code == SHIFT) 448 { 449 conflicts[nconflicts++] = p->number; 450 } 451 else if (p->action_code == REDUCE && p->number != defred[i]) 452 { 453 if (cbase == nconflicts) 454 { 455 if (cbase) 456 cbase--; 457 else 458 conflicts[nconflicts++] = -1; 459 } 460 conflicts[nconflicts++] = (Value_t)(p->number - 2); 461 } 462 } 463 #endif 464 } 465 #if defined(YYBTYACC) 466 if (backtrack && csym != -1) 467 { 468 conflictcount++; 469 conflicts[nconflicts++] = -1; 470 j = find_conflict_base(cbase); 471 actionrow[csym + 2 * ntokens] = (Value_t)(j + 1); 472 if (j == cbase) 473 { 474 cbase = nconflicts; 475 } 476 else 477 { 478 if (conflicts[cbase] == -1) 479 cbase++; 480 nconflicts = cbase; 481 } 482 } 483 #endif 484 485 tally[i] = shiftcount; 486 tally[nstates + i] = reducecount; 487 #if defined(YYBTYACC) 488 if (backtrack) 489 tally[2 * nstates + i] = conflictcount; 490 #endif 491 width[i] = 0; 492 width[nstates + i] = 0; 493 #if defined(YYBTYACC) 494 if (backtrack) 495 width[2 * nstates + i] = 0; 496 #endif 497 if (shiftcount > 0) 498 { 499 froms[i] = r = NEW2(shiftcount, Value_t); 500 tos[i] = s = NEW2(shiftcount, Value_t); 501 min = MAXYYINT; 502 max = 0; 503 for (j = 0; j < ntokens; ++j) 504 { 505 if (actionrow[j]) 506 { 507 if (min > symbol_value[j]) 508 min = symbol_value[j]; 509 if (max < symbol_value[j]) 510 max = symbol_value[j]; 511 *r++ = symbol_value[j]; 512 *s++ = actionrow[j]; 513 } 514 } 515 width[i] = (Value_t)(max - min + 1); 516 } 517 if (reducecount > 0) 518 { 519 froms[nstates + i] = r = NEW2(reducecount, Value_t); 520 tos[nstates + i] = s = NEW2(reducecount, Value_t); 521 min = MAXYYINT; 522 max = 0; 523 for (j = 0; j < ntokens; ++j) 524 { 525 if (actionrow[ntokens + j]) 526 { 527 if (min > symbol_value[j]) 528 min = symbol_value[j]; 529 if (max < symbol_value[j]) 530 max = symbol_value[j]; 531 *r++ = symbol_value[j]; 532 *s++ = (Value_t)(actionrow[ntokens + j] - 2); 533 } 534 } 535 width[nstates + i] = (Value_t)(max - min + 1); 536 } 537 #if defined(YYBTYACC) 538 if (backtrack && conflictcount > 0) 539 { 540 froms[2 * nstates + i] = r = NEW2(conflictcount, Value_t); 541 tos[2 * nstates + i] = s = NEW2(conflictcount, Value_t); 542 min = MAXYYINT; 543 max = 0; 544 for (j = 0; j < ntokens; ++j) 545 { 546 if (actionrow[2 * ntokens + j]) 547 { 548 if (min > symbol_value[j]) 549 min = symbol_value[j]; 550 if (max < symbol_value[j]) 551 max = symbol_value[j]; 552 *r++ = symbol_value[j]; 553 *s++ = (Value_t)(actionrow[2 * ntokens + j] - 1); 554 } 555 } 556 width[2 * nstates + i] = (Value_t)(max - min + 1); 557 } 558 #endif 559 } 560 } 561 FREE(actionrow); 562 } 563 564 static int 565 default_goto(int symbol) 566 { 567 int i; 568 int m; 569 int n; 570 int default_state; 571 int max; 572 573 m = goto_map[symbol]; 574 n = goto_map[symbol + 1]; 575 576 if (m == n) 577 return (0); 578 579 for (i = 0; i < nstates; i++) 580 state_count[i] = 0; 581 582 for (i = m; i < n; i++) 583 state_count[to_state[i]]++; 584 585 max = 0; 586 default_state = 0; 587 for (i = 0; i < nstates; i++) 588 { 589 if (state_count[i] > max) 590 { 591 max = state_count[i]; 592 default_state = i; 593 } 594 } 595 596 return (default_state); 597 } 598 599 static void 600 save_column(int symbol, int default_state) 601 { 602 int i; 603 int m; 604 int n; 605 Value_t *sp; 606 Value_t *sp1; 607 Value_t *sp2; 608 Value_t count; 609 int symno; 610 611 m = goto_map[symbol]; 612 n = goto_map[symbol + 1]; 613 614 count = 0; 615 for (i = m; i < n; i++) 616 { 617 if (to_state[i] != default_state) 618 ++count; 619 } 620 if (count == 0) 621 return; 622 623 symno = symbol_value[symbol] + PER_STATE * nstates; 624 625 froms[symno] = sp1 = sp = NEW2(count, Value_t); 626 tos[symno] = sp2 = NEW2(count, Value_t); 627 628 for (i = m; i < n; i++) 629 { 630 if (to_state[i] != default_state) 631 { 632 *sp1++ = from_state[i]; 633 *sp2++ = to_state[i]; 634 } 635 } 636 637 tally[symno] = count; 638 width[symno] = (Value_t)(sp1[-1] - sp[0] + 1); 639 } 640 641 static void 642 goto_actions(void) 643 { 644 int i, j, k; 645 646 state_count = NEW2(nstates, Value_t); 647 648 k = default_goto(start_symbol + 1); 649 start_int_table("dgoto", k); 650 save_column(start_symbol + 1, k); 651 652 j = 10; 653 for (i = start_symbol + 2; i < nsyms; i++) 654 { 655 if (j >= 10) 656 { 657 output_newline(); 658 j = 1; 659 } 660 else 661 ++j; 662 663 k = default_goto(i); 664 output_int(k); 665 save_column(i, k); 666 } 667 668 end_table(); 669 FREE(state_count); 670 } 671 672 static void 673 sort_actions(void) 674 { 675 Value_t i; 676 int j; 677 int k; 678 int t; 679 int w; 680 681 order = NEW2(nvectors, Value_t); 682 nentries = 0; 683 684 for (i = 0; i < nvectors; i++) 685 { 686 if (tally[i] > 0) 687 { 688 t = tally[i]; 689 w = width[i]; 690 j = nentries - 1; 691 692 while (j >= 0 && (width[order[j]] < w)) 693 j--; 694 695 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t)) 696 j--; 697 698 for (k = nentries - 1; k > j; k--) 699 order[k + 1] = order[k]; 700 701 order[j + 1] = i; 702 nentries++; 703 } 704 } 705 } 706 707 /* The function matching_vector determines if the vector specified by */ 708 /* the input parameter matches a previously considered vector. The */ 709 /* test at the start of the function checks if the vector represents */ 710 /* a row of shifts over terminal symbols or a row of reductions, or a */ 711 /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */ 712 /* check if a column of shifts over a nonterminal symbols matches a */ 713 /* previously considered vector. Because of the nature of LR parsing */ 714 /* tables, no two columns can match. Therefore, the only possible */ 715 /* match would be between a row and a column. Such matches are */ 716 /* unlikely. Therefore, to save time, no attempt is made to see if a */ 717 /* column matches a previously considered vector. */ 718 /* */ 719 /* Matching_vector is poorly designed. The test could easily be made */ 720 /* faster. Also, it depends on the vectors being in a specific */ 721 /* order. */ 722 #if defined(YYBTYACC) 723 /* */ 724 /* Not really any point in checking for matching conflicts -- it is */ 725 /* extremely unlikely to occur, and conflicts are (hopefully) rare. */ 726 #endif 727 728 static int 729 matching_vector(int vector) 730 { 731 int i; 732 int j; 733 int k; 734 int t; 735 int w; 736 int match; 737 int prev; 738 739 i = order[vector]; 740 if (i >= 2 * nstates) 741 return (-1); 742 743 t = tally[i]; 744 w = width[i]; 745 746 for (prev = vector - 1; prev >= 0; prev--) 747 { 748 j = order[prev]; 749 if (width[j] != w || tally[j] != t) 750 return (-1); 751 752 match = 1; 753 for (k = 0; match && k < t; k++) 754 { 755 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k]) 756 match = 0; 757 } 758 759 if (match) 760 return (j); 761 } 762 763 return (-1); 764 } 765 766 static int 767 pack_vector(int vector) 768 { 769 int i, j, k, l; 770 int t; 771 int loc; 772 int ok; 773 Value_t *from; 774 Value_t *to; 775 int newmax; 776 777 i = order[vector]; 778 t = tally[i]; 779 assert(t); 780 781 from = froms[i]; 782 to = tos[i]; 783 784 j = lowzero - from[0]; 785 for (k = 1; k < t; ++k) 786 if (lowzero - from[k] > j) 787 j = lowzero - from[k]; 788 for (;; ++j) 789 { 790 if (j == 0) 791 continue; 792 ok = 1; 793 for (k = 0; ok && k < t; k++) 794 { 795 loc = j + from[k]; 796 if (loc >= maxtable - 1) 797 { 798 if (loc >= MAXTABLE - 1) 799 fatal("maximum table size exceeded"); 800 801 newmax = maxtable; 802 do 803 { 804 newmax += 200; 805 } 806 while (newmax <= loc); 807 808 table = TREALLOC(Value_t, table, newmax); 809 NO_SPACE(table); 810 811 check = TREALLOC(Value_t, check, newmax); 812 NO_SPACE(check); 813 814 for (l = maxtable; l < newmax; ++l) 815 { 816 table[l] = 0; 817 check[l] = -1; 818 } 819 maxtable = newmax; 820 } 821 822 if (check[loc] != -1) 823 ok = 0; 824 } 825 for (k = 0; ok && k < vector; k++) 826 { 827 if (pos[k] == j) 828 ok = 0; 829 } 830 if (ok) 831 { 832 for (k = 0; k < t; k++) 833 { 834 loc = j + from[k]; 835 table[loc] = to[k]; 836 check[loc] = from[k]; 837 if (loc > high) 838 high = loc; 839 } 840 841 while (check[lowzero] != -1) 842 ++lowzero; 843 844 return (j); 845 } 846 } 847 } 848 849 static void 850 pack_table(void) 851 { 852 int i; 853 Value_t place; 854 int state; 855 856 base = NEW2(nvectors, Value_t); 857 pos = NEW2(nentries, Value_t); 858 859 maxtable = 1000; 860 table = NEW2(maxtable, Value_t); 861 check = NEW2(maxtable, Value_t); 862 863 lowzero = 0; 864 high = 0; 865 866 for (i = 0; i < maxtable; i++) 867 check[i] = -1; 868 869 for (i = 0; i < nentries; i++) 870 { 871 state = matching_vector(i); 872 873 if (state < 0) 874 place = (Value_t)pack_vector(i); 875 else 876 place = base[state]; 877 878 pos[i] = place; 879 base[order[i]] = place; 880 } 881 882 for (i = 0; i < nvectors; i++) 883 { 884 if (froms[i]) 885 FREE(froms[i]); 886 if (tos[i]) 887 FREE(tos[i]); 888 } 889 890 DO_FREE(froms); 891 DO_FREE(tos); 892 DO_FREE(tally); 893 DO_FREE(width); 894 DO_FREE(pos); 895 } 896 897 static void 898 output_base(void) 899 { 900 int i, j; 901 902 start_int_table("sindex", base[0]); 903 904 j = 10; 905 for (i = 1; i < nstates; i++) 906 { 907 if (j >= 10) 908 { 909 output_newline(); 910 j = 1; 911 } 912 else 913 ++j; 914 915 output_int(base[i]); 916 } 917 918 end_table(); 919 920 start_int_table("rindex", base[nstates]); 921 922 j = 10; 923 for (i = nstates + 1; i < 2 * nstates; i++) 924 { 925 if (j >= 10) 926 { 927 output_newline(); 928 j = 1; 929 } 930 else 931 ++j; 932 933 output_int(base[i]); 934 } 935 936 end_table(); 937 938 #if defined(YYBTYACC) 939 output_line("#if YYBTYACC"); 940 start_int_table("cindex", base[2 * nstates]); 941 942 j = 10; 943 for (i = 2 * nstates + 1; i < 3 * nstates; i++) 944 { 945 if (j >= 10) 946 { 947 output_newline(); 948 j = 1; 949 } 950 else 951 ++j; 952 953 output_int(base[i]); 954 } 955 956 end_table(); 957 output_line("#endif"); 958 #endif 959 960 start_int_table("gindex", base[PER_STATE * nstates]); 961 962 j = 10; 963 for (i = PER_STATE * nstates + 1; i < nvectors - 1; i++) 964 { 965 if (j >= 10) 966 { 967 output_newline(); 968 j = 1; 969 } 970 else 971 ++j; 972 973 output_int(base[i]); 974 } 975 976 end_table(); 977 FREE(base); 978 } 979 980 static void 981 output_table(void) 982 { 983 int i; 984 int j; 985 986 if (high >= MAXYYINT) 987 { 988 fprintf(stderr, "YYTABLESIZE: %ld\n", high); 989 fprintf(stderr, "Table is longer than %d elements.\n", MAXYYINT); 990 done(1); 991 } 992 993 ++outline; 994 fprintf(code_file, "#define YYTABLESIZE %ld\n", high); 995 start_int_table("table", table[0]); 996 997 j = 10; 998 for (i = 1; i <= high; i++) 999 { 1000 if (j >= 10) 1001 { 1002 output_newline(); 1003 j = 1; 1004 } 1005 else 1006 ++j; 1007 1008 output_int(table[i]); 1009 } 1010 1011 end_table(); 1012 FREE(table); 1013 } 1014 1015 static void 1016 output_check(void) 1017 { 1018 int i; 1019 int j; 1020 1021 start_int_table("check", check[0]); 1022 1023 j = 10; 1024 for (i = 1; i <= high; i++) 1025 { 1026 if (j >= 10) 1027 { 1028 output_newline(); 1029 j = 1; 1030 } 1031 else 1032 ++j; 1033 1034 output_int(check[i]); 1035 } 1036 1037 end_table(); 1038 FREE(check); 1039 } 1040 1041 #if defined(YYBTYACC) 1042 static void 1043 output_ctable(void) 1044 { 1045 int i; 1046 int j; 1047 int limit = (conflicts != 0) ? nconflicts : 0; 1048 1049 if (limit < high) 1050 limit = (int)high; 1051 1052 output_line("#if YYBTYACC"); 1053 start_int_table("ctable", conflicts ? conflicts[0] : -1); 1054 1055 j = 10; 1056 for (i = 1; i < limit; i++) 1057 { 1058 if (j >= 10) 1059 { 1060 output_newline(); 1061 j = 1; 1062 } 1063 else 1064 ++j; 1065 1066 output_int((conflicts != 0 && i < nconflicts) ? conflicts[i] : -1); 1067 } 1068 1069 if (conflicts) 1070 FREE(conflicts); 1071 1072 end_table(); 1073 output_line("#endif"); 1074 } 1075 #endif 1076 1077 static void 1078 output_actions(void) 1079 { 1080 nvectors = PER_STATE * nstates + nvars; 1081 1082 froms = NEW2(nvectors, Value_t *); 1083 tos = NEW2(nvectors, Value_t *); 1084 tally = NEW2(nvectors, Value_t); 1085 width = NEW2(nvectors, Value_t); 1086 1087 #if defined(YYBTYACC) 1088 if (backtrack && (SRtotal + RRtotal) != 0) 1089 conflicts = NEW2(4 * (SRtotal + RRtotal), Value_t); 1090 #endif 1091 1092 token_actions(); 1093 FREE(lookaheads); 1094 FREE(LA); 1095 FREE(LAruleno); 1096 FREE(accessing_symbol); 1097 1098 goto_actions(); 1099 FREE(goto_base); 1100 FREE(from_state); 1101 FREE(to_state); 1102 1103 sort_actions(); 1104 pack_table(); 1105 output_base(); 1106 output_table(); 1107 output_check(); 1108 #if defined(YYBTYACC) 1109 output_ctable(); 1110 #endif 1111 } 1112 1113 static int 1114 is_C_identifier(char *name) 1115 { 1116 char *s; 1117 int c; 1118 1119 s = name; 1120 c = *s; 1121 if (c == '"') 1122 { 1123 c = *++s; 1124 if (!isalpha(c) && c != '_' && c != '$') 1125 return (0); 1126 while ((c = *++s) != '"') 1127 { 1128 if (!isalnum(c) && c != '_' && c != '$') 1129 return (0); 1130 } 1131 return (1); 1132 } 1133 1134 if (!isalpha(c) && c != '_' && c != '$') 1135 return (0); 1136 while ((c = *++s) != 0) 1137 { 1138 if (!isalnum(c) && c != '_' && c != '$') 1139 return (0); 1140 } 1141 return (1); 1142 } 1143 1144 #if USE_HEADER_GUARDS 1145 static void 1146 start_defines_file(void) 1147 { 1148 fprintf(defines_file, "#ifndef _%s_defines_h_\n", symbol_prefix); 1149 fprintf(defines_file, "#define _%s_defines_h_\n\n", symbol_prefix); 1150 } 1151 1152 static void 1153 end_defines_file(void) 1154 { 1155 fprintf(defines_file, "\n#endif /* _%s_defines_h_ */\n", symbol_prefix); 1156 } 1157 #else 1158 #define start_defines_file() /* nothing */ 1159 #define end_defines_file() /* nothing */ 1160 #endif 1161 1162 static void 1163 output_defines(FILE * fp) 1164 { 1165 int c, i; 1166 char *s; 1167 1168 for (i = 2; i < ntokens; ++i) 1169 { 1170 s = symbol_name[i]; 1171 if (is_C_identifier(s) && (!sflag || *s != '"')) 1172 { 1173 fprintf(fp, "#define "); 1174 c = *s; 1175 if (c == '"') 1176 { 1177 while ((c = *++s) != '"') 1178 { 1179 putc(c, fp); 1180 } 1181 } 1182 else 1183 { 1184 do 1185 { 1186 putc(c, fp); 1187 } 1188 while ((c = *++s) != 0); 1189 } 1190 if (fp == code_file) 1191 ++outline; 1192 fprintf(fp, " %d\n", symbol_value[i]); 1193 } 1194 } 1195 1196 if (fp == code_file) 1197 ++outline; 1198 if (fp != defines_file || iflag) 1199 fprintf(fp, "#define YYERRCODE %d\n", symbol_value[1]); 1200 1201 if (token_table && rflag && fp != externs_file) 1202 { 1203 if (fp == code_file) 1204 ++outline; 1205 fputs("#undef yytname\n", fp); 1206 if (fp == code_file) 1207 ++outline; 1208 fputs("#define yytname yyname\n", fp); 1209 } 1210 1211 if (fp == defines_file || (iflag && !dflag)) 1212 { 1213 if (unionized) 1214 { 1215 if (union_file != 0) 1216 { 1217 rewind(union_file); 1218 while ((c = getc(union_file)) != EOF) 1219 putc_code(fp, c); 1220 } 1221 fprintf(fp, "extern YYSTYPE %slval;\n", symbol_prefix); 1222 } 1223 } 1224 } 1225 1226 static void 1227 output_stored_text(FILE * fp) 1228 { 1229 int c; 1230 FILE *in; 1231 1232 rewind(text_file); 1233 if (text_file == NULL) 1234 open_error("text_file"); 1235 in = text_file; 1236 if ((c = getc(in)) == EOF) 1237 return; 1238 putc_code(fp, c); 1239 while ((c = getc(in)) != EOF) 1240 { 1241 putc_code(fp, c); 1242 } 1243 write_code_lineno(fp); 1244 } 1245 1246 static void 1247 output_debug(void) 1248 { 1249 int i, j, k, max, maxtok; 1250 const char **symnam; 1251 const char *s; 1252 1253 ++outline; 1254 fprintf(code_file, "#define YYFINAL %d\n", final_state); 1255 1256 putl_code(code_file, "#ifndef YYDEBUG\n"); 1257 ++outline; 1258 fprintf(code_file, "#define YYDEBUG %d\n", tflag); 1259 putl_code(code_file, "#endif\n"); 1260 1261 if (rflag) 1262 { 1263 fprintf(output_file, "#ifndef YYDEBUG\n"); 1264 fprintf(output_file, "#define YYDEBUG %d\n", tflag); 1265 fprintf(output_file, "#endif\n"); 1266 } 1267 1268 maxtok = 0; 1269 for (i = 0; i < ntokens; ++i) 1270 if (symbol_value[i] > maxtok) 1271 maxtok = symbol_value[i]; 1272 1273 /* symbol_value[$accept] = -1 */ 1274 /* symbol_value[<goal>] = 0 */ 1275 /* remaining non-terminals start at 1 */ 1276 max = maxtok; 1277 for (i = ntokens; i < nsyms; ++i) 1278 if (((maxtok + 1) + (symbol_value[i] + 1)) > max) 1279 max = (maxtok + 1) + (symbol_value[i] + 1); 1280 1281 ++outline; 1282 fprintf(code_file, "#define YYMAXTOKEN %d\n", maxtok); 1283 1284 ++outline; 1285 fprintf(code_file, "#define YYUNDFTOKEN %d\n", max + 1); 1286 1287 ++outline; 1288 fprintf(code_file, "#define YYTRANSLATE(a) ((a) > YYMAXTOKEN ? " 1289 "YYUNDFTOKEN : (a))\n"); 1290 1291 symnam = TMALLOC(const char *, max + 2); 1292 NO_SPACE(symnam); 1293 1294 /* Note that it is not necessary to initialize the element */ 1295 /* symnam[max]. */ 1296 #if defined(YYBTYACC) 1297 for (i = 0; i < max; ++i) 1298 symnam[i] = 0; 1299 for (i = nsyms - 1; i >= 0; --i) 1300 symnam[symbol_pval[i]] = symbol_name[i]; 1301 symnam[max + 1] = "illegal-symbol"; 1302 #else 1303 for (i = 0; i <= max; ++i) 1304 symnam[i] = 0; 1305 for (i = ntokens - 1; i >= 2; --i) 1306 symnam[symbol_value[i]] = symbol_name[i]; 1307 symnam[0] = "end-of-file"; 1308 symnam[max + 1] = "illegal-symbol"; 1309 #endif 1310 1311 /* 1312 * bison's yytname[] array is roughly the same as byacc's yyname[] array. 1313 * The difference is that byacc does not predefine "$undefined". 1314 * 1315 * If the grammar declares "%token-table", define symbol "yytname" so 1316 * an application such as ntpd can build. 1317 */ 1318 if (token_table) 1319 { 1320 if (!rflag) 1321 { 1322 output_line("#undef yytname"); 1323 output_line("#define yytname yyname"); 1324 } 1325 } 1326 else 1327 { 1328 output_line("#if YYDEBUG"); 1329 } 1330 1331 start_str_table("name"); 1332 j = 80; 1333 for (i = 0; i <= max + 1; ++i) 1334 { 1335 if ((s = symnam[i]) != 0) 1336 { 1337 if (s[0] == '"') 1338 { 1339 k = 7; 1340 while (*++s != '"') 1341 { 1342 ++k; 1343 if (*s == '\\') 1344 { 1345 k += 2; 1346 if (*++s == '\\') 1347 ++k; 1348 } 1349 } 1350 j += k; 1351 if (j > 80) 1352 { 1353 output_newline(); 1354 j = k; 1355 } 1356 fprintf(output_file, "\"\\\""); 1357 s = symnam[i]; 1358 while (*++s != '"') 1359 { 1360 if (*s == '\\') 1361 { 1362 fprintf(output_file, "\\\\"); 1363 if (*++s == '\\') 1364 fprintf(output_file, "\\\\"); 1365 else 1366 putc(*s, output_file); 1367 } 1368 else 1369 putc(*s, output_file); 1370 } 1371 fprintf(output_file, "\\\"\","); 1372 } 1373 else if (s[0] == '\'') 1374 { 1375 if (s[1] == '"') 1376 { 1377 j += 7; 1378 if (j > 80) 1379 { 1380 output_newline(); 1381 j = 7; 1382 } 1383 fprintf(output_file, "\"'\\\"'\","); 1384 } 1385 else 1386 { 1387 k = 5; 1388 while (*++s != '\'') 1389 { 1390 ++k; 1391 if (*s == '\\') 1392 { 1393 k += 2; 1394 if (*++s == '\\') 1395 ++k; 1396 } 1397 } 1398 j += k; 1399 if (j > 80) 1400 { 1401 output_newline(); 1402 j = k; 1403 } 1404 fprintf(output_file, "\"'"); 1405 s = symnam[i]; 1406 while (*++s != '\'') 1407 { 1408 if (*s == '\\') 1409 { 1410 fprintf(output_file, "\\\\"); 1411 if (*++s == '\\') 1412 fprintf(output_file, "\\\\"); 1413 else 1414 putc(*s, output_file); 1415 } 1416 else 1417 putc(*s, output_file); 1418 } 1419 fprintf(output_file, "'\","); 1420 } 1421 } 1422 else 1423 { 1424 k = (int)strlen(s) + 3; 1425 j += k; 1426 if (j > 80) 1427 { 1428 output_newline(); 1429 j = k; 1430 } 1431 putc('"', output_file); 1432 do 1433 { 1434 putc(*s, output_file); 1435 } 1436 while (*++s); 1437 fprintf(output_file, "\","); 1438 } 1439 } 1440 else 1441 { 1442 j += 2; 1443 if (j > 80) 1444 { 1445 output_newline(); 1446 j = 2; 1447 } 1448 fprintf(output_file, "0,"); 1449 } 1450 } 1451 end_table(); 1452 FREE(symnam); 1453 1454 if (token_table) 1455 output_line("#if YYDEBUG"); 1456 start_str_table("rule"); 1457 for (i = 2; i < nrules; ++i) 1458 { 1459 fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]); 1460 for (j = rrhs[i]; ritem[j] > 0; ++j) 1461 { 1462 s = symbol_name[ritem[j]]; 1463 if (s[0] == '"') 1464 { 1465 fprintf(output_file, " \\\""); 1466 while (*++s != '"') 1467 { 1468 if (*s == '\\') 1469 { 1470 if (s[1] == '\\') 1471 fprintf(output_file, "\\\\\\\\"); 1472 else 1473 fprintf(output_file, "\\\\%c", s[1]); 1474 ++s; 1475 } 1476 else 1477 putc(*s, output_file); 1478 } 1479 fprintf(output_file, "\\\""); 1480 } 1481 else if (s[0] == '\'') 1482 { 1483 if (s[1] == '"') 1484 fprintf(output_file, " '\\\"'"); 1485 else if (s[1] == '\\') 1486 { 1487 if (s[2] == '\\') 1488 fprintf(output_file, " '\\\\\\\\"); 1489 else 1490 fprintf(output_file, " '\\\\%c", s[2]); 1491 s += 2; 1492 while (*++s != '\'') 1493 putc(*s, output_file); 1494 putc('\'', output_file); 1495 } 1496 else 1497 fprintf(output_file, " '%c'", s[1]); 1498 } 1499 else 1500 fprintf(output_file, " %s", s); 1501 } 1502 fprintf(output_file, "\","); 1503 output_newline(); 1504 } 1505 1506 end_table(); 1507 output_line("#endif"); 1508 } 1509 1510 #if defined(YYBTYACC) 1511 static void 1512 output_backtracking_parser(FILE * fp) 1513 { 1514 putl_code(fp, "#undef YYBTYACC\n"); 1515 #if defined(YYBTYACC) 1516 if (backtrack) 1517 { 1518 putl_code(fp, "#define YYBTYACC 1\n"); 1519 putl_code(fp, 1520 "#define YYDEBUGSTR (yytrial ? YYPREFIX \"debug(trial)\" : YYPREFIX \"debug\")\n"); 1521 } 1522 else 1523 #endif 1524 { 1525 putl_code(fp, "#define YYBTYACC 0\n"); 1526 putl_code(fp, "#define YYDEBUGSTR YYPREFIX \"debug\"\n"); 1527 } 1528 } 1529 #endif 1530 1531 static void 1532 output_pure_parser(FILE * fp) 1533 { 1534 putc_code(fp, '\n'); 1535 1536 if (fp == code_file) 1537 ++outline; 1538 fprintf(fp, "#define YYPURE %d\n", pure_parser); 1539 putc_code(fp, '\n'); 1540 } 1541 1542 static void 1543 output_stype(FILE * fp) 1544 { 1545 if (!unionized && ntags == 0) 1546 { 1547 putc_code(fp, '\n'); 1548 putl_code(fp, "#if " 1549 "! defined(YYSTYPE) && " 1550 "! defined(YYSTYPE_IS_DECLARED)\n"); 1551 putl_code(fp, "/* Default: YYSTYPE is the semantic value type. */\n"); 1552 putl_code(fp, "typedef int YYSTYPE;\n"); 1553 putl_code(fp, "# define YYSTYPE_IS_DECLARED 1\n"); 1554 putl_code(fp, "#endif\n"); 1555 } 1556 } 1557 1558 #if defined(YYBTYACC) 1559 static void 1560 output_ltype(FILE * fp) 1561 { 1562 putc_code(fp, '\n'); 1563 putl_code(fp, "#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED\n"); 1564 putl_code(fp, "/* Default: YYLTYPE is the text position type. */\n"); 1565 putl_code(fp, "typedef struct YYLTYPE\n"); 1566 putl_code(fp, "{\n"); 1567 putl_code(fp, " int first_line;\n"); 1568 putl_code(fp, " int first_column;\n"); 1569 putl_code(fp, " int last_line;\n"); 1570 putl_code(fp, " int last_column;\n"); 1571 putl_code(fp, "} YYLTYPE;\n"); 1572 putl_code(fp, "#define YYLTYPE_IS_DECLARED 1\n"); 1573 putl_code(fp, "#endif\n"); 1574 } 1575 #endif 1576 1577 static void 1578 output_trailing_text(void) 1579 { 1580 int c, last; 1581 FILE *in; 1582 1583 if (line == 0) 1584 return; 1585 1586 in = input_file; 1587 c = *cptr; 1588 if (c == '\n') 1589 { 1590 ++lineno; 1591 if ((c = getc(in)) == EOF) 1592 return; 1593 write_input_lineno(); 1594 putc_code(code_file, c); 1595 last = c; 1596 } 1597 else 1598 { 1599 write_input_lineno(); 1600 do 1601 { 1602 putc_code(code_file, c); 1603 } 1604 while ((c = *++cptr) != '\n'); 1605 putc_code(code_file, c); 1606 last = '\n'; 1607 } 1608 1609 while ((c = getc(in)) != EOF) 1610 { 1611 putc_code(code_file, c); 1612 last = c; 1613 } 1614 1615 if (last != '\n') 1616 { 1617 putc_code(code_file, '\n'); 1618 } 1619 write_code_lineno(code_file); 1620 } 1621 1622 static void 1623 output_semantic_actions(void) 1624 { 1625 int c, last; 1626 1627 rewind(action_file); 1628 if ((c = getc(action_file)) == EOF) 1629 return; 1630 1631 last = c; 1632 putc_code(code_file, c); 1633 while ((c = getc(action_file)) != EOF) 1634 { 1635 putc_code(code_file, c); 1636 last = c; 1637 } 1638 1639 if (last != '\n') 1640 { 1641 putc_code(code_file, '\n'); 1642 } 1643 1644 write_code_lineno(code_file); 1645 } 1646 1647 static void 1648 output_parse_decl(FILE * fp) 1649 { 1650 putc_code(fp, '\n'); 1651 putl_code(fp, "/* compatibility with bison */\n"); 1652 putl_code(fp, "#ifdef YYPARSE_PARAM\n"); 1653 putl_code(fp, "/* compatibility with FreeBSD */\n"); 1654 putl_code(fp, "# ifdef YYPARSE_PARAM_TYPE\n"); 1655 putl_code(fp, 1656 "# define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)\n"); 1657 putl_code(fp, "# else\n"); 1658 putl_code(fp, "# define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)\n"); 1659 putl_code(fp, "# endif\n"); 1660 putl_code(fp, "#else\n"); 1661 1662 puts_code(fp, "# define YYPARSE_DECL() yyparse("); 1663 puts_param_types(fp, parse_param, 0); 1664 putl_code(fp, ")\n"); 1665 1666 putl_code(fp, "#endif\n"); 1667 } 1668 1669 static void 1670 output_lex_decl(FILE * fp) 1671 { 1672 putc_code(fp, '\n'); 1673 putl_code(fp, "/* Parameters sent to lex. */\n"); 1674 putl_code(fp, "#ifdef YYLEX_PARAM\n"); 1675 if (pure_parser) 1676 { 1677 putl_code(fp, "# ifdef YYLEX_PARAM_TYPE\n"); 1678 #if defined(YYBTYACC) 1679 if (locations) 1680 { 1681 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval," 1682 " YYLTYPE *yylloc," 1683 " YYLEX_PARAM_TYPE YYLEX_PARAM)\n"); 1684 } 1685 else 1686 #endif 1687 { 1688 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval," 1689 " YYLEX_PARAM_TYPE YYLEX_PARAM)\n"); 1690 } 1691 putl_code(fp, "# else\n"); 1692 #if defined(YYBTYACC) 1693 if (locations) 1694 { 1695 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval," 1696 " YYLTYPE *yylloc," 1697 " void * YYLEX_PARAM)\n"); 1698 } 1699 else 1700 #endif 1701 { 1702 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval," 1703 " void * YYLEX_PARAM)\n"); 1704 } 1705 putl_code(fp, "# endif\n"); 1706 #if defined(YYBTYACC) 1707 if (locations) 1708 putl_code(fp, 1709 "# define YYLEX yylex(&yylval, &yylloc, YYLEX_PARAM)\n"); 1710 else 1711 #endif 1712 putl_code(fp, "# define YYLEX yylex(&yylval, YYLEX_PARAM)\n"); 1713 } 1714 else 1715 { 1716 putl_code(fp, "# define YYLEX_DECL() yylex(void *YYLEX_PARAM)\n"); 1717 putl_code(fp, "# define YYLEX yylex(YYLEX_PARAM)\n"); 1718 } 1719 putl_code(fp, "#else\n"); 1720 if (pure_parser && lex_param) 1721 { 1722 #if defined(YYBTYACC) 1723 if (locations) 1724 puts_code(fp, 1725 "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc, "); 1726 else 1727 #endif 1728 puts_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval, "); 1729 puts_param_types(fp, lex_param, 0); 1730 putl_code(fp, ")\n"); 1731 1732 #if defined(YYBTYACC) 1733 if (locations) 1734 puts_code(fp, "# define YYLEX yylex(&yylval, &yylloc, "); 1735 else 1736 #endif 1737 puts_code(fp, "# define YYLEX yylex(&yylval, "); 1738 puts_param_names(fp, lex_param, 0); 1739 putl_code(fp, ")\n"); 1740 } 1741 else if (pure_parser) 1742 { 1743 #if defined(YYBTYACC) 1744 if (locations) 1745 { 1746 putl_code(fp, 1747 "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc)\n"); 1748 putl_code(fp, "# define YYLEX yylex(&yylval, &yylloc)\n"); 1749 } 1750 else 1751 #endif 1752 { 1753 putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval)\n"); 1754 putl_code(fp, "# define YYLEX yylex(&yylval)\n"); 1755 } 1756 } 1757 else if (lex_param) 1758 { 1759 puts_code(fp, "# define YYLEX_DECL() yylex("); 1760 puts_param_types(fp, lex_param, 0); 1761 putl_code(fp, ")\n"); 1762 1763 puts_code(fp, "# define YYLEX yylex("); 1764 puts_param_names(fp, lex_param, 0); 1765 putl_code(fp, ")\n"); 1766 } 1767 else 1768 { 1769 putl_code(fp, "# define YYLEX_DECL() yylex(void)\n"); 1770 putl_code(fp, "# define YYLEX yylex()\n"); 1771 } 1772 putl_code(fp, "#endif\n"); 1773 } 1774 1775 static void 1776 output_error_decl(FILE * fp) 1777 { 1778 putc_code(fp, '\n'); 1779 putl_code(fp, "/* Parameters sent to yyerror. */\n"); 1780 putl_code(fp, "#ifndef YYERROR_DECL\n"); 1781 puts_code(fp, "#define YYERROR_DECL() yyerror("); 1782 #if defined(YYBTYACC) 1783 if (locations) 1784 puts_code(fp, "YYLTYPE loc, "); 1785 #endif 1786 puts_param_types(fp, parse_param, 1); 1787 putl_code(fp, "const char *s)\n"); 1788 putl_code(fp, "#endif\n"); 1789 1790 putl_code(fp, "#ifndef YYERROR_CALL\n"); 1791 1792 puts_code(fp, "#define YYERROR_CALL(msg) yyerror("); 1793 #if defined(YYBTYACC) 1794 if (locations) 1795 puts_code(fp, "yylloc, "); 1796 #endif 1797 puts_param_names(fp, parse_param, 1); 1798 putl_code(fp, "msg)\n"); 1799 1800 putl_code(fp, "#endif\n"); 1801 } 1802 1803 #if defined(YYBTYACC) 1804 static void 1805 output_yydestruct_decl(FILE * fp) 1806 { 1807 putc_code(fp, '\n'); 1808 putl_code(fp, "#ifndef YYDESTRUCT_DECL\n"); 1809 1810 puts_code(fp, 1811 "#define YYDESTRUCT_DECL() " 1812 "yydestruct(const char *msg, int psymb, YYSTYPE *val"); 1813 #if defined(YYBTYACC) 1814 if (locations) 1815 puts_code(fp, ", YYLTYPE *loc"); 1816 #endif 1817 if (parse_param) 1818 { 1819 puts_code(fp, ", "); 1820 puts_param_types(fp, parse_param, 0); 1821 } 1822 putl_code(fp, ")\n"); 1823 1824 putl_code(fp, "#endif\n"); 1825 1826 putl_code(fp, "#ifndef YYDESTRUCT_CALL\n"); 1827 1828 puts_code(fp, "#define YYDESTRUCT_CALL(msg, psymb, val"); 1829 #if defined(YYBTYACC) 1830 if (locations) 1831 puts_code(fp, ", loc"); 1832 #endif 1833 puts_code(fp, ") yydestruct(msg, psymb, val"); 1834 #if defined(YYBTYACC) 1835 if (locations) 1836 puts_code(fp, ", loc"); 1837 #endif 1838 if (parse_param) 1839 { 1840 puts_code(fp, ", "); 1841 puts_param_names(fp, parse_param, 0); 1842 } 1843 putl_code(fp, ")\n"); 1844 1845 putl_code(fp, "#endif\n"); 1846 } 1847 1848 static void 1849 output_yydestruct_impl(void) 1850 { 1851 int i; 1852 char *s, *destructor_code; 1853 1854 putc_code(code_file, '\n'); 1855 putl_code(code_file, "/* Release memory associated with symbol. */\n"); 1856 putl_code(code_file, "#if ! defined YYDESTRUCT_IS_DECLARED\n"); 1857 putl_code(code_file, "static void\n"); 1858 putl_code(code_file, "YYDESTRUCT_DECL()\n"); 1859 putl_code(code_file, "{\n"); 1860 putl_code(code_file, " switch (psymb)\n"); 1861 putl_code(code_file, " {\n"); 1862 for (i = 2; i < nsyms; ++i) 1863 { 1864 if ((destructor_code = symbol_destructor[i]) != NULL) 1865 { 1866 ++outline; 1867 fprintf(code_file, "\tcase %d:\n", symbol_pval[i]); 1868 /* comprehend the number of lines in the destructor code */ 1869 for (s = destructor_code; (s = strchr(s, '\n')) != NULL; s++) 1870 ++outline; 1871 puts_code(code_file, destructor_code); 1872 putc_code(code_file, '\n'); 1873 putl_code(code_file, "\tbreak;\n"); 1874 write_code_lineno(code_file); 1875 FREE(destructor_code); 1876 } 1877 } 1878 putl_code(code_file, " }\n"); 1879 putl_code(code_file, "}\n"); 1880 putl_code(code_file, "#define YYDESTRUCT_IS_DECLARED 1\n"); 1881 putl_code(code_file, "#endif\n"); 1882 1883 DO_FREE(symbol_destructor); 1884 } 1885 #endif 1886 1887 static void 1888 free_itemsets(void) 1889 { 1890 core *cp, *next; 1891 1892 FREE(state_table); 1893 for (cp = first_state; cp; cp = next) 1894 { 1895 next = cp->next; 1896 FREE(cp); 1897 } 1898 } 1899 1900 static void 1901 free_shifts(void) 1902 { 1903 shifts *sp, *next; 1904 1905 FREE(shift_table); 1906 for (sp = first_shift; sp; sp = next) 1907 { 1908 next = sp->next; 1909 FREE(sp); 1910 } 1911 } 1912 1913 static void 1914 free_reductions(void) 1915 { 1916 reductions *rp, *next; 1917 1918 FREE(reduction_table); 1919 for (rp = first_reduction; rp; rp = next) 1920 { 1921 next = rp->next; 1922 FREE(rp); 1923 } 1924 } 1925 1926 static void 1927 output_externs(FILE * fp, const char *const section[]) 1928 { 1929 int i; 1930 const char *s; 1931 1932 for (i = 0; (s = section[i]) != 0; ++i) 1933 { 1934 /* prefix non-blank lines that don't start with 1935 C pre-processor directives with 'extern ' */ 1936 if (*s && (*s != '#')) 1937 fputs("extern\t", fp); 1938 if (fp == code_file) 1939 ++outline; 1940 fprintf(fp, "%s\n", s); 1941 } 1942 } 1943 1944 void 1945 output(void) 1946 { 1947 FILE *fp; 1948 1949 free_itemsets(); 1950 free_shifts(); 1951 free_reductions(); 1952 1953 #if defined(YYBTYACC) 1954 output_backtracking_parser(output_file); 1955 if (rflag) 1956 output_backtracking_parser(code_file); 1957 #endif 1958 1959 if (iflag) 1960 { 1961 write_code_lineno(code_file); 1962 ++outline; 1963 fprintf(code_file, "#include \"%s\"\n", externs_file_name); 1964 fp = externs_file; 1965 } 1966 else 1967 fp = code_file; 1968 1969 output_prefix(fp); 1970 output_pure_parser(fp); 1971 output_stored_text(fp); 1972 output_stype(fp); 1973 #if defined(YYBTYACC) 1974 if (locations) 1975 output_ltype(fp); 1976 #endif 1977 output_parse_decl(fp); 1978 output_lex_decl(fp); 1979 output_error_decl(fp); 1980 #if defined(YYBTYACC) 1981 if (destructor) 1982 output_yydestruct_decl(fp); 1983 #endif 1984 if (iflag || !rflag) 1985 { 1986 write_section(fp, xdecls); 1987 } 1988 1989 if (iflag) 1990 { 1991 output_externs(externs_file, global_vars); 1992 if (!pure_parser) 1993 output_externs(externs_file, impure_vars); 1994 } 1995 1996 if (iflag) 1997 { 1998 if (dflag) 1999 { 2000 ++outline; 2001 fprintf(code_file, "#include \"%s\"\n", defines_file_name); 2002 } 2003 else 2004 output_defines(externs_file); 2005 } 2006 else 2007 { 2008 putc_code(code_file, '\n'); 2009 output_defines(code_file); 2010 } 2011 2012 if (dflag) 2013 { 2014 start_defines_file(); 2015 output_defines(defines_file); 2016 end_defines_file(); 2017 } 2018 2019 output_rule_data(); 2020 output_yydefred(); 2021 #if defined(YYBTYACC) 2022 output_accessing_symbols(); 2023 #endif 2024 output_actions(); 2025 free_parser(); 2026 output_debug(); 2027 if (rflag) 2028 { 2029 write_section(code_file, xdecls); 2030 output_YYINT_typedef(code_file); 2031 write_section(code_file, tables); 2032 } 2033 write_section(code_file, global_vars); 2034 if (!pure_parser) 2035 { 2036 write_section(code_file, impure_vars); 2037 } 2038 write_section(code_file, hdr_defs); 2039 if (!pure_parser) 2040 { 2041 write_section(code_file, hdr_vars); 2042 } 2043 output_trailing_text(); 2044 #if defined(YYBTYACC) 2045 if (destructor) 2046 output_yydestruct_impl(); 2047 #endif 2048 write_section(code_file, body_1); 2049 if (pure_parser) 2050 { 2051 write_section(code_file, body_vars); 2052 } 2053 write_section(code_file, body_2); 2054 output_semantic_actions(); 2055 write_section(code_file, trailer); 2056 } 2057 2058 #ifdef NO_LEAKS 2059 void 2060 output_leaks(void) 2061 { 2062 DO_FREE(tally); 2063 DO_FREE(width); 2064 DO_FREE(order); 2065 } 2066 #endif 2067