Remove building with NOCRYPTO option
[minix.git] / external / bsd / byacc / dist / verbose.c
blob9a9ba3c193dc30a22ab7812b4f224be4d338f63e
1 /* $NetBSD: verbose.c,v 1.8 2015/01/03 23:22:52 christos Exp $ */
3 /* Id: verbose.c,v 1.11 2014/04/01 23:15:59 Tom.Shields Exp */
5 #include "defs.h"
7 #include <sys/cdefs.h>
8 __RCSID("$NetBSD: verbose.c,v 1.8 2015/01/03 23:22:52 christos Exp $");
10 static void log_conflicts(void);
11 static void log_unused(void);
12 static void print_actions(int stateno);
13 static void print_conflicts(int state);
14 static void print_core(int state);
15 static void print_gotos(int stateno);
16 static void print_nulls(int state);
17 static void print_shifts(action *p);
18 static void print_state(int state);
19 static void print_reductions(action *p, int defred2);
21 static Value_t *null_rules;
23 void
24 verbose(void)
26 int i;
28 if (!vflag)
29 return;
31 null_rules = TMALLOC(Value_t, nrules);
32 NO_SPACE(null_rules);
34 fprintf(verbose_file, "\f\n");
35 for (i = 0; i < nstates; i++)
36 print_state(i);
37 FREE(null_rules);
39 if (nunused)
40 log_unused();
41 if (SRtotal || RRtotal)
42 log_conflicts();
44 fprintf(verbose_file, "\n\n%d terminals, %d nonterminals\n", ntokens,
45 nvars);
46 fprintf(verbose_file, "%d grammar rules, %d states\n", nrules - 2, nstates);
47 #if defined(YYBTYACC)
48 { /* print out the grammar symbol # and parser internal symbol # for each
49 symbol as an aide to writing the implementation for YYDESTRUCT_CALL()
50 and YYSTYPE_TOSTRING() */
51 int maxtok = 0;
53 fputs("\ngrammar parser grammar\n", verbose_file);
54 fputs("symbol# value# symbol\n", verbose_file);
55 for (i = 0; i < ntokens; ++i)
57 fprintf(verbose_file, " %5d %5d %s\n",
58 i, symbol_value[i], symbol_name[i]);
59 if (symbol_value[i] > maxtok)
60 maxtok = symbol_value[i];
62 for (i = ntokens; i < nsyms; ++i)
63 fprintf(verbose_file, " %5d %5d %s\n",
64 i, (maxtok + 1) + symbol_value[i] + 1, symbol_name[i]);
66 #endif
69 static void
70 log_unused(void)
72 int i;
73 Value_t *p;
75 fprintf(verbose_file, "\n\nRules never reduced:\n");
76 for (i = 3; i < nrules; ++i)
78 if (!rules_used[i])
80 fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
81 for (p = ritem + rrhs[i]; *p >= 0; ++p)
82 fprintf(verbose_file, " %s", symbol_name[*p]);
83 fprintf(verbose_file, " (%d)\n", i - 2);
88 static void
89 log_conflicts(void)
91 int i;
93 fprintf(verbose_file, "\n\n");
94 for (i = 0; i < nstates; i++)
96 if (SRconflicts[i] || RRconflicts[i])
98 fprintf(verbose_file, "State %d contains ", i);
99 if (SRconflicts[i] > 0)
100 fprintf(verbose_file, "%d shift/reduce conflict%s",
101 SRconflicts[i],
102 PLURAL(SRconflicts[i]));
103 if (SRconflicts[i] && RRconflicts[i])
104 fprintf(verbose_file, ", ");
105 if (RRconflicts[i] > 0)
106 fprintf(verbose_file, "%d reduce/reduce conflict%s",
107 RRconflicts[i],
108 PLURAL(RRconflicts[i]));
109 fprintf(verbose_file, ".\n");
114 static void
115 print_state(int state)
117 if (state)
118 fprintf(verbose_file, "\n\n");
119 if (SRconflicts[state] || RRconflicts[state])
120 print_conflicts(state);
121 fprintf(verbose_file, "state %d\n", state);
122 print_core(state);
123 print_nulls(state);
124 print_actions(state);
127 static void
128 print_conflicts(int state)
130 int symbol, act, number;
131 action *p;
133 act = 0; /* not shift/reduce... */
134 number = -1;
135 symbol = -1;
136 for (p = parser[state]; p; p = p->next)
138 if (p->suppressed == 2)
139 continue;
141 if (p->symbol != symbol)
143 symbol = p->symbol;
144 number = p->number;
145 if (p->action_code == SHIFT)
146 act = SHIFT;
147 else
148 act = REDUCE;
150 else if (p->suppressed == 1)
152 if (state == final_state && symbol == 0)
154 fprintf(verbose_file, "%d: shift/reduce conflict \
155 (accept, reduce %d) on $end\n", state, p->number - 2);
157 else
159 if (act == SHIFT)
161 fprintf(verbose_file, "%d: shift/reduce conflict \
162 (shift %d, reduce %d) on %s\n", state, number, p->number - 2,
163 symbol_name[symbol]);
165 else
167 fprintf(verbose_file, "%d: reduce/reduce conflict \
168 (reduce %d, reduce %d) on %s\n", state, number - 2, p->number - 2,
169 symbol_name[symbol]);
176 static void
177 print_core(int state)
179 int i;
180 int k;
181 int rule;
182 core *statep;
183 Value_t *sp;
184 Value_t *sp1;
186 statep = state_table[state];
187 k = statep->nitems;
189 for (i = 0; i < k; i++)
191 sp1 = sp = ritem + statep->items[i];
193 while (*sp >= 0)
194 ++sp;
195 rule = -(*sp);
196 fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
198 for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
199 fprintf(verbose_file, "%s ", symbol_name[*sp]);
201 putc('.', verbose_file);
203 while (*sp >= 0)
205 fprintf(verbose_file, " %s", symbol_name[*sp]);
206 sp++;
208 fprintf(verbose_file, " (%d)\n", -2 - *sp);
212 static void
213 print_nulls(int state)
215 action *p;
216 Value_t i, j, k, nnulls;
218 nnulls = 0;
219 for (p = parser[state]; p; p = p->next)
221 if (p->action_code == REDUCE &&
222 (p->suppressed == 0 || p->suppressed == 1))
224 i = p->number;
225 if (rrhs[i] + 1 == rrhs[i + 1])
227 for (j = 0; j < nnulls && i > null_rules[j]; ++j)
228 continue;
230 if (j == nnulls)
232 ++nnulls;
233 null_rules[j] = i;
235 else if (i != null_rules[j])
237 ++nnulls;
238 for (k = (Value_t) (nnulls - 1); k > j; --k)
239 null_rules[k] = null_rules[k - 1];
240 null_rules[j] = i;
246 for (i = 0; i < nnulls; ++i)
248 j = null_rules[i];
249 fprintf(verbose_file, "\t%s : . (%d)\n", symbol_name[rlhs[j]],
250 j - 2);
252 fprintf(verbose_file, "\n");
255 static void
256 print_actions(int stateno)
258 action *p;
259 shifts *sp;
260 int as;
262 if (stateno == final_state)
263 fprintf(verbose_file, "\t$end accept\n");
265 p = parser[stateno];
266 if (p)
268 print_shifts(p);
269 print_reductions(p, defred[stateno]);
272 sp = shift_table[stateno];
273 if (sp && sp->nshifts > 0)
275 as = accessing_symbol[sp->shift[sp->nshifts - 1]];
276 if (ISVAR(as))
277 print_gotos(stateno);
281 static void
282 print_shifts(action *p)
284 int count;
285 action *q;
287 count = 0;
288 for (q = p; q; q = q->next)
290 if (q->suppressed < 2 && q->action_code == SHIFT)
291 ++count;
294 if (count > 0)
296 for (; p; p = p->next)
298 if (p->action_code == SHIFT && p->suppressed == 0)
299 fprintf(verbose_file, "\t%s shift %d\n",
300 symbol_name[p->symbol], p->number);
301 #if defined(YYBTYACC)
302 if (backtrack && p->action_code == SHIFT && p->suppressed == 1)
303 fprintf(verbose_file, "\t%s [trial] shift %d\n",
304 symbol_name[p->symbol], p->number);
305 #endif
310 static void
311 print_reductions(action *p, int defred2)
313 int k, anyreds;
314 action *q;
316 anyreds = 0;
317 for (q = p; q; q = q->next)
319 if (q->action_code == REDUCE && q->suppressed < 2)
321 anyreds = 1;
322 break;
326 if (anyreds == 0)
327 fprintf(verbose_file, "\t. error\n");
328 else
330 for (; p; p = p->next)
332 if (p->action_code == REDUCE && p->number != defred2)
334 k = p->number - 2;
335 if (p->suppressed == 0)
336 fprintf(verbose_file, "\t%s reduce %d\n",
337 symbol_name[p->symbol], k);
338 #if defined(YYBTYACC)
339 if (backtrack && p->suppressed == 1)
340 fprintf(verbose_file, "\t%s [trial] reduce %d\n",
341 symbol_name[p->symbol], k);
342 #endif
346 if (defred2 > 0)
347 fprintf(verbose_file, "\t. reduce %d\n", defred2 - 2);
351 static void
352 print_gotos(int stateno)
354 int i, k;
355 int as;
356 Value_t *to_state2;
357 shifts *sp;
359 putc('\n', verbose_file);
360 sp = shift_table[stateno];
361 to_state2 = sp->shift;
362 for (i = 0; i < sp->nshifts; ++i)
364 k = to_state2[i];
365 as = accessing_symbol[k];
366 if (ISVAR(as))
367 fprintf(verbose_file, "\t%s goto %d\n", symbol_name[as], k);