00001 #include "defs.h"
00002
00003 static int nvectors;
00004 static int nentries;
00005 static short **froms;
00006 static short **tos;
00007 static short *tally;
00008 static short *width;
00009 static short *state_count;
00010 static short *order;
00011 static short *base;
00012 static short *pos;
00013 static int maxtable;
00014 static short *table;
00015 static short *check;
00016 static int lowzero;
00017 static int high;
00018
00019
00020 output()
00021 {
00022 free_itemsets();
00023 free_shifts();
00024 free_reductions();
00025 output_prefix();
00026 output_stored_text();
00027 output_defines();
00028 output_rule_data();
00029 output_yydefred();
00030 output_actions();
00031 free_parser();
00032 output_debug();
00033 output_stype();
00034 if (rflag) write_section(tables);
00035 write_section(header);
00036 output_trailing_text();
00037 write_section(body);
00038 output_semantic_actions();
00039 write_section(trailer);
00040 }
00041
00042
00043 output_prefix()
00044 {
00045 if (symbol_prefix == NULL)
00046 symbol_prefix = "yy";
00047 else
00048 {
00049 ++outline;
00050 fprintf(code_file, "#define yyparse %sparse\n", symbol_prefix);
00051 ++outline;
00052 fprintf(code_file, "#define yylex %slex\n", symbol_prefix);
00053 ++outline;
00054 fprintf(code_file, "#define yyerror %serror\n", symbol_prefix);
00055 ++outline;
00056 fprintf(code_file, "#define yychar %schar\n", symbol_prefix);
00057 ++outline;
00058 fprintf(code_file, "#define yyval %sval\n", symbol_prefix);
00059 ++outline;
00060 fprintf(code_file, "#define yylval %slval\n", symbol_prefix);
00061 ++outline;
00062 fprintf(code_file, "#define yydebug %sdebug\n", symbol_prefix);
00063 ++outline;
00064 fprintf(code_file, "#define yynerrs %snerrs\n", symbol_prefix);
00065 ++outline;
00066 fprintf(code_file, "#define yyerrflag %serrflag\n", symbol_prefix);
00067 ++outline;
00068 fprintf(code_file, "#define yyss %sss\n", symbol_prefix);
00069 ++outline;
00070 fprintf(code_file, "#define yyssp %sssp\n", symbol_prefix);
00071 ++outline;
00072 fprintf(code_file, "#define yyvs %svs\n", symbol_prefix);
00073 ++outline;
00074 fprintf(code_file, "#define yyvsp %svsp\n", symbol_prefix);
00075 ++outline;
00076 fprintf(code_file, "#define yylhs %slhs\n", symbol_prefix);
00077 ++outline;
00078 fprintf(code_file, "#define yylen %slen\n", symbol_prefix);
00079 ++outline;
00080 fprintf(code_file, "#define yydefred %sdefred\n", symbol_prefix);
00081 ++outline;
00082 fprintf(code_file, "#define yydgoto %sdgoto\n", symbol_prefix);
00083 ++outline;
00084 fprintf(code_file, "#define yysindex %ssindex\n", symbol_prefix);
00085 ++outline;
00086 fprintf(code_file, "#define yyrindex %srindex\n", symbol_prefix);
00087 ++outline;
00088 fprintf(code_file, "#define yygindex %sgindex\n", symbol_prefix);
00089 ++outline;
00090 fprintf(code_file, "#define yytable %stable\n", symbol_prefix);
00091 ++outline;
00092 fprintf(code_file, "#define yycheck %scheck\n", symbol_prefix);
00093 ++outline;
00094 fprintf(code_file, "#define yyname %sname\n", symbol_prefix);
00095 ++outline;
00096 fprintf(code_file, "#define yyrule %srule\n", symbol_prefix);
00097 }
00098 ++outline;
00099 fprintf(code_file, "#define YYPREFIX \"%s\"\n", symbol_prefix);
00100 }
00101
00102
00103 output_rule_data()
00104 {
00105 register int i;
00106 register int j;
00107
00108
00109 fprintf(output_file, "short %slhs[] = {%42d,", symbol_prefix,
00110 symbol_value[start_symbol]);
00111
00112 j = 10;
00113 for (i = 3; i < nrules; i++)
00114 {
00115 if (j >= 10)
00116 {
00117 if (!rflag) ++outline;
00118 putc('\n', output_file);
00119 j = 1;
00120 }
00121 else
00122 ++j;
00123
00124 fprintf(output_file, "%5d,", symbol_value[rlhs[i]]);
00125 }
00126 if (!rflag) outline += 2;
00127 fprintf(output_file, "\n};\n");
00128
00129 fprintf(output_file, "short %slen[] = {%42d,", symbol_prefix, 2);
00130
00131 j = 10;
00132 for (i = 3; i < nrules; i++)
00133 {
00134 if (j >= 10)
00135 {
00136 if (!rflag) ++outline;
00137 putc('\n', output_file);
00138 j = 1;
00139 }
00140 else
00141 j++;
00142
00143 fprintf(output_file, "%5d,", rrhs[i + 1] - rrhs[i] - 1);
00144 }
00145 if (!rflag) outline += 2;
00146 fprintf(output_file, "\n};\n");
00147 }
00148
00149
00150 output_yydefred()
00151 {
00152 register int i, j;
00153
00154 fprintf(output_file, "short %sdefred[] = {%39d,", symbol_prefix,
00155 (defred[0] ? defred[0] - 2 : 0));
00156
00157 j = 10;
00158 for (i = 1; i < nstates; i++)
00159 {
00160 if (j < 10)
00161 ++j;
00162 else
00163 {
00164 if (!rflag) ++outline;
00165 putc('\n', output_file);
00166 j = 1;
00167 }
00168
00169 fprintf(output_file, "%5d,", (defred[i] ? defred[i] - 2 : 0));
00170 }
00171
00172 if (!rflag) outline += 2;
00173 fprintf(output_file, "\n};\n");
00174 }
00175
00176
00177 output_actions()
00178 {
00179 nvectors = 2*nstates + nvars;
00180
00181 froms = NEW2(nvectors, short *);
00182 tos = NEW2(nvectors, short *);
00183 tally = NEW2(nvectors, short);
00184 width = NEW2(nvectors, short);
00185
00186 token_actions();
00187 FREE(lookaheads);
00188 FREE(LA);
00189 FREE(LAruleno);
00190 FREE(accessing_symbol);
00191
00192 goto_actions();
00193 FREE(goto_map + ntokens);
00194 FREE(from_state);
00195 FREE(to_state);
00196
00197 sort_actions();
00198 pack_table();
00199 output_base();
00200 output_table();
00201 output_check();
00202 }
00203
00204
00205 token_actions()
00206 {
00207 register int i, j;
00208 register int shiftcount, reducecount;
00209 register int max, min;
00210 register short *actionrow, *r, *s;
00211 register action *p;
00212
00213 actionrow = NEW2(2*ntokens, short);
00214 for (i = 0; i < nstates; ++i)
00215 {
00216 if (parser[i])
00217 {
00218 for (j = 0; j < 2*ntokens; ++j)
00219 actionrow[j] = 0;
00220
00221 shiftcount = 0;
00222 reducecount = 0;
00223 for (p = parser[i]; p; p = p->next)
00224 {
00225 if (p->suppressed == 0)
00226 {
00227 if (p->action_code == SHIFT)
00228 {
00229 ++shiftcount;
00230 actionrow[p->symbol] = p->number;
00231 }
00232 else if (p->action_code == REDUCE && p->number != defred[i])
00233 {
00234 ++reducecount;
00235 actionrow[p->symbol + ntokens] = p->number;
00236 }
00237 }
00238 }
00239
00240 tally[i] = shiftcount;
00241 tally[nstates+i] = reducecount;
00242 width[i] = 0;
00243 width[nstates+i] = 0;
00244 if (shiftcount > 0)
00245 {
00246 froms[i] = r = NEW2(shiftcount, short);
00247 tos[i] = s = NEW2(shiftcount, short);
00248 min = MAXSHORT;
00249 max = 0;
00250 for (j = 0; j < ntokens; ++j)
00251 {
00252 if (actionrow[j])
00253 {
00254 if (min > symbol_value[j])
00255 min = symbol_value[j];
00256 if (max < symbol_value[j])
00257 max = symbol_value[j];
00258 *r++ = symbol_value[j];
00259 *s++ = actionrow[j];
00260 }
00261 }
00262 width[i] = max - min + 1;
00263 }
00264 if (reducecount > 0)
00265 {
00266 froms[nstates+i] = r = NEW2(reducecount, short);
00267 tos[nstates+i] = s = NEW2(reducecount, short);
00268 min = MAXSHORT;
00269 max = 0;
00270 for (j = 0; j < ntokens; ++j)
00271 {
00272 if (actionrow[ntokens+j])
00273 {
00274 if (min > symbol_value[j])
00275 min = symbol_value[j];
00276 if (max < symbol_value[j])
00277 max = symbol_value[j];
00278 *r++ = symbol_value[j];
00279 *s++ = actionrow[ntokens+j] - 2;
00280 }
00281 }
00282 width[nstates+i] = max - min + 1;
00283 }
00284 }
00285 }
00286 FREE(actionrow);
00287 }
00288
00289 goto_actions()
00290 {
00291 register int i, j, k;
00292
00293 state_count = NEW2(nstates, short);
00294
00295 k = default_goto(start_symbol + 1);
00296 fprintf(output_file, "short %sdgoto[] = {%40d,", symbol_prefix, k);
00297 save_column(start_symbol + 1, k);
00298
00299 j = 10;
00300 for (i = start_symbol + 2; i < nsyms; i++)
00301 {
00302 if (j >= 10)
00303 {
00304 if (!rflag) ++outline;
00305 putc('\n', output_file);
00306 j = 1;
00307 }
00308 else
00309 ++j;
00310
00311 k = default_goto(i);
00312 fprintf(output_file, "%5d,", k);
00313 save_column(i, k);
00314 }
00315
00316 if (!rflag) outline += 2;
00317 fprintf(output_file, "\n};\n");
00318 FREE(state_count);
00319 }
00320
00321 int
00322 default_goto(symbol)
00323 int symbol;
00324 {
00325 register int i;
00326 register int m;
00327 register int n;
00328 register int default_state;
00329 register int max;
00330
00331 m = goto_map[symbol];
00332 n = goto_map[symbol + 1];
00333
00334 if (m == n) return (0);
00335
00336 for (i = 0; i < nstates; i++)
00337 state_count[i] = 0;
00338
00339 for (i = m; i < n; i++)
00340 state_count[to_state[i]]++;
00341
00342 max = 0;
00343 default_state = 0;
00344 for (i = 0; i < nstates; i++)
00345 {
00346 if (state_count[i] > max)
00347 {
00348 max = state_count[i];
00349 default_state = i;
00350 }
00351 }
00352
00353 return (default_state);
00354 }
00355
00356
00357
00358 save_column(symbol, default_state)
00359 int symbol;
00360 int default_state;
00361 {
00362 register int i;
00363 register int m;
00364 register int n;
00365 register short *sp;
00366 register short *sp1;
00367 register short *sp2;
00368 register int count;
00369 register int symno;
00370
00371 m = goto_map[symbol];
00372 n = goto_map[symbol + 1];
00373
00374 count = 0;
00375 for (i = m; i < n; i++)
00376 {
00377 if (to_state[i] != default_state)
00378 ++count;
00379 }
00380 if (count == 0) return;
00381
00382 symno = symbol_value[symbol] + 2*nstates;
00383
00384 froms[symno] = sp1 = sp = NEW2(count, short);
00385 tos[symno] = sp2 = NEW2(count, short);
00386
00387 for (i = m; i < n; i++)
00388 {
00389 if (to_state[i] != default_state)
00390 {
00391 *sp1++ = from_state[i];
00392 *sp2++ = to_state[i];
00393 }
00394 }
00395
00396 tally[symno] = count;
00397 width[symno] = sp1[-1] - sp[0] + 1;
00398 }
00399
00400 sort_actions()
00401 {
00402 register int i;
00403 register int j;
00404 register int k;
00405 register int t;
00406 register int w;
00407
00408 order = NEW2(nvectors, short);
00409 nentries = 0;
00410
00411 for (i = 0; i < nvectors; i++)
00412 {
00413 if (tally[i] > 0)
00414 {
00415 t = tally[i];
00416 w = width[i];
00417 j = nentries - 1;
00418
00419 while (j >= 0 && (width[order[j]] < w))
00420 j--;
00421
00422 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
00423 j--;
00424
00425 for (k = nentries - 1; k > j; k--)
00426 order[k + 1] = order[k];
00427
00428 order[j + 1] = i;
00429 nentries++;
00430 }
00431 }
00432 }
00433
00434
00435 pack_table()
00436 {
00437 register int i;
00438 register int place;
00439 register int state;
00440
00441 base = NEW2(nvectors, short);
00442 pos = NEW2(nentries, short);
00443
00444 maxtable = BITS_PER_WORD == 16 ? 400 : 1000;
00445 table = NEW2(maxtable, short);
00446 check = NEW2(maxtable, short);
00447
00448 lowzero = 0;
00449 high = 0;
00450
00451 for (i = 0; i < maxtable; i++)
00452 check[i] = -1;
00453
00454 for (i = 0; i < nentries; i++)
00455 {
00456 state = matching_vector(i);
00457
00458 if (state < 0)
00459 place = pack_vector(i);
00460 else
00461 place = base[state];
00462
00463 pos[i] = place;
00464 base[order[i]] = place;
00465 }
00466
00467 for (i = 0; i < nvectors; i++)
00468 {
00469 if (froms[i])
00470 FREE(froms[i]);
00471 if (tos[i])
00472 FREE(tos[i]);
00473 }
00474
00475 FREE(froms);
00476 FREE(tos);
00477 FREE(pos);
00478 }
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497 int
00498 matching_vector(vector)
00499 int vector;
00500 {
00501 register int i;
00502 register int j;
00503 register int k;
00504 register int t;
00505 register int w;
00506 register int match;
00507 register int prev;
00508
00509 i = order[vector];
00510 if (i >= 2*nstates)
00511 return (-1);
00512
00513 t = tally[i];
00514 w = width[i];
00515
00516 for (prev = vector - 1; prev >= 0; prev--)
00517 {
00518 j = order[prev];
00519 if (width[j] != w || tally[j] != t)
00520 return (-1);
00521
00522 match = 1;
00523 for (k = 0; match && k < t; k++)
00524 {
00525 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
00526 match = 0;
00527 }
00528
00529 if (match)
00530 return (j);
00531 }
00532
00533 return (-1);
00534 }
00535
00536
00537
00538 int
00539 pack_vector(vector)
00540 int vector;
00541 {
00542 register int i, j, k, l;
00543 register int t;
00544 register int loc;
00545 register int ok;
00546 register short *from;
00547 register short *to;
00548 int newmax;
00549
00550 i = order[vector];
00551 t = tally[i];
00552 assert(t);
00553
00554 from = froms[i];
00555 to = tos[i];
00556
00557 j = lowzero - from[0];
00558 for (k = 1; k < t; ++k)
00559 if (lowzero - from[k] > j)
00560 j = lowzero - from[k];
00561 for (;; ++j)
00562 {
00563 if (j == 0)
00564 continue;
00565 ok = 1;
00566 for (k = 0; ok && k < t; k++)
00567 {
00568 loc = j + from[k];
00569 if (loc >= maxtable)
00570 {
00571 if (loc >= MAXTABLE)
00572 fatal("maximum table size exceeded");
00573
00574 newmax = maxtable;
00575 do { newmax += 200; } while (newmax <= loc);
00576 table = (short *) REALLOC(table, newmax*sizeof(short));
00577 if (table == 0) no_space();
00578 check = (short *) REALLOC(check, newmax*sizeof(short));
00579 if (check == 0) no_space();
00580 for (l = maxtable; l < newmax; ++l)
00581 {
00582 table[l] = 0;
00583 check[l] = -1;
00584 }
00585 maxtable = newmax;
00586 }
00587
00588 if (check[loc] != -1)
00589 ok = 0;
00590 }
00591 for (k = 0; ok && k < vector; k++)
00592 {
00593 if (pos[k] == j)
00594 ok = 0;
00595 }
00596 if (ok)
00597 {
00598 for (k = 0; k < t; k++)
00599 {
00600 loc = j + from[k];
00601 table[loc] = to[k];
00602 check[loc] = from[k];
00603 if (loc > high) high = loc;
00604 }
00605
00606 while (check[lowzero] != -1)
00607 ++lowzero;
00608
00609 return (j);
00610 }
00611 }
00612 }
00613
00614
00615
00616 output_base()
00617 {
00618 register int i, j;
00619
00620 fprintf(output_file, "short %ssindex[] = {%39d,", symbol_prefix, base[0]);
00621
00622 j = 10;
00623 for (i = 1; i < nstates; i++)
00624 {
00625 if (j >= 10)
00626 {
00627 if (!rflag) ++outline;
00628 putc('\n', output_file);
00629 j = 1;
00630 }
00631 else
00632 ++j;
00633
00634 fprintf(output_file, "%5d,", base[i]);
00635 }
00636
00637 if (!rflag) outline += 2;
00638 fprintf(output_file, "\n};\nshort %srindex[] = {%39d,", symbol_prefix,
00639 base[nstates]);
00640
00641 j = 10;
00642 for (i = nstates + 1; i < 2*nstates; i++)
00643 {
00644 if (j >= 10)
00645 {
00646 if (!rflag) ++outline;
00647 putc('\n', output_file);
00648 j = 1;
00649 }
00650 else
00651 ++j;
00652
00653 fprintf(output_file, "%5d,", base[i]);
00654 }
00655
00656 if (!rflag) outline += 2;
00657 fprintf(output_file, "\n};\nshort %sgindex[] = {%39d,", symbol_prefix,
00658 base[2*nstates]);
00659
00660 j = 10;
00661 for (i = 2*nstates + 1; i < nvectors - 1; i++)
00662 {
00663 if (j >= 10)
00664 {
00665 if (!rflag) ++outline;
00666 putc('\n', output_file);
00667 j = 1;
00668 }
00669 else
00670 ++j;
00671
00672 fprintf(output_file, "%5d,", base[i]);
00673 }
00674
00675 if (!rflag) outline += 2;
00676 fprintf(output_file, "\n};\n");
00677 FREE(base);
00678 }
00679
00680
00681
00682 output_table()
00683 {
00684 register int i;
00685 register int j;
00686
00687 ++outline;
00688 fprintf(code_file, "#define YYTABLESIZE %d\n", high);
00689 fprintf(output_file, "short %stable[] = {%40d,", symbol_prefix,
00690 table[0]);
00691
00692 j = 10;
00693 for (i = 1; i <= high; i++)
00694 {
00695 if (j >= 10)
00696 {
00697 if (!rflag) ++outline;
00698 putc('\n', output_file);
00699 j = 1;
00700 }
00701 else
00702 ++j;
00703
00704 fprintf(output_file, "%5d,", table[i]);
00705 }
00706
00707 if (!rflag) outline += 2;
00708 fprintf(output_file, "\n};\n");
00709 FREE(table);
00710 }
00711
00712
00713
00714 output_check()
00715 {
00716 register int i;
00717 register int j;
00718
00719 fprintf(output_file, "short %scheck[] = {%40d,", symbol_prefix,
00720 check[0]);
00721
00722 j = 10;
00723 for (i = 1; i <= high; i++)
00724 {
00725 if (j >= 10)
00726 {
00727 if (!rflag) ++outline;
00728 putc('\n', output_file);
00729 j = 1;
00730 }
00731 else
00732 ++j;
00733
00734 fprintf(output_file, "%5d,", check[i]);
00735 }
00736
00737 if (!rflag) outline += 2;
00738 fprintf(output_file, "\n};\n");
00739 FREE(check);
00740 }
00741
00742
00743 int
00744 is_C_identifier(name)
00745 char *name;
00746 {
00747 register char *s;
00748 register int c;
00749
00750 s = name;
00751 c = *s;
00752 if (c == '"')
00753 {
00754 c = *++s;
00755 if (!isalpha(c) && c != '_' && c != '$')
00756 return (0);
00757 while ((c = *++s) != '"')
00758 {
00759 if (!isalnum(c) && c != '_' && c != '$')
00760 return (0);
00761 }
00762 return (1);
00763 }
00764
00765 if (!isalpha(c) && c != '_' && c != '$')
00766 return (0);
00767 while (c = *++s)
00768 {
00769 if (!isalnum(c) && c != '_' && c != '$')
00770 return (0);
00771 }
00772 return (1);
00773 }
00774
00775
00776 output_defines()
00777 {
00778 register int c, i;
00779 register char *s;
00780
00781 for (i = 2; i < ntokens; ++i)
00782 {
00783 s = symbol_name[i];
00784 if (is_C_identifier(s))
00785 {
00786 fprintf(code_file, "#define ");
00787 if (dflag) fprintf(defines_file, "#define ");
00788 c = *s;
00789 if (c == '"')
00790 {
00791 while ((c = *++s) != '"')
00792 {
00793 putc(c, code_file);
00794 if (dflag) putc(c, defines_file);
00795 }
00796 }
00797 else
00798 {
00799 do
00800 {
00801 putc(c, code_file);
00802 if (dflag) putc(c, defines_file);
00803 }
00804 while (c = *++s);
00805 }
00806 ++outline;
00807 fprintf(code_file, " %d\n", symbol_value[i]);
00808 if (dflag) fprintf(defines_file, " %d\n", symbol_value[i]);
00809 }
00810 }
00811
00812 ++outline;
00813 fprintf(code_file, "#define YYERRCODE %d\n", symbol_value[1]);
00814
00815 if (dflag && unionized)
00816 {
00817 fclose(union_file);
00818 union_file = fopen(union_file_name, "r");
00819 if (union_file == NULL) open_error(union_file_name);
00820 while ((c = getc(union_file)) != EOF)
00821 putc(c, defines_file);
00822 fprintf(defines_file, " YYSTYPE;\nextern YYSTYPE %slval;\n",
00823 symbol_prefix);
00824 }
00825 }
00826
00827
00828 output_stored_text()
00829 {
00830 register int c;
00831 register FILE *in, *out;
00832
00833 fclose(text_file);
00834 text_file = fopen(text_file_name, "r");
00835 if (text_file == NULL)
00836 open_error(text_file_name);
00837 in = text_file;
00838 if ((c = getc(in)) == EOF)
00839 return;
00840 out = code_file;
00841 if (c == '\n')
00842 ++outline;
00843 putc(c, out);
00844 while ((c = getc(in)) != EOF)
00845 {
00846 if (c == '\n')
00847 ++outline;
00848 putc(c, out);
00849 }
00850 if (!lflag)
00851 fprintf(out, line_format, ++outline + 1, code_file_name);
00852 }
00853
00854
00855 output_debug()
00856 {
00857 register int i, j, k, max;
00858 char **symnam, *s;
00859
00860 ++outline;
00861 fprintf(code_file, "#define YYFINAL %d\n", final_state);
00862 outline += 3;
00863 fprintf(code_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
00864 tflag);
00865 if (rflag)
00866 fprintf(output_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
00867 tflag);
00868
00869 max = 0;
00870 for (i = 2; i < ntokens; ++i)
00871 if (symbol_value[i] > max)
00872 max = symbol_value[i];
00873 ++outline;
00874 fprintf(code_file, "#define YYMAXTOKEN %d\n", max);
00875
00876 symnam = (char **) MALLOC((max+1)*sizeof(char *));
00877 if (symnam == 0) no_space();
00878
00879
00880
00881 for (i = 0; i < max; ++i)
00882 symnam[i] = 0;
00883 for (i = ntokens - 1; i >= 2; --i)
00884 symnam[symbol_value[i]] = symbol_name[i];
00885 symnam[0] = "end-of-file";
00886
00887 if (!rflag) ++outline;
00888 fprintf(output_file, "#if YYDEBUG\nchar *%sname[] = {", symbol_prefix);
00889 j = 80;
00890 for (i = 0; i <= max; ++i)
00891 {
00892 if (s = symnam[i])
00893 {
00894 if (s[0] == '"')
00895 {
00896 k = 7;
00897 while (*++s != '"')
00898 {
00899 ++k;
00900 if (*s == '\\')
00901 {
00902 k += 2;
00903 if (*++s == '\\')
00904 ++k;
00905 }
00906 }
00907 j += k;
00908 if (j > 80)
00909 {
00910 if (!rflag) ++outline;
00911 putc('\n', output_file);
00912 j = k;
00913 }
00914 fprintf(output_file, "\"\\\"");
00915 s = symnam[i];
00916 while (*++s != '"')
00917 {
00918 if (*s == '\\')
00919 {
00920 fprintf(output_file, "\\\\");
00921 if (*++s == '\\')
00922 fprintf(output_file, "\\\\");
00923 else
00924 putc(*s, output_file);
00925 }
00926 else
00927 putc(*s, output_file);
00928 }
00929 fprintf(output_file, "\\\"\",");
00930 }
00931 else if (s[0] == '\'')
00932 {
00933 if (s[1] == '"')
00934 {
00935 j += 7;
00936 if (j > 80)
00937 {
00938 if (!rflag) ++outline;
00939 putc('\n', output_file);
00940 j = 7;
00941 }
00942 fprintf(output_file, "\"'\\\"'\",");
00943 }
00944 else
00945 {
00946 k = 5;
00947 while (*++s != '\'')
00948 {
00949 ++k;
00950 if (*s == '\\')
00951 {
00952 k += 2;
00953 if (*++s == '\\')
00954 ++k;
00955 }
00956 }
00957 j += k;
00958 if (j > 80)
00959 {
00960 if (!rflag) ++outline;
00961 putc('\n', output_file);
00962 j = k;
00963 }
00964 fprintf(output_file, "\"'");
00965 s = symnam[i];
00966 while (*++s != '\'')
00967 {
00968 if (*s == '\\')
00969 {
00970 fprintf(output_file, "\\\\");
00971 if (*++s == '\\')
00972 fprintf(output_file, "\\\\");
00973 else
00974 putc(*s, output_file);
00975 }
00976 else
00977 putc(*s, output_file);
00978 }
00979 fprintf(output_file, "'\",");
00980 }
00981 }
00982 else
00983 {
00984 k = strlen(s) + 3;
00985 j += k;
00986 if (j > 80)
00987 {
00988 if (!rflag) ++outline;
00989 putc('\n', output_file);
00990 j = k;
00991 }
00992 putc('"', output_file);
00993 do { putc(*s, output_file); } while (*++s);
00994 fprintf(output_file, "\",");
00995 }
00996 }
00997 else
00998 {
00999 j += 2;
01000 if (j > 80)
01001 {
01002 if (!rflag) ++outline;
01003 putc('\n', output_file);
01004 j = 2;
01005 }
01006 fprintf(output_file, "0,");
01007 }
01008 }
01009 if (!rflag) outline += 2;
01010 fprintf(output_file, "\n};\n");
01011 FREE(symnam);
01012
01013 if (!rflag) ++outline;
01014 fprintf(output_file, "char *%srule[] = {\n", symbol_prefix);
01015 for (i = 2; i < nrules; ++i)
01016 {
01017 fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
01018 for (j = rrhs[i]; ritem[j] > 0; ++j)
01019 {
01020 s = symbol_name[ritem[j]];
01021 if (s[0] == '"')
01022 {
01023 fprintf(output_file, " \\\"");
01024 while (*++s != '"')
01025 {
01026 if (*s == '\\')
01027 {
01028 if (s[1] == '\\')
01029 fprintf(output_file, "\\\\\\\\");
01030 else
01031 fprintf(output_file, "\\\\%c", s[1]);
01032 ++s;
01033 }
01034 else
01035 putc(*s, output_file);
01036 }
01037 fprintf(output_file, "\\\"");
01038 }
01039 else if (s[0] == '\'')
01040 {
01041 if (s[1] == '"')
01042 fprintf(output_file, " '\\\"'");
01043 else if (s[1] == '\\')
01044 {
01045 if (s[2] == '\\')
01046 fprintf(output_file, " '\\\\\\\\");
01047 else
01048 fprintf(output_file, " '\\\\%c", s[2]);
01049 s += 2;
01050 while (*++s != '\'')
01051 putc(*s, output_file);
01052 putc('\'', output_file);
01053 }
01054 else
01055 fprintf(output_file, " '%c'", s[1]);
01056 }
01057 else
01058 fprintf(output_file, " %s", s);
01059 }
01060 if (!rflag) ++outline;
01061 fprintf(output_file, "\",\n");
01062 }
01063
01064 if (!rflag) outline += 2;
01065 fprintf(output_file, "};\n#endif\n");
01066 }
01067
01068
01069 output_stype()
01070 {
01071 if (!unionized && ntags == 0)
01072 {
01073 outline += 3;
01074 fprintf(code_file, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
01075 }
01076 }
01077
01078
01079 output_trailing_text()
01080 {
01081 register int c, last;
01082 register FILE *in, *out;
01083
01084 if (line == 0)
01085 return;
01086
01087 in = input_file;
01088 out = code_file;
01089 c = *cptr;
01090 if (c == '\n')
01091 {
01092 ++lineno;
01093 if ((c = getc(in)) == EOF)
01094 return;
01095 if (!lflag)
01096 {
01097 ++outline;
01098 fprintf(out, line_format, lineno, input_file_name);
01099 }
01100 if (c == '\n')
01101 ++outline;
01102 putc(c, out);
01103 last = c;
01104 }
01105 else
01106 {
01107 if (!lflag)
01108 {
01109 ++outline;
01110 fprintf(out, line_format, lineno, input_file_name);
01111 }
01112 do { putc(c, out); } while ((c = *++cptr) != '\n');
01113 ++outline;
01114 putc('\n', out);
01115 last = '\n';
01116 }
01117
01118 while ((c = getc(in)) != EOF)
01119 {
01120 if (c == '\n')
01121 ++outline;
01122 putc(c, out);
01123 last = c;
01124 }
01125
01126 if (last != '\n')
01127 {
01128 ++outline;
01129 putc('\n', out);
01130 }
01131 if (!lflag)
01132 fprintf(out, line_format, ++outline + 1, code_file_name);
01133 }
01134
01135
01136 output_semantic_actions()
01137 {
01138 register int c, last;
01139 register FILE *out;
01140
01141 fclose(action_file);
01142 action_file = fopen(action_file_name, "r");
01143 if (action_file == NULL)
01144 open_error(action_file_name);
01145
01146 if ((c = getc(action_file)) == EOF)
01147 return;
01148
01149 out = code_file;
01150 last = c;
01151 if (c == '\n')
01152 ++outline;
01153 putc(c, out);
01154 while ((c = getc(action_file)) != EOF)
01155 {
01156 if (c == '\n')
01157 ++outline;
01158 putc(c, out);
01159 last = c;
01160 }
01161
01162 if (last != '\n')
01163 {
01164 ++outline;
01165 putc('\n', out);
01166 }
01167
01168 if (!lflag)
01169 fprintf(out, line_format, ++outline + 1, code_file_name);
01170 }
01171
01172
01173 free_itemsets()
01174 {
01175 register core *cp, *next;
01176
01177 FREE(state_table);
01178 for (cp = first_state; cp; cp = next)
01179 {
01180 next = cp->next;
01181 FREE(cp);
01182 }
01183 }
01184
01185
01186 free_shifts()
01187 {
01188 register shifts *sp, *next;
01189
01190 FREE(shift_table);
01191 for (sp = first_shift; sp; sp = next)
01192 {
01193 next = sp->next;
01194 FREE(sp);
01195 }
01196 }
01197
01198
01199
01200 free_reductions()
01201 {
01202 register reductions *rp, *next;
01203
01204 FREE(reduction_table);
01205 for (rp = first_reduction; rp; rp = next)
01206 {
01207 next = rp->next;
01208 FREE(rp);
01209 }
01210 }