1268899Sbapt/* $Id: output.c,v 1.67 2014/04/22 23:16:57 tom Exp $ */ 2234949Sbapt 3234949Sbapt#include "defs.h" 4234949Sbapt 5234949Sbapt#define StaticOrR (rflag ? "" : "static ") 6234949Sbapt#define CountLine(fp) (!rflag || ((fp) == code_file)) 7234949Sbapt 8268899Sbapt#if defined(YYBTYACC) 9268899Sbapt#define PER_STATE 3 10268899Sbapt#else 11268899Sbapt#define PER_STATE 2 12268899Sbapt#endif 13268899Sbapt 14234949Sbaptstatic int nvectors; 15234949Sbaptstatic int nentries; 16234949Sbaptstatic Value_t **froms; 17234949Sbaptstatic Value_t **tos; 18268899Sbapt#if defined(YYBTYACC) 19268899Sbaptstatic Value_t *conflicts = NULL; 20268899Sbaptstatic Value_t nconflicts = 0; 21268899Sbapt#endif 22234949Sbaptstatic Value_t *tally; 23234949Sbaptstatic Value_t *width; 24234949Sbaptstatic Value_t *state_count; 25234949Sbaptstatic Value_t *order; 26234949Sbaptstatic Value_t *base; 27234949Sbaptstatic Value_t *pos; 28234949Sbaptstatic int maxtable; 29234949Sbaptstatic Value_t *table; 30234949Sbaptstatic Value_t *check; 31234949Sbaptstatic int lowzero; 32268899Sbaptstatic long high; 33234949Sbapt 34234949Sbaptstatic void 35234949Sbaptputc_code(FILE * fp, int c) 36234949Sbapt{ 37234949Sbapt if ((c == '\n') && (fp == code_file)) 38234949Sbapt ++outline; 39234949Sbapt putc(c, fp); 40234949Sbapt} 41234949Sbapt 42234949Sbaptstatic void 43234949Sbaptputl_code(FILE * fp, const char *s) 44234949Sbapt{ 45234949Sbapt if (fp == code_file) 46234949Sbapt ++outline; 47234949Sbapt fputs(s, fp); 48234949Sbapt} 49234949Sbapt 50234949Sbaptstatic void 51234949Sbaptputs_code(FILE * fp, const char *s) 52234949Sbapt{ 53234949Sbapt fputs(s, fp); 54234949Sbapt} 55234949Sbapt 56234949Sbaptstatic void 57234949Sbaptwrite_code_lineno(FILE * fp) 58234949Sbapt{ 59234949Sbapt if (!lflag && (fp == code_file)) 60234949Sbapt { 61234949Sbapt ++outline; 62268899Sbapt fprintf(fp, line_format, outline + 1, code_file_name); 63234949Sbapt } 64234949Sbapt} 65234949Sbapt 66234949Sbaptstatic void 67234949Sbaptwrite_input_lineno(void) 68234949Sbapt{ 69234949Sbapt if (!lflag) 70234949Sbapt { 71234949Sbapt ++outline; 72234949Sbapt fprintf(code_file, line_format, lineno, input_file_name); 73234949Sbapt } 74234949Sbapt} 75234949Sbapt 76234949Sbaptstatic void 77234949Sbaptdefine_prefixed(FILE * fp, const char *name) 78234949Sbapt{ 79234949Sbapt int bump_line = CountLine(fp); 80234949Sbapt if (bump_line) 81234949Sbapt ++outline; 82234949Sbapt fprintf(fp, "\n"); 83234949Sbapt 84234949Sbapt if (bump_line) 85234949Sbapt ++outline; 86234949Sbapt fprintf(fp, "#ifndef %s\n", name); 87234949Sbapt 88234949Sbapt if (bump_line) 89234949Sbapt ++outline; 90234949Sbapt fprintf(fp, "#define %-10s %s%s\n", name, symbol_prefix, name + 2); 91234949Sbapt 92234949Sbapt if (bump_line) 93234949Sbapt ++outline; 94234949Sbapt fprintf(fp, "#endif /* %s */\n", name); 95234949Sbapt} 96234949Sbapt 97234949Sbaptstatic void 98234949Sbaptoutput_prefix(FILE * fp) 99234949Sbapt{ 100234949Sbapt if (symbol_prefix == NULL) 101234949Sbapt { 102234949Sbapt symbol_prefix = "yy"; 103234949Sbapt } 104234949Sbapt else 105234949Sbapt { 106234949Sbapt define_prefixed(fp, "yyparse"); 107234949Sbapt define_prefixed(fp, "yylex"); 108234949Sbapt define_prefixed(fp, "yyerror"); 109234949Sbapt define_prefixed(fp, "yychar"); 110234949Sbapt define_prefixed(fp, "yyval"); 111234949Sbapt define_prefixed(fp, "yylval"); 112234949Sbapt define_prefixed(fp, "yydebug"); 113234949Sbapt define_prefixed(fp, "yynerrs"); 114234949Sbapt define_prefixed(fp, "yyerrflag"); 115234949Sbapt define_prefixed(fp, "yylhs"); 116234949Sbapt define_prefixed(fp, "yylen"); 117234949Sbapt define_prefixed(fp, "yydefred"); 118268899Sbapt#if defined(YYBTYACC) 119268899Sbapt define_prefixed(fp, "yystos"); 120268899Sbapt#endif 121234949Sbapt define_prefixed(fp, "yydgoto"); 122234949Sbapt define_prefixed(fp, "yysindex"); 123234949Sbapt define_prefixed(fp, "yyrindex"); 124234949Sbapt define_prefixed(fp, "yygindex"); 125234949Sbapt define_prefixed(fp, "yytable"); 126234949Sbapt define_prefixed(fp, "yycheck"); 127234949Sbapt define_prefixed(fp, "yyname"); 128234949Sbapt define_prefixed(fp, "yyrule"); 129268899Sbapt#if defined(YYBTYACC) 130268899Sbapt if (locations) 131268899Sbapt { 132268899Sbapt define_prefixed(fp, "yyloc"); 133268899Sbapt define_prefixed(fp, "yylloc"); 134268899Sbapt } 135268899Sbapt putc_code(fp, '\n'); 136268899Sbapt putl_code(fp, "#if YYBTYACC\n"); 137268899Sbapt 138268899Sbapt define_prefixed(fp, "yycindex"); 139268899Sbapt define_prefixed(fp, "yyctable"); 140268899Sbapt 141268899Sbapt putc_code(fp, '\n'); 142268899Sbapt putl_code(fp, "#endif /* YYBTYACC */\n"); 143268899Sbapt putc_code(fp, '\n'); 144268899Sbapt#endif 145234949Sbapt } 146234949Sbapt if (CountLine(fp)) 147234949Sbapt ++outline; 148234949Sbapt fprintf(fp, "#define YYPREFIX \"%s\"\n", symbol_prefix); 149234949Sbapt} 150234949Sbapt 151234949Sbaptstatic void 152234949Sbaptoutput_newline(void) 153234949Sbapt{ 154234949Sbapt if (!rflag) 155234949Sbapt ++outline; 156234949Sbapt putc('\n', output_file); 157234949Sbapt} 158234949Sbapt 159234949Sbaptstatic void 160234949Sbaptoutput_line(const char *value) 161234949Sbapt{ 162234949Sbapt fputs(value, output_file); 163234949Sbapt output_newline(); 164234949Sbapt} 165234949Sbapt 166234949Sbaptstatic void 167234949Sbaptoutput_int(int value) 168234949Sbapt{ 169234949Sbapt fprintf(output_file, "%5d,", value); 170234949Sbapt} 171234949Sbapt 172234949Sbaptstatic void 173234949Sbaptstart_int_table(const char *name, int value) 174234949Sbapt{ 175234949Sbapt int need = 34 - (int)(strlen(symbol_prefix) + strlen(name)); 176234949Sbapt 177234949Sbapt if (need < 6) 178234949Sbapt need = 6; 179234949Sbapt fprintf(output_file, 180268899Sbapt "%sconst YYINT %s%s[] = {%*d,", 181234949Sbapt StaticOrR, symbol_prefix, name, need, value); 182234949Sbapt} 183234949Sbapt 184234949Sbaptstatic void 185234949Sbaptstart_str_table(const char *name) 186234949Sbapt{ 187234949Sbapt fprintf(output_file, 188268899Sbapt "%sconst char *const %s%s[] = {", 189268899Sbapt StaticOrR, symbol_prefix, name); 190234949Sbapt output_newline(); 191234949Sbapt} 192234949Sbapt 193234949Sbaptstatic void 194234949Sbaptend_table(void) 195234949Sbapt{ 196234949Sbapt output_newline(); 197234949Sbapt output_line("};"); 198234949Sbapt} 199234949Sbapt 200234949Sbaptstatic void 201268899Sbaptoutput_YYINT_typedef(FILE * fp) 202268899Sbapt{ 203268899Sbapt /* generate the type used to index the various parser tables */ 204268899Sbapt if (CountLine(fp)) 205268899Sbapt ++outline; 206268899Sbapt fprintf(fp, "typedef %s YYINT;\n", CONCAT1("", YYINT)); 207268899Sbapt} 208268899Sbapt 209268899Sbaptstatic void 210234949Sbaptoutput_rule_data(void) 211234949Sbapt{ 212234949Sbapt int i; 213234949Sbapt int j; 214234949Sbapt 215268899Sbapt output_YYINT_typedef(output_file); 216268899Sbapt 217234949Sbapt start_int_table("lhs", symbol_value[start_symbol]); 218234949Sbapt 219234949Sbapt j = 10; 220234949Sbapt for (i = 3; i < nrules; i++) 221234949Sbapt { 222234949Sbapt if (j >= 10) 223234949Sbapt { 224234949Sbapt output_newline(); 225234949Sbapt j = 1; 226234949Sbapt } 227234949Sbapt else 228234949Sbapt ++j; 229234949Sbapt 230234949Sbapt output_int(symbol_value[rlhs[i]]); 231234949Sbapt } 232234949Sbapt end_table(); 233234949Sbapt 234234949Sbapt start_int_table("len", 2); 235234949Sbapt 236234949Sbapt j = 10; 237234949Sbapt for (i = 3; i < nrules; i++) 238234949Sbapt { 239234949Sbapt if (j >= 10) 240234949Sbapt { 241234949Sbapt output_newline(); 242234949Sbapt j = 1; 243234949Sbapt } 244234949Sbapt else 245234949Sbapt j++; 246234949Sbapt 247234949Sbapt output_int(rrhs[i + 1] - rrhs[i] - 1); 248234949Sbapt } 249234949Sbapt end_table(); 250234949Sbapt} 251234949Sbapt 252234949Sbaptstatic void 253234949Sbaptoutput_yydefred(void) 254234949Sbapt{ 255234949Sbapt int i, j; 256234949Sbapt 257234949Sbapt start_int_table("defred", (defred[0] ? defred[0] - 2 : 0)); 258234949Sbapt 259234949Sbapt j = 10; 260234949Sbapt for (i = 1; i < nstates; i++) 261234949Sbapt { 262234949Sbapt if (j < 10) 263234949Sbapt ++j; 264234949Sbapt else 265234949Sbapt { 266234949Sbapt output_newline(); 267234949Sbapt j = 1; 268234949Sbapt } 269234949Sbapt 270234949Sbapt output_int((defred[i] ? defred[i] - 2 : 0)); 271234949Sbapt } 272234949Sbapt 273234949Sbapt end_table(); 274234949Sbapt} 275234949Sbapt 276268899Sbapt#if defined(YYBTYACC) 277234949Sbaptstatic void 278268899Sbaptoutput_accessing_symbols(void) 279268899Sbapt{ 280268899Sbapt int i, j; 281268899Sbapt int *translate; 282268899Sbapt 283268899Sbapt if (nstates != 0) 284268899Sbapt { 285268899Sbapt translate = TMALLOC(int, nstates); 286268899Sbapt NO_SPACE(translate); 287268899Sbapt 288268899Sbapt for (i = 0; i < nstates; ++i) 289268899Sbapt { 290268899Sbapt int gsymb = accessing_symbol[i]; 291268899Sbapt 292268899Sbapt translate[i] = symbol_pval[gsymb]; 293268899Sbapt } 294268899Sbapt 295268899Sbapt /* yystos[] may be unused, depending on compile-time defines */ 296268899Sbapt start_int_table("stos", translate[0]); 297268899Sbapt 298268899Sbapt j = 10; 299268899Sbapt for (i = 1; i < nstates; ++i) 300268899Sbapt { 301268899Sbapt if (j < 10) 302268899Sbapt ++j; 303268899Sbapt else 304268899Sbapt { 305268899Sbapt output_newline(); 306268899Sbapt j = 1; 307268899Sbapt } 308268899Sbapt 309268899Sbapt output_int(translate[i]); 310268899Sbapt } 311268899Sbapt 312268899Sbapt end_table(); 313268899Sbapt FREE(translate); 314268899Sbapt } 315268899Sbapt} 316268899Sbapt 317268899Sbaptstatic Value_t 318268899Sbaptfind_conflict_base(int cbase) 319268899Sbapt{ 320268899Sbapt int i, j; 321268899Sbapt 322268899Sbapt for (i = 0; i < cbase; i++) 323268899Sbapt { 324268899Sbapt for (j = 0; j + cbase < nconflicts; j++) 325268899Sbapt { 326268899Sbapt if (conflicts[i + j] != conflicts[cbase + j]) 327268899Sbapt break; 328268899Sbapt } 329268899Sbapt if (j + cbase >= nconflicts) 330268899Sbapt break; 331268899Sbapt } 332268899Sbapt return (Value_t) i; 333268899Sbapt} 334268899Sbapt#endif 335268899Sbapt 336268899Sbaptstatic void 337234949Sbapttoken_actions(void) 338234949Sbapt{ 339234949Sbapt int i, j; 340234949Sbapt Value_t shiftcount, reducecount; 341268899Sbapt#if defined(YYBTYACC) 342268899Sbapt Value_t conflictcount = 0; 343268899Sbapt Value_t csym = -1; 344268899Sbapt Value_t cbase = 0; 345268899Sbapt#endif 346234949Sbapt int max, min; 347234949Sbapt Value_t *actionrow, *r, *s; 348234949Sbapt action *p; 349234949Sbapt 350268899Sbapt actionrow = NEW2(PER_STATE * ntokens, Value_t); 351234949Sbapt for (i = 0; i < nstates; ++i) 352234949Sbapt { 353234949Sbapt if (parser[i]) 354234949Sbapt { 355268899Sbapt for (j = 0; j < PER_STATE * ntokens; ++j) 356234949Sbapt actionrow[j] = 0; 357234949Sbapt 358234949Sbapt shiftcount = 0; 359234949Sbapt reducecount = 0; 360268899Sbapt#if defined(YYBTYACC) 361268899Sbapt if (backtrack) 362268899Sbapt { 363268899Sbapt conflictcount = 0; 364268899Sbapt csym = -1; 365268899Sbapt cbase = nconflicts; 366268899Sbapt } 367268899Sbapt#endif 368234949Sbapt for (p = parser[i]; p; p = p->next) 369234949Sbapt { 370268899Sbapt#if defined(YYBTYACC) 371268899Sbapt if (backtrack) 372268899Sbapt { 373268899Sbapt if (csym != -1 && csym != p->symbol) 374268899Sbapt { 375268899Sbapt conflictcount++; 376268899Sbapt conflicts[nconflicts++] = -1; 377268899Sbapt j = find_conflict_base(cbase); 378268899Sbapt actionrow[csym + 2 * ntokens] = (Value_t) (j + 1); 379268899Sbapt if (j == cbase) 380268899Sbapt { 381268899Sbapt cbase = nconflicts; 382268899Sbapt } 383268899Sbapt else 384268899Sbapt { 385268899Sbapt if (conflicts[cbase] == -1) 386268899Sbapt cbase++; 387268899Sbapt nconflicts = cbase; 388268899Sbapt } 389268899Sbapt csym = -1; 390268899Sbapt } 391268899Sbapt } 392268899Sbapt#endif 393234949Sbapt if (p->suppressed == 0) 394234949Sbapt { 395234949Sbapt if (p->action_code == SHIFT) 396234949Sbapt { 397234949Sbapt ++shiftcount; 398234949Sbapt actionrow[p->symbol] = p->number; 399234949Sbapt } 400234949Sbapt else if (p->action_code == REDUCE && p->number != defred[i]) 401234949Sbapt { 402234949Sbapt ++reducecount; 403234949Sbapt actionrow[p->symbol + ntokens] = p->number; 404234949Sbapt } 405234949Sbapt } 406268899Sbapt#if defined(YYBTYACC) 407268899Sbapt else if (backtrack && p->suppressed == 1) 408268899Sbapt { 409268899Sbapt csym = p->symbol; 410268899Sbapt if (p->action_code == SHIFT) 411268899Sbapt { 412268899Sbapt conflicts[nconflicts++] = p->number; 413268899Sbapt } 414268899Sbapt else if (p->action_code == REDUCE && p->number != defred[i]) 415268899Sbapt { 416268899Sbapt if (cbase == nconflicts) 417268899Sbapt { 418268899Sbapt if (cbase) 419268899Sbapt cbase--; 420268899Sbapt else 421268899Sbapt conflicts[nconflicts++] = -1; 422268899Sbapt } 423268899Sbapt conflicts[nconflicts++] = (Value_t) (p->number - 2); 424268899Sbapt } 425268899Sbapt } 426268899Sbapt#endif 427234949Sbapt } 428268899Sbapt#if defined(YYBTYACC) 429268899Sbapt if (backtrack && csym != -1) 430268899Sbapt { 431268899Sbapt conflictcount++; 432268899Sbapt conflicts[nconflicts++] = -1; 433268899Sbapt j = find_conflict_base(cbase); 434268899Sbapt actionrow[csym + 2 * ntokens] = (Value_t) (j + 1); 435268899Sbapt if (j == cbase) 436268899Sbapt { 437268899Sbapt cbase = nconflicts; 438268899Sbapt } 439268899Sbapt else 440268899Sbapt { 441268899Sbapt if (conflicts[cbase] == -1) 442268899Sbapt cbase++; 443268899Sbapt nconflicts = cbase; 444268899Sbapt } 445268899Sbapt } 446268899Sbapt#endif 447234949Sbapt 448234949Sbapt tally[i] = shiftcount; 449234949Sbapt tally[nstates + i] = reducecount; 450268899Sbapt#if defined(YYBTYACC) 451268899Sbapt if (backtrack) 452268899Sbapt tally[2 * nstates + i] = conflictcount; 453268899Sbapt#endif 454234949Sbapt width[i] = 0; 455234949Sbapt width[nstates + i] = 0; 456268899Sbapt#if defined(YYBTYACC) 457268899Sbapt if (backtrack) 458268899Sbapt width[2 * nstates + i] = 0; 459268899Sbapt#endif 460234949Sbapt if (shiftcount > 0) 461234949Sbapt { 462234949Sbapt froms[i] = r = NEW2(shiftcount, Value_t); 463234949Sbapt tos[i] = s = NEW2(shiftcount, Value_t); 464268899Sbapt min = MAXYYINT; 465234949Sbapt max = 0; 466234949Sbapt for (j = 0; j < ntokens; ++j) 467234949Sbapt { 468234949Sbapt if (actionrow[j]) 469234949Sbapt { 470234949Sbapt if (min > symbol_value[j]) 471234949Sbapt min = symbol_value[j]; 472234949Sbapt if (max < symbol_value[j]) 473234949Sbapt max = symbol_value[j]; 474234949Sbapt *r++ = symbol_value[j]; 475234949Sbapt *s++ = actionrow[j]; 476234949Sbapt } 477234949Sbapt } 478234949Sbapt width[i] = (Value_t) (max - min + 1); 479234949Sbapt } 480234949Sbapt if (reducecount > 0) 481234949Sbapt { 482234949Sbapt froms[nstates + i] = r = NEW2(reducecount, Value_t); 483234949Sbapt tos[nstates + i] = s = NEW2(reducecount, Value_t); 484268899Sbapt min = MAXYYINT; 485234949Sbapt max = 0; 486234949Sbapt for (j = 0; j < ntokens; ++j) 487234949Sbapt { 488234949Sbapt if (actionrow[ntokens + j]) 489234949Sbapt { 490234949Sbapt if (min > symbol_value[j]) 491234949Sbapt min = symbol_value[j]; 492234949Sbapt if (max < symbol_value[j]) 493234949Sbapt max = symbol_value[j]; 494234949Sbapt *r++ = symbol_value[j]; 495234949Sbapt *s++ = (Value_t) (actionrow[ntokens + j] - 2); 496234949Sbapt } 497234949Sbapt } 498234949Sbapt width[nstates + i] = (Value_t) (max - min + 1); 499234949Sbapt } 500268899Sbapt#if defined(YYBTYACC) 501268899Sbapt if (backtrack && conflictcount > 0) 502268899Sbapt { 503268899Sbapt froms[2 * nstates + i] = r = NEW2(conflictcount, Value_t); 504268899Sbapt tos[2 * nstates + i] = s = NEW2(conflictcount, Value_t); 505268899Sbapt min = MAXYYINT; 506268899Sbapt max = 0; 507268899Sbapt for (j = 0; j < ntokens; ++j) 508268899Sbapt { 509268899Sbapt if (actionrow[2 * ntokens + j]) 510268899Sbapt { 511268899Sbapt if (min > symbol_value[j]) 512268899Sbapt min = symbol_value[j]; 513268899Sbapt if (max < symbol_value[j]) 514268899Sbapt max = symbol_value[j]; 515268899Sbapt *r++ = symbol_value[j]; 516268899Sbapt *s++ = (Value_t) (actionrow[2 * ntokens + j] - 1); 517268899Sbapt } 518268899Sbapt } 519268899Sbapt width[2 * nstates + i] = (Value_t) (max - min + 1); 520268899Sbapt } 521268899Sbapt#endif 522234949Sbapt } 523234949Sbapt } 524234949Sbapt FREE(actionrow); 525234949Sbapt} 526234949Sbapt 527234949Sbaptstatic int 528234949Sbaptdefault_goto(int symbol) 529234949Sbapt{ 530234949Sbapt int i; 531234949Sbapt int m; 532234949Sbapt int n; 533234949Sbapt int default_state; 534234949Sbapt int max; 535234949Sbapt 536234949Sbapt m = goto_map[symbol]; 537234949Sbapt n = goto_map[symbol + 1]; 538234949Sbapt 539234949Sbapt if (m == n) 540234949Sbapt return (0); 541234949Sbapt 542234949Sbapt for (i = 0; i < nstates; i++) 543234949Sbapt state_count[i] = 0; 544234949Sbapt 545234949Sbapt for (i = m; i < n; i++) 546234949Sbapt state_count[to_state[i]]++; 547234949Sbapt 548234949Sbapt max = 0; 549234949Sbapt default_state = 0; 550234949Sbapt for (i = 0; i < nstates; i++) 551234949Sbapt { 552234949Sbapt if (state_count[i] > max) 553234949Sbapt { 554234949Sbapt max = state_count[i]; 555234949Sbapt default_state = i; 556234949Sbapt } 557234949Sbapt } 558234949Sbapt 559234949Sbapt return (default_state); 560234949Sbapt} 561234949Sbapt 562234949Sbaptstatic void 563234949Sbaptsave_column(int symbol, int default_state) 564234949Sbapt{ 565234949Sbapt int i; 566234949Sbapt int m; 567234949Sbapt int n; 568234949Sbapt Value_t *sp; 569234949Sbapt Value_t *sp1; 570234949Sbapt Value_t *sp2; 571234949Sbapt Value_t count; 572234949Sbapt int symno; 573234949Sbapt 574234949Sbapt m = goto_map[symbol]; 575234949Sbapt n = goto_map[symbol + 1]; 576234949Sbapt 577234949Sbapt count = 0; 578234949Sbapt for (i = m; i < n; i++) 579234949Sbapt { 580234949Sbapt if (to_state[i] != default_state) 581234949Sbapt ++count; 582234949Sbapt } 583234949Sbapt if (count == 0) 584234949Sbapt return; 585234949Sbapt 586268899Sbapt symno = symbol_value[symbol] + PER_STATE * nstates; 587234949Sbapt 588234949Sbapt froms[symno] = sp1 = sp = NEW2(count, Value_t); 589234949Sbapt tos[symno] = sp2 = NEW2(count, Value_t); 590234949Sbapt 591234949Sbapt for (i = m; i < n; i++) 592234949Sbapt { 593234949Sbapt if (to_state[i] != default_state) 594234949Sbapt { 595234949Sbapt *sp1++ = from_state[i]; 596234949Sbapt *sp2++ = to_state[i]; 597234949Sbapt } 598234949Sbapt } 599234949Sbapt 600234949Sbapt tally[symno] = count; 601234949Sbapt width[symno] = (Value_t) (sp1[-1] - sp[0] + 1); 602234949Sbapt} 603234949Sbapt 604234949Sbaptstatic void 605234949Sbaptgoto_actions(void) 606234949Sbapt{ 607234949Sbapt int i, j, k; 608234949Sbapt 609234949Sbapt state_count = NEW2(nstates, Value_t); 610234949Sbapt 611234949Sbapt k = default_goto(start_symbol + 1); 612234949Sbapt start_int_table("dgoto", k); 613234949Sbapt save_column(start_symbol + 1, k); 614234949Sbapt 615234949Sbapt j = 10; 616234949Sbapt for (i = start_symbol + 2; i < nsyms; i++) 617234949Sbapt { 618234949Sbapt if (j >= 10) 619234949Sbapt { 620234949Sbapt output_newline(); 621234949Sbapt j = 1; 622234949Sbapt } 623234949Sbapt else 624234949Sbapt ++j; 625234949Sbapt 626234949Sbapt k = default_goto(i); 627234949Sbapt output_int(k); 628234949Sbapt save_column(i, k); 629234949Sbapt } 630234949Sbapt 631234949Sbapt end_table(); 632234949Sbapt FREE(state_count); 633234949Sbapt} 634234949Sbapt 635234949Sbaptstatic void 636234949Sbaptsort_actions(void) 637234949Sbapt{ 638234949Sbapt Value_t i; 639234949Sbapt int j; 640234949Sbapt int k; 641234949Sbapt int t; 642234949Sbapt int w; 643234949Sbapt 644234949Sbapt order = NEW2(nvectors, Value_t); 645234949Sbapt nentries = 0; 646234949Sbapt 647234949Sbapt for (i = 0; i < nvectors; i++) 648234949Sbapt { 649234949Sbapt if (tally[i] > 0) 650234949Sbapt { 651234949Sbapt t = tally[i]; 652234949Sbapt w = width[i]; 653234949Sbapt j = nentries - 1; 654234949Sbapt 655234949Sbapt while (j >= 0 && (width[order[j]] < w)) 656234949Sbapt j--; 657234949Sbapt 658234949Sbapt while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t)) 659234949Sbapt j--; 660234949Sbapt 661234949Sbapt for (k = nentries - 1; k > j; k--) 662234949Sbapt order[k + 1] = order[k]; 663234949Sbapt 664234949Sbapt order[j + 1] = i; 665234949Sbapt nentries++; 666234949Sbapt } 667234949Sbapt } 668234949Sbapt} 669234949Sbapt 670234949Sbapt/* The function matching_vector determines if the vector specified by */ 671234949Sbapt/* the input parameter matches a previously considered vector. The */ 672234949Sbapt/* test at the start of the function checks if the vector represents */ 673234949Sbapt/* a row of shifts over terminal symbols or a row of reductions, or a */ 674234949Sbapt/* column of shifts over a nonterminal symbol. Berkeley Yacc does not */ 675234949Sbapt/* check if a column of shifts over a nonterminal symbols matches a */ 676234949Sbapt/* previously considered vector. Because of the nature of LR parsing */ 677234949Sbapt/* tables, no two columns can match. Therefore, the only possible */ 678234949Sbapt/* match would be between a row and a column. Such matches are */ 679234949Sbapt/* unlikely. Therefore, to save time, no attempt is made to see if a */ 680234949Sbapt/* column matches a previously considered vector. */ 681234949Sbapt/* */ 682234949Sbapt/* Matching_vector is poorly designed. The test could easily be made */ 683234949Sbapt/* faster. Also, it depends on the vectors being in a specific */ 684234949Sbapt/* order. */ 685268899Sbapt#if defined(YYBTYACC) 686268899Sbapt/* */ 687268899Sbapt/* Not really any point in checking for matching conflicts -- it is */ 688268899Sbapt/* extremely unlikely to occur, and conflicts are (hopefully) rare. */ 689268899Sbapt#endif 690234949Sbapt 691234949Sbaptstatic int 692234949Sbaptmatching_vector(int vector) 693234949Sbapt{ 694234949Sbapt int i; 695234949Sbapt int j; 696234949Sbapt int k; 697234949Sbapt int t; 698234949Sbapt int w; 699234949Sbapt int match; 700234949Sbapt int prev; 701234949Sbapt 702234949Sbapt i = order[vector]; 703234949Sbapt if (i >= 2 * nstates) 704234949Sbapt return (-1); 705234949Sbapt 706234949Sbapt t = tally[i]; 707234949Sbapt w = width[i]; 708234949Sbapt 709234949Sbapt for (prev = vector - 1; prev >= 0; prev--) 710234949Sbapt { 711234949Sbapt j = order[prev]; 712234949Sbapt if (width[j] != w || tally[j] != t) 713234949Sbapt return (-1); 714234949Sbapt 715234949Sbapt match = 1; 716234949Sbapt for (k = 0; match && k < t; k++) 717234949Sbapt { 718234949Sbapt if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k]) 719234949Sbapt match = 0; 720234949Sbapt } 721234949Sbapt 722234949Sbapt if (match) 723234949Sbapt return (j); 724234949Sbapt } 725234949Sbapt 726234949Sbapt return (-1); 727234949Sbapt} 728234949Sbapt 729234949Sbaptstatic int 730234949Sbaptpack_vector(int vector) 731234949Sbapt{ 732234949Sbapt int i, j, k, l; 733234949Sbapt int t; 734234949Sbapt int loc; 735234949Sbapt int ok; 736234949Sbapt Value_t *from; 737234949Sbapt Value_t *to; 738234949Sbapt int newmax; 739234949Sbapt 740234949Sbapt i = order[vector]; 741234949Sbapt t = tally[i]; 742234949Sbapt assert(t); 743234949Sbapt 744234949Sbapt from = froms[i]; 745234949Sbapt to = tos[i]; 746234949Sbapt 747234949Sbapt j = lowzero - from[0]; 748234949Sbapt for (k = 1; k < t; ++k) 749234949Sbapt if (lowzero - from[k] > j) 750234949Sbapt j = lowzero - from[k]; 751234949Sbapt for (;; ++j) 752234949Sbapt { 753234949Sbapt if (j == 0) 754234949Sbapt continue; 755234949Sbapt ok = 1; 756234949Sbapt for (k = 0; ok && k < t; k++) 757234949Sbapt { 758234949Sbapt loc = j + from[k]; 759234949Sbapt if (loc >= maxtable - 1) 760234949Sbapt { 761234949Sbapt if (loc >= MAXTABLE - 1) 762234949Sbapt fatal("maximum table size exceeded"); 763234949Sbapt 764234949Sbapt newmax = maxtable; 765234949Sbapt do 766234949Sbapt { 767234949Sbapt newmax += 200; 768234949Sbapt } 769234949Sbapt while (newmax <= loc); 770234949Sbapt 771240517Sbapt table = TREALLOC(Value_t, table, newmax); 772234949Sbapt NO_SPACE(table); 773234949Sbapt 774240517Sbapt check = TREALLOC(Value_t, check, newmax); 775234949Sbapt NO_SPACE(check); 776234949Sbapt 777234949Sbapt for (l = maxtable; l < newmax; ++l) 778234949Sbapt { 779234949Sbapt table[l] = 0; 780234949Sbapt check[l] = -1; 781234949Sbapt } 782234949Sbapt maxtable = newmax; 783234949Sbapt } 784234949Sbapt 785234949Sbapt if (check[loc] != -1) 786234949Sbapt ok = 0; 787234949Sbapt } 788234949Sbapt for (k = 0; ok && k < vector; k++) 789234949Sbapt { 790234949Sbapt if (pos[k] == j) 791234949Sbapt ok = 0; 792234949Sbapt } 793234949Sbapt if (ok) 794234949Sbapt { 795234949Sbapt for (k = 0; k < t; k++) 796234949Sbapt { 797234949Sbapt loc = j + from[k]; 798234949Sbapt table[loc] = to[k]; 799234949Sbapt check[loc] = from[k]; 800234949Sbapt if (loc > high) 801234949Sbapt high = loc; 802234949Sbapt } 803234949Sbapt 804234949Sbapt while (check[lowzero] != -1) 805234949Sbapt ++lowzero; 806234949Sbapt 807234949Sbapt return (j); 808234949Sbapt } 809234949Sbapt } 810234949Sbapt} 811234949Sbapt 812234949Sbaptstatic void 813234949Sbaptpack_table(void) 814234949Sbapt{ 815234949Sbapt int i; 816234949Sbapt Value_t place; 817234949Sbapt int state; 818234949Sbapt 819234949Sbapt base = NEW2(nvectors, Value_t); 820234949Sbapt pos = NEW2(nentries, Value_t); 821234949Sbapt 822234949Sbapt maxtable = 1000; 823234949Sbapt table = NEW2(maxtable, Value_t); 824234949Sbapt check = NEW2(maxtable, Value_t); 825234949Sbapt 826234949Sbapt lowzero = 0; 827234949Sbapt high = 0; 828234949Sbapt 829234949Sbapt for (i = 0; i < maxtable; i++) 830234949Sbapt check[i] = -1; 831234949Sbapt 832234949Sbapt for (i = 0; i < nentries; i++) 833234949Sbapt { 834234949Sbapt state = matching_vector(i); 835234949Sbapt 836234949Sbapt if (state < 0) 837234949Sbapt place = (Value_t) pack_vector(i); 838234949Sbapt else 839234949Sbapt place = base[state]; 840234949Sbapt 841234949Sbapt pos[i] = place; 842234949Sbapt base[order[i]] = place; 843234949Sbapt } 844234949Sbapt 845234949Sbapt for (i = 0; i < nvectors; i++) 846234949Sbapt { 847234949Sbapt if (froms[i]) 848234949Sbapt FREE(froms[i]); 849234949Sbapt if (tos[i]) 850234949Sbapt FREE(tos[i]); 851234949Sbapt } 852234949Sbapt 853268899Sbapt DO_FREE(froms); 854268899Sbapt DO_FREE(tos); 855268899Sbapt DO_FREE(tally); 856268899Sbapt DO_FREE(width); 857268899Sbapt DO_FREE(pos); 858234949Sbapt} 859234949Sbapt 860234949Sbaptstatic void 861234949Sbaptoutput_base(void) 862234949Sbapt{ 863234949Sbapt int i, j; 864234949Sbapt 865234949Sbapt start_int_table("sindex", base[0]); 866234949Sbapt 867234949Sbapt j = 10; 868234949Sbapt for (i = 1; i < nstates; i++) 869234949Sbapt { 870234949Sbapt if (j >= 10) 871234949Sbapt { 872234949Sbapt output_newline(); 873234949Sbapt j = 1; 874234949Sbapt } 875234949Sbapt else 876234949Sbapt ++j; 877234949Sbapt 878234949Sbapt output_int(base[i]); 879234949Sbapt } 880234949Sbapt 881234949Sbapt end_table(); 882234949Sbapt 883234949Sbapt start_int_table("rindex", base[nstates]); 884234949Sbapt 885234949Sbapt j = 10; 886234949Sbapt for (i = nstates + 1; i < 2 * nstates; i++) 887234949Sbapt { 888234949Sbapt if (j >= 10) 889234949Sbapt { 890234949Sbapt output_newline(); 891234949Sbapt j = 1; 892234949Sbapt } 893234949Sbapt else 894234949Sbapt ++j; 895234949Sbapt 896234949Sbapt output_int(base[i]); 897234949Sbapt } 898234949Sbapt 899234949Sbapt end_table(); 900234949Sbapt 901268899Sbapt#if defined(YYBTYACC) 902268899Sbapt output_line("#if YYBTYACC"); 903268899Sbapt start_int_table("cindex", base[2 * nstates]); 904234949Sbapt 905234949Sbapt j = 10; 906268899Sbapt for (i = 2 * nstates + 1; i < 3 * nstates; i++) 907234949Sbapt { 908234949Sbapt if (j >= 10) 909234949Sbapt { 910234949Sbapt output_newline(); 911234949Sbapt j = 1; 912234949Sbapt } 913234949Sbapt else 914234949Sbapt ++j; 915234949Sbapt 916234949Sbapt output_int(base[i]); 917234949Sbapt } 918234949Sbapt 919234949Sbapt end_table(); 920268899Sbapt output_line("#endif"); 921268899Sbapt#endif 922268899Sbapt 923268899Sbapt start_int_table("gindex", base[PER_STATE * nstates]); 924268899Sbapt 925268899Sbapt j = 10; 926268899Sbapt for (i = PER_STATE * nstates + 1; i < nvectors - 1; i++) 927268899Sbapt { 928268899Sbapt if (j >= 10) 929268899Sbapt { 930268899Sbapt output_newline(); 931268899Sbapt j = 1; 932268899Sbapt } 933268899Sbapt else 934268899Sbapt ++j; 935268899Sbapt 936268899Sbapt output_int(base[i]); 937268899Sbapt } 938268899Sbapt 939268899Sbapt end_table(); 940234949Sbapt FREE(base); 941234949Sbapt} 942234949Sbapt 943234949Sbaptstatic void 944234949Sbaptoutput_table(void) 945234949Sbapt{ 946234949Sbapt int i; 947234949Sbapt int j; 948234949Sbapt 949268899Sbapt if (high >= MAXYYINT) 950268899Sbapt { 951268899Sbapt fprintf(stderr, "YYTABLESIZE: %ld\n", high); 952268899Sbapt fprintf(stderr, "Table is longer than %d elements.\n", MAXYYINT); 953268899Sbapt done(1); 954268899Sbapt } 955268899Sbapt 956234949Sbapt ++outline; 957268899Sbapt fprintf(code_file, "#define YYTABLESIZE %ld\n", high); 958234949Sbapt start_int_table("table", table[0]); 959234949Sbapt 960234949Sbapt j = 10; 961234949Sbapt for (i = 1; i <= high; i++) 962234949Sbapt { 963234949Sbapt if (j >= 10) 964234949Sbapt { 965234949Sbapt output_newline(); 966234949Sbapt j = 1; 967234949Sbapt } 968234949Sbapt else 969234949Sbapt ++j; 970234949Sbapt 971234949Sbapt output_int(table[i]); 972234949Sbapt } 973234949Sbapt 974234949Sbapt end_table(); 975234949Sbapt FREE(table); 976234949Sbapt} 977234949Sbapt 978234949Sbaptstatic void 979234949Sbaptoutput_check(void) 980234949Sbapt{ 981234949Sbapt int i; 982234949Sbapt int j; 983234949Sbapt 984234949Sbapt start_int_table("check", check[0]); 985234949Sbapt 986234949Sbapt j = 10; 987234949Sbapt for (i = 1; i <= high; i++) 988234949Sbapt { 989234949Sbapt if (j >= 10) 990234949Sbapt { 991234949Sbapt output_newline(); 992234949Sbapt j = 1; 993234949Sbapt } 994234949Sbapt else 995234949Sbapt ++j; 996234949Sbapt 997234949Sbapt output_int(check[i]); 998234949Sbapt } 999234949Sbapt 1000234949Sbapt end_table(); 1001234949Sbapt FREE(check); 1002234949Sbapt} 1003234949Sbapt 1004268899Sbapt#if defined(YYBTYACC) 1005234949Sbaptstatic void 1006268899Sbaptoutput_ctable(void) 1007268899Sbapt{ 1008268899Sbapt int i; 1009268899Sbapt int j; 1010268899Sbapt 1011268899Sbapt if (conflicts) 1012268899Sbapt { 1013268899Sbapt output_line("#if YYBTYACC"); 1014268899Sbapt start_int_table("ctable", conflicts[0]); 1015268899Sbapt 1016268899Sbapt j = 10; 1017268899Sbapt for (i = 1; i < nconflicts; i++) 1018268899Sbapt { 1019268899Sbapt if (j >= 10) 1020268899Sbapt { 1021268899Sbapt output_newline(); 1022268899Sbapt j = 1; 1023268899Sbapt } 1024268899Sbapt else 1025268899Sbapt ++j; 1026268899Sbapt 1027268899Sbapt output_int(conflicts[i]); 1028268899Sbapt } 1029268899Sbapt 1030268899Sbapt end_table(); 1031268899Sbapt output_line("#endif"); 1032268899Sbapt FREE(conflicts); 1033268899Sbapt } 1034268899Sbapt} 1035268899Sbapt#endif 1036268899Sbapt 1037268899Sbaptstatic void 1038234949Sbaptoutput_actions(void) 1039234949Sbapt{ 1040268899Sbapt nvectors = PER_STATE * nstates + nvars; 1041234949Sbapt 1042234949Sbapt froms = NEW2(nvectors, Value_t *); 1043234949Sbapt tos = NEW2(nvectors, Value_t *); 1044234949Sbapt tally = NEW2(nvectors, Value_t); 1045234949Sbapt width = NEW2(nvectors, Value_t); 1046234949Sbapt 1047268899Sbapt#if defined(YYBTYACC) 1048268899Sbapt if (backtrack && (SRtotal + RRtotal) != 0) 1049268899Sbapt conflicts = NEW2(4 * (SRtotal + RRtotal), Value_t); 1050268899Sbapt#endif 1051268899Sbapt 1052234949Sbapt token_actions(); 1053234949Sbapt FREE(lookaheads); 1054234949Sbapt FREE(LA); 1055234949Sbapt FREE(LAruleno); 1056234949Sbapt FREE(accessing_symbol); 1057234949Sbapt 1058234949Sbapt goto_actions(); 1059234949Sbapt FREE(goto_map + ntokens); 1060234949Sbapt FREE(from_state); 1061234949Sbapt FREE(to_state); 1062234949Sbapt 1063234949Sbapt sort_actions(); 1064234949Sbapt pack_table(); 1065234949Sbapt output_base(); 1066234949Sbapt output_table(); 1067234949Sbapt output_check(); 1068268899Sbapt#if defined(YYBTYACC) 1069268899Sbapt output_ctable(); 1070268899Sbapt#endif 1071234949Sbapt} 1072234949Sbapt 1073234949Sbaptstatic int 1074234949Sbaptis_C_identifier(char *name) 1075234949Sbapt{ 1076234949Sbapt char *s; 1077234949Sbapt int c; 1078234949Sbapt 1079234949Sbapt s = name; 1080234949Sbapt c = *s; 1081234949Sbapt if (c == '"') 1082234949Sbapt { 1083234949Sbapt c = *++s; 1084234949Sbapt if (!isalpha(c) && c != '_' && c != '$') 1085234949Sbapt return (0); 1086234949Sbapt while ((c = *++s) != '"') 1087234949Sbapt { 1088234949Sbapt if (!isalnum(c) && c != '_' && c != '$') 1089234949Sbapt return (0); 1090234949Sbapt } 1091234949Sbapt return (1); 1092234949Sbapt } 1093234949Sbapt 1094234949Sbapt if (!isalpha(c) && c != '_' && c != '$') 1095234949Sbapt return (0); 1096234949Sbapt while ((c = *++s) != 0) 1097234949Sbapt { 1098234949Sbapt if (!isalnum(c) && c != '_' && c != '$') 1099234949Sbapt return (0); 1100234949Sbapt } 1101234949Sbapt return (1); 1102234949Sbapt} 1103234949Sbapt 1104268899Sbapt#if USE_HEADER_GUARDS 1105234949Sbaptstatic void 1106268899Sbaptstart_defines_file(void) 1107268899Sbapt{ 1108268899Sbapt fprintf(defines_file, "#ifndef _%s_defines_h_\n", symbol_prefix); 1109268899Sbapt fprintf(defines_file, "#define _%s_defines_h_\n\n", symbol_prefix); 1110268899Sbapt} 1111268899Sbapt 1112268899Sbaptstatic void 1113268899Sbaptend_defines_file(void) 1114268899Sbapt{ 1115268899Sbapt fprintf(defines_file, "\n#endif /* _%s_defines_h_ */\n", symbol_prefix); 1116268899Sbapt} 1117268899Sbapt#else 1118268899Sbapt#define start_defines_file() /* nothing */ 1119268899Sbapt#define end_defines_file() /* nothing */ 1120268899Sbapt#endif 1121268899Sbapt 1122268899Sbaptstatic void 1123234949Sbaptoutput_defines(FILE * fp) 1124234949Sbapt{ 1125234949Sbapt int c, i; 1126234949Sbapt char *s; 1127234949Sbapt 1128234949Sbapt for (i = 2; i < ntokens; ++i) 1129234949Sbapt { 1130234949Sbapt s = symbol_name[i]; 1131234949Sbapt if (is_C_identifier(s) && (!sflag || *s != '"')) 1132234949Sbapt { 1133234949Sbapt fprintf(fp, "#define "); 1134234949Sbapt c = *s; 1135234949Sbapt if (c == '"') 1136234949Sbapt { 1137234949Sbapt while ((c = *++s) != '"') 1138234949Sbapt { 1139234949Sbapt putc(c, fp); 1140234949Sbapt } 1141234949Sbapt } 1142234949Sbapt else 1143234949Sbapt { 1144234949Sbapt do 1145234949Sbapt { 1146234949Sbapt putc(c, fp); 1147234949Sbapt } 1148234949Sbapt while ((c = *++s) != 0); 1149234949Sbapt } 1150234949Sbapt if (fp == code_file) 1151234949Sbapt ++outline; 1152234949Sbapt fprintf(fp, " %d\n", symbol_value[i]); 1153234949Sbapt } 1154234949Sbapt } 1155234949Sbapt 1156234949Sbapt if (fp == code_file) 1157234949Sbapt ++outline; 1158234949Sbapt if (fp != defines_file || iflag) 1159234949Sbapt fprintf(fp, "#define YYERRCODE %d\n", symbol_value[1]); 1160234949Sbapt 1161234949Sbapt if (fp == defines_file || (iflag && !dflag)) 1162234949Sbapt { 1163234949Sbapt if (unionized) 1164234949Sbapt { 1165251143Sbapt if (union_file != 0) 1166251143Sbapt { 1167251143Sbapt rewind(union_file); 1168251143Sbapt while ((c = getc(union_file)) != EOF) 1169268899Sbapt putc_code(fp, c); 1170251143Sbapt } 1171234949Sbapt fprintf(fp, "extern YYSTYPE %slval;\n", symbol_prefix); 1172234949Sbapt } 1173234949Sbapt } 1174234949Sbapt} 1175234949Sbapt 1176234949Sbaptstatic void 1177234949Sbaptoutput_stored_text(FILE * fp) 1178234949Sbapt{ 1179234949Sbapt int c; 1180234949Sbapt FILE *in; 1181234949Sbapt 1182234949Sbapt rewind(text_file); 1183234949Sbapt if (text_file == NULL) 1184234949Sbapt open_error("text_file"); 1185234949Sbapt in = text_file; 1186234949Sbapt if ((c = getc(in)) == EOF) 1187234949Sbapt return; 1188234949Sbapt putc_code(fp, c); 1189234949Sbapt while ((c = getc(in)) != EOF) 1190234949Sbapt { 1191234949Sbapt putc_code(fp, c); 1192234949Sbapt } 1193234949Sbapt write_code_lineno(fp); 1194234949Sbapt} 1195234949Sbapt 1196234949Sbaptstatic void 1197234949Sbaptoutput_debug(void) 1198234949Sbapt{ 1199268899Sbapt int i, j, k, max, maxtok; 1200234949Sbapt const char **symnam; 1201234949Sbapt const char *s; 1202234949Sbapt 1203234949Sbapt ++outline; 1204234949Sbapt fprintf(code_file, "#define YYFINAL %d\n", final_state); 1205234949Sbapt 1206234949Sbapt putl_code(code_file, "#ifndef YYDEBUG\n"); 1207234949Sbapt ++outline; 1208234949Sbapt fprintf(code_file, "#define YYDEBUG %d\n", tflag); 1209234949Sbapt putl_code(code_file, "#endif\n"); 1210234949Sbapt 1211234949Sbapt if (rflag) 1212234949Sbapt { 1213234949Sbapt fprintf(output_file, "#ifndef YYDEBUG\n"); 1214234949Sbapt fprintf(output_file, "#define YYDEBUG %d\n", tflag); 1215234949Sbapt fprintf(output_file, "#endif\n"); 1216234949Sbapt } 1217234949Sbapt 1218268899Sbapt maxtok = 0; 1219268899Sbapt for (i = 0; i < ntokens; ++i) 1220268899Sbapt if (symbol_value[i] > maxtok) 1221268899Sbapt maxtok = symbol_value[i]; 1222234949Sbapt 1223268899Sbapt /* symbol_value[$accept] = -1 */ 1224268899Sbapt /* symbol_value[<goal>] = 0 */ 1225268899Sbapt /* remaining non-terminals start at 1 */ 1226268899Sbapt max = maxtok; 1227268899Sbapt for (i = ntokens; i < nsyms; ++i) 1228268899Sbapt if (((maxtok + 1) + (symbol_value[i] + 1)) > max) 1229268899Sbapt max = (maxtok + 1) + (symbol_value[i] + 1); 1230268899Sbapt 1231234949Sbapt ++outline; 1232268899Sbapt fprintf(code_file, "#define YYMAXTOKEN %d\n", maxtok); 1233234949Sbapt 1234268899Sbapt ++outline; 1235268899Sbapt fprintf(code_file, "#define YYUNDFTOKEN %d\n", max + 1); 1236268899Sbapt 1237268899Sbapt ++outline; 1238268899Sbapt fprintf(code_file, "#define YYTRANSLATE(a) ((a) > YYMAXTOKEN ? " 1239268899Sbapt "YYUNDFTOKEN : (a))\n"); 1240268899Sbapt 1241268899Sbapt symnam = TMALLOC(const char *, max + 2); 1242234949Sbapt NO_SPACE(symnam); 1243234949Sbapt 1244268899Sbapt /* Note that it is not necessary to initialize the element */ 1245234949Sbapt /* symnam[max]. */ 1246268899Sbapt#if defined(YYBTYACC) 1247234949Sbapt for (i = 0; i < max; ++i) 1248234949Sbapt symnam[i] = 0; 1249268899Sbapt for (i = nsyms - 1; i >= 0; --i) 1250268899Sbapt symnam[symbol_pval[i]] = symbol_name[i]; 1251268899Sbapt symnam[max + 1] = "illegal-symbol"; 1252268899Sbapt#else 1253268899Sbapt for (i = 0; i <= max; ++i) 1254268899Sbapt symnam[i] = 0; 1255234949Sbapt for (i = ntokens - 1; i >= 2; --i) 1256234949Sbapt symnam[symbol_value[i]] = symbol_name[i]; 1257234949Sbapt symnam[0] = "end-of-file"; 1258268899Sbapt symnam[max + 1] = "illegal-symbol"; 1259268899Sbapt#endif 1260234949Sbapt 1261268899Sbapt /* 1262268899Sbapt * bison's yytname[] array is roughly the same as byacc's yyname[] array. 1263268899Sbapt * The difference is that byacc does not predefine "$undefined". 1264268899Sbapt * 1265268899Sbapt * If the grammar declares "%token-table", define symbol "yytname" so 1266268899Sbapt * an application such as ntpd can build. 1267268899Sbapt */ 1268268899Sbapt if (token_table) 1269268899Sbapt { 1270268899Sbapt output_line("#undef yytname"); 1271268899Sbapt output_line("#define yytname yyname"); 1272268899Sbapt } 1273268899Sbapt else 1274268899Sbapt { 1275268899Sbapt output_line("#if YYDEBUG"); 1276268899Sbapt } 1277234949Sbapt 1278234949Sbapt start_str_table("name"); 1279234949Sbapt j = 80; 1280268899Sbapt for (i = 0; i <= max + 1; ++i) 1281234949Sbapt { 1282234949Sbapt if ((s = symnam[i]) != 0) 1283234949Sbapt { 1284234949Sbapt if (s[0] == '"') 1285234949Sbapt { 1286234949Sbapt k = 7; 1287234949Sbapt while (*++s != '"') 1288234949Sbapt { 1289234949Sbapt ++k; 1290234949Sbapt if (*s == '\\') 1291234949Sbapt { 1292234949Sbapt k += 2; 1293234949Sbapt if (*++s == '\\') 1294234949Sbapt ++k; 1295234949Sbapt } 1296234949Sbapt } 1297234949Sbapt j += k; 1298234949Sbapt if (j > 80) 1299234949Sbapt { 1300234949Sbapt output_newline(); 1301234949Sbapt j = k; 1302234949Sbapt } 1303234949Sbapt fprintf(output_file, "\"\\\""); 1304234949Sbapt s = symnam[i]; 1305234949Sbapt while (*++s != '"') 1306234949Sbapt { 1307234949Sbapt if (*s == '\\') 1308234949Sbapt { 1309234949Sbapt fprintf(output_file, "\\\\"); 1310234949Sbapt if (*++s == '\\') 1311234949Sbapt fprintf(output_file, "\\\\"); 1312234949Sbapt else 1313234949Sbapt putc(*s, output_file); 1314234949Sbapt } 1315234949Sbapt else 1316234949Sbapt putc(*s, output_file); 1317234949Sbapt } 1318234949Sbapt fprintf(output_file, "\\\"\","); 1319234949Sbapt } 1320234949Sbapt else if (s[0] == '\'') 1321234949Sbapt { 1322234949Sbapt if (s[1] == '"') 1323234949Sbapt { 1324234949Sbapt j += 7; 1325234949Sbapt if (j > 80) 1326234949Sbapt { 1327234949Sbapt output_newline(); 1328234949Sbapt j = 7; 1329234949Sbapt } 1330234949Sbapt fprintf(output_file, "\"'\\\"'\","); 1331234949Sbapt } 1332234949Sbapt else 1333234949Sbapt { 1334234949Sbapt k = 5; 1335234949Sbapt while (*++s != '\'') 1336234949Sbapt { 1337234949Sbapt ++k; 1338234949Sbapt if (*s == '\\') 1339234949Sbapt { 1340234949Sbapt k += 2; 1341234949Sbapt if (*++s == '\\') 1342234949Sbapt ++k; 1343234949Sbapt } 1344234949Sbapt } 1345234949Sbapt j += k; 1346234949Sbapt if (j > 80) 1347234949Sbapt { 1348234949Sbapt output_newline(); 1349234949Sbapt j = k; 1350234949Sbapt } 1351234949Sbapt fprintf(output_file, "\"'"); 1352234949Sbapt s = symnam[i]; 1353234949Sbapt while (*++s != '\'') 1354234949Sbapt { 1355234949Sbapt if (*s == '\\') 1356234949Sbapt { 1357234949Sbapt fprintf(output_file, "\\\\"); 1358234949Sbapt if (*++s == '\\') 1359234949Sbapt fprintf(output_file, "\\\\"); 1360234949Sbapt else 1361234949Sbapt putc(*s, output_file); 1362234949Sbapt } 1363234949Sbapt else 1364234949Sbapt putc(*s, output_file); 1365234949Sbapt } 1366234949Sbapt fprintf(output_file, "'\","); 1367234949Sbapt } 1368234949Sbapt } 1369234949Sbapt else 1370234949Sbapt { 1371234949Sbapt k = (int)strlen(s) + 3; 1372234949Sbapt j += k; 1373234949Sbapt if (j > 80) 1374234949Sbapt { 1375234949Sbapt output_newline(); 1376234949Sbapt j = k; 1377234949Sbapt } 1378234949Sbapt putc('"', output_file); 1379234949Sbapt do 1380234949Sbapt { 1381234949Sbapt putc(*s, output_file); 1382234949Sbapt } 1383234949Sbapt while (*++s); 1384234949Sbapt fprintf(output_file, "\","); 1385234949Sbapt } 1386234949Sbapt } 1387234949Sbapt else 1388234949Sbapt { 1389234949Sbapt j += 2; 1390234949Sbapt if (j > 80) 1391234949Sbapt { 1392234949Sbapt output_newline(); 1393234949Sbapt j = 2; 1394234949Sbapt } 1395234949Sbapt fprintf(output_file, "0,"); 1396234949Sbapt } 1397234949Sbapt } 1398234949Sbapt end_table(); 1399234949Sbapt FREE(symnam); 1400234949Sbapt 1401268899Sbapt if (token_table) 1402268899Sbapt output_line("#if YYDEBUG"); 1403234949Sbapt start_str_table("rule"); 1404234949Sbapt for (i = 2; i < nrules; ++i) 1405234949Sbapt { 1406234949Sbapt fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]); 1407234949Sbapt for (j = rrhs[i]; ritem[j] > 0; ++j) 1408234949Sbapt { 1409234949Sbapt s = symbol_name[ritem[j]]; 1410234949Sbapt if (s[0] == '"') 1411234949Sbapt { 1412234949Sbapt fprintf(output_file, " \\\""); 1413234949Sbapt while (*++s != '"') 1414234949Sbapt { 1415234949Sbapt if (*s == '\\') 1416234949Sbapt { 1417234949Sbapt if (s[1] == '\\') 1418234949Sbapt fprintf(output_file, "\\\\\\\\"); 1419234949Sbapt else 1420234949Sbapt fprintf(output_file, "\\\\%c", s[1]); 1421234949Sbapt ++s; 1422234949Sbapt } 1423234949Sbapt else 1424234949Sbapt putc(*s, output_file); 1425234949Sbapt } 1426234949Sbapt fprintf(output_file, "\\\""); 1427234949Sbapt } 1428234949Sbapt else if (s[0] == '\'') 1429234949Sbapt { 1430234949Sbapt if (s[1] == '"') 1431234949Sbapt fprintf(output_file, " '\\\"'"); 1432234949Sbapt else if (s[1] == '\\') 1433234949Sbapt { 1434234949Sbapt if (s[2] == '\\') 1435234949Sbapt fprintf(output_file, " '\\\\\\\\"); 1436234949Sbapt else 1437234949Sbapt fprintf(output_file, " '\\\\%c", s[2]); 1438234949Sbapt s += 2; 1439234949Sbapt while (*++s != '\'') 1440234949Sbapt putc(*s, output_file); 1441234949Sbapt putc('\'', output_file); 1442234949Sbapt } 1443234949Sbapt else 1444234949Sbapt fprintf(output_file, " '%c'", s[1]); 1445234949Sbapt } 1446234949Sbapt else 1447234949Sbapt fprintf(output_file, " %s", s); 1448234949Sbapt } 1449234949Sbapt fprintf(output_file, "\","); 1450234949Sbapt output_newline(); 1451234949Sbapt } 1452234949Sbapt 1453234949Sbapt end_table(); 1454234949Sbapt output_line("#endif"); 1455234949Sbapt} 1456234949Sbapt 1457268899Sbapt#if defined(YYBTYACC) 1458234949Sbaptstatic void 1459268899Sbaptoutput_backtracking_parser(FILE * fp) 1460268899Sbapt{ 1461268899Sbapt putl_code(fp, "#undef YYBTYACC\n"); 1462268899Sbapt#if defined(YYBTYACC) 1463268899Sbapt if (backtrack) 1464268899Sbapt { 1465268899Sbapt putl_code(fp, "#define YYBTYACC 1\n"); 1466268899Sbapt putl_code(fp, 1467268899Sbapt "#define YYDEBUGSTR (yytrial ? YYPREFIX \"debug(trial)\" : YYPREFIX \"debug\")\n"); 1468268899Sbapt } 1469268899Sbapt else 1470268899Sbapt#endif 1471268899Sbapt { 1472268899Sbapt putl_code(fp, "#define YYBTYACC 0\n"); 1473268899Sbapt putl_code(fp, "#define YYDEBUGSTR YYPREFIX \"debug\"\n"); 1474268899Sbapt } 1475268899Sbapt} 1476268899Sbapt#endif 1477268899Sbapt 1478268899Sbaptstatic void 1479234949Sbaptoutput_pure_parser(FILE * fp) 1480234949Sbapt{ 1481234949Sbapt putc_code(fp, '\n'); 1482234949Sbapt 1483234949Sbapt if (fp == code_file) 1484268899Sbapt ++outline; 1485234949Sbapt fprintf(fp, "#define YYPURE %d\n", pure_parser); 1486234949Sbapt putc_code(fp, '\n'); 1487234949Sbapt} 1488234949Sbapt 1489234949Sbaptstatic void 1490234949Sbaptoutput_stype(FILE * fp) 1491234949Sbapt{ 1492234949Sbapt if (!unionized && ntags == 0) 1493234949Sbapt { 1494234949Sbapt putc_code(fp, '\n'); 1495268899Sbapt putl_code(fp, "#if " 1496268899Sbapt "! defined(YYSTYPE) && " 1497268899Sbapt "! defined(YYSTYPE_IS_DECLARED)\n"); 1498268899Sbapt putl_code(fp, "/* Default: YYSTYPE is the semantic value type. */\n"); 1499234949Sbapt putl_code(fp, "typedef int YYSTYPE;\n"); 1500268899Sbapt putl_code(fp, "# define YYSTYPE_IS_DECLARED 1\n"); 1501234949Sbapt putl_code(fp, "#endif\n"); 1502234949Sbapt } 1503234949Sbapt} 1504234949Sbapt 1505268899Sbapt#if defined(YYBTYACC) 1506234949Sbaptstatic void 1507268899Sbaptoutput_ltype(FILE * fp) 1508268899Sbapt{ 1509268899Sbapt putc_code(fp, '\n'); 1510268899Sbapt putl_code(fp, "#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED\n"); 1511268899Sbapt putl_code(fp, "/* Default: YYLTYPE is the text position type. */\n"); 1512268899Sbapt putl_code(fp, "typedef struct YYLTYPE\n"); 1513268899Sbapt putl_code(fp, "{\n"); 1514268899Sbapt putl_code(fp, " int first_line;\n"); 1515268899Sbapt putl_code(fp, " int first_column;\n"); 1516268899Sbapt putl_code(fp, " int last_line;\n"); 1517268899Sbapt putl_code(fp, " int last_column;\n"); 1518268899Sbapt putl_code(fp, "} YYLTYPE;\n"); 1519268899Sbapt putl_code(fp, "#define YYLTYPE_IS_DECLARED 1\n"); 1520268899Sbapt putl_code(fp, "#endif\n"); 1521268899Sbapt} 1522268899Sbapt#endif 1523268899Sbapt 1524268899Sbaptstatic void 1525234949Sbaptoutput_trailing_text(void) 1526234949Sbapt{ 1527234949Sbapt int c, last; 1528234949Sbapt FILE *in; 1529234949Sbapt 1530234949Sbapt if (line == 0) 1531234949Sbapt return; 1532234949Sbapt 1533234949Sbapt in = input_file; 1534234949Sbapt c = *cptr; 1535234949Sbapt if (c == '\n') 1536234949Sbapt { 1537234949Sbapt ++lineno; 1538234949Sbapt if ((c = getc(in)) == EOF) 1539234949Sbapt return; 1540234949Sbapt write_input_lineno(); 1541234949Sbapt putc_code(code_file, c); 1542234949Sbapt last = c; 1543234949Sbapt } 1544234949Sbapt else 1545234949Sbapt { 1546234949Sbapt write_input_lineno(); 1547234949Sbapt do 1548234949Sbapt { 1549234949Sbapt putc_code(code_file, c); 1550234949Sbapt } 1551234949Sbapt while ((c = *++cptr) != '\n'); 1552234949Sbapt putc_code(code_file, c); 1553234949Sbapt last = '\n'; 1554234949Sbapt } 1555234949Sbapt 1556234949Sbapt while ((c = getc(in)) != EOF) 1557234949Sbapt { 1558234949Sbapt putc_code(code_file, c); 1559234949Sbapt last = c; 1560234949Sbapt } 1561234949Sbapt 1562234949Sbapt if (last != '\n') 1563234949Sbapt { 1564234949Sbapt putc_code(code_file, '\n'); 1565234949Sbapt } 1566234949Sbapt write_code_lineno(code_file); 1567234949Sbapt} 1568234949Sbapt 1569234949Sbaptstatic void 1570234949Sbaptoutput_semantic_actions(void) 1571234949Sbapt{ 1572234949Sbapt int c, last; 1573234949Sbapt 1574234949Sbapt rewind(action_file); 1575234949Sbapt if ((c = getc(action_file)) == EOF) 1576234949Sbapt return; 1577234949Sbapt 1578234949Sbapt last = c; 1579234949Sbapt putc_code(code_file, c); 1580234949Sbapt while ((c = getc(action_file)) != EOF) 1581234949Sbapt { 1582234949Sbapt putc_code(code_file, c); 1583234949Sbapt last = c; 1584234949Sbapt } 1585234949Sbapt 1586234949Sbapt if (last != '\n') 1587234949Sbapt { 1588234949Sbapt putc_code(code_file, '\n'); 1589234949Sbapt } 1590234949Sbapt 1591234949Sbapt write_code_lineno(code_file); 1592234949Sbapt} 1593234949Sbapt 1594234949Sbaptstatic void 1595234949Sbaptoutput_parse_decl(FILE * fp) 1596234949Sbapt{ 1597268899Sbapt putc_code(fp, '\n'); 1598234949Sbapt putl_code(fp, "/* compatibility with bison */\n"); 1599234949Sbapt putl_code(fp, "#ifdef YYPARSE_PARAM\n"); 1600234949Sbapt putl_code(fp, "/* compatibility with FreeBSD */\n"); 1601234949Sbapt putl_code(fp, "# ifdef YYPARSE_PARAM_TYPE\n"); 1602234949Sbapt putl_code(fp, 1603234949Sbapt "# define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)\n"); 1604234949Sbapt putl_code(fp, "# else\n"); 1605234949Sbapt putl_code(fp, "# define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)\n"); 1606234949Sbapt putl_code(fp, "# endif\n"); 1607234949Sbapt putl_code(fp, "#else\n"); 1608234949Sbapt 1609234949Sbapt puts_code(fp, "# define YYPARSE_DECL() yyparse("); 1610234949Sbapt if (!parse_param) 1611234949Sbapt puts_code(fp, "void"); 1612234949Sbapt else 1613234949Sbapt { 1614234949Sbapt param *p; 1615234949Sbapt for (p = parse_param; p; p = p->next) 1616234949Sbapt fprintf(fp, "%s %s%s%s", p->type, p->name, p->type2, 1617234949Sbapt p->next ? ", " : ""); 1618234949Sbapt } 1619234949Sbapt putl_code(fp, ")\n"); 1620234949Sbapt 1621234949Sbapt putl_code(fp, "#endif\n"); 1622234949Sbapt} 1623234949Sbapt 1624234949Sbaptstatic void 1625234949Sbaptoutput_lex_decl(FILE * fp) 1626234949Sbapt{ 1627268899Sbapt putc_code(fp, '\n'); 1628234949Sbapt putl_code(fp, "/* Parameters sent to lex. */\n"); 1629234949Sbapt putl_code(fp, "#ifdef YYLEX_PARAM\n"); 1630234949Sbapt if (pure_parser) 1631234949Sbapt { 1632234949Sbapt putl_code(fp, "# ifdef YYLEX_PARAM_TYPE\n"); 1633268899Sbapt#if defined(YYBTYACC) 1634268899Sbapt if (locations) 1635268899Sbapt { 1636268899Sbapt putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc," 1637268899Sbapt " YYLEX_PARAM_TYPE YYLEX_PARAM)\n"); 1638268899Sbapt } 1639268899Sbapt else 1640268899Sbapt#endif 1641268899Sbapt { 1642268899Sbapt putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval," 1643268899Sbapt " YYLEX_PARAM_TYPE YYLEX_PARAM)\n"); 1644268899Sbapt } 1645234949Sbapt putl_code(fp, "# else\n"); 1646268899Sbapt#if defined(YYBTYACC) 1647268899Sbapt if (locations) 1648268899Sbapt { 1649268899Sbapt putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc," 1650268899Sbapt " void * YYLEX_PARAM)\n"); 1651268899Sbapt } 1652268899Sbapt else 1653268899Sbapt#endif 1654268899Sbapt { 1655268899Sbapt putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval," 1656268899Sbapt " void * YYLEX_PARAM)\n"); 1657268899Sbapt } 1658234949Sbapt putl_code(fp, "# endif\n"); 1659268899Sbapt#if defined(YYBTYACC) 1660268899Sbapt if (locations) 1661268899Sbapt putl_code(fp, 1662268899Sbapt "# define YYLEX yylex(&yylval, &yylloc, YYLEX_PARAM)\n"); 1663268899Sbapt else 1664268899Sbapt#endif 1665268899Sbapt putl_code(fp, "# define YYLEX yylex(&yylval, YYLEX_PARAM)\n"); 1666234949Sbapt } 1667234949Sbapt else 1668234949Sbapt { 1669234949Sbapt putl_code(fp, "# define YYLEX_DECL() yylex(void *YYLEX_PARAM)\n"); 1670234949Sbapt putl_code(fp, "# define YYLEX yylex(YYLEX_PARAM)\n"); 1671234949Sbapt } 1672234949Sbapt putl_code(fp, "#else\n"); 1673234949Sbapt if (pure_parser && lex_param) 1674234949Sbapt { 1675234949Sbapt param *p; 1676268899Sbapt 1677268899Sbapt#if defined(YYBTYACC) 1678268899Sbapt if (locations) 1679268899Sbapt puts_code(fp, 1680268899Sbapt "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc, "); 1681268899Sbapt else 1682268899Sbapt#endif 1683268899Sbapt puts_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval, "); 1684234949Sbapt for (p = lex_param; p; p = p->next) 1685234949Sbapt fprintf(fp, "%s %s%s%s", p->type, p->name, p->type2, 1686234949Sbapt p->next ? ", " : ""); 1687234949Sbapt putl_code(fp, ")\n"); 1688234949Sbapt 1689268899Sbapt#if defined(YYBTYACC) 1690268899Sbapt if (locations) 1691268899Sbapt puts_code(fp, "# define YYLEX yylex(&yylval, &yylloc, "); 1692268899Sbapt else 1693268899Sbapt#endif 1694268899Sbapt puts_code(fp, "# define YYLEX yylex(&yylval, "); 1695234949Sbapt for (p = lex_param; p; p = p->next) 1696234949Sbapt fprintf(fp, "%s%s", p->name, p->next ? ", " : ""); 1697234949Sbapt putl_code(fp, ")\n"); 1698234949Sbapt } 1699234949Sbapt else if (pure_parser) 1700234949Sbapt { 1701268899Sbapt#if defined(YYBTYACC) 1702268899Sbapt if (locations) 1703268899Sbapt { 1704268899Sbapt putl_code(fp, 1705268899Sbapt "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc)\n"); 1706268899Sbapt putl_code(fp, "# define YYLEX yylex(&yylval, &yylloc)\n"); 1707268899Sbapt } 1708268899Sbapt else 1709268899Sbapt#endif 1710268899Sbapt { 1711268899Sbapt putl_code(fp, "# define YYLEX_DECL() yylex(YYSTYPE *yylval)\n"); 1712268899Sbapt putl_code(fp, "# define YYLEX yylex(&yylval)\n"); 1713268899Sbapt } 1714234949Sbapt } 1715234949Sbapt else if (lex_param) 1716234949Sbapt { 1717234949Sbapt param *p; 1718268899Sbapt 1719234949Sbapt puts_code(fp, "# define YYLEX_DECL() yylex("); 1720234949Sbapt for (p = lex_param; p; p = p->next) 1721234949Sbapt fprintf(fp, "%s %s%s%s", p->type, p->name, p->type2, 1722234949Sbapt p->next ? ", " : ""); 1723234949Sbapt putl_code(fp, ")\n"); 1724234949Sbapt 1725234949Sbapt puts_code(fp, "# define YYLEX yylex("); 1726234949Sbapt for (p = lex_param; p; p = p->next) 1727234949Sbapt fprintf(fp, "%s%s", p->name, p->next ? ", " : ""); 1728234949Sbapt putl_code(fp, ")\n"); 1729234949Sbapt } 1730234949Sbapt else 1731234949Sbapt { 1732234949Sbapt putl_code(fp, "# define YYLEX_DECL() yylex(void)\n"); 1733234949Sbapt putl_code(fp, "# define YYLEX yylex()\n"); 1734234949Sbapt } 1735234949Sbapt putl_code(fp, "#endif\n"); 1736234949Sbapt} 1737234949Sbapt 1738234949Sbaptstatic void 1739234949Sbaptoutput_error_decl(FILE * fp) 1740234949Sbapt{ 1741268899Sbapt param *p; 1742268899Sbapt 1743268899Sbapt putc_code(fp, '\n'); 1744234949Sbapt putl_code(fp, "/* Parameters sent to yyerror. */\n"); 1745268899Sbapt putl_code(fp, "#ifndef YYERROR_DECL\n"); 1746268899Sbapt puts_code(fp, "#define YYERROR_DECL() yyerror("); 1747268899Sbapt#if defined(YYBTYACC) 1748268899Sbapt if (locations) 1749268899Sbapt puts_code(fp, "YYLTYPE loc, "); 1750268899Sbapt#endif 1751268899Sbapt for (p = parse_param; p; p = p->next) 1752268899Sbapt fprintf(fp, "%s %s%s, ", p->type, p->name, p->type2); 1753268899Sbapt putl_code(fp, "const char *s)\n"); 1754268899Sbapt putl_code(fp, "#endif\n"); 1755234949Sbapt 1756268899Sbapt putl_code(fp, "#ifndef YYERROR_CALL\n"); 1757268899Sbapt puts_code(fp, "#define YYERROR_CALL(msg) yyerror("); 1758234949Sbapt 1759268899Sbapt#if defined(YYBTYACC) 1760268899Sbapt if (locations) 1761268899Sbapt puts_code(fp, "yylloc, "); 1762268899Sbapt#endif 1763268899Sbapt for (p = parse_param; p; p = p->next) 1764268899Sbapt fprintf(fp, "%s, ", p->name); 1765234949Sbapt 1766268899Sbapt putl_code(fp, "msg)\n"); 1767268899Sbapt putl_code(fp, "#endif\n"); 1768268899Sbapt} 1769234949Sbapt 1770268899Sbapt#if defined(YYBTYACC) 1771268899Sbaptstatic void 1772268899Sbaptoutput_yydestruct_decl(FILE * fp) 1773268899Sbapt{ 1774268899Sbapt putc_code(fp, '\n'); 1775268899Sbapt putl_code(fp, "#ifndef YYDESTRUCT_DECL\n"); 1776268899Sbapt#if defined(YYBTYACC) 1777268899Sbapt if (locations) 1778268899Sbapt putl_code(fp, 1779268899Sbapt "#define YYDESTRUCT_DECL() yydestruct(const char *msg, int psymb, YYSTYPE *val, YYLTYPE *loc)\n"); 1780234949Sbapt else 1781268899Sbapt#endif 1782268899Sbapt putl_code(fp, 1783268899Sbapt "#define YYDESTRUCT_DECL() yydestruct(const char *msg, int psymb, YYSTYPE *val)\n"); 1784268899Sbapt putl_code(fp, "#endif\n"); 1785268899Sbapt putl_code(fp, "#ifndef YYDESTRUCT_CALL\n"); 1786268899Sbapt#if defined(YYBTYACC) 1787268899Sbapt if (locations) 1788268899Sbapt putl_code(fp, 1789268899Sbapt "#define YYDESTRUCT_CALL(msg, psymb, val, loc) yydestruct(msg, psymb, val, loc)\n"); 1790268899Sbapt else 1791268899Sbapt#endif 1792268899Sbapt putl_code(fp, 1793268899Sbapt "#define YYDESTRUCT_CALL(msg, psymb, val) yydestruct(msg, psymb, val)\n"); 1794268899Sbapt putl_code(fp, "#endif\n"); 1795268899Sbapt} 1796268899Sbapt 1797268899Sbaptstatic void 1798268899Sbaptoutput_yydestruct_impl(void) 1799268899Sbapt{ 1800268899Sbapt int i; 1801268899Sbapt char *s, *destructor_code; 1802268899Sbapt 1803268899Sbapt putc_code(code_file, '\n'); 1804268899Sbapt putl_code(code_file, "/* Release memory associated with symbol. */\n"); 1805268899Sbapt putl_code(code_file, "#if ! defined YYDESTRUCT_IS_DECLARED\n"); 1806268899Sbapt putl_code(code_file, "static void\n"); 1807268899Sbapt putl_code(code_file, "YYDESTRUCT_DECL()\n"); 1808268899Sbapt putl_code(code_file, "{\n"); 1809268899Sbapt putl_code(code_file, " switch (psymb)\n"); 1810268899Sbapt putl_code(code_file, " {\n"); 1811268899Sbapt for (i = 2; i < nsyms; ++i) 1812234949Sbapt { 1813268899Sbapt if ((destructor_code = symbol_destructor[i]) != NULL) 1814268899Sbapt { 1815268899Sbapt ++outline; 1816268899Sbapt fprintf(code_file, "\tcase %d:\n", symbol_pval[i]); 1817268899Sbapt /* comprehend the number of lines in the destructor code */ 1818268899Sbapt for (s = destructor_code; (s = strchr(s, '\n')) != NULL; s++) 1819268899Sbapt ++outline; 1820268899Sbapt puts_code(code_file, destructor_code); 1821268899Sbapt putc_code(code_file, '\n'); 1822268899Sbapt putl_code(code_file, "\tbreak;\n"); 1823268899Sbapt write_code_lineno(code_file); 1824268899Sbapt FREE(destructor_code); 1825268899Sbapt } 1826234949Sbapt } 1827268899Sbapt putl_code(code_file, " }\n"); 1828268899Sbapt putl_code(code_file, "}\n"); 1829268899Sbapt putl_code(code_file, "#define YYDESTRUCT_IS_DECLARED 1\n"); 1830268899Sbapt putl_code(code_file, "#endif\n"); 1831268899Sbapt 1832268899Sbapt DO_FREE(symbol_destructor); 1833234949Sbapt} 1834268899Sbapt#endif 1835234949Sbapt 1836234949Sbaptstatic void 1837234949Sbaptfree_itemsets(void) 1838234949Sbapt{ 1839234949Sbapt core *cp, *next; 1840234949Sbapt 1841234949Sbapt FREE(state_table); 1842234949Sbapt for (cp = first_state; cp; cp = next) 1843234949Sbapt { 1844234949Sbapt next = cp->next; 1845234949Sbapt FREE(cp); 1846234949Sbapt } 1847234949Sbapt} 1848234949Sbapt 1849234949Sbaptstatic void 1850234949Sbaptfree_shifts(void) 1851234949Sbapt{ 1852234949Sbapt shifts *sp, *next; 1853234949Sbapt 1854234949Sbapt FREE(shift_table); 1855234949Sbapt for (sp = first_shift; sp; sp = next) 1856234949Sbapt { 1857234949Sbapt next = sp->next; 1858234949Sbapt FREE(sp); 1859234949Sbapt } 1860234949Sbapt} 1861234949Sbapt 1862234949Sbaptstatic void 1863234949Sbaptfree_reductions(void) 1864234949Sbapt{ 1865234949Sbapt reductions *rp, *next; 1866234949Sbapt 1867234949Sbapt FREE(reduction_table); 1868234949Sbapt for (rp = first_reduction; rp; rp = next) 1869234949Sbapt { 1870234949Sbapt next = rp->next; 1871234949Sbapt FREE(rp); 1872234949Sbapt } 1873234949Sbapt} 1874234949Sbapt 1875234949Sbaptstatic void 1876234949Sbaptoutput_externs(FILE * fp, const char *const section[]) 1877234949Sbapt{ 1878234949Sbapt int i; 1879234949Sbapt const char *s; 1880234949Sbapt 1881234949Sbapt for (i = 0; (s = section[i]) != 0; ++i) 1882234949Sbapt { 1883268899Sbapt /* prefix non-blank lines that don't start with 1884268899Sbapt C pre-processor directives with 'extern ' */ 1885268899Sbapt if (*s && (*s != '#')) 1886234949Sbapt fputs("extern\t", fp); 1887234949Sbapt if (fp == code_file) 1888234949Sbapt ++outline; 1889268899Sbapt fprintf(fp, "%s\n", s); 1890234949Sbapt } 1891234949Sbapt} 1892234949Sbapt 1893234949Sbaptvoid 1894234949Sbaptoutput(void) 1895234949Sbapt{ 1896234949Sbapt FILE *fp; 1897234949Sbapt 1898234949Sbapt free_itemsets(); 1899234949Sbapt free_shifts(); 1900234949Sbapt free_reductions(); 1901234949Sbapt 1902268899Sbapt#if defined(YYBTYACC) 1903268899Sbapt output_backtracking_parser(output_file); 1904268899Sbapt if (rflag) 1905268899Sbapt output_backtracking_parser(code_file); 1906268899Sbapt#endif 1907268899Sbapt 1908234949Sbapt if (iflag) 1909234949Sbapt { 1910268899Sbapt write_code_lineno(code_file); 1911234949Sbapt ++outline; 1912234949Sbapt fprintf(code_file, "#include \"%s\"\n", externs_file_name); 1913234949Sbapt fp = externs_file; 1914234949Sbapt } 1915234949Sbapt else 1916234949Sbapt fp = code_file; 1917234949Sbapt 1918268899Sbapt output_prefix(fp); 1919234949Sbapt output_pure_parser(fp); 1920234949Sbapt output_stored_text(fp); 1921234949Sbapt output_stype(fp); 1922268899Sbapt#if defined(YYBTYACC) 1923268899Sbapt if (locations) 1924268899Sbapt output_ltype(fp); 1925268899Sbapt#endif 1926234949Sbapt output_parse_decl(fp); 1927234949Sbapt output_lex_decl(fp); 1928234949Sbapt output_error_decl(fp); 1929268899Sbapt#if defined(YYBTYACC) 1930268899Sbapt if (destructor) 1931268899Sbapt output_yydestruct_decl(fp); 1932268899Sbapt#endif 1933268899Sbapt if (iflag || !rflag) 1934268899Sbapt { 1935268899Sbapt write_section(fp, xdecls); 1936268899Sbapt } 1937234949Sbapt 1938234949Sbapt if (iflag) 1939234949Sbapt { 1940234949Sbapt output_externs(externs_file, global_vars); 1941234949Sbapt if (!pure_parser) 1942234949Sbapt output_externs(externs_file, impure_vars); 1943234949Sbapt } 1944234949Sbapt 1945234949Sbapt if (iflag) 1946234949Sbapt { 1947251143Sbapt if (dflag) 1948251143Sbapt { 1949251143Sbapt ++outline; 1950251143Sbapt fprintf(code_file, "#include \"%s\"\n", defines_file_name); 1951251143Sbapt } 1952251143Sbapt else 1953234949Sbapt output_defines(externs_file); 1954234949Sbapt } 1955234949Sbapt else 1956234949Sbapt { 1957234949Sbapt putc_code(code_file, '\n'); 1958234949Sbapt output_defines(code_file); 1959234949Sbapt } 1960234949Sbapt 1961234949Sbapt if (dflag) 1962268899Sbapt { 1963268899Sbapt start_defines_file(); 1964234949Sbapt output_defines(defines_file); 1965268899Sbapt end_defines_file(); 1966268899Sbapt } 1967234949Sbapt 1968234949Sbapt output_rule_data(); 1969234949Sbapt output_yydefred(); 1970268899Sbapt#if defined(YYBTYACC) 1971268899Sbapt output_accessing_symbols(); 1972268899Sbapt#endif 1973234949Sbapt output_actions(); 1974234949Sbapt free_parser(); 1975234949Sbapt output_debug(); 1976234949Sbapt if (rflag) 1977234949Sbapt { 1978234949Sbapt write_section(code_file, xdecls); 1979268899Sbapt output_YYINT_typedef(code_file); 1980234949Sbapt write_section(code_file, tables); 1981234949Sbapt } 1982234949Sbapt write_section(code_file, global_vars); 1983234949Sbapt if (!pure_parser) 1984234949Sbapt { 1985234949Sbapt write_section(code_file, impure_vars); 1986234949Sbapt } 1987234949Sbapt write_section(code_file, hdr_defs); 1988234949Sbapt if (!pure_parser) 1989234949Sbapt { 1990234949Sbapt write_section(code_file, hdr_vars); 1991234949Sbapt } 1992234949Sbapt output_trailing_text(); 1993268899Sbapt#if defined(YYBTYACC) 1994268899Sbapt if (destructor) 1995268899Sbapt output_yydestruct_impl(); 1996268899Sbapt#endif 1997234949Sbapt write_section(code_file, body_1); 1998234949Sbapt if (pure_parser) 1999234949Sbapt { 2000234949Sbapt write_section(code_file, body_vars); 2001234949Sbapt } 2002234949Sbapt write_section(code_file, body_2); 2003234949Sbapt output_semantic_actions(); 2004234949Sbapt write_section(code_file, trailer); 2005234949Sbapt} 2006234949Sbapt 2007234949Sbapt#ifdef NO_LEAKS 2008234949Sbaptvoid 2009234949Sbaptoutput_leaks(void) 2010234949Sbapt{ 2011234949Sbapt DO_FREE(tally); 2012234949Sbapt DO_FREE(width); 2013234949Sbapt DO_FREE(order); 2014234949Sbapt} 2015234949Sbapt#endif 2016