improve behaviour under VPC, fixes from nicolas tittley.
[minix.git] / commands / byacc / output.c
blob6e4bb79c595638bdeb429643678013d316416672
1 #include "defs.h"
3 static int nvectors;
4 static int nentries;
5 static short **froms;
6 static short **tos;
7 static short *tally;
8 static short *width;
9 static short *state_count;
10 static short *order;
11 static short *base;
12 static short *pos;
13 static int maxtable;
14 static short *table;
15 static short *check;
16 static int lowzero;
17 static int high;
20 output()
22 free_itemsets();
23 free_shifts();
24 free_reductions();
25 output_prefix();
26 output_stored_text();
27 output_defines();
28 output_rule_data();
29 output_yydefred();
30 output_actions();
31 free_parser();
32 output_debug();
33 output_stype();
34 if (rflag) write_section(tables);
35 write_section(header);
36 output_trailing_text();
37 write_section(body);
38 output_semantic_actions();
39 write_section(trailer);
43 output_prefix()
45 if (symbol_prefix == NULL)
46 symbol_prefix = "yy";
47 else
49 ++outline;
50 fprintf(code_file, "#define yyparse %sparse\n", symbol_prefix);
51 ++outline;
52 fprintf(code_file, "#define yylex %slex\n", symbol_prefix);
53 ++outline;
54 fprintf(code_file, "#define yyerror %serror\n", symbol_prefix);
55 ++outline;
56 fprintf(code_file, "#define yychar %schar\n", symbol_prefix);
57 ++outline;
58 fprintf(code_file, "#define yyval %sval\n", symbol_prefix);
59 ++outline;
60 fprintf(code_file, "#define yylval %slval\n", symbol_prefix);
61 ++outline;
62 fprintf(code_file, "#define yydebug %sdebug\n", symbol_prefix);
63 ++outline;
64 fprintf(code_file, "#define yynerrs %snerrs\n", symbol_prefix);
65 ++outline;
66 fprintf(code_file, "#define yyerrflag %serrflag\n", symbol_prefix);
67 ++outline;
68 fprintf(code_file, "#define yyss %sss\n", symbol_prefix);
69 ++outline;
70 fprintf(code_file, "#define yyssp %sssp\n", symbol_prefix);
71 ++outline;
72 fprintf(code_file, "#define yyvs %svs\n", symbol_prefix);
73 ++outline;
74 fprintf(code_file, "#define yyvsp %svsp\n", symbol_prefix);
75 ++outline;
76 fprintf(code_file, "#define yylhs %slhs\n", symbol_prefix);
77 ++outline;
78 fprintf(code_file, "#define yylen %slen\n", symbol_prefix);
79 ++outline;
80 fprintf(code_file, "#define yydefred %sdefred\n", symbol_prefix);
81 ++outline;
82 fprintf(code_file, "#define yydgoto %sdgoto\n", symbol_prefix);
83 ++outline;
84 fprintf(code_file, "#define yysindex %ssindex\n", symbol_prefix);
85 ++outline;
86 fprintf(code_file, "#define yyrindex %srindex\n", symbol_prefix);
87 ++outline;
88 fprintf(code_file, "#define yygindex %sgindex\n", symbol_prefix);
89 ++outline;
90 fprintf(code_file, "#define yytable %stable\n", symbol_prefix);
91 ++outline;
92 fprintf(code_file, "#define yycheck %scheck\n", symbol_prefix);
93 ++outline;
94 fprintf(code_file, "#define yyname %sname\n", symbol_prefix);
95 ++outline;
96 fprintf(code_file, "#define yyrule %srule\n", symbol_prefix);
98 ++outline;
99 fprintf(code_file, "#define YYPREFIX \"%s\"\n", symbol_prefix);
103 output_rule_data()
105 register int i;
106 register int j;
109 fprintf(output_file, "short %slhs[] = {%42d,", symbol_prefix,
110 symbol_value[start_symbol]);
112 j = 10;
113 for (i = 3; i < nrules; i++)
115 if (j >= 10)
117 if (!rflag) ++outline;
118 putc('\n', output_file);
119 j = 1;
121 else
122 ++j;
124 fprintf(output_file, "%5d,", symbol_value[rlhs[i]]);
126 if (!rflag) outline += 2;
127 fprintf(output_file, "\n};\n");
129 fprintf(output_file, "short %slen[] = {%42d,", symbol_prefix, 2);
131 j = 10;
132 for (i = 3; i < nrules; i++)
134 if (j >= 10)
136 if (!rflag) ++outline;
137 putc('\n', output_file);
138 j = 1;
140 else
141 j++;
143 fprintf(output_file, "%5d,", rrhs[i + 1] - rrhs[i] - 1);
145 if (!rflag) outline += 2;
146 fprintf(output_file, "\n};\n");
150 output_yydefred()
152 register int i, j;
154 fprintf(output_file, "short %sdefred[] = {%39d,", symbol_prefix,
155 (defred[0] ? defred[0] - 2 : 0));
157 j = 10;
158 for (i = 1; i < nstates; i++)
160 if (j < 10)
161 ++j;
162 else
164 if (!rflag) ++outline;
165 putc('\n', output_file);
166 j = 1;
169 fprintf(output_file, "%5d,", (defred[i] ? defred[i] - 2 : 0));
172 if (!rflag) outline += 2;
173 fprintf(output_file, "\n};\n");
177 output_actions()
179 nvectors = 2*nstates + nvars;
181 froms = NEW2(nvectors, short *);
182 tos = NEW2(nvectors, short *);
183 tally = NEW2(nvectors, short);
184 width = NEW2(nvectors, short);
186 token_actions();
187 FREE(lookaheads);
188 FREE(LA);
189 FREE(LAruleno);
190 FREE(accessing_symbol);
192 goto_actions();
193 FREE(goto_map + ntokens);
194 FREE(from_state);
195 FREE(to_state);
197 sort_actions();
198 pack_table();
199 output_base();
200 output_table();
201 output_check();
205 token_actions()
207 register int i, j;
208 register int shiftcount, reducecount;
209 register int max, min;
210 register short *actionrow, *r, *s;
211 register action *p;
213 actionrow = NEW2(2*ntokens, short);
214 for (i = 0; i < nstates; ++i)
216 if (parser[i])
218 for (j = 0; j < 2*ntokens; ++j)
219 actionrow[j] = 0;
221 shiftcount = 0;
222 reducecount = 0;
223 for (p = parser[i]; p; p = p->next)
225 if (p->suppressed == 0)
227 if (p->action_code == SHIFT)
229 ++shiftcount;
230 actionrow[p->symbol] = p->number;
232 else if (p->action_code == REDUCE && p->number != defred[i])
234 ++reducecount;
235 actionrow[p->symbol + ntokens] = p->number;
240 tally[i] = shiftcount;
241 tally[nstates+i] = reducecount;
242 width[i] = 0;
243 width[nstates+i] = 0;
244 if (shiftcount > 0)
246 froms[i] = r = NEW2(shiftcount, short);
247 tos[i] = s = NEW2(shiftcount, short);
248 min = MAXSHORT;
249 max = 0;
250 for (j = 0; j < ntokens; ++j)
252 if (actionrow[j])
254 if (min > symbol_value[j])
255 min = symbol_value[j];
256 if (max < symbol_value[j])
257 max = symbol_value[j];
258 *r++ = symbol_value[j];
259 *s++ = actionrow[j];
262 width[i] = max - min + 1;
264 if (reducecount > 0)
266 froms[nstates+i] = r = NEW2(reducecount, short);
267 tos[nstates+i] = s = NEW2(reducecount, short);
268 min = MAXSHORT;
269 max = 0;
270 for (j = 0; j < ntokens; ++j)
272 if (actionrow[ntokens+j])
274 if (min > symbol_value[j])
275 min = symbol_value[j];
276 if (max < symbol_value[j])
277 max = symbol_value[j];
278 *r++ = symbol_value[j];
279 *s++ = actionrow[ntokens+j] - 2;
282 width[nstates+i] = max - min + 1;
286 FREE(actionrow);
289 goto_actions()
291 register int i, j, k;
293 state_count = NEW2(nstates, short);
295 k = default_goto(start_symbol + 1);
296 fprintf(output_file, "short %sdgoto[] = {%40d,", symbol_prefix, k);
297 save_column(start_symbol + 1, k);
299 j = 10;
300 for (i = start_symbol + 2; i < nsyms; i++)
302 if (j >= 10)
304 if (!rflag) ++outline;
305 putc('\n', output_file);
306 j = 1;
308 else
309 ++j;
311 k = default_goto(i);
312 fprintf(output_file, "%5d,", k);
313 save_column(i, k);
316 if (!rflag) outline += 2;
317 fprintf(output_file, "\n};\n");
318 FREE(state_count);
322 default_goto(symbol)
323 int symbol;
325 register int i;
326 register int m;
327 register int n;
328 register int default_state;
329 register int max;
331 m = goto_map[symbol];
332 n = goto_map[symbol + 1];
334 if (m == n) return (0);
336 for (i = 0; i < nstates; i++)
337 state_count[i] = 0;
339 for (i = m; i < n; i++)
340 state_count[to_state[i]]++;
342 max = 0;
343 default_state = 0;
344 for (i = 0; i < nstates; i++)
346 if (state_count[i] > max)
348 max = state_count[i];
349 default_state = i;
353 return (default_state);
358 save_column(symbol, default_state)
359 int symbol;
360 int default_state;
362 register int i;
363 register int m;
364 register int n;
365 register short *sp;
366 register short *sp1;
367 register short *sp2;
368 register int count;
369 register int symno;
371 m = goto_map[symbol];
372 n = goto_map[symbol + 1];
374 count = 0;
375 for (i = m; i < n; i++)
377 if (to_state[i] != default_state)
378 ++count;
380 if (count == 0) return;
382 symno = symbol_value[symbol] + 2*nstates;
384 froms[symno] = sp1 = sp = NEW2(count, short);
385 tos[symno] = sp2 = NEW2(count, short);
387 for (i = m; i < n; i++)
389 if (to_state[i] != default_state)
391 *sp1++ = from_state[i];
392 *sp2++ = to_state[i];
396 tally[symno] = count;
397 width[symno] = sp1[-1] - sp[0] + 1;
400 sort_actions()
402 register int i;
403 register int j;
404 register int k;
405 register int t;
406 register int w;
408 order = NEW2(nvectors, short);
409 nentries = 0;
411 for (i = 0; i < nvectors; i++)
413 if (tally[i] > 0)
415 t = tally[i];
416 w = width[i];
417 j = nentries - 1;
419 while (j >= 0 && (width[order[j]] < w))
420 j--;
422 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
423 j--;
425 for (k = nentries - 1; k > j; k--)
426 order[k + 1] = order[k];
428 order[j + 1] = i;
429 nentries++;
435 pack_table()
437 register int i;
438 register int place;
439 register int state;
441 base = NEW2(nvectors, short);
442 pos = NEW2(nentries, short);
444 maxtable = BITS_PER_WORD == 16 ? 400 : 1000;
445 table = NEW2(maxtable, short);
446 check = NEW2(maxtable, short);
448 lowzero = 0;
449 high = 0;
451 for (i = 0; i < maxtable; i++)
452 check[i] = -1;
454 for (i = 0; i < nentries; i++)
456 state = matching_vector(i);
458 if (state < 0)
459 place = pack_vector(i);
460 else
461 place = base[state];
463 pos[i] = place;
464 base[order[i]] = place;
467 for (i = 0; i < nvectors; i++)
469 if (froms[i])
470 FREE(froms[i]);
471 if (tos[i])
472 FREE(tos[i]);
475 FREE(froms);
476 FREE(tos);
477 FREE(pos);
481 /* The function matching_vector determines if the vector specified by */
482 /* the input parameter matches a previously considered vector. The */
483 /* test at the start of the function checks if the vector represents */
484 /* a row of shifts over terminal symbols or a row of reductions, or a */
485 /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
486 /* check if a column of shifts over a nonterminal symbols matches a */
487 /* previously considered vector. Because of the nature of LR parsing */
488 /* tables, no two columns can match. Therefore, the only possible */
489 /* match would be between a row and a column. Such matches are */
490 /* unlikely. Therefore, to save time, no attempt is made to see if a */
491 /* column matches a previously considered vector. */
492 /* */
493 /* Matching_vector is poorly designed. The test could easily be made */
494 /* faster. Also, it depends on the vectors being in a specific */
495 /* order. */
498 matching_vector(vector)
499 int vector;
501 register int i;
502 register int j;
503 register int k;
504 register int t;
505 register int w;
506 register int match;
507 register int prev;
509 i = order[vector];
510 if (i >= 2*nstates)
511 return (-1);
513 t = tally[i];
514 w = width[i];
516 for (prev = vector - 1; prev >= 0; prev--)
518 j = order[prev];
519 if (width[j] != w || tally[j] != t)
520 return (-1);
522 match = 1;
523 for (k = 0; match && k < t; k++)
525 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
526 match = 0;
529 if (match)
530 return (j);
533 return (-1);
539 pack_vector(vector)
540 int vector;
542 register int i, j, k, l;
543 register int t;
544 register int loc;
545 register int ok;
546 register short *from;
547 register short *to;
548 int newmax;
550 i = order[vector];
551 t = tally[i];
552 assert(t);
554 from = froms[i];
555 to = tos[i];
557 j = lowzero - from[0];
558 for (k = 1; k < t; ++k)
559 if (lowzero - from[k] > j)
560 j = lowzero - from[k];
561 for (;; ++j)
563 if (j == 0)
564 continue;
565 ok = 1;
566 for (k = 0; ok && k < t; k++)
568 loc = j + from[k];
569 if (loc >= maxtable)
571 if (loc >= MAXTABLE)
572 fatal("maximum table size exceeded");
574 newmax = maxtable;
575 do { newmax += 200; } while (newmax <= loc);
576 table = (short *) REALLOC(table, newmax*sizeof(short));
577 if (table == 0) no_space();
578 check = (short *) REALLOC(check, newmax*sizeof(short));
579 if (check == 0) no_space();
580 for (l = maxtable; l < newmax; ++l)
582 table[l] = 0;
583 check[l] = -1;
585 maxtable = newmax;
588 if (check[loc] != -1)
589 ok = 0;
591 for (k = 0; ok && k < vector; k++)
593 if (pos[k] == j)
594 ok = 0;
596 if (ok)
598 for (k = 0; k < t; k++)
600 loc = j + from[k];
601 table[loc] = to[k];
602 check[loc] = from[k];
603 if (loc > high) high = loc;
606 while (check[lowzero] != -1)
607 ++lowzero;
609 return (j);
616 output_base()
618 register int i, j;
620 fprintf(output_file, "short %ssindex[] = {%39d,", symbol_prefix, base[0]);
622 j = 10;
623 for (i = 1; i < nstates; i++)
625 if (j >= 10)
627 if (!rflag) ++outline;
628 putc('\n', output_file);
629 j = 1;
631 else
632 ++j;
634 fprintf(output_file, "%5d,", base[i]);
637 if (!rflag) outline += 2;
638 fprintf(output_file, "\n};\nshort %srindex[] = {%39d,", symbol_prefix,
639 base[nstates]);
641 j = 10;
642 for (i = nstates + 1; i < 2*nstates; i++)
644 if (j >= 10)
646 if (!rflag) ++outline;
647 putc('\n', output_file);
648 j = 1;
650 else
651 ++j;
653 fprintf(output_file, "%5d,", base[i]);
656 if (!rflag) outline += 2;
657 fprintf(output_file, "\n};\nshort %sgindex[] = {%39d,", symbol_prefix,
658 base[2*nstates]);
660 j = 10;
661 for (i = 2*nstates + 1; i < nvectors - 1; i++)
663 if (j >= 10)
665 if (!rflag) ++outline;
666 putc('\n', output_file);
667 j = 1;
669 else
670 ++j;
672 fprintf(output_file, "%5d,", base[i]);
675 if (!rflag) outline += 2;
676 fprintf(output_file, "\n};\n");
677 FREE(base);
682 output_table()
684 register int i;
685 register int j;
687 ++outline;
688 fprintf(code_file, "#define YYTABLESIZE %d\n", high);
689 fprintf(output_file, "short %stable[] = {%40d,", symbol_prefix,
690 table[0]);
692 j = 10;
693 for (i = 1; i <= high; i++)
695 if (j >= 10)
697 if (!rflag) ++outline;
698 putc('\n', output_file);
699 j = 1;
701 else
702 ++j;
704 fprintf(output_file, "%5d,", table[i]);
707 if (!rflag) outline += 2;
708 fprintf(output_file, "\n};\n");
709 FREE(table);
714 output_check()
716 register int i;
717 register int j;
719 fprintf(output_file, "short %scheck[] = {%40d,", symbol_prefix,
720 check[0]);
722 j = 10;
723 for (i = 1; i <= high; i++)
725 if (j >= 10)
727 if (!rflag) ++outline;
728 putc('\n', output_file);
729 j = 1;
731 else
732 ++j;
734 fprintf(output_file, "%5d,", check[i]);
737 if (!rflag) outline += 2;
738 fprintf(output_file, "\n};\n");
739 FREE(check);
744 is_C_identifier(name)
745 char *name;
747 register char *s;
748 register int c;
750 s = name;
751 c = *s;
752 if (c == '"')
754 c = *++s;
755 if (!isalpha(c) && c != '_' && c != '$')
756 return (0);
757 while ((c = *++s) != '"')
759 if (!isalnum(c) && c != '_' && c != '$')
760 return (0);
762 return (1);
765 if (!isalpha(c) && c != '_' && c != '$')
766 return (0);
767 while (c = *++s)
769 if (!isalnum(c) && c != '_' && c != '$')
770 return (0);
772 return (1);
776 output_defines()
778 register int c, i;
779 register char *s;
781 for (i = 2; i < ntokens; ++i)
783 s = symbol_name[i];
784 if (is_C_identifier(s))
786 fprintf(code_file, "#define ");
787 if (dflag) fprintf(defines_file, "#define ");
788 c = *s;
789 if (c == '"')
791 while ((c = *++s) != '"')
793 putc(c, code_file);
794 if (dflag) putc(c, defines_file);
797 else
801 putc(c, code_file);
802 if (dflag) putc(c, defines_file);
804 while (c = *++s);
806 ++outline;
807 fprintf(code_file, " %d\n", symbol_value[i]);
808 if (dflag) fprintf(defines_file, " %d\n", symbol_value[i]);
812 ++outline;
813 fprintf(code_file, "#define YYERRCODE %d\n", symbol_value[1]);
815 if (dflag && unionized)
817 fclose(union_file);
818 union_file = fopen(union_file_name, "r");
819 if (union_file == NULL) open_error(union_file_name);
820 while ((c = getc(union_file)) != EOF)
821 putc(c, defines_file);
822 fprintf(defines_file, " YYSTYPE;\nextern YYSTYPE %slval;\n",
823 symbol_prefix);
828 output_stored_text()
830 register int c;
831 register FILE *in, *out;
833 fclose(text_file);
834 text_file = fopen(text_file_name, "r");
835 if (text_file == NULL)
836 open_error(text_file_name);
837 in = text_file;
838 if ((c = getc(in)) == EOF)
839 return;
840 out = code_file;
841 if (c == '\n')
842 ++outline;
843 putc(c, out);
844 while ((c = getc(in)) != EOF)
846 if (c == '\n')
847 ++outline;
848 putc(c, out);
850 if (!lflag)
851 fprintf(out, line_format, ++outline + 1, code_file_name);
855 output_debug()
857 register int i, j, k, max;
858 char **symnam, *s;
860 ++outline;
861 fprintf(code_file, "#define YYFINAL %d\n", final_state);
862 outline += 3;
863 fprintf(code_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
864 tflag);
865 if (rflag)
866 fprintf(output_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
867 tflag);
869 max = 0;
870 for (i = 2; i < ntokens; ++i)
871 if (symbol_value[i] > max)
872 max = symbol_value[i];
873 ++outline;
874 fprintf(code_file, "#define YYMAXTOKEN %d\n", max);
876 symnam = (char **) MALLOC((max+1)*sizeof(char *));
877 if (symnam == 0) no_space();
879 /* Note that it is not necessary to initialize the element */
880 /* symnam[max]. */
881 for (i = 0; i < max; ++i)
882 symnam[i] = 0;
883 for (i = ntokens - 1; i >= 2; --i)
884 symnam[symbol_value[i]] = symbol_name[i];
885 symnam[0] = "end-of-file";
887 if (!rflag) ++outline;
888 fprintf(output_file, "#if YYDEBUG\nchar *%sname[] = {", symbol_prefix);
889 j = 80;
890 for (i = 0; i <= max; ++i)
892 if (s = symnam[i])
894 if (s[0] == '"')
896 k = 7;
897 while (*++s != '"')
899 ++k;
900 if (*s == '\\')
902 k += 2;
903 if (*++s == '\\')
904 ++k;
907 j += k;
908 if (j > 80)
910 if (!rflag) ++outline;
911 putc('\n', output_file);
912 j = k;
914 fprintf(output_file, "\"\\\"");
915 s = symnam[i];
916 while (*++s != '"')
918 if (*s == '\\')
920 fprintf(output_file, "\\\\");
921 if (*++s == '\\')
922 fprintf(output_file, "\\\\");
923 else
924 putc(*s, output_file);
926 else
927 putc(*s, output_file);
929 fprintf(output_file, "\\\"\",");
931 else if (s[0] == '\'')
933 if (s[1] == '"')
935 j += 7;
936 if (j > 80)
938 if (!rflag) ++outline;
939 putc('\n', output_file);
940 j = 7;
942 fprintf(output_file, "\"'\\\"'\",");
944 else
946 k = 5;
947 while (*++s != '\'')
949 ++k;
950 if (*s == '\\')
952 k += 2;
953 if (*++s == '\\')
954 ++k;
957 j += k;
958 if (j > 80)
960 if (!rflag) ++outline;
961 putc('\n', output_file);
962 j = k;
964 fprintf(output_file, "\"'");
965 s = symnam[i];
966 while (*++s != '\'')
968 if (*s == '\\')
970 fprintf(output_file, "\\\\");
971 if (*++s == '\\')
972 fprintf(output_file, "\\\\");
973 else
974 putc(*s, output_file);
976 else
977 putc(*s, output_file);
979 fprintf(output_file, "'\",");
982 else
984 k = strlen(s) + 3;
985 j += k;
986 if (j > 80)
988 if (!rflag) ++outline;
989 putc('\n', output_file);
990 j = k;
992 putc('"', output_file);
993 do { putc(*s, output_file); } while (*++s);
994 fprintf(output_file, "\",");
997 else
999 j += 2;
1000 if (j > 80)
1002 if (!rflag) ++outline;
1003 putc('\n', output_file);
1004 j = 2;
1006 fprintf(output_file, "0,");
1009 if (!rflag) outline += 2;
1010 fprintf(output_file, "\n};\n");
1011 FREE(symnam);
1013 if (!rflag) ++outline;
1014 fprintf(output_file, "char *%srule[] = {\n", symbol_prefix);
1015 for (i = 2; i < nrules; ++i)
1017 fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
1018 for (j = rrhs[i]; ritem[j] > 0; ++j)
1020 s = symbol_name[ritem[j]];
1021 if (s[0] == '"')
1023 fprintf(output_file, " \\\"");
1024 while (*++s != '"')
1026 if (*s == '\\')
1028 if (s[1] == '\\')
1029 fprintf(output_file, "\\\\\\\\");
1030 else
1031 fprintf(output_file, "\\\\%c", s[1]);
1032 ++s;
1034 else
1035 putc(*s, output_file);
1037 fprintf(output_file, "\\\"");
1039 else if (s[0] == '\'')
1041 if (s[1] == '"')
1042 fprintf(output_file, " '\\\"'");
1043 else if (s[1] == '\\')
1045 if (s[2] == '\\')
1046 fprintf(output_file, " '\\\\\\\\");
1047 else
1048 fprintf(output_file, " '\\\\%c", s[2]);
1049 s += 2;
1050 while (*++s != '\'')
1051 putc(*s, output_file);
1052 putc('\'', output_file);
1054 else
1055 fprintf(output_file, " '%c'", s[1]);
1057 else
1058 fprintf(output_file, " %s", s);
1060 if (!rflag) ++outline;
1061 fprintf(output_file, "\",\n");
1064 if (!rflag) outline += 2;
1065 fprintf(output_file, "};\n#endif\n");
1069 output_stype()
1071 if (!unionized && ntags == 0)
1073 outline += 3;
1074 fprintf(code_file, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
1079 output_trailing_text()
1081 register int c, last;
1082 register FILE *in, *out;
1084 if (line == 0)
1085 return;
1087 in = input_file;
1088 out = code_file;
1089 c = *cptr;
1090 if (c == '\n')
1092 ++lineno;
1093 if ((c = getc(in)) == EOF)
1094 return;
1095 if (!lflag)
1097 ++outline;
1098 fprintf(out, line_format, lineno, input_file_name);
1100 if (c == '\n')
1101 ++outline;
1102 putc(c, out);
1103 last = c;
1105 else
1107 if (!lflag)
1109 ++outline;
1110 fprintf(out, line_format, lineno, input_file_name);
1112 do { putc(c, out); } while ((c = *++cptr) != '\n');
1113 ++outline;
1114 putc('\n', out);
1115 last = '\n';
1118 while ((c = getc(in)) != EOF)
1120 if (c == '\n')
1121 ++outline;
1122 putc(c, out);
1123 last = c;
1126 if (last != '\n')
1128 ++outline;
1129 putc('\n', out);
1131 if (!lflag)
1132 fprintf(out, line_format, ++outline + 1, code_file_name);
1136 output_semantic_actions()
1138 register int c, last;
1139 register FILE *out;
1141 fclose(action_file);
1142 action_file = fopen(action_file_name, "r");
1143 if (action_file == NULL)
1144 open_error(action_file_name);
1146 if ((c = getc(action_file)) == EOF)
1147 return;
1149 out = code_file;
1150 last = c;
1151 if (c == '\n')
1152 ++outline;
1153 putc(c, out);
1154 while ((c = getc(action_file)) != EOF)
1156 if (c == '\n')
1157 ++outline;
1158 putc(c, out);
1159 last = c;
1162 if (last != '\n')
1164 ++outline;
1165 putc('\n', out);
1168 if (!lflag)
1169 fprintf(out, line_format, ++outline + 1, code_file_name);
1173 free_itemsets()
1175 register core *cp, *next;
1177 FREE(state_table);
1178 for (cp = first_state; cp; cp = next)
1180 next = cp->next;
1181 FREE(cp);
1186 free_shifts()
1188 register shifts *sp, *next;
1190 FREE(shift_table);
1191 for (sp = first_shift; sp; sp = next)
1193 next = sp->next;
1194 FREE(sp);
1200 free_reductions()
1202 register reductions *rp, *next;
1204 FREE(reduction_table);
1205 for (rp = first_reduction; rp; rp = next)
1207 next = rp->next;
1208 FREE(rp);