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/verbose.c,v 1.6 1999/08/28 01:08:03 peter Exp $
40 static char const sccsid[] = "@(#)verbose.c 5.3 (Berkeley) 1/20/91";
46 static void log_unused __P((void));
47 static void log_conflicts __P((void));
48 static void print_actions __P((int));
49 static void print_conflicts __P((int));
50 static void print_core __P((int));
51 static void print_gotos __P((int));
52 static void print_nulls __P((int));
53 static void print_reductions __P((register action *, register int));
54 static void print_shifts __P((register action *));
55 static void print_state __P((int));
57 static short *null_rules;
66 null_rules = (short *) MALLOC(nrules*sizeof(short));
67 if (null_rules == 0) no_space();
68 fprintf(verbose_file, "\f\n");
69 for (i = 0; i < nstates; i++)
75 if (SRtotal || RRtotal)
78 fprintf(verbose_file, "\n\n%d terminals, %d nonterminals\n", ntokens,
80 fprintf(verbose_file, "%d grammar rules, %d states\n", nrules - 2, nstates);
90 fprintf(verbose_file, "\n\nRules never reduced:\n");
91 for (i = 3; i < nrules; ++i)
95 fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
96 for (p = ritem + rrhs[i]; *p >= 0; ++p)
97 fprintf(verbose_file, " %s", symbol_name[*p]);
98 fprintf(verbose_file, " (%d)\n", i - 2);
109 fprintf(verbose_file, "\n\n");
110 for (i = 0; i < nstates; i++)
112 if (SRconflicts[i] || RRconflicts[i])
114 fprintf(verbose_file, "State %d contains ", i);
115 if (SRconflicts[i] == 1)
116 fprintf(verbose_file, "1 shift/reduce conflict");
117 else if (SRconflicts[i] > 1)
118 fprintf(verbose_file, "%d shift/reduce conflicts",
120 if (SRconflicts[i] && RRconflicts[i])
121 fprintf(verbose_file, ", ");
122 if (RRconflicts[i] == 1)
123 fprintf(verbose_file, "1 reduce/reduce conflict");
124 else if (RRconflicts[i] > 1)
125 fprintf(verbose_file, "%d reduce/reduce conflicts",
127 fprintf(verbose_file, ".\n");
138 fprintf(verbose_file, "\n\n");
139 if (SRconflicts[state] || RRconflicts[state])
140 print_conflicts(state);
141 fprintf(verbose_file, "state %d\n", state);
144 print_actions(state);
149 print_conflicts(state)
152 register int symbol, act = 0, number = 0;
156 for (p = parser[state]; p; p = p->next)
158 if (p->suppressed == 2)
161 if (p->symbol != symbol)
165 if (p->action_code == SHIFT)
170 else if (p->suppressed == 1)
172 if (state == final_state && symbol == 0)
174 fprintf(verbose_file, "%d: shift/reduce conflict \
175 (accept, reduce %d) on $end\n", state, p->number - 2);
181 fprintf(verbose_file, "%d: shift/reduce conflict \
182 (shift %d, reduce %d) on %s\n", state, number, p->number - 2,
183 symbol_name[symbol]);
187 fprintf(verbose_file, "%d: reduce/reduce conflict \
188 (reduce %d, reduce %d) on %s\n", state, number - 2, p->number - 2,
189 symbol_name[symbol]);
204 register core *statep;
208 statep = state_table[state];
211 for (i = 0; i < k; i++)
213 sp1 = sp = ritem + statep->items[i];
215 while (*sp >= 0) ++sp;
217 fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
219 for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
220 fprintf(verbose_file, "%s ", symbol_name[*sp]);
222 putc('.', verbose_file);
226 fprintf(verbose_file, " %s", symbol_name[*sp]);
229 fprintf(verbose_file, " (%d)\n", -2 - *sp);
239 register int i, j, k, nnulls;
242 for (p = parser[state]; p; p = p->next)
244 if (p->action_code == REDUCE &&
245 (p->suppressed == 0 || p->suppressed == 1))
248 if (rrhs[i] + 1 == rrhs[i+1])
250 for (j = 0; j < nnulls && i > null_rules[j]; ++j)
258 else if (i != null_rules[j])
261 for (k = nnulls - 1; k > j; --k)
262 null_rules[k] = null_rules[k-1];
269 for (i = 0; i < nnulls; ++i)
272 fprintf(verbose_file, "\t%s : . (%d)\n", symbol_name[rlhs[j]],
275 fprintf(verbose_file, "\n");
280 print_actions(stateno)
287 if (stateno == final_state)
288 fprintf(verbose_file, "\t$end accept\n");
294 print_reductions(p, defred[stateno]);
297 sp = shift_table[stateno];
298 if (sp && sp->nshifts > 0)
300 as = accessing_symbol[sp->shift[sp->nshifts - 1]];
302 print_gotos(stateno);
315 for (q = p; q; q = q->next)
317 if (q->suppressed < 2 && q->action_code == SHIFT)
323 for (; p; p = p->next)
325 if (p->action_code == SHIFT && p->suppressed == 0)
326 fprintf(verbose_file, "\t%s shift %d\n",
327 symbol_name[p->symbol], p->number);
334 print_reductions(p, defred)
338 register int k, anyreds;
342 for (q = p; q ; q = q->next)
344 if (q->action_code == REDUCE && q->suppressed < 2)
352 fprintf(verbose_file, "\t. error\n");
355 for (; p; p = p->next)
357 if (p->action_code == REDUCE && p->number != defred)
360 if (p->suppressed == 0)
361 fprintf(verbose_file, "\t%s reduce %d\n",
362 symbol_name[p->symbol], k);
367 fprintf(verbose_file, "\t. reduce %d\n", defred - 2);
378 register short *to_state;
381 putc('\n', verbose_file);
382 sp = shift_table[stateno];
383 to_state = sp->shift;
384 for (i = 0; i < sp->nshifts; ++i)
387 as = accessing_symbol[k];
389 fprintf(verbose_file, "\t%s goto %d\n", symbol_name[as], k);