Annotation of src/usr.bin/yacc/output.c, Revision 1.1
1.1 ! deraadt 1: #ifndef lint
! 2: static char rcsid[] = "$Id: output.c,v 1.3 1993/08/02 17:56:43 mycroft Exp $";
! 3: #endif /* not lint */
! 4:
! 5: #include "defs.h"
! 6:
! 7: static int nvectors;
! 8: static int nentries;
! 9: static short **froms;
! 10: static short **tos;
! 11: static short *tally;
! 12: static short *width;
! 13: static short *state_count;
! 14: static short *order;
! 15: static short *base;
! 16: static short *pos;
! 17: static int maxtable;
! 18: static short *table;
! 19: static short *check;
! 20: static int lowzero;
! 21: static int high;
! 22:
! 23:
! 24: output()
! 25: {
! 26: free_itemsets();
! 27: free_shifts();
! 28: free_reductions();
! 29: output_prefix();
! 30: output_stored_text();
! 31: output_defines();
! 32: output_rule_data();
! 33: output_yydefred();
! 34: output_actions();
! 35: free_parser();
! 36: output_debug();
! 37: output_stype();
! 38: if (rflag) write_section(tables);
! 39: write_section(header);
! 40: output_trailing_text();
! 41: write_section(body);
! 42: output_semantic_actions();
! 43: write_section(trailer);
! 44: }
! 45:
! 46:
! 47: output_prefix()
! 48: {
! 49: if (symbol_prefix == NULL)
! 50: symbol_prefix = "yy";
! 51: else
! 52: {
! 53: ++outline;
! 54: fprintf(code_file, "#define yyparse %sparse\n", symbol_prefix);
! 55: ++outline;
! 56: fprintf(code_file, "#define yylex %slex\n", symbol_prefix);
! 57: ++outline;
! 58: fprintf(code_file, "#define yyerror %serror\n", symbol_prefix);
! 59: ++outline;
! 60: fprintf(code_file, "#define yychar %schar\n", symbol_prefix);
! 61: ++outline;
! 62: fprintf(code_file, "#define yyval %sval\n", symbol_prefix);
! 63: ++outline;
! 64: fprintf(code_file, "#define yylval %slval\n", symbol_prefix);
! 65: ++outline;
! 66: fprintf(code_file, "#define yydebug %sdebug\n", symbol_prefix);
! 67: ++outline;
! 68: fprintf(code_file, "#define yynerrs %snerrs\n", symbol_prefix);
! 69: ++outline;
! 70: fprintf(code_file, "#define yyerrflag %serrflag\n", symbol_prefix);
! 71: ++outline;
! 72: fprintf(code_file, "#define yyss %sss\n", symbol_prefix);
! 73: ++outline;
! 74: fprintf(code_file, "#define yyssp %sssp\n", symbol_prefix);
! 75: ++outline;
! 76: fprintf(code_file, "#define yyvs %svs\n", symbol_prefix);
! 77: ++outline;
! 78: fprintf(code_file, "#define yyvsp %svsp\n", symbol_prefix);
! 79: ++outline;
! 80: fprintf(code_file, "#define yylhs %slhs\n", symbol_prefix);
! 81: ++outline;
! 82: fprintf(code_file, "#define yylen %slen\n", symbol_prefix);
! 83: ++outline;
! 84: fprintf(code_file, "#define yydefred %sdefred\n", symbol_prefix);
! 85: ++outline;
! 86: fprintf(code_file, "#define yydgoto %sdgoto\n", symbol_prefix);
! 87: ++outline;
! 88: fprintf(code_file, "#define yysindex %ssindex\n", symbol_prefix);
! 89: ++outline;
! 90: fprintf(code_file, "#define yyrindex %srindex\n", symbol_prefix);
! 91: ++outline;
! 92: fprintf(code_file, "#define yygindex %sgindex\n", symbol_prefix);
! 93: ++outline;
! 94: fprintf(code_file, "#define yytable %stable\n", symbol_prefix);
! 95: ++outline;
! 96: fprintf(code_file, "#define yycheck %scheck\n", symbol_prefix);
! 97: ++outline;
! 98: fprintf(code_file, "#define yyname %sname\n", symbol_prefix);
! 99: ++outline;
! 100: fprintf(code_file, "#define yyrule %srule\n", symbol_prefix);
! 101: }
! 102: ++outline;
! 103: fprintf(code_file, "#define YYPREFIX \"%s\"\n", symbol_prefix);
! 104: }
! 105:
! 106:
! 107: output_rule_data()
! 108: {
! 109: register int i;
! 110: register int j;
! 111:
! 112:
! 113: fprintf(output_file, "short %slhs[] = {%42d,", symbol_prefix,
! 114: symbol_value[start_symbol]);
! 115:
! 116: j = 10;
! 117: for (i = 3; i < nrules; i++)
! 118: {
! 119: if (j >= 10)
! 120: {
! 121: if (!rflag) ++outline;
! 122: putc('\n', output_file);
! 123: j = 1;
! 124: }
! 125: else
! 126: ++j;
! 127:
! 128: fprintf(output_file, "%5d,", symbol_value[rlhs[i]]);
! 129: }
! 130: if (!rflag) outline += 2;
! 131: fprintf(output_file, "\n};\n");
! 132:
! 133: fprintf(output_file, "short %slen[] = {%42d,", symbol_prefix, 2);
! 134:
! 135: j = 10;
! 136: for (i = 3; i < nrules; i++)
! 137: {
! 138: if (j >= 10)
! 139: {
! 140: if (!rflag) ++outline;
! 141: putc('\n', output_file);
! 142: j = 1;
! 143: }
! 144: else
! 145: j++;
! 146:
! 147: fprintf(output_file, "%5d,", rrhs[i + 1] - rrhs[i] - 1);
! 148: }
! 149: if (!rflag) outline += 2;
! 150: fprintf(output_file, "\n};\n");
! 151: }
! 152:
! 153:
! 154: output_yydefred()
! 155: {
! 156: register int i, j;
! 157:
! 158: fprintf(output_file, "short %sdefred[] = {%39d,", symbol_prefix,
! 159: (defred[0] ? defred[0] - 2 : 0));
! 160:
! 161: j = 10;
! 162: for (i = 1; i < nstates; i++)
! 163: {
! 164: if (j < 10)
! 165: ++j;
! 166: else
! 167: {
! 168: if (!rflag) ++outline;
! 169: putc('\n', output_file);
! 170: j = 1;
! 171: }
! 172:
! 173: fprintf(output_file, "%5d,", (defred[i] ? defred[i] - 2 : 0));
! 174: }
! 175:
! 176: if (!rflag) outline += 2;
! 177: fprintf(output_file, "\n};\n");
! 178: }
! 179:
! 180:
! 181: output_actions()
! 182: {
! 183: nvectors = 2*nstates + nvars;
! 184:
! 185: froms = NEW2(nvectors, short *);
! 186: tos = NEW2(nvectors, short *);
! 187: tally = NEW2(nvectors, short);
! 188: width = NEW2(nvectors, short);
! 189:
! 190: token_actions();
! 191: FREE(lookaheads);
! 192: FREE(LA);
! 193: FREE(LAruleno);
! 194: FREE(accessing_symbol);
! 195:
! 196: goto_actions();
! 197: FREE(goto_map + ntokens);
! 198: FREE(from_state);
! 199: FREE(to_state);
! 200:
! 201: sort_actions();
! 202: pack_table();
! 203: output_base();
! 204: output_table();
! 205: output_check();
! 206: }
! 207:
! 208:
! 209: token_actions()
! 210: {
! 211: register int i, j;
! 212: register int shiftcount, reducecount;
! 213: register int max, min;
! 214: register short *actionrow, *r, *s;
! 215: register action *p;
! 216:
! 217: actionrow = NEW2(2*ntokens, short);
! 218: for (i = 0; i < nstates; ++i)
! 219: {
! 220: if (parser[i])
! 221: {
! 222: for (j = 0; j < 2*ntokens; ++j)
! 223: actionrow[j] = 0;
! 224:
! 225: shiftcount = 0;
! 226: reducecount = 0;
! 227: for (p = parser[i]; p; p = p->next)
! 228: {
! 229: if (p->suppressed == 0)
! 230: {
! 231: if (p->action_code == SHIFT)
! 232: {
! 233: ++shiftcount;
! 234: actionrow[p->symbol] = p->number;
! 235: }
! 236: else if (p->action_code == REDUCE && p->number != defred[i])
! 237: {
! 238: ++reducecount;
! 239: actionrow[p->symbol + ntokens] = p->number;
! 240: }
! 241: }
! 242: }
! 243:
! 244: tally[i] = shiftcount;
! 245: tally[nstates+i] = reducecount;
! 246: width[i] = 0;
! 247: width[nstates+i] = 0;
! 248: if (shiftcount > 0)
! 249: {
! 250: froms[i] = r = NEW2(shiftcount, short);
! 251: tos[i] = s = NEW2(shiftcount, short);
! 252: min = MAXSHORT;
! 253: max = 0;
! 254: for (j = 0; j < ntokens; ++j)
! 255: {
! 256: if (actionrow[j])
! 257: {
! 258: if (min > symbol_value[j])
! 259: min = symbol_value[j];
! 260: if (max < symbol_value[j])
! 261: max = symbol_value[j];
! 262: *r++ = symbol_value[j];
! 263: *s++ = actionrow[j];
! 264: }
! 265: }
! 266: width[i] = max - min + 1;
! 267: }
! 268: if (reducecount > 0)
! 269: {
! 270: froms[nstates+i] = r = NEW2(reducecount, short);
! 271: tos[nstates+i] = s = NEW2(reducecount, short);
! 272: min = MAXSHORT;
! 273: max = 0;
! 274: for (j = 0; j < ntokens; ++j)
! 275: {
! 276: if (actionrow[ntokens+j])
! 277: {
! 278: if (min > symbol_value[j])
! 279: min = symbol_value[j];
! 280: if (max < symbol_value[j])
! 281: max = symbol_value[j];
! 282: *r++ = symbol_value[j];
! 283: *s++ = actionrow[ntokens+j] - 2;
! 284: }
! 285: }
! 286: width[nstates+i] = max - min + 1;
! 287: }
! 288: }
! 289: }
! 290: FREE(actionrow);
! 291: }
! 292:
! 293: goto_actions()
! 294: {
! 295: register int i, j, k;
! 296:
! 297: state_count = NEW2(nstates, short);
! 298:
! 299: k = default_goto(start_symbol + 1);
! 300: fprintf(output_file, "short %sdgoto[] = {%40d,", symbol_prefix, k);
! 301: save_column(start_symbol + 1, k);
! 302:
! 303: j = 10;
! 304: for (i = start_symbol + 2; i < nsyms; i++)
! 305: {
! 306: if (j >= 10)
! 307: {
! 308: if (!rflag) ++outline;
! 309: putc('\n', output_file);
! 310: j = 1;
! 311: }
! 312: else
! 313: ++j;
! 314:
! 315: k = default_goto(i);
! 316: fprintf(output_file, "%5d,", k);
! 317: save_column(i, k);
! 318: }
! 319:
! 320: if (!rflag) outline += 2;
! 321: fprintf(output_file, "\n};\n");
! 322: FREE(state_count);
! 323: }
! 324:
! 325: int
! 326: default_goto(symbol)
! 327: int symbol;
! 328: {
! 329: register int i;
! 330: register int m;
! 331: register int n;
! 332: register int default_state;
! 333: register int max;
! 334:
! 335: m = goto_map[symbol];
! 336: n = goto_map[symbol + 1];
! 337:
! 338: if (m == n) return (0);
! 339:
! 340: for (i = 0; i < nstates; i++)
! 341: state_count[i] = 0;
! 342:
! 343: for (i = m; i < n; i++)
! 344: state_count[to_state[i]]++;
! 345:
! 346: max = 0;
! 347: default_state = 0;
! 348: for (i = 0; i < nstates; i++)
! 349: {
! 350: if (state_count[i] > max)
! 351: {
! 352: max = state_count[i];
! 353: default_state = i;
! 354: }
! 355: }
! 356:
! 357: return (default_state);
! 358: }
! 359:
! 360:
! 361:
! 362: save_column(symbol, default_state)
! 363: int symbol;
! 364: int default_state;
! 365: {
! 366: register int i;
! 367: register int m;
! 368: register int n;
! 369: register short *sp;
! 370: register short *sp1;
! 371: register short *sp2;
! 372: register int count;
! 373: register int symno;
! 374:
! 375: m = goto_map[symbol];
! 376: n = goto_map[symbol + 1];
! 377:
! 378: count = 0;
! 379: for (i = m; i < n; i++)
! 380: {
! 381: if (to_state[i] != default_state)
! 382: ++count;
! 383: }
! 384: if (count == 0) return;
! 385:
! 386: symno = symbol_value[symbol] + 2*nstates;
! 387:
! 388: froms[symno] = sp1 = sp = NEW2(count, short);
! 389: tos[symno] = sp2 = NEW2(count, short);
! 390:
! 391: for (i = m; i < n; i++)
! 392: {
! 393: if (to_state[i] != default_state)
! 394: {
! 395: *sp1++ = from_state[i];
! 396: *sp2++ = to_state[i];
! 397: }
! 398: }
! 399:
! 400: tally[symno] = count;
! 401: width[symno] = sp1[-1] - sp[0] + 1;
! 402: }
! 403:
! 404: sort_actions()
! 405: {
! 406: register int i;
! 407: register int j;
! 408: register int k;
! 409: register int t;
! 410: register int w;
! 411:
! 412: order = NEW2(nvectors, short);
! 413: nentries = 0;
! 414:
! 415: for (i = 0; i < nvectors; i++)
! 416: {
! 417: if (tally[i] > 0)
! 418: {
! 419: t = tally[i];
! 420: w = width[i];
! 421: j = nentries - 1;
! 422:
! 423: while (j >= 0 && (width[order[j]] < w))
! 424: j--;
! 425:
! 426: while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
! 427: j--;
! 428:
! 429: for (k = nentries - 1; k > j; k--)
! 430: order[k + 1] = order[k];
! 431:
! 432: order[j + 1] = i;
! 433: nentries++;
! 434: }
! 435: }
! 436: }
! 437:
! 438:
! 439: pack_table()
! 440: {
! 441: register int i;
! 442: register int place;
! 443: register int state;
! 444:
! 445: base = NEW2(nvectors, short);
! 446: pos = NEW2(nentries, short);
! 447:
! 448: maxtable = 1000;
! 449: table = NEW2(maxtable, short);
! 450: check = NEW2(maxtable, short);
! 451:
! 452: lowzero = 0;
! 453: high = 0;
! 454:
! 455: for (i = 0; i < maxtable; i++)
! 456: check[i] = -1;
! 457:
! 458: for (i = 0; i < nentries; i++)
! 459: {
! 460: state = matching_vector(i);
! 461:
! 462: if (state < 0)
! 463: place = pack_vector(i);
! 464: else
! 465: place = base[state];
! 466:
! 467: pos[i] = place;
! 468: base[order[i]] = place;
! 469: }
! 470:
! 471: for (i = 0; i < nvectors; i++)
! 472: {
! 473: if (froms[i])
! 474: FREE(froms[i]);
! 475: if (tos[i])
! 476: FREE(tos[i]);
! 477: }
! 478:
! 479: FREE(froms);
! 480: FREE(tos);
! 481: FREE(pos);
! 482: }
! 483:
! 484:
! 485: /* The function matching_vector determines if the vector specified by */
! 486: /* the input parameter matches a previously considered vector. The */
! 487: /* test at the start of the function checks if the vector represents */
! 488: /* a row of shifts over terminal symbols or a row of reductions, or a */
! 489: /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
! 490: /* check if a column of shifts over a nonterminal symbols matches a */
! 491: /* previously considered vector. Because of the nature of LR parsing */
! 492: /* tables, no two columns can match. Therefore, the only possible */
! 493: /* match would be between a row and a column. Such matches are */
! 494: /* unlikely. Therefore, to save time, no attempt is made to see if a */
! 495: /* column matches a previously considered vector. */
! 496: /* */
! 497: /* Matching_vector is poorly designed. The test could easily be made */
! 498: /* faster. Also, it depends on the vectors being in a specific */
! 499: /* order. */
! 500:
! 501: int
! 502: matching_vector(vector)
! 503: int vector;
! 504: {
! 505: register int i;
! 506: register int j;
! 507: register int k;
! 508: register int t;
! 509: register int w;
! 510: register int match;
! 511: register int prev;
! 512:
! 513: i = order[vector];
! 514: if (i >= 2*nstates)
! 515: return (-1);
! 516:
! 517: t = tally[i];
! 518: w = width[i];
! 519:
! 520: for (prev = vector - 1; prev >= 0; prev--)
! 521: {
! 522: j = order[prev];
! 523: if (width[j] != w || tally[j] != t)
! 524: return (-1);
! 525:
! 526: match = 1;
! 527: for (k = 0; match && k < t; k++)
! 528: {
! 529: if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
! 530: match = 0;
! 531: }
! 532:
! 533: if (match)
! 534: return (j);
! 535: }
! 536:
! 537: return (-1);
! 538: }
! 539:
! 540:
! 541:
! 542: int
! 543: pack_vector(vector)
! 544: int vector;
! 545: {
! 546: register int i, j, k, l;
! 547: register int t;
! 548: register int loc;
! 549: register int ok;
! 550: register short *from;
! 551: register short *to;
! 552: int newmax;
! 553:
! 554: i = order[vector];
! 555: t = tally[i];
! 556: assert(t);
! 557:
! 558: from = froms[i];
! 559: to = tos[i];
! 560:
! 561: j = lowzero - from[0];
! 562: for (k = 1; k < t; ++k)
! 563: if (lowzero - from[k] > j)
! 564: j = lowzero - from[k];
! 565: for (;; ++j)
! 566: {
! 567: if (j == 0)
! 568: continue;
! 569: ok = 1;
! 570: for (k = 0; ok && k < t; k++)
! 571: {
! 572: loc = j + from[k];
! 573: if (loc >= maxtable)
! 574: {
! 575: if (loc >= MAXTABLE)
! 576: fatal("maximum table size exceeded");
! 577:
! 578: newmax = maxtable;
! 579: do { newmax += 200; } while (newmax <= loc);
! 580: table = (short *) REALLOC(table, newmax*sizeof(short));
! 581: if (table == 0) no_space();
! 582: check = (short *) REALLOC(check, newmax*sizeof(short));
! 583: if (check == 0) no_space();
! 584: for (l = maxtable; l < newmax; ++l)
! 585: {
! 586: table[l] = 0;
! 587: check[l] = -1;
! 588: }
! 589: maxtable = newmax;
! 590: }
! 591:
! 592: if (check[loc] != -1)
! 593: ok = 0;
! 594: }
! 595: for (k = 0; ok && k < vector; k++)
! 596: {
! 597: if (pos[k] == j)
! 598: ok = 0;
! 599: }
! 600: if (ok)
! 601: {
! 602: for (k = 0; k < t; k++)
! 603: {
! 604: loc = j + from[k];
! 605: table[loc] = to[k];
! 606: check[loc] = from[k];
! 607: if (loc > high) high = loc;
! 608: }
! 609:
! 610: while (check[lowzero] != -1)
! 611: ++lowzero;
! 612:
! 613: return (j);
! 614: }
! 615: }
! 616: }
! 617:
! 618:
! 619:
! 620: output_base()
! 621: {
! 622: register int i, j;
! 623:
! 624: fprintf(output_file, "short %ssindex[] = {%39d,", symbol_prefix, base[0]);
! 625:
! 626: j = 10;
! 627: for (i = 1; i < nstates; i++)
! 628: {
! 629: if (j >= 10)
! 630: {
! 631: if (!rflag) ++outline;
! 632: putc('\n', output_file);
! 633: j = 1;
! 634: }
! 635: else
! 636: ++j;
! 637:
! 638: fprintf(output_file, "%5d,", base[i]);
! 639: }
! 640:
! 641: if (!rflag) outline += 2;
! 642: fprintf(output_file, "\n};\nshort %srindex[] = {%39d,", symbol_prefix,
! 643: base[nstates]);
! 644:
! 645: j = 10;
! 646: for (i = nstates + 1; i < 2*nstates; i++)
! 647: {
! 648: if (j >= 10)
! 649: {
! 650: if (!rflag) ++outline;
! 651: putc('\n', output_file);
! 652: j = 1;
! 653: }
! 654: else
! 655: ++j;
! 656:
! 657: fprintf(output_file, "%5d,", base[i]);
! 658: }
! 659:
! 660: if (!rflag) outline += 2;
! 661: fprintf(output_file, "\n};\nshort %sgindex[] = {%39d,", symbol_prefix,
! 662: base[2*nstates]);
! 663:
! 664: j = 10;
! 665: for (i = 2*nstates + 1; i < nvectors - 1; i++)
! 666: {
! 667: if (j >= 10)
! 668: {
! 669: if (!rflag) ++outline;
! 670: putc('\n', output_file);
! 671: j = 1;
! 672: }
! 673: else
! 674: ++j;
! 675:
! 676: fprintf(output_file, "%5d,", base[i]);
! 677: }
! 678:
! 679: if (!rflag) outline += 2;
! 680: fprintf(output_file, "\n};\n");
! 681: FREE(base);
! 682: }
! 683:
! 684:
! 685:
! 686: output_table()
! 687: {
! 688: register int i;
! 689: register int j;
! 690:
! 691: ++outline;
! 692: fprintf(code_file, "#define YYTABLESIZE %d\n", high);
! 693: fprintf(output_file, "short %stable[] = {%40d,", symbol_prefix,
! 694: table[0]);
! 695:
! 696: j = 10;
! 697: for (i = 1; i <= high; i++)
! 698: {
! 699: if (j >= 10)
! 700: {
! 701: if (!rflag) ++outline;
! 702: putc('\n', output_file);
! 703: j = 1;
! 704: }
! 705: else
! 706: ++j;
! 707:
! 708: fprintf(output_file, "%5d,", table[i]);
! 709: }
! 710:
! 711: if (!rflag) outline += 2;
! 712: fprintf(output_file, "\n};\n");
! 713: FREE(table);
! 714: }
! 715:
! 716:
! 717:
! 718: output_check()
! 719: {
! 720: register int i;
! 721: register int j;
! 722:
! 723: fprintf(output_file, "short %scheck[] = {%40d,", symbol_prefix,
! 724: check[0]);
! 725:
! 726: j = 10;
! 727: for (i = 1; i <= high; i++)
! 728: {
! 729: if (j >= 10)
! 730: {
! 731: if (!rflag) ++outline;
! 732: putc('\n', output_file);
! 733: j = 1;
! 734: }
! 735: else
! 736: ++j;
! 737:
! 738: fprintf(output_file, "%5d,", check[i]);
! 739: }
! 740:
! 741: if (!rflag) outline += 2;
! 742: fprintf(output_file, "\n};\n");
! 743: FREE(check);
! 744: }
! 745:
! 746:
! 747: int
! 748: is_C_identifier(name)
! 749: char *name;
! 750: {
! 751: register char *s;
! 752: register int c;
! 753:
! 754: s = name;
! 755: c = *s;
! 756: if (c == '"')
! 757: {
! 758: c = *++s;
! 759: if (!isalpha(c) && c != '_' && c != '$')
! 760: return (0);
! 761: while ((c = *++s) != '"')
! 762: {
! 763: if (!isalnum(c) && c != '_' && c != '$')
! 764: return (0);
! 765: }
! 766: return (1);
! 767: }
! 768:
! 769: if (!isalpha(c) && c != '_' && c != '$')
! 770: return (0);
! 771: while (c = *++s)
! 772: {
! 773: if (!isalnum(c) && c != '_' && c != '$')
! 774: return (0);
! 775: }
! 776: return (1);
! 777: }
! 778:
! 779:
! 780: output_defines()
! 781: {
! 782: register int c, i;
! 783: register char *s;
! 784:
! 785: for (i = 2; i < ntokens; ++i)
! 786: {
! 787: s = symbol_name[i];
! 788: if (is_C_identifier(s))
! 789: {
! 790: fprintf(code_file, "#define ");
! 791: if (dflag) fprintf(defines_file, "#define ");
! 792: c = *s;
! 793: if (c == '"')
! 794: {
! 795: while ((c = *++s) != '"')
! 796: {
! 797: putc(c, code_file);
! 798: if (dflag) putc(c, defines_file);
! 799: }
! 800: }
! 801: else
! 802: {
! 803: do
! 804: {
! 805: putc(c, code_file);
! 806: if (dflag) putc(c, defines_file);
! 807: }
! 808: while (c = *++s);
! 809: }
! 810: ++outline;
! 811: fprintf(code_file, " %d\n", symbol_value[i]);
! 812: if (dflag) fprintf(defines_file, " %d\n", symbol_value[i]);
! 813: }
! 814: }
! 815:
! 816: ++outline;
! 817: fprintf(code_file, "#define YYERRCODE %d\n", symbol_value[1]);
! 818:
! 819: if (dflag && unionized)
! 820: {
! 821: fclose(union_file);
! 822: union_file = fopen(union_file_name, "r");
! 823: if (union_file == NULL) open_error(union_file_name);
! 824: while ((c = getc(union_file)) != EOF)
! 825: putc(c, defines_file);
! 826: fprintf(defines_file, " YYSTYPE;\nextern YYSTYPE %slval;\n",
! 827: symbol_prefix);
! 828: }
! 829: }
! 830:
! 831:
! 832: output_stored_text()
! 833: {
! 834: register int c;
! 835: register FILE *in, *out;
! 836:
! 837: fclose(text_file);
! 838: text_file = fopen(text_file_name, "r");
! 839: if (text_file == NULL)
! 840: open_error(text_file_name);
! 841: in = text_file;
! 842: if ((c = getc(in)) == EOF)
! 843: return;
! 844: out = code_file;
! 845: if (c == '\n')
! 846: ++outline;
! 847: putc(c, out);
! 848: while ((c = getc(in)) != EOF)
! 849: {
! 850: if (c == '\n')
! 851: ++outline;
! 852: putc(c, out);
! 853: }
! 854: if (!lflag)
! 855: fprintf(out, line_format, ++outline + 1, code_file_name);
! 856: }
! 857:
! 858:
! 859: output_debug()
! 860: {
! 861: register int i, j, k, max;
! 862: char **symnam, *s;
! 863:
! 864: ++outline;
! 865: fprintf(code_file, "#define YYFINAL %d\n", final_state);
! 866: outline += 3;
! 867: fprintf(code_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
! 868: tflag);
! 869: if (rflag)
! 870: fprintf(output_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
! 871: tflag);
! 872:
! 873: max = 0;
! 874: for (i = 2; i < ntokens; ++i)
! 875: if (symbol_value[i] > max)
! 876: max = symbol_value[i];
! 877: ++outline;
! 878: fprintf(code_file, "#define YYMAXTOKEN %d\n", max);
! 879:
! 880: symnam = (char **) MALLOC((max+1)*sizeof(char *));
! 881: if (symnam == 0) no_space();
! 882:
! 883: /* Note that it is not necessary to initialize the element */
! 884: /* symnam[max]. */
! 885: for (i = 0; i < max; ++i)
! 886: symnam[i] = 0;
! 887: for (i = ntokens - 1; i >= 2; --i)
! 888: symnam[symbol_value[i]] = symbol_name[i];
! 889: symnam[0] = "end-of-file";
! 890:
! 891: if (!rflag) ++outline;
! 892: fprintf(output_file, "#if YYDEBUG\nchar *%sname[] = {", symbol_prefix);
! 893: j = 80;
! 894: for (i = 0; i <= max; ++i)
! 895: {
! 896: if (s = symnam[i])
! 897: {
! 898: if (s[0] == '"')
! 899: {
! 900: k = 7;
! 901: while (*++s != '"')
! 902: {
! 903: ++k;
! 904: if (*s == '\\')
! 905: {
! 906: k += 2;
! 907: if (*++s == '\\')
! 908: ++k;
! 909: }
! 910: }
! 911: j += k;
! 912: if (j > 80)
! 913: {
! 914: if (!rflag) ++outline;
! 915: putc('\n', output_file);
! 916: j = k;
! 917: }
! 918: fprintf(output_file, "\"\\\"");
! 919: s = symnam[i];
! 920: while (*++s != '"')
! 921: {
! 922: if (*s == '\\')
! 923: {
! 924: fprintf(output_file, "\\\\");
! 925: if (*++s == '\\')
! 926: fprintf(output_file, "\\\\");
! 927: else
! 928: putc(*s, output_file);
! 929: }
! 930: else
! 931: putc(*s, output_file);
! 932: }
! 933: fprintf(output_file, "\\\"\",");
! 934: }
! 935: else if (s[0] == '\'')
! 936: {
! 937: if (s[1] == '"')
! 938: {
! 939: j += 7;
! 940: if (j > 80)
! 941: {
! 942: if (!rflag) ++outline;
! 943: putc('\n', output_file);
! 944: j = 7;
! 945: }
! 946: fprintf(output_file, "\"'\\\"'\",");
! 947: }
! 948: else
! 949: {
! 950: k = 5;
! 951: while (*++s != '\'')
! 952: {
! 953: ++k;
! 954: if (*s == '\\')
! 955: {
! 956: k += 2;
! 957: if (*++s == '\\')
! 958: ++k;
! 959: }
! 960: }
! 961: j += k;
! 962: if (j > 80)
! 963: {
! 964: if (!rflag) ++outline;
! 965: putc('\n', output_file);
! 966: j = k;
! 967: }
! 968: fprintf(output_file, "\"'");
! 969: s = symnam[i];
! 970: while (*++s != '\'')
! 971: {
! 972: if (*s == '\\')
! 973: {
! 974: fprintf(output_file, "\\\\");
! 975: if (*++s == '\\')
! 976: fprintf(output_file, "\\\\");
! 977: else
! 978: putc(*s, output_file);
! 979: }
! 980: else
! 981: putc(*s, output_file);
! 982: }
! 983: fprintf(output_file, "'\",");
! 984: }
! 985: }
! 986: else
! 987: {
! 988: k = strlen(s) + 3;
! 989: j += k;
! 990: if (j > 80)
! 991: {
! 992: if (!rflag) ++outline;
! 993: putc('\n', output_file);
! 994: j = k;
! 995: }
! 996: putc('"', output_file);
! 997: do { putc(*s, output_file); } while (*++s);
! 998: fprintf(output_file, "\",");
! 999: }
! 1000: }
! 1001: else
! 1002: {
! 1003: j += 2;
! 1004: if (j > 80)
! 1005: {
! 1006: if (!rflag) ++outline;
! 1007: putc('\n', output_file);
! 1008: j = 2;
! 1009: }
! 1010: fprintf(output_file, "0,");
! 1011: }
! 1012: }
! 1013: if (!rflag) outline += 2;
! 1014: fprintf(output_file, "\n};\n");
! 1015: FREE(symnam);
! 1016:
! 1017: if (!rflag) ++outline;
! 1018: fprintf(output_file, "char *%srule[] = {\n", symbol_prefix);
! 1019: for (i = 2; i < nrules; ++i)
! 1020: {
! 1021: fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
! 1022: for (j = rrhs[i]; ritem[j] > 0; ++j)
! 1023: {
! 1024: s = symbol_name[ritem[j]];
! 1025: if (s[0] == '"')
! 1026: {
! 1027: fprintf(output_file, " \\\"");
! 1028: while (*++s != '"')
! 1029: {
! 1030: if (*s == '\\')
! 1031: {
! 1032: if (s[1] == '\\')
! 1033: fprintf(output_file, "\\\\\\\\");
! 1034: else
! 1035: fprintf(output_file, "\\\\%c", s[1]);
! 1036: ++s;
! 1037: }
! 1038: else
! 1039: putc(*s, output_file);
! 1040: }
! 1041: fprintf(output_file, "\\\"");
! 1042: }
! 1043: else if (s[0] == '\'')
! 1044: {
! 1045: if (s[1] == '"')
! 1046: fprintf(output_file, " '\\\"'");
! 1047: else if (s[1] == '\\')
! 1048: {
! 1049: if (s[2] == '\\')
! 1050: fprintf(output_file, " '\\\\\\\\");
! 1051: else
! 1052: fprintf(output_file, " '\\\\%c", s[2]);
! 1053: s += 2;
! 1054: while (*++s != '\'')
! 1055: putc(*s, output_file);
! 1056: putc('\'', output_file);
! 1057: }
! 1058: else
! 1059: fprintf(output_file, " '%c'", s[1]);
! 1060: }
! 1061: else
! 1062: fprintf(output_file, " %s", s);
! 1063: }
! 1064: if (!rflag) ++outline;
! 1065: fprintf(output_file, "\",\n");
! 1066: }
! 1067:
! 1068: if (!rflag) outline += 2;
! 1069: fprintf(output_file, "};\n#endif\n");
! 1070: }
! 1071:
! 1072:
! 1073: output_stype()
! 1074: {
! 1075: if (!unionized && ntags == 0)
! 1076: {
! 1077: outline += 3;
! 1078: fprintf(code_file, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
! 1079: }
! 1080: }
! 1081:
! 1082:
! 1083: output_trailing_text()
! 1084: {
! 1085: register int c, last;
! 1086: register FILE *in, *out;
! 1087:
! 1088: if (line == 0)
! 1089: return;
! 1090:
! 1091: in = input_file;
! 1092: out = code_file;
! 1093: c = *cptr;
! 1094: if (c == '\n')
! 1095: {
! 1096: ++lineno;
! 1097: if ((c = getc(in)) == EOF)
! 1098: return;
! 1099: if (!lflag)
! 1100: {
! 1101: ++outline;
! 1102: fprintf(out, line_format, lineno, input_file_name);
! 1103: }
! 1104: if (c == '\n')
! 1105: ++outline;
! 1106: putc(c, out);
! 1107: last = c;
! 1108: }
! 1109: else
! 1110: {
! 1111: if (!lflag)
! 1112: {
! 1113: ++outline;
! 1114: fprintf(out, line_format, lineno, input_file_name);
! 1115: }
! 1116: do { putc(c, out); } while ((c = *++cptr) != '\n');
! 1117: ++outline;
! 1118: putc('\n', out);
! 1119: last = '\n';
! 1120: }
! 1121:
! 1122: while ((c = getc(in)) != EOF)
! 1123: {
! 1124: if (c == '\n')
! 1125: ++outline;
! 1126: putc(c, out);
! 1127: last = c;
! 1128: }
! 1129:
! 1130: if (last != '\n')
! 1131: {
! 1132: ++outline;
! 1133: putc('\n', out);
! 1134: }
! 1135: if (!lflag)
! 1136: fprintf(out, line_format, ++outline + 1, code_file_name);
! 1137: }
! 1138:
! 1139:
! 1140: output_semantic_actions()
! 1141: {
! 1142: register int c, last;
! 1143: register FILE *out;
! 1144:
! 1145: fclose(action_file);
! 1146: action_file = fopen(action_file_name, "r");
! 1147: if (action_file == NULL)
! 1148: open_error(action_file_name);
! 1149:
! 1150: if ((c = getc(action_file)) == EOF)
! 1151: return;
! 1152:
! 1153: out = code_file;
! 1154: last = c;
! 1155: if (c == '\n')
! 1156: ++outline;
! 1157: putc(c, out);
! 1158: while ((c = getc(action_file)) != EOF)
! 1159: {
! 1160: if (c == '\n')
! 1161: ++outline;
! 1162: putc(c, out);
! 1163: last = c;
! 1164: }
! 1165:
! 1166: if (last != '\n')
! 1167: {
! 1168: ++outline;
! 1169: putc('\n', out);
! 1170: }
! 1171:
! 1172: if (!lflag)
! 1173: fprintf(out, line_format, ++outline + 1, code_file_name);
! 1174: }
! 1175:
! 1176:
! 1177: free_itemsets()
! 1178: {
! 1179: register core *cp, *next;
! 1180:
! 1181: FREE(state_table);
! 1182: for (cp = first_state; cp; cp = next)
! 1183: {
! 1184: next = cp->next;
! 1185: FREE(cp);
! 1186: }
! 1187: }
! 1188:
! 1189:
! 1190: free_shifts()
! 1191: {
! 1192: register shifts *sp, *next;
! 1193:
! 1194: FREE(shift_table);
! 1195: for (sp = first_shift; sp; sp = next)
! 1196: {
! 1197: next = sp->next;
! 1198: FREE(sp);
! 1199: }
! 1200: }
! 1201:
! 1202:
! 1203:
! 1204: free_reductions()
! 1205: {
! 1206: register reductions *rp, *next;
! 1207:
! 1208: FREE(reduction_table);
! 1209: for (rp = first_reduction; rp; rp = next)
! 1210: {
! 1211: next = rp->next;
! 1212: FREE(rp);
! 1213: }
! 1214: }