1 /* $Id: output.c,v 1.24 2010/02/17 01:48:22 tom Exp $ */
5 #define StaticOrR (rflag ? "" : "static ")
9 static Value_t **froms;
11 static Value_t *tally;
12 static Value_t *width;
13 static Value_t *state_count;
14 static Value_t *order;
18 static Value_t *table;
19 static Value_t *check;
24 write_char(FILE * out, int c)
32 write_code_lineno(FILE * out)
35 fprintf(out, line_format, (outline++) + 1, code_file_name);
39 write_input_lineno(FILE * out)
44 fprintf(out, line_format, lineno, input_file_name);
49 define_prefixed(FILE * fp, const char *name)
55 fprintf(fp, "#ifndef %s\n", name);
58 fprintf(fp, "#define %-10s %s%s\n", name, symbol_prefix, name + 2);
61 fprintf(fp, "#endif /* %s */\n", name);
65 output_prefix(FILE * fp)
67 if (symbol_prefix == NULL)
73 define_prefixed(fp, "yyparse");
74 define_prefixed(fp, "yylex");
75 define_prefixed(fp, "yyerror");
76 define_prefixed(fp, "yychar");
77 define_prefixed(fp, "yyval");
78 define_prefixed(fp, "yylval");
79 define_prefixed(fp, "yydebug");
80 define_prefixed(fp, "yynerrs");
81 define_prefixed(fp, "yyerrflag");
82 define_prefixed(fp, "yylhs");
83 define_prefixed(fp, "yylen");
84 define_prefixed(fp, "yydefred");
85 define_prefixed(fp, "yydgoto");
86 define_prefixed(fp, "yysindex");
87 define_prefixed(fp, "yyrindex");
88 define_prefixed(fp, "yygindex");
89 define_prefixed(fp, "yytable");
90 define_prefixed(fp, "yycheck");
91 define_prefixed(fp, "yyname");
92 define_prefixed(fp, "yyrule");
95 fprintf(fp, "#define YYPREFIX \"%s\"\n", symbol_prefix);
103 putc('\n', output_file);
107 output_line(const char *value)
109 fputs(value, output_file);
114 output_int(int value)
116 fprintf(output_file, "%5d,", value);
120 start_int_table(const char *name, int value)
122 int need = 34 - (int)(strlen(symbol_prefix) + strlen(name));
127 "%sconst short %s%s[] = {%*d,",
128 StaticOrR, symbol_prefix, name, need, value);
132 start_str_table(const char *name)
135 "%sconst char *%s%s[] = {",
136 StaticOrR, "yy", name);
148 output_rule_data(void)
153 start_int_table("lhs", symbol_value[start_symbol]);
156 for (i = 3; i < nrules; i++)
166 output_int(symbol_value[rlhs[i]]);
170 start_int_table("len", 2);
173 for (i = 3; i < nrules; i++)
183 output_int(rrhs[i + 1] - rrhs[i] - 1);
189 output_yydefred(void)
193 start_int_table("defred", (defred[0] ? defred[0] - 2 : 0));
196 for (i = 1; i < nstates; i++)
206 output_int((defred[i] ? defred[i] - 2 : 0));
216 Value_t shiftcount, reducecount;
218 Value_t *actionrow, *r, *s;
221 actionrow = NEW2(2 * ntokens, Value_t);
222 for (i = 0; i < nstates; ++i)
226 for (j = 0; j < 2 * ntokens; ++j)
231 for (p = parser[i]; p; p = p->next)
233 if (p->suppressed == 0)
235 if (p->action_code == SHIFT)
238 actionrow[p->symbol] = p->number;
240 else if (p->action_code == REDUCE && p->number != defred[i])
243 actionrow[p->symbol + ntokens] = p->number;
248 tally[i] = shiftcount;
249 tally[nstates + i] = reducecount;
251 width[nstates + i] = 0;
254 froms[i] = r = NEW2(shiftcount, Value_t);
255 tos[i] = s = NEW2(shiftcount, Value_t);
258 for (j = 0; j < ntokens; ++j)
262 if (min > symbol_value[j])
263 min = symbol_value[j];
264 if (max < symbol_value[j])
265 max = symbol_value[j];
266 *r++ = symbol_value[j];
270 width[i] = (Value_t) (max - min + 1);
274 froms[nstates + i] = r = NEW2(reducecount, Value_t);
275 tos[nstates + i] = s = NEW2(reducecount, Value_t);
278 for (j = 0; j < ntokens; ++j)
280 if (actionrow[ntokens + j])
282 if (min > symbol_value[j])
283 min = symbol_value[j];
284 if (max < symbol_value[j])
285 max = symbol_value[j];
286 *r++ = symbol_value[j];
287 *s++ = (Value_t) (actionrow[ntokens + j] - 2);
290 width[nstates + i] = (Value_t) (max - min + 1);
298 default_goto(int symbol)
306 m = goto_map[symbol];
307 n = goto_map[symbol + 1];
312 for (i = 0; i < nstates; i++)
315 for (i = m; i < n; i++)
316 state_count[to_state[i]]++;
320 for (i = 0; i < nstates; i++)
322 if (state_count[i] > max)
324 max = state_count[i];
329 return (default_state);
333 save_column(int symbol, int default_state)
344 m = goto_map[symbol];
345 n = goto_map[symbol + 1];
348 for (i = m; i < n; i++)
350 if (to_state[i] != default_state)
356 symno = symbol_value[symbol] + 2 * nstates;
358 froms[symno] = sp1 = sp = NEW2(count, Value_t);
359 tos[symno] = sp2 = NEW2(count, Value_t);
361 for (i = m; i < n; i++)
363 if (to_state[i] != default_state)
365 *sp1++ = from_state[i];
366 *sp2++ = to_state[i];
370 tally[symno] = count;
371 width[symno] = (Value_t) (sp1[-1] - sp[0] + 1);
379 state_count = NEW2(nstates, Value_t);
381 k = default_goto(start_symbol + 1);
382 start_int_table("dgoto", k);
383 save_column(start_symbol + 1, k);
386 for (i = start_symbol + 2; i < nsyms; i++)
414 order = NEW2(nvectors, Value_t);
417 for (i = 0; i < nvectors; i++)
425 while (j >= 0 && (width[order[j]] < w))
428 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
431 for (k = nentries - 1; k > j; k--)
432 order[k + 1] = order[k];
440 /* The function matching_vector determines if the vector specified by */
441 /* the input parameter matches a previously considered vector. The */
442 /* test at the start of the function checks if the vector represents */
443 /* a row of shifts over terminal symbols or a row of reductions, or a */
444 /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
445 /* check if a column of shifts over a nonterminal symbols matches a */
446 /* previously considered vector. Because of the nature of LR parsing */
447 /* tables, no two columns can match. Therefore, the only possible */
448 /* match would be between a row and a column. Such matches are */
449 /* unlikely. Therefore, to save time, no attempt is made to see if a */
450 /* column matches a previously considered vector. */
452 /* Matching_vector is poorly designed. The test could easily be made */
453 /* faster. Also, it depends on the vectors being in a specific */
457 matching_vector(int vector)
468 if (i >= 2 * nstates)
474 for (prev = vector - 1; prev >= 0; prev--)
477 if (width[j] != w || tally[j] != t)
481 for (k = 0; match && k < t; k++)
483 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
495 pack_vector(int vector)
512 j = lowzero - from[0];
513 for (k = 1; k < t; ++k)
514 if (lowzero - from[k] > j)
515 j = lowzero - from[k];
521 for (k = 0; ok && k < t; k++)
524 if (loc >= maxtable - 1)
526 if (loc >= MAXTABLE - 1)
527 fatal("maximum table size exceeded");
534 while (newmax <= loc);
535 table = (Value_t *) REALLOC(table, (unsigned)newmax * sizeof(Value_t));
538 check = (Value_t *) REALLOC(check, (unsigned)newmax * sizeof(Value_t));
541 for (l = maxtable; l < newmax; ++l)
549 if (check[loc] != -1)
552 for (k = 0; ok && k < vector; k++)
559 for (k = 0; k < t; k++)
563 check[loc] = from[k];
568 while (check[lowzero] != -1)
583 base = NEW2(nvectors, Value_t);
584 pos = NEW2(nentries, Value_t);
587 table = NEW2(maxtable, Value_t);
588 check = NEW2(maxtable, Value_t);
593 for (i = 0; i < maxtable; i++)
596 for (i = 0; i < nentries; i++)
598 state = matching_vector(i);
601 place = (Value_t) pack_vector(i);
606 base[order[i]] = place;
609 for (i = 0; i < nvectors; i++)
627 start_int_table("sindex", base[0]);
630 for (i = 1; i < nstates; i++)
645 start_int_table("rindex", base[nstates]);
648 for (i = nstates + 1; i < 2 * nstates; i++)
663 start_int_table("gindex", base[2 * nstates]);
666 for (i = 2 * nstates + 1; i < nvectors - 1; i++)
690 fprintf(code_file, "#define YYTABLESIZE %d\n", high);
691 start_int_table("table", table[0]);
694 for (i = 1; i <= high; i++)
704 output_int(table[i]);
717 start_int_table("check", check[0]);
720 for (i = 1; i <= high; i++)
730 output_int(check[i]);
740 nvectors = 2 * nstates + nvars;
742 froms = NEW2(nvectors, Value_t *);
743 tos = NEW2(nvectors, Value_t *);
744 tally = NEW2(nvectors, Value_t);
745 width = NEW2(nvectors, Value_t);
751 FREE(accessing_symbol);
754 FREE(goto_map + ntokens);
766 is_C_identifier(char *name)
776 if (!isalpha(c) && c != '_' && c != '$')
778 while ((c = *++s) != '"')
780 if (!isalnum(c) && c != '_' && c != '$')
786 if (!isalpha(c) && c != '_' && c != '$')
788 while ((c = *++s) != 0)
790 if (!isalnum(c) && c != '_' && c != '$')
802 for (i = 2; i < ntokens; ++i)
805 if (is_C_identifier(s))
807 fprintf(code_file, "#define ");
809 fprintf(defines_file, "#define ");
813 while ((c = *++s) != '"')
817 putc(c, defines_file);
826 putc(c, defines_file);
828 while ((c = *++s) != 0);
831 fprintf(code_file, " %d\n", symbol_value[i]);
833 fprintf(defines_file, " %d\n", symbol_value[i]);
838 fprintf(code_file, "#define YYERRCODE %d\n", symbol_value[1]);
840 if (dflag && unionized)
843 while ((c = getc(union_file)) != EOF)
844 putc(c, defines_file);
845 fprintf(defines_file, " YYSTYPE;\nextern YYSTYPE %slval;\n",
851 output_stored_text(void)
857 if (text_file == NULL)
858 open_error("text_file");
860 if ((c = getc(in)) == EOF)
864 while ((c = getc(in)) != EOF)
868 write_code_lineno(out);
879 fprintf(code_file, "#define YYFINAL %d\n", final_state);
882 fprintf(code_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n", tflag);
886 fprintf(output_file, "#ifndef YYDEBUG\n");
887 fprintf(output_file, "#define YYDEBUG %d\n", tflag);
888 fprintf(output_file, "#endif\n");
892 for (i = 2; i < ntokens; ++i)
893 if (symbol_value[i] > max)
894 max = symbol_value[i];
897 fprintf(code_file, "#define YYMAXTOKEN %d\n", max);
899 symnam = (const char **)MALLOC((unsigned)(max + 1) * sizeof(char *));
903 /* Note that it is not necessary to initialize the element */
905 for (i = 0; i < max; ++i)
907 for (i = ntokens - 1; i >= 2; --i)
908 symnam[symbol_value[i]] = symbol_name[i];
909 symnam[0] = "end-of-file";
911 output_line("#if YYDEBUG");
913 start_str_table("name");
915 for (i = 0; i <= max; ++i)
917 if ((s = symnam[i]) != 0)
938 fprintf(output_file, "\"\\\"");
944 fprintf(output_file, "\\\\");
946 fprintf(output_file, "\\\\");
948 putc(*s, output_file);
951 putc(*s, output_file);
953 fprintf(output_file, "\\\"\",");
955 else if (s[0] == '\'')
965 fprintf(output_file, "\"'\\\"'\",");
986 fprintf(output_file, "\"'");
992 fprintf(output_file, "\\\\");
994 fprintf(output_file, "\\\\");
996 putc(*s, output_file);
999 putc(*s, output_file);
1001 fprintf(output_file, "'\",");
1006 k = (int)strlen(s) + 3;
1013 putc('"', output_file);
1016 putc(*s, output_file);
1019 fprintf(output_file, "\",");
1030 fprintf(output_file, "0,");
1036 start_str_table("rule");
1037 for (i = 2; i < nrules; ++i)
1039 fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
1040 for (j = rrhs[i]; ritem[j] > 0; ++j)
1042 s = symbol_name[ritem[j]];
1045 fprintf(output_file, " \\\"");
1051 fprintf(output_file, "\\\\\\\\");
1053 fprintf(output_file, "\\\\%c", s[1]);
1057 putc(*s, output_file);
1059 fprintf(output_file, "\\\"");
1061 else if (s[0] == '\'')
1064 fprintf(output_file, " '\\\"'");
1065 else if (s[1] == '\\')
1068 fprintf(output_file, " '\\\\\\\\");
1070 fprintf(output_file, " '\\\\%c", s[2]);
1072 while (*++s != '\'')
1073 putc(*s, output_file);
1074 putc('\'', output_file);
1077 fprintf(output_file, " '%c'", s[1]);
1080 fprintf(output_file, " %s", s);
1082 fprintf(output_file, "\",");
1087 output_line("#endif");
1091 output_pure_parser(void)
1094 fprintf(code_file, "\n#define YYPURE %d\n\n", pure_parser);
1100 if (!unionized && ntags == 0)
1103 fprintf(code_file, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
1108 output_trailing_text(void)
1122 if ((c = getc(in)) == EOF)
1124 write_input_lineno(out);
1130 write_input_lineno(out);
1135 while ((c = *++cptr) != '\n');
1140 while ((c = getc(in)) != EOF)
1148 write_char(out, '\n');
1150 write_code_lineno(out);
1154 output_semantic_actions(void)
1159 rewind(action_file);
1160 if ((c = getc(action_file)) == EOF)
1166 while ((c = getc(action_file)) != EOF)
1174 write_char(out, '\n');
1177 write_code_lineno(out);
1186 for (cp = first_state; cp; cp = next)
1199 for (sp = first_shift; sp; sp = next)
1207 free_reductions(void)
1209 reductions *rp, *next;
1211 FREE(reduction_table);
1212 for (rp = first_reduction; rp; rp = next)
1225 output_prefix(output_file);
1226 write_section(xdecls);
1227 output_stored_text();
1237 output_prefix(code_file);
1238 write_section(xdecls);
1239 write_section(tables);
1241 write_section(hdr_defs);
1242 output_pure_parser();
1245 write_section(hdr_vars);
1247 output_trailing_text();
1248 write_section(body_1);
1251 write_section(body_vars);
1253 write_section(body_2);
1254 output_semantic_actions();
1255 write_section(trailer);