9 static short *state_count;
33 if (rflag) write_section(tables);
34 write_section(header);
35 output_trailing_text();
37 output_semantic_actions();
38 write_section(trailer);
48 fprintf(output_file, "short yylhs[] = {%42d,",
49 symbol_value[start_symbol]);
52 for (i = 3; i < nrules; i++)
56 if (!rflag) ++outline;
57 putc('\n', output_file);
63 fprintf(output_file, "%5d,", symbol_value[rlhs[i]]);
65 if (!rflag) outline += 2;
66 fprintf(output_file, "\n};\n");
68 fprintf(output_file, "short yylen[] = {%42d,", 2);
71 for (i = 3; i < nrules; i++)
75 if (!rflag) ++outline;
76 putc('\n', output_file);
82 fprintf(output_file, "%5d,", rrhs[i + 1] - rrhs[i] - 1);
84 if (!rflag) outline += 2;
85 fprintf(output_file, "\n};\n");
93 fprintf(output_file, "short yydefred[] = {%39d,",
94 (defred[0] ? defred[0] - 2 : 0));
97 for (i = 1; i < nstates; i++)
103 if (!rflag) ++outline;
104 putc('\n', output_file);
108 fprintf(output_file, "%5d,", (defred[i] ? defred[i] - 2 : 0));
111 if (!rflag) outline += 2;
112 fprintf(output_file, "\n};\n");
118 nvectors = 2*nstates + nvars;
120 froms = NEW2(nvectors, short *);
121 tos = NEW2(nvectors, short *);
122 tally = NEW2(nvectors, short);
123 width = NEW2(nvectors, short);
129 FREE(accessing_symbol);
132 FREE(goto_map + ntokens);
147 register int shiftcount, reducecount;
148 register int max, min;
149 register short *actionrow, *r, *s;
152 actionrow = NEW2(2*ntokens, short);
153 for (i = 0; i < nstates; ++i)
157 for (j = 0; j < 2*ntokens; ++j)
162 for (p = parser[i]; p; p = p->next)
164 if (p->suppressed == 0)
166 if (p->action_code == SHIFT)
169 actionrow[p->symbol] = p->number;
171 else if (p->action_code == REDUCE && p->number != defred[i])
174 actionrow[p->symbol + ntokens] = p->number;
179 tally[i] = shiftcount;
180 tally[nstates+i] = reducecount;
182 width[nstates+i] = 0;
185 froms[i] = r = NEW2(shiftcount, short);
186 tos[i] = s = NEW2(shiftcount, short);
189 for (j = 0; j < ntokens; ++j)
193 if (min > symbol_value[j])
194 min = symbol_value[j];
195 if (max < symbol_value[j])
196 max = symbol_value[j];
197 *r++ = symbol_value[j];
201 width[i] = max - min + 1;
205 froms[nstates+i] = r = NEW2(reducecount, short);
206 tos[nstates+i] = s = NEW2(reducecount, short);
209 for (j = 0; j < ntokens; ++j)
211 if (actionrow[ntokens+j])
213 if (min > symbol_value[j])
214 min = symbol_value[j];
215 if (max < symbol_value[j])
216 max = symbol_value[j];
217 *r++ = symbol_value[j];
218 *s++ = actionrow[ntokens+j] - 2;
221 width[nstates+i] = max - min + 1;
230 register int i, j, k;
232 state_count = NEW2(nstates, short);
234 k = default_goto(start_symbol + 1);
235 fprintf(output_file, "short yydgoto[] = {%40d,", k);
236 save_column(start_symbol + 1, k);
239 for (i = start_symbol + 2; i < nsyms; i++)
243 if (!rflag) ++outline;
244 putc('\n', output_file);
251 fprintf(output_file, "%5d,", k);
255 if (!rflag) outline += 2;
256 fprintf(output_file, "\n};\n");
267 register int default_state;
270 m = goto_map[symbol];
271 n = goto_map[symbol + 1];
273 if (m == n) return (0);
275 for (i = 0; i < nstates; i++)
278 for (i = m; i < n; i++)
279 state_count[to_state[i]]++;
283 for (i = 0; i < nstates; i++)
285 if (state_count[i] > max)
287 max = state_count[i];
292 return (default_state);
297 save_column(symbol, default_state)
310 m = goto_map[symbol];
311 n = goto_map[symbol + 1];
314 for (i = m; i < n; i++)
316 if (to_state[i] != default_state)
319 if (count == 0) return;
321 symno = symbol_value[symbol] + 2*nstates;
323 froms[symno] = sp1 = sp = NEW2(count, short);
324 tos[symno] = sp2 = NEW2(count, short);
326 for (i = m; i < n; i++)
328 if (to_state[i] != default_state)
330 *sp1++ = from_state[i];
331 *sp2++ = to_state[i];
335 tally[symno] = count;
336 width[symno] = sp1[-1] - sp[0] + 1;
347 order = NEW2(nvectors, short);
350 for (i = 0; i < nvectors; i++)
358 while (j >= 0 && (width[order[j]] < w))
361 while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
364 for (k = nentries - 1; k > j; k--)
365 order[k + 1] = order[k];
380 base = NEW2(nvectors, short);
381 pos = NEW2(nentries, short);
384 table = NEW2(maxtable, short);
385 check = NEW2(maxtable, short);
390 for (i = 0; i < maxtable; i++)
393 for (i = 0; i < nentries; i++)
395 state = matching_vector(i);
398 place = pack_vector(i);
403 base[order[i]] = place;
406 for (i = 0; i < nvectors; i++)
420 /* The function matching_vector determines if the vector specified by */
421 /* the input parameter matches a previously considered vector. The */
422 /* test at the start of the function checks if the vector represents */
423 /* a row of shifts over terminal symbols or a row of reductions, or a */
424 /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
425 /* check if a column of shifts over a nonterminal symbols matches a */
426 /* previously considered vector. Because of the nature of LR parsing */
427 /* tables, no two columns can match. Therefore, the only possible */
428 /* match would be between a row and a column. Such matches are */
429 /* unlikely. Therefore, to save time, no attempt is made to see if a */
430 /* column matches a previously considered vector. */
432 /* Matching_vector is poorly designed. The test could easily be made */
433 /* faster. Also, it depends on the vectors being in a specific */
437 matching_vector(vector)
455 for (prev = vector - 1; prev >= 0; prev--)
458 if (width[j] != w || tally[j] != t)
462 for (k = 0; match && k < t; k++)
464 if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
481 register int i, j, k, l;
485 register short *from;
496 j = lowzero - from[0];
497 for (k = 1; k < t; ++k)
498 if (lowzero - from[k] > j)
499 j = lowzero - from[k];
505 for (k = 0; ok && k < t; k++)
511 fatal("maximum table size exceeded");
514 do { newmax += 200; } while (newmax <= loc);
515 table = (short *) REALLOC(table, newmax*sizeof(short));
516 if (table == 0) no_space();
517 check = (short *) REALLOC(check, newmax*sizeof(short));
518 if (check == 0) no_space();
519 for (l = maxtable; l < newmax; ++l)
527 if (check[loc] != -1)
530 for (k = 0; ok && k < vector; k++)
537 for (k = 0; k < t; k++)
541 check[loc] = from[k];
542 if (loc > high) high = loc;
545 while (check[lowzero] != -1)
559 fprintf(output_file, "short yysindex[] = {%39d,", base[0]);
562 for (i = 1; i < nstates; i++)
566 if (!rflag) ++outline;
567 putc('\n', output_file);
573 fprintf(output_file, "%5d,", base[i]);
576 if (!rflag) outline += 2;
577 fprintf(output_file, "\n};\nshort yyrindex[] = {%39d,",
581 for (i = nstates + 1; i < 2*nstates; i++)
585 if (!rflag) ++outline;
586 putc('\n', output_file);
592 fprintf(output_file, "%5d,", base[i]);
595 if (!rflag) outline += 2;
596 fprintf(output_file, "\n};\nshort yygindex[] = {%39d,",
600 for (i = 2*nstates + 1; i < nvectors - 1; i++)
604 if (!rflag) ++outline;
605 putc('\n', output_file);
611 fprintf(output_file, "%5d,", base[i]);
614 if (!rflag) outline += 2;
615 fprintf(output_file, "\n};\n");
627 fprintf(code_file, "#define YYTABLESIZE %d\n", high);
628 fprintf(output_file, "short yytable[] = {%40d,", table[0]);
631 for (i = 1; i <= high; i++)
635 if (!rflag) ++outline;
636 putc('\n', output_file);
642 fprintf(output_file, "%5d,", table[i]);
645 if (!rflag) outline += 2;
646 fprintf(output_file, "\n};\n");
657 fprintf(output_file, "short yycheck[] = {%40d,", check[0]);
660 for (i = 1; i <= high; i++)
664 if (!rflag) ++outline;
665 putc('\n', output_file);
671 fprintf(output_file, "%5d,", check[i]);
674 if (!rflag) outline += 2;
675 fprintf(output_file, "\n};\n");
681 is_C_identifier(name)
692 if (!isalpha(c) && c != '_' && c != '$')
694 while ((c = *++s) != '"')
696 if (!isalnum(c) && c != '_' && c != '$')
702 if (!isalpha(c) && c != '_' && c != '$')
706 if (!isalnum(c) && c != '_' && c != '$')
718 for (i = 2; i < ntokens; ++i)
721 if (is_C_identifier(s))
723 fprintf(code_file, "#define ");
724 if (dflag) fprintf(defines_file, "#define ");
728 while ((c = *++s) != '"')
731 if (dflag) putc(c, defines_file);
739 if (dflag) putc(c, defines_file);
744 fprintf(code_file, " %d\n", symbol_value[i]);
745 if (dflag) fprintf(defines_file, " %d\n", symbol_value[i]);
750 fprintf(code_file, "#define YYERRCODE %d\n", symbol_value[1]);
752 if (dflag && unionized)
755 union_file = fopen(union_file_name, "r");
756 if (union_file == NULL) open_error(union_file_name);
757 while ((c = getc(union_file)) != EOF)
758 putc(c, defines_file);
759 fprintf(defines_file, " YYSTYPE;\nextern YYSTYPE yylval;\n");
767 register FILE *in, *out;
770 text_file = fopen(text_file_name, "r");
771 if (text_file == NULL)
772 open_error(text_file_name);
774 if ((c = getc(in)) == EOF)
780 while ((c = getc(in)) != EOF)
787 fprintf(out, line_format, ++outline + 1, code_file_name);
793 register int i, j, k, max;
797 fprintf(code_file, "#define YYFINAL %d\n", final_state);
799 fprintf(code_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
802 fprintf(output_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
806 for (i = 2; i < ntokens; ++i)
807 if (symbol_value[i] > max)
808 max = symbol_value[i];
810 fprintf(code_file, "#define YYMAXTOKEN %d\n", max);
812 symnam = (char **) MALLOC((max+1)*sizeof(char *));
813 if (symnam == 0) no_space();
815 /* Note that it is not necessary to initialize the element */
817 for (i = 0; i < max; ++i)
819 for (i = ntokens - 1; i >= 2; --i)
820 symnam[symbol_value[i]] = symbol_name[i];
821 symnam[0] = "end-of-file";
823 if (!rflag) ++outline;
824 fprintf(output_file, "#if YYDEBUG\nchar *yyname[] = {");
826 for (i = 0; i <= max; ++i)
846 if (!rflag) ++outline;
847 putc('\n', output_file);
850 fprintf(output_file, "\"\\\"");
856 fprintf(output_file, "\\\\");
858 fprintf(output_file, "\\\\");
860 putc(*s, output_file);
863 putc(*s, output_file);
865 fprintf(output_file, "\\\"\",");
867 else if (s[0] == '\'')
874 if (!rflag) ++outline;
875 putc('\n', output_file);
878 fprintf(output_file, "\"'\\\"'\",");
896 if (!rflag) ++outline;
897 putc('\n', output_file);
900 fprintf(output_file, "\"'");
906 fprintf(output_file, "\\\\");
908 fprintf(output_file, "\\\\");
910 putc(*s, output_file);
913 putc(*s, output_file);
915 fprintf(output_file, "'\",");
924 if (!rflag) ++outline;
925 putc('\n', output_file);
928 putc('"', output_file);
929 do { putc(*s, output_file); } while (*++s);
930 fprintf(output_file, "\",");
938 if (!rflag) ++outline;
939 putc('\n', output_file);
942 fprintf(output_file, "0,");
945 if (!rflag) outline += 2;
946 fprintf(output_file, "\n};\n");
949 if (!rflag) ++outline;
950 fprintf(output_file, "char *yyrule[] = {\n");
951 for (i = 2; i < nrules; ++i)
953 fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
954 for (j = rrhs[i]; ritem[j] > 0; ++j)
956 s = symbol_name[ritem[j]];
959 fprintf(output_file, " \\\"");
965 fprintf(output_file, "\\\\\\\\");
967 fprintf(output_file, "\\\\%c", s[1]);
971 putc(*s, output_file);
973 fprintf(output_file, "\\\"");
975 else if (s[0] == '\'')
978 fprintf(output_file, " '\\\"'");
979 else if (s[1] == '\\')
982 fprintf(output_file, " '\\\\\\\\");
984 fprintf(output_file, " '\\\\%c", s[2]);
987 putc(*s, output_file);
988 putc('\'', output_file);
991 fprintf(output_file, " '%c'", s[1]);
994 fprintf(output_file, " %s", s);
996 if (!rflag) ++outline;
997 fprintf(output_file, "\",\n");
1000 if (!rflag) outline += 2;
1001 fprintf(output_file, "};\n#endif\n");
1007 if (!unionized && ntags == 0)
1010 fprintf(code_file, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
1015 output_trailing_text()
1017 register int c, last;
1018 register FILE *in, *out;
1029 if ((c = getc(in)) == EOF)
1034 fprintf(out, line_format, lineno, input_file_name);
1046 fprintf(out, line_format, lineno, input_file_name);
1048 do { putc(c, out); } while ((c = *++cptr) != '\n');
1054 while ((c = getc(in)) != EOF)
1068 fprintf(out, line_format, ++outline + 1, code_file_name);
1072 output_semantic_actions()
1074 register int c, last;
1077 fclose(action_file);
1078 action_file = fopen(action_file_name, "r");
1079 if (action_file == NULL)
1080 open_error(action_file_name);
1082 if ((c = getc(action_file)) == EOF)
1090 while ((c = getc(action_file)) != EOF)
1105 fprintf(out, line_format, ++outline + 1, code_file_name);
1111 register core *cp, *next;
1114 for (cp = first_state; cp; cp = next)
1124 register shifts *sp, *next;
1127 for (sp = first_shift; sp; sp = next)
1138 register reductions *rp, *next;
1140 FREE(reduction_table);
1141 for (rp = first_reduction; rp; rp = next)