sdhci - Implement ADMA2 transfer support. Keep SDMA support for now.
[dragonfly.git] / contrib / byacc / verbose.c
blobb4e110212abff094184193493f5336227028e7e9
1 /* $Id: verbose.c,v 1.11 2014/04/01 23:15:59 Tom.Shields Exp $ */
3 #include "defs.h"
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;
18 void
19 verbose(void)
21 int i;
23 if (!vflag)
24 return;
26 null_rules = TMALLOC(Value_t, nrules);
27 NO_SPACE(null_rules);
29 fprintf(verbose_file, "\f\n");
30 for (i = 0; i < nstates; i++)
31 print_state(i);
32 FREE(null_rules);
34 if (nunused)
35 log_unused();
36 if (SRtotal || RRtotal)
37 log_conflicts();
39 fprintf(verbose_file, "\n\n%d terminals, %d nonterminals\n", ntokens,
40 nvars);
41 fprintf(verbose_file, "%d grammar rules, %d states\n", nrules - 2, nstates);
42 #if defined(YYBTYACC)
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() */
46 int maxtok = 0;
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]);
61 #endif
64 static void
65 log_unused(void)
67 int i;
68 Value_t *p;
70 fprintf(verbose_file, "\n\nRules never reduced:\n");
71 for (i = 3; i < nrules; ++i)
73 if (!rules_used[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);
83 static void
84 log_conflicts(void)
86 int i;
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",
96 SRconflicts[i],
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",
102 RRconflicts[i],
103 PLURAL(RRconflicts[i]));
104 fprintf(verbose_file, ".\n");
109 static void
110 print_state(int state)
112 if (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);
117 print_core(state);
118 print_nulls(state);
119 print_actions(state);
122 static void
123 print_conflicts(int state)
125 int symbol, act, number;
126 action *p;
128 act = 0; /* not shift/reduce... */
129 number = -1;
130 symbol = -1;
131 for (p = parser[state]; p; p = p->next)
133 if (p->suppressed == 2)
134 continue;
136 if (p->symbol != symbol)
138 symbol = p->symbol;
139 number = p->number;
140 if (p->action_code == SHIFT)
141 act = SHIFT;
142 else
143 act = REDUCE;
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);
152 else
154 if (act == SHIFT)
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]);
160 else
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]);
171 static void
172 print_core(int state)
174 int i;
175 int k;
176 int rule;
177 core *statep;
178 Value_t *sp;
179 Value_t *sp1;
181 statep = state_table[state];
182 k = statep->nitems;
184 for (i = 0; i < k; i++)
186 sp1 = sp = ritem + statep->items[i];
188 while (*sp >= 0)
189 ++sp;
190 rule = -(*sp);
191 fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
193 for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
194 fprintf(verbose_file, "%s ", symbol_name[*sp]);
196 putc('.', verbose_file);
198 while (*sp >= 0)
200 fprintf(verbose_file, " %s", symbol_name[*sp]);
201 sp++;
203 fprintf(verbose_file, " (%d)\n", -2 - *sp);
207 static void
208 print_nulls(int state)
210 action *p;
211 Value_t i, j, k, nnulls;
213 nnulls = 0;
214 for (p = parser[state]; p; p = p->next)
216 if (p->action_code == REDUCE &&
217 (p->suppressed == 0 || p->suppressed == 1))
219 i = p->number;
220 if (rrhs[i] + 1 == rrhs[i + 1])
222 for (j = 0; j < nnulls && i > null_rules[j]; ++j)
223 continue;
225 if (j == nnulls)
227 ++nnulls;
228 null_rules[j] = i;
230 else if (i != null_rules[j])
232 ++nnulls;
233 for (k = (Value_t) (nnulls - 1); k > j; --k)
234 null_rules[k] = null_rules[k - 1];
235 null_rules[j] = i;
241 for (i = 0; i < nnulls; ++i)
243 j = null_rules[i];
244 fprintf(verbose_file, "\t%s : . (%d)\n", symbol_name[rlhs[j]],
245 j - 2);
247 fprintf(verbose_file, "\n");
250 static void
251 print_actions(int stateno)
253 action *p;
254 shifts *sp;
255 int as;
257 if (stateno == final_state)
258 fprintf(verbose_file, "\t$end accept\n");
260 p = parser[stateno];
261 if (p)
263 print_shifts(p);
264 print_reductions(p, defred[stateno]);
267 sp = shift_table[stateno];
268 if (sp && sp->nshifts > 0)
270 as = accessing_symbol[sp->shift[sp->nshifts - 1]];
271 if (ISVAR(as))
272 print_gotos(stateno);
276 static void
277 print_shifts(action *p)
279 int count;
280 action *q;
282 count = 0;
283 for (q = p; q; q = q->next)
285 if (q->suppressed < 2 && q->action_code == SHIFT)
286 ++count;
289 if (count > 0)
291 for (; p; p = p->next)
293 if (p->action_code == SHIFT && p->suppressed == 0)
294 fprintf(verbose_file, "\t%s shift %d\n",
295 symbol_name[p->symbol], p->number);
296 #if defined(YYBTYACC)
297 if (backtrack && p->action_code == SHIFT && p->suppressed == 1)
298 fprintf(verbose_file, "\t%s [trial] shift %d\n",
299 symbol_name[p->symbol], p->number);
300 #endif
305 static void
306 print_reductions(action *p, int defred2)
308 int k, anyreds;
309 action *q;
311 anyreds = 0;
312 for (q = p; q; q = q->next)
314 if (q->action_code == REDUCE && q->suppressed < 2)
316 anyreds = 1;
317 break;
321 if (anyreds == 0)
322 fprintf(verbose_file, "\t. error\n");
323 else
325 for (; p; p = p->next)
327 if (p->action_code == REDUCE && p->number != defred2)
329 k = p->number - 2;
330 if (p->suppressed == 0)
331 fprintf(verbose_file, "\t%s reduce %d\n",
332 symbol_name[p->symbol], k);
333 #if defined(YYBTYACC)
334 if (backtrack && p->suppressed == 1)
335 fprintf(verbose_file, "\t%s [trial] reduce %d\n",
336 symbol_name[p->symbol], k);
337 #endif
341 if (defred2 > 0)
342 fprintf(verbose_file, "\t. reduce %d\n", defred2 - 2);
346 static void
347 print_gotos(int stateno)
349 int i, k;
350 int as;
351 Value_t *to_state2;
352 shifts *sp;
354 putc('\n', verbose_file);
355 sp = shift_table[stateno];
356 to_state2 = sp->shift;
357 for (i = 0; i < sp->nshifts; ++i)
359 k = to_state2[i];
360 as = accessing_symbol[k];
361 if (ISVAR(as))
362 fprintf(verbose_file, "\t%s goto %d\n", symbol_name[as], k);