2 * Copyright (c) 1989 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $FreeBSD: src/usr.bin/yacc/output.c,v 1.13.2.3 2001/10/05 03:03:14 obrien Exp $
40 static char const sccsid[] = "@(#)output.c 5.7 (Berkeley) 5/24/93";
47 static int default_goto __P((int));
48 static void free_itemsets __P((void));
49 static void free_reductions __P((void));
50 static void free_shifts __P((void));
51 static void goto_actions __P((void));
52 static int is_C_identifier __P((char *));
53 static int matching_vector __P((int));
54 static void output_actions __P((void));
55 static void output_base __P((void));
56 static void output_check __P((void));
57 static void output_debug __P((void));
58 static void output_defines __P((void));
59 static void output_prefix __P((void));
60 static void output_rule_data __P((void));
61 static void output_semantic_actions __P((void));
62 static void output_stored_text __P((void));
63 static void output_stype __P((void));
64 static void output_table __P((void));
65 static void output_trailing_text __P((void));
66 static void output_yydefred __P((void));
67 static void pack_table __P((void));
68 static int pack_vector __P((int));
69 static void save_column __P((int, int));
70 static void sort_actions __P((void));
71 static void token_actions __P((void));
72 static int increase_maxtable __P((int));
74 static const char line_format[] = "#line %d \"%s\"\n";
81 static short *state_count;
107 if (rflag) write_section(tables);
108 write_section(header);
109 output_trailing_text();
111 output_semantic_actions();
112 write_section(trailer);
119 if (symbol_prefix == NULL)
120 symbol_prefix = "yy";
124 fprintf(code_file, "#define yyparse %sparse\n", symbol_prefix);
126 fprintf(code_file, "#define yylex %slex\n", symbol_prefix);
128 fprintf(code_file, "#define yyerror %serror\n", symbol_prefix);
130 fprintf(code_file, "#define yychar %schar\n", symbol_prefix);
132 fprintf(code_file, "#define yyval %sval\n", symbol_prefix);
134 fprintf(code_file, "#define yylval %slval\n", symbol_prefix);
136 fprintf(code_file, "#define yydebug %sdebug\n", symbol_prefix);
138 fprintf(code_file, "#define yynerrs %snerrs\n", symbol_prefix);
140 fprintf(code_file, "#define yyerrflag %serrflag\n", symbol_prefix);
142 fprintf(code_file, "#define yyss %sss\n", symbol_prefix);
144 fprintf(code_file, "#define yyssp %sssp\n", symbol_prefix);
146 fprintf(code_file, "#define yyvs %svs\n", symbol_prefix);
148 fprintf(code_file, "#define yyvsp %svsp\n", symbol_prefix);
150 fprintf(code_file, "#define yylhs %slhs\n", symbol_prefix);
152 fprintf(code_file, "#define yylen %slen\n", symbol_prefix);
154 fprintf(code_file, "#define yydefred %sdefred\n", symbol_prefix);
156 fprintf(code_file, "#define yydgoto %sdgoto\n", symbol_prefix);
158 fprintf(code_file, "#define yysindex %ssindex\n", symbol_prefix);
160 fprintf(code_file, "#define yyrindex %srindex\n", symbol_prefix);
162 fprintf(code_file, "#define yygindex %sgindex\n", symbol_prefix);
164 fprintf(code_file, "#define yytable %stable\n", symbol_prefix);
166 fprintf(code_file, "#define yycheck %scheck\n", symbol_prefix);
168 fprintf(code_file, "#define yyname %sname\n", symbol_prefix);
170 fprintf(code_file, "#define yyrule %srule\n", symbol_prefix);
172 fprintf(code_file, "#define yysslim %ssslim\n", symbol_prefix);
174 fprintf(code_file, "#define yystacksize %sstacksize\n", symbol_prefix);
177 fprintf(code_file, "#define YYPREFIX \"%s\"\n", symbol_prefix);
188 fprintf(output_file, "const short %slhs[] = {%42d,", symbol_prefix,
189 symbol_value[start_symbol]);
192 for (i = 3; i < nrules; i++)
196 if (!rflag) ++outline;
197 putc('\n', output_file);
203 fprintf(output_file, "%5d,", symbol_value[rlhs[i]]);
205 if (!rflag) outline += 2;
206 fprintf(output_file, "\n};\n");
208 fprintf(output_file, "const short %slen[] = {%42d,", symbol_prefix, 2);
211 for (i = 3; i < nrules; i++)
215 if (!rflag) ++outline;
216 putc('\n', output_file);
222 fprintf(output_file, "%5d,", rrhs[i + 1] - rrhs[i] - 1);
224 if (!rflag) outline += 2;
225 fprintf(output_file, "\n};\n");
234 fprintf(output_file, "const short %sdefred[] = {%39d,", symbol_prefix,
235 (defred[0] ? defred[0] - 2 : 0));
238 for (i = 1; i < nstates; i++)
244 if (!rflag) ++outline;
245 putc('\n', output_file);
249 fprintf(output_file, "%5d,", (defred[i] ? defred[i] - 2 : 0));
252 if (!rflag) outline += 2;
253 fprintf(output_file, "\n};\n");
260 nvectors = 2*nstates + nvars;
262 froms = NEW2(nvectors, short *);
263 tos = NEW2(nvectors, short *);
264 tally = NEW2(nvectors, short);
265 width = NEW2(nvectors, short);
271 FREE(accessing_symbol);
274 FREE(goto_map + ntokens);
290 register int shiftcount, reducecount;
291 register int max, min;
292 register short *actionrow, *r, *s;
295 actionrow = NEW2(2*ntokens, short);
296 for (i = 0; i < nstates; ++i)
300 for (j = 0; j < 2*ntokens; ++j)
305 for (p = parser[i]; p; p = p->next)
307 if (p->suppressed == 0)
309 if (p->action_code == SHIFT)
312 actionrow[p->symbol] = p->number;
314 else if (p->action_code == REDUCE && p->number != defred[i])
317 actionrow[p->symbol + ntokens] = p->number;
322 tally[i] = shiftcount;
323 tally[nstates+i] = reducecount;
325 width[nstates+i] = 0;
328 froms[i] = r = NEW2(shiftcount, short);
329 tos[i] = s = NEW2(shiftcount, short);
332 for (j = 0; j < ntokens; ++j)
336 if (min > symbol_value[j])
337 min = symbol_value[j];
338 if (max < symbol_value[j])
339 max = symbol_value[j];
340 *r++ = symbol_value[j];
344 width[i] = max - min + 1;
348 froms[nstates+i] = r = NEW2(reducecount, short);
349 tos[nstates+i] = s = NEW2(reducecount, short);
352 for (j = 0; j < ntokens; ++j)
354 if (actionrow[ntokens+j])
356 if (min > symbol_value[j])
357 min = symbol_value[j];
358 if (max < symbol_value[j])
359 max = symbol_value[j];
360 *r++ = symbol_value[j];
361 *s++ = actionrow[ntokens+j] - 2;
364 width[nstates+i] = max - min + 1;
374 register int i, j, k;
376 state_count = NEW2(nstates, short);
378 k = default_goto(start_symbol + 1);
379 fprintf(output_file, "const short %sdgoto[] = {%40d,", symbol_prefix, k);
380 save_column(start_symbol + 1, k);
383 for (i = start_symbol + 2; i < nsyms; i++)
387 if (!rflag) ++outline;
388 putc('\n', output_file);
395 fprintf(output_file, "%5d,", k);
399 if (!rflag) outline += 2;
400 fprintf(output_file, "\n};\n");
411 register int default_state;
414 m = goto_map[symbol];
415 n = goto_map[symbol + 1];
417 if (m == n) return (0);
419 for (i = 0; i < nstates; i++)
422 for (i = m; i < n; i++)
423 state_count[to_state[i]]++;
427 for (i = 0; i < nstates; i++)
429 if (state_count[i] > max)
431 max = state_count[i];
436 return (default_state);
442 save_column(symbol, default_state)
455 m = goto_map[symbol];
456 n = goto_map[symbol + 1];
459 for (i = m; i < n; i++)
461 if (to_state[i] != default_state)
464 if (count == 0) return;
466 symno = symbol_value[symbol] + 2*nstates;
468 froms[symno] = sp1 = sp = NEW2(count, short);
469 tos[symno] = sp2 = NEW2(count, short);
471 for (i = m; i < n; i++)
473 if (to_state[i] != default_state)
475 *sp1++ = from_state[i];
476 *sp2++ = to_state[i];
480 tally[symno] = count;
481 width[symno] = sp1[-1] - sp[0] + 1;
493 order = NEW2(nvectors, short);
496 for (i = 0; i < nvectors; i++)
504 while (j >= 0 && (width[order[j]] < w))
507 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
510 for (k = nentries - 1; k > j; k--)
511 order[k + 1] = order[k];
527 base = NEW2(nvectors, short);
528 pos = NEW2(nentries, short);
531 table = NEW2(maxtable, short);
532 check = NEW2(maxtable, short);
537 for (i = 0; i < maxtable; i++)
540 for (i = 0; i < nentries; i++)
542 state = matching_vector(i);
545 place = pack_vector(i);
550 base[order[i]] = place;
553 for (i = 0; i < nvectors; i++)
567 /* The function matching_vector determines if the vector specified by */
568 /* the input parameter matches a previously considered vector. The */
569 /* test at the start of the function checks if the vector represents */
570 /* a row of shifts over terminal symbols or a row of reductions, or a */
571 /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
572 /* check if a column of shifts over a nonterminal symbols matches a */
573 /* previously considered vector. Because of the nature of LR parsing */
574 /* tables, no two columns can match. Therefore, the only possible */
575 /* match would be between a row and a column. Such matches are */
576 /* unlikely. Therefore, to save time, no attempt is made to see if a */
577 /* column matches a previously considered vector. */
579 /* Matching_vector is poorly designed. The test could easily be made */
580 /* faster. Also, it depends on the vectors being in a specific */
584 matching_vector(vector)
602 for (prev = vector - 1; prev >= 0; prev--)
605 if (width[j] != w || tally[j] != t)
609 for (k = 0; match && k < t; k++)
611 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
628 register int i, j, k, l;
632 register short *from;
642 j = lowzero - from[0];
643 for (k = 1; k < t; ++k)
644 if (lowzero - from[k] > j)
645 j = lowzero - from[k];
651 for (k = 0; ok && k < t; k++)
657 fatal("maximum table size exceeded");
658 maxtable = increase_maxtable(loc);
661 if (check[loc] != -1)
664 for (k = 0; ok && k < vector; k++)
671 for (k = 0; k < t; k++)
675 check[loc] = from[k];
676 if (loc > high) high = loc;
679 while (check[lowzero] != -1)
681 if (lowzero >= maxtable)
683 if (lowzero >= MAXTABLE)
685 fatal("maximum table size exceeded in check\n");
688 maxtable = increase_maxtable(loc);
706 fprintf(output_file, "const short %ssindex[] = {%39d,", symbol_prefix,
710 for (i = 1; i < nstates; i++)
714 if (!rflag) ++outline;
715 putc('\n', output_file);
721 fprintf(output_file, "%5d,", base[i]);
724 if (!rflag) outline += 2;
725 fprintf(output_file, "\n};\nconst short %srindex[] = {%39d,", symbol_prefix,
729 for (i = nstates + 1; i < 2*nstates; i++)
733 if (!rflag) ++outline;
734 putc('\n', output_file);
740 fprintf(output_file, "%5d,", base[i]);
743 if (!rflag) outline += 2;
744 fprintf(output_file, "\n};\nconst short %sgindex[] = {%39d,", symbol_prefix,
748 for (i = 2*nstates + 1; i < nvectors - 1; i++)
752 if (!rflag) ++outline;
753 putc('\n', output_file);
759 fprintf(output_file, "%5d,", base[i]);
762 if (!rflag) outline += 2;
763 fprintf(output_file, "\n};\n");
776 fprintf(code_file, "#define YYTABLESIZE %d\n", high);
777 fprintf(output_file, "const short %stable[] = {%40d,", symbol_prefix,
781 for (i = 1; i <= high; i++)
785 if (!rflag) ++outline;
786 putc('\n', output_file);
792 fprintf(output_file, "%5d,", table[i]);
795 if (!rflag) outline += 2;
796 fprintf(output_file, "\n};\n");
808 fprintf(output_file, "const short %scheck[] = {%40d,", symbol_prefix,
812 for (i = 1; i <= high; i++)
816 if (!rflag) ++outline;
817 putc('\n', output_file);
823 fprintf(output_file, "%5d,", check[i]);
826 if (!rflag) outline += 2;
827 fprintf(output_file, "\n};\n");
833 is_C_identifier(name)
844 if (!isalpha(c) && c != '_' && c != '$')
846 while ((c = *++s) != '"')
848 if (!isalnum(c) && c != '_' && c != '$')
854 if (!isalpha(c) && c != '_' && c != '$')
858 if (!isalnum(c) && c != '_' && c != '$')
872 fprintf(code_file, "#define YYERRCODE %d\n", symbol_value[1]);
876 fprintf(defines_file, "#ifndef YYERRCODE\n");
877 fprintf(defines_file, "#define YYERRCODE %d\n", symbol_value[1]);
878 fprintf(defines_file, "#endif\n\n");
880 for (i = 2; i < ntokens; ++i)
883 if (is_C_identifier(s))
885 fprintf(code_file, "#define ");
886 if (dflag) fprintf(defines_file, "#define ");
890 while ((c = *++s) != '"')
893 if (dflag) putc(c, defines_file);
901 if (dflag) putc(c, defines_file);
906 fprintf(code_file, " %d\n", symbol_value[i]);
907 if (dflag) fprintf(defines_file, " %d\n", symbol_value[i]);
911 if (dflag && unionized)
914 union_file = fopen(union_file_name, "r");
915 if (union_file == NULL) open_error(union_file_name);
916 while ((c = getc(union_file)) != EOF)
917 putc(c, defines_file);
918 fprintf(defines_file, " YYSTYPE;\nextern YYSTYPE %slval;\n",
928 register FILE *in, *out;
931 text_file = fopen(text_file_name, "r");
932 if (text_file == NULL)
933 open_error(text_file_name);
935 if ((c = getc(in)) == EOF)
941 while ((c = getc(in)) != EOF)
948 fprintf(out, line_format, ++outline + 1, code_file_name);
955 register int i, j, k, max;
959 fprintf(code_file, "#define YYFINAL %d\n", final_state);
961 fprintf(code_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
964 fprintf(output_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
968 for (i = 2; i < ntokens; ++i)
969 if (symbol_value[i] > max)
970 max = symbol_value[i];
972 fprintf(code_file, "#define YYMAXTOKEN %d\n", max);
974 symnam = (char **) MALLOC((max+1)*sizeof(char *));
975 if (symnam == 0) no_space();
977 /* Note that it is not necessary to initialize the element */
979 for (i = 0; i < max; ++i)
981 for (i = ntokens - 1; i >= 2; --i)
982 symnam[symbol_value[i]] = symbol_name[i];
983 symnam[0] = "end-of-file";
985 if (!rflag) ++outline;
986 fprintf(output_file, "#if YYDEBUG\n");
987 fprintf(output_file, "const char * const %sname[] = {", symbol_prefix);
989 for (i = 0; i <= max; ++i)
1009 if (!rflag) ++outline;
1010 putc('\n', output_file);
1013 fprintf(output_file, "\"\\\"");
1019 fprintf(output_file, "\\\\");
1021 fprintf(output_file, "\\\\");
1023 putc(*s, output_file);
1026 putc(*s, output_file);
1028 fprintf(output_file, "\\\"\",");
1030 else if (s[0] == '\'')
1037 if (!rflag) ++outline;
1038 putc('\n', output_file);
1041 fprintf(output_file, "\"'\\\"'\",");
1046 while (*++s != '\'')
1059 if (!rflag) ++outline;
1060 putc('\n', output_file);
1063 fprintf(output_file, "\"'");
1065 while (*++s != '\'')
1069 fprintf(output_file, "\\\\");
1071 fprintf(output_file, "\\\\");
1073 putc(*s, output_file);
1076 putc(*s, output_file);
1078 fprintf(output_file, "'\",");
1087 if (!rflag) ++outline;
1088 putc('\n', output_file);
1091 putc('"', output_file);
1092 do { putc(*s, output_file); } while (*++s);
1093 fprintf(output_file, "\",");
1101 if (!rflag) ++outline;
1102 putc('\n', output_file);
1105 fprintf(output_file, "0,");
1108 if (!rflag) outline += 2;
1109 fprintf(output_file, "\n};\n");
1112 if (!rflag) ++outline;
1113 fprintf(output_file, "const char * const %srule[] = {\n", symbol_prefix);
1114 for (i = 2; i < nrules; ++i)
1116 fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
1117 for (j = rrhs[i]; ritem[j] > 0; ++j)
1119 s = symbol_name[ritem[j]];
1122 fprintf(output_file, " \\\"");
1128 fprintf(output_file, "\\\\\\\\");
1130 fprintf(output_file, "\\\\%c", s[1]);
1134 putc(*s, output_file);
1136 fprintf(output_file, "\\\"");
1138 else if (s[0] == '\'')
1141 fprintf(output_file, " '\\\"'");
1142 else if (s[1] == '\\')
1145 fprintf(output_file, " '\\\\\\\\");
1147 fprintf(output_file, " '\\\\%c", s[2]);
1149 while (*++s != '\'')
1150 putc(*s, output_file);
1151 putc('\'', output_file);
1154 fprintf(output_file, " '%c'", s[1]);
1157 fprintf(output_file, " %s", s);
1159 if (!rflag) ++outline;
1160 fprintf(output_file, "\",\n");
1163 if (!rflag) outline += 2;
1164 fprintf(output_file, "};\n#endif\n");
1171 if (!unionized && ntags == 0)
1174 fprintf(code_file, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
1180 output_trailing_text()
1182 register int c, last;
1183 register FILE *in, *out;
1194 if ((c = getc(in)) == EOF)
1199 fprintf(out, line_format, lineno, input_file_name);
1211 fprintf(out, line_format, lineno, input_file_name);
1213 do { putc(c, out); } while ((c = *++cptr) != '\n');
1219 while ((c = getc(in)) != EOF)
1233 fprintf(out, line_format, ++outline + 1, code_file_name);
1238 output_semantic_actions()
1240 register int c, last;
1243 fclose(action_file);
1244 action_file = fopen(action_file_name, "r");
1245 if (action_file == NULL)
1246 open_error(action_file_name);
1248 if ((c = getc(action_file)) == EOF)
1256 while ((c = getc(action_file)) != EOF)
1271 fprintf(out, line_format, ++outline + 1, code_file_name);
1278 register core *cp, *next;
1281 for (cp = first_state; cp; cp = next)
1292 register shifts *sp, *next;
1295 for (sp = first_shift; sp; sp = next)
1307 register reductions *rp, *next;
1309 FREE(reduction_table);
1310 for (rp = first_reduction; rp; rp = next)
1320 * inputs - loc location in table
1321 * output - size increased to
1322 * side effects - table is increase by at least 200 short words
1326 increase_maxtable(int loc)
1333 do { newmax += 200; } while (newmax <= loc);
1334 table = (short *) REALLOC(table, newmax*sizeof(short));
1335 if (table == 0) no_space();
1336 check = (short *) REALLOC(check, newmax*sizeof(short));
1337 if (check == 0) no_space();
1338 for (l = maxtable; l < newmax; ++l)