1 /* $Id: verbose.c,v 1.14 2021/05/20 23:57:23 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%ld terminals, %ld nonterminals\n",
40 (long)ntokens, (long)nvars);
41 fprintf(verbose_file, "%ld grammar rules, %ld states\n",
42 (long)(nrules - 2), (long)nstates);
44 { /* print out the grammar symbol # and parser internal symbol # for each
45 symbol as an aide to writing the implementation for YYDESTRUCT_CALL()
46 and YYSTYPE_TOSTRING() */
49 fputs("\ngrammar parser grammar\n", verbose_file);
50 fputs("symbol# value# symbol\n", verbose_file);
51 for (i = 0; i < ntokens; ++i)
53 fprintf(verbose_file, " %5d %5d %s\n",
54 i, symbol_value[i], symbol_name[i]);
55 if (symbol_value[i] > maxtok)
56 maxtok = symbol_value[i];
58 for (i = ntokens; i < nsyms; ++i)
59 fprintf(verbose_file, " %5d %5d %s\n",
60 i, (maxtok + 1) + symbol_value[i] + 1, symbol_name[i]);
71 fprintf(verbose_file, "\n\nRules never reduced:\n");
72 for (i = 3; i < nrules; ++i)
76 fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
77 for (p = ritem + rrhs[i]; *p >= 0; ++p)
78 fprintf(verbose_file, " %s", symbol_name[*p]);
79 fprintf(verbose_file, " (%d)\n", i - 2);
89 fprintf(verbose_file, "\n\n");
90 for (i = 0; i < nstates; i++)
92 if (SRconflicts[i] || RRconflicts[i])
94 fprintf(verbose_file, "State %d contains ", i);
95 if (SRconflicts[i] > 0)
96 fprintf(verbose_file, "%ld shift/reduce conflict%s",
98 PLURAL(SRconflicts[i]));
99 if (SRconflicts[i] && RRconflicts[i])
100 fprintf(verbose_file, ", ");
101 if (RRconflicts[i] > 0)
102 fprintf(verbose_file, "%ld reduce/reduce conflict%s",
103 (long)RRconflicts[i],
104 PLURAL(RRconflicts[i]));
105 fprintf(verbose_file, ".\n");
111 print_state(int state)
114 fprintf(verbose_file, "\n\n");
115 if (SRconflicts[state] || RRconflicts[state])
116 print_conflicts(state);
117 fprintf(verbose_file, "state %d\n", state);
120 print_actions(state);
124 print_conflicts(int state)
126 int symbol, act, number;
129 act = 0; /* not shift/reduce... */
132 for (p = parser[state]; p; p = p->next)
134 if (p->suppressed == 2)
137 if (p->symbol != symbol)
141 if (p->action_code == SHIFT)
146 else if (p->suppressed == 1)
148 if (state == final_state && symbol == 0)
150 fprintf(verbose_file, "%d: shift/reduce conflict \
151 (accept, reduce %ld) on $end\n", state, (long)(p->number - 2));
157 fprintf(verbose_file, "%d: shift/reduce conflict \
158 (shift %ld, reduce %ld) on %s\n", state, (long)number, (long)(p->number - 2),
159 symbol_name[symbol]);
163 fprintf(verbose_file, "%d: reduce/reduce conflict \
164 (reduce %ld, reduce %ld) on %s\n", state, (long)(number - 2), (long)(p->number - 2),
165 symbol_name[symbol]);
173 print_core(int state)
176 core *statep = state_table[state];
177 int k = statep->nitems;
179 for (i = 0; i < k; i++)
182 Value_t *sp = ritem + statep->items[i];
188 fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
190 for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
191 fprintf(verbose_file, "%s ", symbol_name[*sp]);
193 putc('.', verbose_file);
197 fprintf(verbose_file, " %s", symbol_name[*sp]);
200 fprintf(verbose_file, " (%ld)\n", (long)(-2 - *sp));
205 print_nulls(int state)
208 Value_t i, j, k, nnulls;
211 for (p = parser[state]; p; p = p->next)
213 if (p->action_code == REDUCE &&
214 (p->suppressed == 0 || p->suppressed == 1))
217 if (rrhs[i] + 1 == rrhs[i + 1])
219 for (j = 0; j < nnulls && i > null_rules[j]; ++j)
227 else if (i != null_rules[j])
230 for (k = (Value_t)(nnulls - 1); k > j; --k)
231 null_rules[k] = null_rules[k - 1];
238 for (i = 0; i < nnulls; ++i)
241 fprintf(verbose_file, "\t%s : . (%ld)\n", symbol_name[rlhs[j]],
244 fprintf(verbose_file, "\n");
248 print_actions(int stateno)
253 if (stateno == final_state)
254 fprintf(verbose_file, "\t$end accept\n");
260 print_reductions(p, defred[stateno]);
263 sp = shift_table[stateno];
264 if (sp && sp->nshifts > 0)
266 int as = accessing_symbol[sp->shift[sp->nshifts - 1]];
269 print_gotos(stateno);
274 print_shifts(action *p)
280 for (q = p; q; q = q->next)
282 if (q->suppressed < 2 && q->action_code == SHIFT)
288 for (; p; p = p->next)
290 if (p->action_code == SHIFT && p->suppressed == 0)
291 fprintf(verbose_file, "\t%s shift %ld\n",
292 symbol_name[p->symbol], (long)p->number);
293 #if defined(YYBTYACC)
294 if (backtrack && p->action_code == SHIFT && p->suppressed == 1)
295 fprintf(verbose_file, "\t%s [trial] shift %d\n",
296 symbol_name[p->symbol], p->number);
303 print_reductions(action *p, int defred2)
309 for (q = p; q; q = q->next)
311 if (q->action_code == REDUCE && q->suppressed < 2)
319 fprintf(verbose_file, "\t. error\n");
322 for (; p; p = p->next)
324 if (p->action_code == REDUCE && p->number != defred2)
326 int k = p->number - 2;
328 if (p->suppressed == 0)
329 fprintf(verbose_file, "\t%s reduce %d\n",
330 symbol_name[p->symbol], k);
331 #if defined(YYBTYACC)
332 if (backtrack && p->suppressed == 1)
333 fprintf(verbose_file, "\t%s [trial] reduce %d\n",
334 symbol_name[p->symbol], k);
340 fprintf(verbose_file, "\t. reduce %d\n", defred2 - 2);
345 print_gotos(int stateno)
351 putc('\n', verbose_file);
352 sp = shift_table[stateno];
353 to_state2 = sp->shift;
354 for (i = 0; i < sp->nshifts; ++i)
356 int k = to_state2[i];
357 int as = accessing_symbol[k];
360 fprintf(verbose_file, "\t%s goto %d\n", symbol_name[as], k);