Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / byacc / dist / output.c
blob408b9b2e90937795c20c673ee5f3ecd79c394863
1 /* $NetBSD: output.c,v 1.3 2009/10/29 21:03:59 christos Exp $ */
2 /* Id: output.c,v 1.21 2009/10/27 10:55:05 tom Exp */
4 #include "defs.h"
6 #include <sys/cdefs.h>
7 __RCSID("$NetBSD: output.c,v 1.3 2009/10/29 21:03:59 christos Exp $");
9 static int nvectors;
10 static int nentries;
11 static Value_t **froms;
12 static Value_t **tos;
13 static Value_t *tally;
14 static Value_t *width;
15 static Value_t *state_count;
16 static Value_t *order;
17 static Value_t *base;
18 static Value_t *pos;
19 static int maxtable;
20 static Value_t *table;
21 static Value_t *check;
22 static int lowzero;
23 static int high;
25 static void
26 write_char(FILE * out, int c)
28 if (c == '\n')
29 ++outline;
30 putc(c, out);
33 static void
34 write_code_lineno(FILE * out)
36 if (!lflag)
37 fprintf(out, line_format, (outline++) + 1, code_file_name);
40 static void
41 write_input_lineno(FILE * out)
43 if (!lflag)
45 ++outline;
46 fprintf(out, line_format, lineno, input_file_name);
50 static void
51 define_prefixed(const char *name)
53 ++outline;
54 fprintf(code_file, "#define %-10s %s%s\n", name, symbol_prefix, name + 2);
57 static void
58 output_yacc_decl(void)
60 param *p;
61 ++outline;
62 fprintf(code_file, "/* compatibility with bison */\n");
63 ++outline;
64 fprintf(code_file, "#ifdef YYPARSE_PARAM\n");
65 ++outline;
66 fprintf(code_file, "/* compatibility with FreeBSD */\n");
67 ++outline;
68 fprintf(code_file, "# ifdef YYPARSE_PARAM_TYPE\n");
69 ++outline;
70 fprintf(code_file, "# define YYPARSE_DECL() "
71 "yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)\n");
72 ++outline;
73 fprintf(code_file, "# else\n");
74 ++outline;
75 fprintf(code_file, "# define YYPARSE_DECL() "
76 "yyparse(void *YYPARSE_PARAM)\n");
77 ++outline;
78 fprintf(code_file, "# endif\n");
79 ++outline;
80 fprintf(code_file, "#else\n");
81 ++outline;
82 fprintf(code_file, "# define YYPARSE_DECL() yyparse(");
83 if (!parse_param)
84 fprintf(code_file, "void");
85 else
86 for (p = lex_param; p; p = p->next)
87 fprintf(code_file, "%s %s%s", p->type, p->name,
88 p->next ? ", " : "");
89 fprintf(code_file, ")\n");
90 outline += 2;
91 fprintf(code_file, "#endif\n\n");
94 static void
95 output_lex_decl(void)
97 param *p;
98 ++outline;
99 fprintf(code_file, "/* Pure parsers. */\n");
100 ++outline;
101 fprintf(code_file, "#define YYPURE %d\n", pure_parser);
102 ++outline;
103 fprintf(code_file, "#ifdef YYLEX_PARAM\n");
104 ++outline;
105 if (pure_parser)
106 fprintf(code_file, "# define YYLEX yylex(&yylval, YYLEX_PARAM)\n");
107 else
108 fprintf(code_file, "# define YYLEX yylex(YYLEX_PARAM)\n");
109 ++outline;
110 fprintf(code_file, "#else\n");
111 ++outline;
112 if (pure_parser)
113 fprintf(code_file, "# define YYLEX yylex(&yylval, ");
114 else
115 fprintf(code_file, "# define YYLEX yylex(");
116 for (p = lex_param; p; p = p->next)
117 fprintf(code_file, "%s%s", p->name, p->next ? ", " : "");
118 fprintf(code_file, ")\n");
119 outline += 2;
120 fprintf(code_file, "#endif\n\n");
122 static void
123 output_prefix(void)
125 if (symbol_prefix == NULL)
126 symbol_prefix = "yy";
127 else
129 define_prefixed("yyparse");
130 define_prefixed("yylex");
131 define_prefixed("yyerror");
132 define_prefixed("yychar");
133 define_prefixed("yyval");
134 define_prefixed("yylval");
135 define_prefixed("yydebug");
136 define_prefixed("yynerrs");
137 define_prefixed("yyerrflag");
138 define_prefixed("yyss");
139 define_prefixed("yyssp");
140 define_prefixed("yyvs");
141 define_prefixed("yyvsp");
142 define_prefixed("yylhs");
143 define_prefixed("yylen");
144 define_prefixed("yydefred");
145 define_prefixed("yydgoto");
146 define_prefixed("yysindex");
147 define_prefixed("yyrindex");
148 define_prefixed("yygindex");
149 define_prefixed("yytable");
150 define_prefixed("yycheck");
151 define_prefixed("yyname");
152 define_prefixed("yyrule");
154 ++outline;
155 fprintf(code_file, "#define YYPREFIX \"%s\"\n", symbol_prefix);
158 static void
159 output_newline(void)
161 if (!rflag)
162 ++outline;
163 putc('\n', output_file);
166 static void
167 output_line(const char *value)
169 fputs(value, output_file);
170 output_newline();
173 static void
174 output_int(int value)
176 fprintf(output_file, "%5d,", value);
179 static void
180 start_int_table(const char *name, int value)
182 int need = 34 - (int)(strlen(symbol_prefix) + strlen(name));
184 if (need < 6)
185 need = 6;
186 fprintf(output_file,
187 "static const short %s%s[] = {%*d,",
188 symbol_prefix, name, need, value);
191 static void
192 start_str_table(const char *name)
194 fprintf(output_file,
195 "static const char *%s%s[] = {",
196 symbol_prefix, name);
197 output_newline();
200 static void
201 end_table(void)
203 output_newline();
204 output_line("};");
207 static void
208 output_rule_data(void)
210 int i;
211 int j;
213 start_int_table("lhs", symbol_value[start_symbol]);
215 j = 10;
216 for (i = 3; i < nrules; i++)
218 if (j >= 10)
220 output_newline();
221 j = 1;
223 else
224 ++j;
226 output_int(symbol_value[rlhs[i]]);
228 end_table();
230 start_int_table("len", 2);
232 j = 10;
233 for (i = 3; i < nrules; i++)
235 if (j >= 10)
237 output_newline();
238 j = 1;
240 else
241 j++;
243 output_int(rrhs[i + 1] - rrhs[i] - 1);
245 end_table();
248 static void
249 output_yydefred(void)
251 int i, j;
253 start_int_table("defred", (defred[0] ? defred[0] - 2 : 0));
255 j = 10;
256 for (i = 1; i < nstates; i++)
258 if (j < 10)
259 ++j;
260 else
262 output_newline();
263 j = 1;
266 output_int((defred[i] ? defred[i] - 2 : 0));
269 end_table();
272 static void
273 token_actions(void)
275 int i, j;
276 Value_t shiftcount, reducecount;
277 int max, min;
278 Value_t *actionrow, *r, *s;
279 action *p;
281 actionrow = NEW2(2 * ntokens, Value_t);
282 for (i = 0; i < nstates; ++i)
284 if (parser[i])
286 for (j = 0; j < 2 * ntokens; ++j)
287 actionrow[j] = 0;
289 shiftcount = 0;
290 reducecount = 0;
291 for (p = parser[i]; p; p = p->next)
293 if (p->suppressed == 0)
295 if (p->action_code == SHIFT)
297 ++shiftcount;
298 actionrow[p->symbol] = p->number;
300 else if (p->action_code == REDUCE && p->number != defred[i])
302 ++reducecount;
303 actionrow[p->symbol + ntokens] = p->number;
308 tally[i] = shiftcount;
309 tally[nstates + i] = reducecount;
310 width[i] = 0;
311 width[nstates + i] = 0;
312 if (shiftcount > 0)
314 froms[i] = r = NEW2(shiftcount, Value_t);
315 tos[i] = s = NEW2(shiftcount, Value_t);
316 min = MAXSHORT;
317 max = 0;
318 for (j = 0; j < ntokens; ++j)
320 if (actionrow[j])
322 if (min > symbol_value[j])
323 min = symbol_value[j];
324 if (max < symbol_value[j])
325 max = symbol_value[j];
326 *r++ = symbol_value[j];
327 *s++ = actionrow[j];
330 width[i] = (Value_t) (max - min + 1);
332 if (reducecount > 0)
334 froms[nstates + i] = r = NEW2(reducecount, Value_t);
335 tos[nstates + i] = s = NEW2(reducecount, Value_t);
336 min = MAXSHORT;
337 max = 0;
338 for (j = 0; j < ntokens; ++j)
340 if (actionrow[ntokens + j])
342 if (min > symbol_value[j])
343 min = symbol_value[j];
344 if (max < symbol_value[j])
345 max = symbol_value[j];
346 *r++ = symbol_value[j];
347 *s++ = (Value_t) (actionrow[ntokens + j] - 2);
350 width[nstates + i] = (Value_t) (max - min + 1);
354 FREE(actionrow);
357 static int
358 default_goto(int symbol)
360 int i;
361 int m;
362 int n;
363 int default_state;
364 int max;
366 m = goto_map[symbol];
367 n = goto_map[symbol + 1];
369 if (m == n)
370 return (0);
372 for (i = 0; i < nstates; i++)
373 state_count[i] = 0;
375 for (i = m; i < n; i++)
376 state_count[to_state[i]]++;
378 max = 0;
379 default_state = 0;
380 for (i = 0; i < nstates; i++)
382 if (state_count[i] > max)
384 max = state_count[i];
385 default_state = i;
389 return (default_state);
392 static void
393 save_column(int symbol, int default_state)
395 int i;
396 int m;
397 int n;
398 Value_t *sp;
399 Value_t *sp1;
400 Value_t *sp2;
401 Value_t count;
402 int symno;
404 m = goto_map[symbol];
405 n = goto_map[symbol + 1];
407 count = 0;
408 for (i = m; i < n; i++)
410 if (to_state[i] != default_state)
411 ++count;
413 if (count == 0)
414 return;
416 symno = symbol_value[symbol] + 2 * nstates;
418 froms[symno] = sp1 = sp = NEW2(count, Value_t);
419 tos[symno] = sp2 = NEW2(count, Value_t);
421 for (i = m; i < n; i++)
423 if (to_state[i] != default_state)
425 *sp1++ = from_state[i];
426 *sp2++ = to_state[i];
430 tally[symno] = count;
431 width[symno] = (Value_t) (sp1[-1] - sp[0] + 1);
434 static void
435 goto_actions(void)
437 int i, j, k;
439 state_count = NEW2(nstates, Value_t);
441 k = default_goto(start_symbol + 1);
442 start_int_table("dgoto", k);
443 save_column(start_symbol + 1, k);
445 j = 10;
446 for (i = start_symbol + 2; i < nsyms; i++)
448 if (j >= 10)
450 output_newline();
451 j = 1;
453 else
454 ++j;
456 k = default_goto(i);
457 output_int(k);
458 save_column(i, k);
461 end_table();
462 FREE(state_count);
465 static void
466 sort_actions(void)
468 Value_t i;
469 int j;
470 int k;
471 int t;
472 int w;
474 order = NEW2(nvectors, Value_t);
475 nentries = 0;
477 for (i = 0; i < nvectors; i++)
479 if (tally[i] > 0)
481 t = tally[i];
482 w = width[i];
483 j = nentries - 1;
485 while (j >= 0 && (width[order[j]] < w))
486 j--;
488 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
489 j--;
491 for (k = nentries - 1; k > j; k--)
492 order[k + 1] = order[k];
494 order[j + 1] = i;
495 nentries++;
500 /* The function matching_vector determines if the vector specified by */
501 /* the input parameter matches a previously considered vector. The */
502 /* test at the start of the function checks if the vector represents */
503 /* a row of shifts over terminal symbols or a row of reductions, or a */
504 /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
505 /* check if a column of shifts over a nonterminal symbols matches a */
506 /* previously considered vector. Because of the nature of LR parsing */
507 /* tables, no two columns can match. Therefore, the only possible */
508 /* match would be between a row and a column. Such matches are */
509 /* unlikely. Therefore, to save time, no attempt is made to see if a */
510 /* column matches a previously considered vector. */
511 /* */
512 /* Matching_vector is poorly designed. The test could easily be made */
513 /* faster. Also, it depends on the vectors being in a specific */
514 /* order. */
516 static int
517 matching_vector(int vector)
519 int i;
520 int j;
521 int k;
522 int t;
523 int w;
524 int match;
525 int prev;
527 i = order[vector];
528 if (i >= 2 * nstates)
529 return (-1);
531 t = tally[i];
532 w = width[i];
534 for (prev = vector - 1; prev >= 0; prev--)
536 j = order[prev];
537 if (width[j] != w || tally[j] != t)
538 return (-1);
540 match = 1;
541 for (k = 0; match && k < t; k++)
543 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
544 match = 0;
547 if (match)
548 return (j);
551 return (-1);
554 static int
555 pack_vector(int vector)
557 int i, j, k, l;
558 int t;
559 int loc;
560 int ok;
561 Value_t *from;
562 Value_t *to;
563 int newmax;
565 i = order[vector];
566 t = tally[i];
567 assert(t);
569 from = froms[i];
570 to = tos[i];
572 j = lowzero - from[0];
573 for (k = 1; k < t; ++k)
574 if (lowzero - from[k] > j)
575 j = lowzero - from[k];
576 for (;; ++j)
578 if (j == 0)
579 continue;
580 ok = 1;
581 for (k = 0; ok && k < t; k++)
583 loc = j + from[k];
584 if (loc >= maxtable - 1)
586 if (loc >= MAXTABLE - 1)
587 fatal("maximum table size exceeded");
589 newmax = maxtable;
592 newmax += 200;
594 while (newmax <= loc);
595 table = (Value_t *) REALLOC(table, (unsigned)newmax * sizeof(Value_t));
596 if (table == 0)
597 no_space();
598 check = (Value_t *) REALLOC(check, (unsigned)newmax * sizeof(Value_t));
599 if (check == 0)
600 no_space();
601 for (l = maxtable; l < newmax; ++l)
603 table[l] = 0;
604 check[l] = -1;
606 maxtable = newmax;
609 if (check[loc] != -1)
610 ok = 0;
612 for (k = 0; ok && k < vector; k++)
614 if (pos[k] == j)
615 ok = 0;
617 if (ok)
619 for (k = 0; k < t; k++)
621 loc = j + from[k];
622 table[loc] = to[k];
623 check[loc] = from[k];
624 if (loc > high)
625 high = loc;
628 while (check[lowzero] != -1)
629 ++lowzero;
631 return (j);
636 static void
637 pack_table(void)
639 int i;
640 Value_t place;
641 int state;
643 base = NEW2(nvectors, Value_t);
644 pos = NEW2(nentries, Value_t);
646 maxtable = 1000;
647 table = NEW2(maxtable, Value_t);
648 check = NEW2(maxtable, Value_t);
650 lowzero = 0;
651 high = 0;
653 for (i = 0; i < maxtable; i++)
654 check[i] = -1;
656 for (i = 0; i < nentries; i++)
658 state = matching_vector(i);
660 if (state < 0)
661 place = (Value_t) pack_vector(i);
662 else
663 place = base[state];
665 pos[i] = place;
666 base[order[i]] = place;
669 for (i = 0; i < nvectors; i++)
671 if (froms[i])
672 FREE(froms[i]);
673 if (tos[i])
674 FREE(tos[i]);
677 FREE(froms);
678 FREE(tos);
679 FREE(pos);
682 static void
683 output_base(void)
685 int i, j;
687 start_int_table("sindex", base[0]);
689 j = 10;
690 for (i = 1; i < nstates; i++)
692 if (j >= 10)
694 output_newline();
695 j = 1;
697 else
698 ++j;
700 output_int(base[i]);
703 end_table();
705 start_int_table("rindex", base[nstates]);
707 j = 10;
708 for (i = nstates + 1; i < 2 * nstates; i++)
710 if (j >= 10)
712 output_newline();
713 j = 1;
715 else
716 ++j;
718 output_int(base[i]);
721 end_table();
723 start_int_table("gindex", base[2 * nstates]);
725 j = 10;
726 for (i = 2 * nstates + 1; i < nvectors - 1; i++)
728 if (j >= 10)
730 output_newline();
731 j = 1;
733 else
734 ++j;
736 output_int(base[i]);
739 end_table();
740 FREE(base);
743 static void
744 output_table(void)
746 int i;
747 int j;
749 ++outline;
750 fprintf(code_file, "#define YYTABLESIZE %d\n", high);
751 start_int_table("table", table[0]);
753 j = 10;
754 for (i = 1; i <= high; i++)
756 if (j >= 10)
758 output_newline();
759 j = 1;
761 else
762 ++j;
764 output_int(table[i]);
767 end_table();
768 FREE(table);
771 static void
772 output_check(void)
774 int i;
775 int j;
777 start_int_table("check", check[0]);
779 j = 10;
780 for (i = 1; i <= high; i++)
782 if (j >= 10)
784 output_newline();
785 j = 1;
787 else
788 ++j;
790 output_int(check[i]);
793 end_table();
794 FREE(check);
797 static void
798 output_actions(void)
800 nvectors = 2 * nstates + nvars;
802 froms = NEW2(nvectors, Value_t *);
803 tos = NEW2(nvectors, Value_t *);
804 tally = NEW2(nvectors, Value_t);
805 width = NEW2(nvectors, Value_t);
807 token_actions();
808 FREE(lookaheads);
809 FREE(LA);
810 FREE(LAruleno);
811 FREE(accessing_symbol);
813 goto_actions();
814 FREE(goto_map + ntokens);
815 FREE(from_state);
816 FREE(to_state);
818 sort_actions();
819 pack_table();
820 output_base();
821 output_table();
822 output_check();
825 static int
826 is_C_identifier(char *name)
828 char *s;
829 int c;
831 s = name;
832 c = *s;
833 if (c == '"')
835 c = *++s;
836 if (!isalpha(c) && c != '_' && c != '$')
837 return (0);
838 while ((c = *++s) != '"')
840 if (!isalnum(c) && c != '_' && c != '$')
841 return (0);
843 return (1);
846 if (!isalpha(c) && c != '_' && c != '$')
847 return (0);
848 while ((c = *++s) != 0)
850 if (!isalnum(c) && c != '_' && c != '$')
851 return (0);
853 return (1);
856 static void
857 output_defines(void)
859 int c, i;
860 char *s;
862 for (i = 2; i < ntokens; ++i)
864 s = symbol_name[i];
865 if (is_C_identifier(s))
867 fprintf(code_file, "#define ");
868 if (dflag)
869 fprintf(defines_file, "#define ");
870 c = *s;
871 if (c == '"')
873 while ((c = *++s) != '"')
875 putc(c, code_file);
876 if (dflag)
877 putc(c, defines_file);
880 else
884 putc(c, code_file);
885 if (dflag)
886 putc(c, defines_file);
888 while ((c = *++s) != 0);
890 ++outline;
891 fprintf(code_file, " %d\n", symbol_value[i]);
892 if (dflag)
893 fprintf(defines_file, " %d\n", symbol_value[i]);
897 ++outline;
898 fprintf(code_file, "#define YYERRCODE %d\n", symbol_value[1]);
900 if (dflag && unionized)
902 rewind(union_file);
903 while ((c = getc(union_file)) != EOF)
904 putc(c, defines_file);
905 if (!pure_parser)
906 fprintf(defines_file, " YYSTYPE;\nextern YYSTYPE %slval;\n",
907 symbol_prefix);
911 static void
912 output_stored_text(void)
914 int c;
915 FILE *in, *out;
917 rewind(text_file);
918 if (text_file == NULL)
919 open_error("text_file");
920 in = text_file;
921 if ((c = getc(in)) == EOF)
922 return;
923 out = code_file;
924 write_char(out, c);
925 while ((c = getc(in)) != EOF)
927 write_char(out, c);
929 write_code_lineno(out);
932 static void
933 output_debug(void)
935 int i, j, k, max;
936 const char **symnam;
937 const char *s;
939 ++outline;
940 fprintf(code_file, "#define YYFINAL %d\n", final_state);
942 outline += 3;
943 fprintf(code_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n", tflag);
945 if (rflag)
947 fprintf(output_file, "#ifndef YYDEBUG\n");
948 fprintf(output_file, "#define YYDEBUG %d\n", tflag);
949 fprintf(output_file, "#endif\n");
952 max = 0;
953 for (i = 2; i < ntokens; ++i)
954 if (symbol_value[i] > max)
955 max = symbol_value[i];
957 ++outline;
958 fprintf(code_file, "#define YYMAXTOKEN %d\n", max);
960 symnam = (const char **)MALLOC((unsigned)(max + 1) * sizeof(char *));
961 if (symnam == 0)
962 no_space();
964 /* Note that it is not necessary to initialize the element */
965 /* symnam[max]. */
966 for (i = 0; i < max; ++i)
967 symnam[i] = 0;
968 for (i = ntokens - 1; i >= 2; --i)
969 symnam[symbol_value[i]] = symbol_name[i];
970 symnam[0] = "end-of-file";
972 output_line("#if YYDEBUG");
974 start_str_table("name");
975 j = 80;
976 for (i = 0; i <= max; ++i)
978 if ((s = symnam[i]) != 0)
980 if (s[0] == '"')
982 k = 7;
983 while (*++s != '"')
985 ++k;
986 if (*s == '\\')
988 k += 2;
989 if (*++s == '\\')
990 ++k;
993 j += k;
994 if (j > 80)
996 output_newline();
997 j = k;
999 fprintf(output_file, "\"\\\"");
1000 s = symnam[i];
1001 while (*++s != '"')
1003 if (*s == '\\')
1005 fprintf(output_file, "\\\\");
1006 if (*++s == '\\')
1007 fprintf(output_file, "\\\\");
1008 else
1009 putc(*s, output_file);
1011 else
1012 putc(*s, output_file);
1014 fprintf(output_file, "\\\"\",");
1016 else if (s[0] == '\'')
1018 if (s[1] == '"')
1020 j += 7;
1021 if (j > 80)
1023 output_newline();
1024 j = 7;
1026 fprintf(output_file, "\"'\\\"'\",");
1028 else
1030 k = 5;
1031 while (*++s != '\'')
1033 ++k;
1034 if (*s == '\\')
1036 k += 2;
1037 if (*++s == '\\')
1038 ++k;
1041 j += k;
1042 if (j > 80)
1044 output_newline();
1045 j = k;
1047 fprintf(output_file, "\"'");
1048 s = symnam[i];
1049 while (*++s != '\'')
1051 if (*s == '\\')
1053 fprintf(output_file, "\\\\");
1054 if (*++s == '\\')
1055 fprintf(output_file, "\\\\");
1056 else
1057 putc(*s, output_file);
1059 else
1060 putc(*s, output_file);
1062 fprintf(output_file, "'\",");
1065 else
1067 k = (int)strlen(s) + 3;
1068 j += k;
1069 if (j > 80)
1071 output_newline();
1072 j = k;
1074 putc('"', output_file);
1077 putc(*s, output_file);
1079 while (*++s);
1080 fprintf(output_file, "\",");
1083 else
1085 j += 2;
1086 if (j > 80)
1088 output_newline();
1089 j = 2;
1091 fprintf(output_file, "0,");
1094 end_table();
1095 FREE(symnam);
1097 start_str_table("rule");
1098 for (i = 2; i < nrules; ++i)
1100 fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
1101 for (j = rrhs[i]; ritem[j] > 0; ++j)
1103 s = symbol_name[ritem[j]];
1104 if (s[0] == '"')
1106 fprintf(output_file, " \\\"");
1107 while (*++s != '"')
1109 if (*s == '\\')
1111 if (s[1] == '\\')
1112 fprintf(output_file, "\\\\\\\\");
1113 else
1114 fprintf(output_file, "\\\\%c", s[1]);
1115 ++s;
1117 else
1118 putc(*s, output_file);
1120 fprintf(output_file, "\\\"");
1122 else if (s[0] == '\'')
1124 if (s[1] == '"')
1125 fprintf(output_file, " '\\\"'");
1126 else if (s[1] == '\\')
1128 if (s[2] == '\\')
1129 fprintf(output_file, " '\\\\\\\\");
1130 else
1131 fprintf(output_file, " '\\\\%c", s[2]);
1132 s += 2;
1133 while (*++s != '\'')
1134 putc(*s, output_file);
1135 putc('\'', output_file);
1137 else
1138 fprintf(output_file, " '%c'", s[1]);
1140 else
1141 fprintf(output_file, " %s", s);
1143 fprintf(output_file, "\",");
1144 output_newline();
1147 end_table();
1148 output_line("#endif");
1151 static void
1152 output_stype(void)
1154 if (!unionized && ntags == 0)
1156 outline += 3;
1157 fprintf(code_file, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
1161 static void
1162 output_trailing_text(void)
1164 int c, last;
1165 FILE *in, *out;
1167 if (line == 0)
1168 return;
1170 in = input_file;
1171 out = code_file;
1172 c = *cptr;
1173 if (c == '\n')
1175 ++lineno;
1176 if ((c = getc(in)) == EOF)
1177 return;
1178 write_input_lineno(out);
1179 write_char(out, c);
1180 last = c;
1182 else
1184 write_input_lineno(out);
1187 putc(c, out);
1189 while ((c = *++cptr) != '\n');
1190 write_char(out, c);
1191 last = '\n';
1194 while ((c = getc(in)) != EOF)
1196 write_char(out, c);
1197 last = c;
1200 if (last != '\n')
1202 write_char(out, '\n');
1204 write_code_lineno(out);
1207 static void
1208 output_semantic_actions(void)
1210 int c, last;
1211 FILE *out;
1213 rewind(action_file);
1214 if ((c = getc(action_file)) == EOF)
1215 return;
1217 out = code_file;
1218 last = c;
1219 write_char(out, c);
1220 while ((c = getc(action_file)) != EOF)
1222 write_char(out, c);
1223 last = c;
1226 if (last != '\n')
1228 write_char(out, '\n');
1231 write_code_lineno(out);
1234 static void
1235 free_itemsets(void)
1237 core *cp, *next;
1239 FREE(state_table);
1240 for (cp = first_state; cp; cp = next)
1242 next = cp->next;
1243 FREE(cp);
1247 static void
1248 free_shifts(void)
1250 shifts *sp, *next;
1252 FREE(shift_table);
1253 for (sp = first_shift; sp; sp = next)
1255 next = sp->next;
1256 FREE(sp);
1260 static void
1261 free_reductions(void)
1263 reductions *rp, *next;
1265 FREE(reduction_table);
1266 for (rp = first_reduction; rp; rp = next)
1268 next = rp->next;
1269 FREE(rp);
1273 void
1274 output(void)
1276 free_itemsets();
1277 free_shifts();
1278 free_reductions();
1279 write_section(banner, 0);
1280 output_yacc_decl();
1281 output_lex_decl();
1282 output_prefix();
1283 output_stored_text();
1284 output_defines();
1285 output_rule_data();
1286 output_yydefred();
1287 output_actions();
1288 free_parser();
1289 output_debug();
1290 output_stype();
1291 if (rflag)
1292 write_section(tables, 0);
1293 write_section(header, !pure_parser);
1294 output_trailing_text();
1295 write_section(body, pure_parser);
1296 output_semantic_actions();
1297 write_section(trailer, 0);
1300 #ifdef NO_LEAKS
1301 void
1302 output_leaks(void)
1304 DO_FREE(tally);
1305 DO_FREE(width);
1306 DO_FREE(order);
1308 #endif