mkfs: move directory entry manipulation
[minix.git] / external / bsd / byacc / dist / verbose.c
blobf068b49ff998a123483ccf9dcc2d75de2a12cc2f
1 /* $NetBSD: verbose.c,v 1.6 2011/09/10 21:29:04 christos Exp $ */
2 /* Id: verbose.c,v 1.9 2010/06/09 08:58:29 tom Exp */
4 #include "defs.h"
6 #include <sys/cdefs.h>
7 __RCSID("$NetBSD: verbose.c,v 1.6 2011/09/10 21:29:04 christos Exp $");
9 static void log_conflicts(void);
10 static void log_unused(void);
11 static void print_actions(int stateno);
12 static void print_conflicts(int state);
13 static void print_core(int state);
14 static void print_gotos(int stateno);
15 static void print_nulls(int state);
16 static void print_shifts(action *p);
17 static void print_state(int state);
18 static void print_reductions(action *p, int defred2);
20 static short *null_rules;
22 void
23 verbose(void)
25 int i;
27 if (!vflag)
28 return;
30 null_rules = (short *)MALLOC((unsigned)nrules * sizeof(short));
31 NO_SPACE(null_rules);
33 fprintf(verbose_file, "\f\n");
34 for (i = 0; i < nstates; i++)
35 print_state(i);
36 FREE(null_rules);
38 if (nunused)
39 log_unused();
40 if (SRtotal || RRtotal)
41 log_conflicts();
43 fprintf(verbose_file, "\n\n%d terminals, %d nonterminals\n", ntokens,
44 nvars);
45 fprintf(verbose_file, "%d grammar rules, %d states\n", nrules - 2, nstates);
48 static void
49 log_unused(void)
51 int i;
52 short *p;
54 fprintf(verbose_file, "\n\nRules never reduced:\n");
55 for (i = 3; i < nrules; ++i)
57 if (!rules_used[i])
59 fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
60 for (p = ritem + rrhs[i]; *p >= 0; ++p)
61 fprintf(verbose_file, " %s", symbol_name[*p]);
62 fprintf(verbose_file, " (%d)\n", i - 2);
67 static void
68 log_conflicts(void)
70 int i;
72 fprintf(verbose_file, "\n\n");
73 for (i = 0; i < nstates; i++)
75 if (SRconflicts[i] || RRconflicts[i])
77 fprintf(verbose_file, "State %d contains ", i);
78 if (SRconflicts[i] > 0)
79 fprintf(verbose_file, "%d shift/reduce conflict%s",
80 SRconflicts[i],
81 PLURAL(SRconflicts[i]));
82 if (SRconflicts[i] && RRconflicts[i])
83 fprintf(verbose_file, ", ");
84 if (RRconflicts[i] > 0)
85 fprintf(verbose_file, "%d reduce/reduce conflict%s",
86 RRconflicts[i],
87 PLURAL(RRconflicts[i]));
88 fprintf(verbose_file, ".\n");
93 static void
94 print_state(int state)
96 if (state)
97 fprintf(verbose_file, "\n\n");
98 if (SRconflicts[state] || RRconflicts[state])
99 print_conflicts(state);
100 fprintf(verbose_file, "state %d\n", state);
101 print_core(state);
102 print_nulls(state);
103 print_actions(state);
106 static void
107 print_conflicts(int state)
109 int symbol, act, number;
110 action *p;
112 act = 0; /* not shift/reduce... */
113 number = -1;
114 symbol = -1;
115 for (p = parser[state]; p; p = p->next)
117 if (p->suppressed == 2)
118 continue;
120 if (p->symbol != symbol)
122 symbol = p->symbol;
123 number = p->number;
124 if (p->action_code == SHIFT)
125 act = SHIFT;
126 else
127 act = REDUCE;
129 else if (p->suppressed == 1)
131 if (state == final_state && symbol == 0)
133 fprintf(verbose_file, "%d: shift/reduce conflict \
134 (accept, reduce %d) on $end\n", state, p->number - 2);
136 else
138 if (act == SHIFT)
140 fprintf(verbose_file, "%d: shift/reduce conflict \
141 (shift %d, reduce %d) on %s\n", state, number, p->number - 2,
142 symbol_name[symbol]);
144 else
146 fprintf(verbose_file, "%d: reduce/reduce conflict \
147 (reduce %d, reduce %d) on %s\n", state, number - 2, p->number - 2,
148 symbol_name[symbol]);
155 static void
156 print_core(int state)
158 int i;
159 int k;
160 int rule;
161 core *statep;
162 short *sp;
163 short *sp1;
165 statep = state_table[state];
166 k = statep->nitems;
168 for (i = 0; i < k; i++)
170 sp1 = sp = ritem + statep->items[i];
172 while (*sp >= 0)
173 ++sp;
174 rule = -(*sp);
175 fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
177 for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
178 fprintf(verbose_file, "%s ", symbol_name[*sp]);
180 putc('.', verbose_file);
182 while (*sp >= 0)
184 fprintf(verbose_file, " %s", symbol_name[*sp]);
185 sp++;
187 fprintf(verbose_file, " (%d)\n", -2 - *sp);
191 static void
192 print_nulls(int state)
194 action *p;
195 Value_t i, j, k, nnulls;
197 nnulls = 0;
198 for (p = parser[state]; p; p = p->next)
200 if (p->action_code == REDUCE &&
201 (p->suppressed == 0 || p->suppressed == 1))
203 i = p->number;
204 if (rrhs[i] + 1 == rrhs[i + 1])
206 for (j = 0; j < nnulls && i > null_rules[j]; ++j)
207 continue;
209 if (j == nnulls)
211 ++nnulls;
212 null_rules[j] = i;
214 else if (i != null_rules[j])
216 ++nnulls;
217 for (k = (Value_t) (nnulls - 1); k > j; --k)
218 null_rules[k] = null_rules[k - 1];
219 null_rules[j] = i;
225 for (i = 0; i < nnulls; ++i)
227 j = null_rules[i];
228 fprintf(verbose_file, "\t%s : . (%d)\n", symbol_name[rlhs[j]],
229 j - 2);
231 fprintf(verbose_file, "\n");
234 static void
235 print_actions(int stateno)
237 action *p;
238 shifts *sp;
239 int as;
241 if (stateno == final_state)
242 fprintf(verbose_file, "\t$end accept\n");
244 p = parser[stateno];
245 if (p)
247 print_shifts(p);
248 print_reductions(p, defred[stateno]);
251 sp = shift_table[stateno];
252 if (sp && sp->nshifts > 0)
254 as = accessing_symbol[sp->shift[sp->nshifts - 1]];
255 if (ISVAR(as))
256 print_gotos(stateno);
260 static void
261 print_shifts(action *p)
263 int count;
264 action *q;
266 count = 0;
267 for (q = p; q; q = q->next)
269 if (q->suppressed < 2 && q->action_code == SHIFT)
270 ++count;
273 if (count > 0)
275 for (; p; p = p->next)
277 if (p->action_code == SHIFT && p->suppressed == 0)
278 fprintf(verbose_file, "\t%s shift %d\n",
279 symbol_name[p->symbol], p->number);
284 static void
285 print_reductions(action *p, int defred2)
287 int k, anyreds;
288 action *q;
290 anyreds = 0;
291 for (q = p; q; q = q->next)
293 if (q->action_code == REDUCE && q->suppressed < 2)
295 anyreds = 1;
296 break;
300 if (anyreds == 0)
301 fprintf(verbose_file, "\t. error\n");
302 else
304 for (; p; p = p->next)
306 if (p->action_code == REDUCE && p->number != defred2)
308 k = p->number - 2;
309 if (p->suppressed == 0)
310 fprintf(verbose_file, "\t%s reduce %d\n",
311 symbol_name[p->symbol], k);
315 if (defred2 > 0)
316 fprintf(verbose_file, "\t. reduce %d\n", defred2 - 2);
320 static void
321 print_gotos(int stateno)
323 int i, k;
324 int as;
325 short *to_state2;
326 shifts *sp;
328 putc('\n', verbose_file);
329 sp = shift_table[stateno];
330 to_state2 = sp->shift;
331 for (i = 0; i < sp->nshifts; ++i)
333 k = to_state2[i];
334 as = accessing_symbol[k];
335 if (ISVAR(as))
336 fprintf(verbose_file, "\t%s goto %d\n", symbol_name[as], k);