1 /* $Id: verbose.c,v 1.13 2020/09/10 17:57:34 tom Exp $ */
5 static void log_conflicts(void);
6 static void log_unused(void);
7 static void print_actions(int stateno);
8 static void print_conflicts(int state);
9 static void print_core(int state);
10 static void print_gotos(int stateno);
11 static void print_nulls(int state);
12 static void print_shifts(action *p);
13 static void print_state(int state);
14 static void print_reductions(action *p, int defred2);
16 static Value_t *null_rules;
26 null_rules = TMALLOC(Value_t, nrules);
29 fprintf(verbose_file, "\f\n");
30 for (i = 0; i < nstates; i++)
36 if (SRtotal || RRtotal)
39 fprintf(verbose_file, "\n\n%d terminals, %d nonterminals\n", ntokens,
41 fprintf(verbose_file, "%d grammar rules, %d states\n", nrules - 2, nstates);
43 { /* print out the grammar symbol # and parser internal symbol # for each
44 symbol as an aide to writing the implementation for YYDESTRUCT_CALL()
45 and YYSTYPE_TOSTRING() */
48 fputs("\ngrammar parser grammar\n", verbose_file);
49 fputs("symbol# value# symbol\n", verbose_file);
50 for (i = 0; i < ntokens; ++i)
52 fprintf(verbose_file, " %5d %5d %s\n",
53 i, symbol_value[i], symbol_name[i]);
54 if (symbol_value[i] > maxtok)
55 maxtok = symbol_value[i];
57 for (i = ntokens; i < nsyms; ++i)
58 fprintf(verbose_file, " %5d %5d %s\n",
59 i, (maxtok + 1) + symbol_value[i] + 1, symbol_name[i]);
70 fprintf(verbose_file, "\n\nRules never reduced:\n");
71 for (i = 3; i < nrules; ++i)
75 fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
76 for (p = ritem + rrhs[i]; *p >= 0; ++p)
77 fprintf(verbose_file, " %s", symbol_name[*p]);
78 fprintf(verbose_file, " (%d)\n", i - 2);
88 fprintf(verbose_file, "\n\n");
89 for (i = 0; i < nstates; i++)
91 if (SRconflicts[i] || RRconflicts[i])
93 fprintf(verbose_file, "State %d contains ", i);
94 if (SRconflicts[i] > 0)
95 fprintf(verbose_file, "%d shift/reduce conflict%s",
97 PLURAL(SRconflicts[i]));
98 if (SRconflicts[i] && RRconflicts[i])
99 fprintf(verbose_file, ", ");
100 if (RRconflicts[i] > 0)
101 fprintf(verbose_file, "%d reduce/reduce conflict%s",
103 PLURAL(RRconflicts[i]));
104 fprintf(verbose_file, ".\n");
110 print_state(int state)
113 fprintf(verbose_file, "\n\n");
114 if (SRconflicts[state] || RRconflicts[state])
115 print_conflicts(state);
116 fprintf(verbose_file, "state %d\n", state);
119 print_actions(state);
123 print_conflicts(int state)
125 int symbol, act, number;
128 act = 0; /* not shift/reduce... */
131 for (p = parser[state]; p; p = p->next)
133 if (p->suppressed == 2)
136 if (p->symbol != symbol)
140 if (p->action_code == SHIFT)
145 else if (p->suppressed == 1)
147 if (state == final_state && symbol == 0)
149 fprintf(verbose_file, "%d: shift/reduce conflict \
150 (accept, reduce %d) on $end\n", state, p->number - 2);
156 fprintf(verbose_file, "%d: shift/reduce conflict \
157 (shift %d, reduce %d) on %s\n", state, number, p->number - 2,
158 symbol_name[symbol]);
162 fprintf(verbose_file, "%d: reduce/reduce conflict \
163 (reduce %d, reduce %d) on %s\n", state, number - 2, p->number - 2,
164 symbol_name[symbol]);
172 print_core(int state)
175 core *statep = state_table[state];
176 int k = statep->nitems;
178 for (i = 0; i < k; i++)
181 Value_t *sp = ritem + statep->items[i];
187 fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
189 for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
190 fprintf(verbose_file, "%s ", symbol_name[*sp]);
192 putc('.', verbose_file);
196 fprintf(verbose_file, " %s", symbol_name[*sp]);
199 fprintf(verbose_file, " (%d)\n", -2 - *sp);
204 print_nulls(int state)
207 Value_t i, j, k, nnulls;
210 for (p = parser[state]; p; p = p->next)
212 if (p->action_code == REDUCE &&
213 (p->suppressed == 0 || p->suppressed == 1))
216 if (rrhs[i] + 1 == rrhs[i + 1])
218 for (j = 0; j < nnulls && i > null_rules[j]; ++j)
226 else if (i != null_rules[j])
229 for (k = (Value_t)(nnulls - 1); k > j; --k)
230 null_rules[k] = null_rules[k - 1];
237 for (i = 0; i < nnulls; ++i)
240 fprintf(verbose_file, "\t%s : . (%d)\n", symbol_name[rlhs[j]],
243 fprintf(verbose_file, "\n");
247 print_actions(int stateno)
252 if (stateno == final_state)
253 fprintf(verbose_file, "\t$end accept\n");
259 print_reductions(p, defred[stateno]);
262 sp = shift_table[stateno];
263 if (sp && sp->nshifts > 0)
265 int as = accessing_symbol[sp->shift[sp->nshifts - 1]];
268 print_gotos(stateno);
273 print_shifts(action *p)
279 for (q = p; q; q = q->next)
281 if (q->suppressed < 2 && q->action_code == SHIFT)
287 for (; p; p = p->next)
289 if (p->action_code == SHIFT && p->suppressed == 0)
290 fprintf(verbose_file, "\t%s shift %d\n",
291 symbol_name[p->symbol], p->number);
292 #if defined(YYBTYACC)
293 if (backtrack && p->action_code == SHIFT && p->suppressed == 1)
294 fprintf(verbose_file, "\t%s [trial] shift %d\n",
295 symbol_name[p->symbol], p->number);
302 print_reductions(action *p, int defred2)
308 for (q = p; q; q = q->next)
310 if (q->action_code == REDUCE && q->suppressed < 2)
318 fprintf(verbose_file, "\t. error\n");
321 for (; p; p = p->next)
323 if (p->action_code == REDUCE && p->number != defred2)
325 int k = p->number - 2;
327 if (p->suppressed == 0)
328 fprintf(verbose_file, "\t%s reduce %d\n",
329 symbol_name[p->symbol], k);
330 #if defined(YYBTYACC)
331 if (backtrack && p->suppressed == 1)
332 fprintf(verbose_file, "\t%s [trial] reduce %d\n",
333 symbol_name[p->symbol], k);
339 fprintf(verbose_file, "\t. reduce %d\n", defred2 - 2);
344 print_gotos(int stateno)
350 putc('\n', verbose_file);
351 sp = shift_table[stateno];
352 to_state2 = sp->shift;
353 for (i = 0; i < sp->nshifts; ++i)
355 int k = to_state2[i];
356 int as = accessing_symbol[k];
359 fprintf(verbose_file, "\t%s goto %d\n", symbol_name[as], k);