xref: /netbsd-src/external/bsd/byacc/dist/verbose.c (revision 7c192b2a5e1093666e67801684f930ef49b3b363)
1 /*	$NetBSD: verbose.c,v 1.10 2017/02/11 19:33:12 christos Exp $	*/
2 
3 /* Id: verbose.c,v 1.12 2016/06/07 00:22:05 tom Exp  */
4 
5 #include "defs.h"
6 
7 #include <sys/cdefs.h>
8 __RCSID("$NetBSD: verbose.c,v 1.10 2017/02/11 19:33:12 christos Exp $");
9 
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);
20 
21 static Value_t *null_rules;
22 
23 void
24 verbose(void)
25 {
26     int i;
27 
28     if (!vflag)
29 	return;
30 
31     null_rules = TMALLOC(Value_t, nrules);
32     NO_SPACE(null_rules);
33 
34     fprintf(verbose_file, "\f\n");
35     for (i = 0; i < nstates; i++)
36 	print_state(i);
37     FREE(null_rules);
38 
39     if (nunused)
40 	log_unused();
41     if (SRtotal || RRtotal)
42 	log_conflicts();
43 
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;
52 
53 	fputs("\ngrammar parser grammar\n", verbose_file);
54 	fputs("symbol# value# symbol\n", verbose_file);
55 	for (i = 0; i < ntokens; ++i)
56 	{
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];
61 	}
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]);
65     }
66 #endif
67 }
68 
69 static void
70 log_unused(void)
71 {
72     int i;
73     Value_t *p;
74 
75     fprintf(verbose_file, "\n\nRules never reduced:\n");
76     for (i = 3; i < nrules; ++i)
77     {
78 	if (!rules_used[i])
79 	{
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);
84 	}
85     }
86 }
87 
88 static void
89 log_conflicts(void)
90 {
91     int i;
92 
93     fprintf(verbose_file, "\n\n");
94     for (i = 0; i < nstates; i++)
95     {
96 	if (SRconflicts[i] || RRconflicts[i])
97 	{
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");
110 	}
111     }
112 }
113 
114 static void
115 print_state(int state)
116 {
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);
125 }
126 
127 static void
128 print_conflicts(int state)
129 {
130     int symbol, act, number;
131     action *p;
132 
133     act = 0;			/* not shift/reduce... */
134     number = -1;
135     symbol = -1;
136     for (p = parser[state]; p; p = p->next)
137     {
138 	if (p->suppressed == 2)
139 	    continue;
140 
141 	if (p->symbol != symbol)
142 	{
143 	    symbol = p->symbol;
144 	    number = p->number;
145 	    if (p->action_code == SHIFT)
146 		act = SHIFT;
147 	    else
148 		act = REDUCE;
149 	}
150 	else if (p->suppressed == 1)
151 	{
152 	    if (state == final_state && symbol == 0)
153 	    {
154 		fprintf(verbose_file, "%d: shift/reduce conflict \
155 (accept, reduce %d) on $end\n", state, p->number - 2);
156 	    }
157 	    else
158 	    {
159 		if (act == SHIFT)
160 		{
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]);
164 		}
165 		else
166 		{
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]);
170 		}
171 	    }
172 	}
173     }
174 }
175 
176 static void
177 print_core(int state)
178 {
179     int i;
180     int k;
181     int rule;
182     core *statep;
183     Value_t *sp;
184     Value_t *sp1;
185 
186     statep = state_table[state];
187     k = statep->nitems;
188 
189     for (i = 0; i < k; i++)
190     {
191 	sp1 = sp = ritem + statep->items[i];
192 
193 	while (*sp >= 0)
194 	    ++sp;
195 	rule = -(*sp);
196 	fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
197 
198 	for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
199 	    fprintf(verbose_file, "%s ", symbol_name[*sp]);
200 
201 	putc('.', verbose_file);
202 
203 	while (*sp >= 0)
204 	{
205 	    fprintf(verbose_file, " %s", symbol_name[*sp]);
206 	    sp++;
207 	}
208 	fprintf(verbose_file, "  (%d)\n", -2 - *sp);
209     }
210 }
211 
212 static void
213 print_nulls(int state)
214 {
215     action *p;
216     Value_t i, j, k, nnulls;
217 
218     nnulls = 0;
219     for (p = parser[state]; p; p = p->next)
220     {
221 	if (p->action_code == REDUCE &&
222 	    (p->suppressed == 0 || p->suppressed == 1))
223 	{
224 	    i = p->number;
225 	    if (rrhs[i] + 1 == rrhs[i + 1])
226 	    {
227 		for (j = 0; j < nnulls && i > null_rules[j]; ++j)
228 		    continue;
229 
230 		if (j == nnulls)
231 		{
232 		    ++nnulls;
233 		    null_rules[j] = i;
234 		}
235 		else if (i != null_rules[j])
236 		{
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;
241 		}
242 	    }
243 	}
244     }
245 
246     for (i = 0; i < nnulls; ++i)
247     {
248 	j = null_rules[i];
249 	fprintf(verbose_file, "\t%s : .  (%d)\n", symbol_name[rlhs[j]],
250 		j - 2);
251     }
252     fprintf(verbose_file, "\n");
253 }
254 
255 static void
256 print_actions(int stateno)
257 {
258     action *p;
259     shifts *sp;
260     int as;
261 
262     if (stateno == final_state)
263 	fprintf(verbose_file, "\t$end  accept\n");
264 
265     p = parser[stateno];
266     if (p)
267     {
268 	print_shifts(p);
269 	print_reductions(p, defred[stateno]);
270     }
271 
272     sp = shift_table[stateno];
273     if (sp && sp->nshifts > 0)
274     {
275 	as = accessing_symbol[sp->shift[sp->nshifts - 1]];
276 	if (ISVAR(as))
277 	    print_gotos(stateno);
278     }
279 }
280 
281 static void
282 print_shifts(action *p)
283 {
284     int count;
285     action *q;
286 
287     count = 0;
288     for (q = p; q; q = q->next)
289     {
290 	if (q->suppressed < 2 && q->action_code == SHIFT)
291 	    ++count;
292     }
293 
294     if (count > 0)
295     {
296 	for (; p; p = p->next)
297 	{
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
306 	}
307     }
308 }
309 
310 static void
311 print_reductions(action *p, int defred2)
312 {
313     int k, anyreds;
314     action *q;
315 
316     anyreds = 0;
317     for (q = p; q; q = q->next)
318     {
319 	if (q->action_code == REDUCE && q->suppressed < 2)
320 	{
321 	    anyreds = 1;
322 	    break;
323 	}
324     }
325 
326     if (anyreds == 0)
327 	fprintf(verbose_file, "\t.  error\n");
328     else
329     {
330 	for (; p; p = p->next)
331 	{
332 	    if (p->action_code == REDUCE && p->number != defred2)
333 	    {
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
343 	    }
344 	}
345 
346 	if (defred2 > 0)
347 	    fprintf(verbose_file, "\t.  reduce %d\n", defred2 - 2);
348     }
349 }
350 
351 static void
352 print_gotos(int stateno)
353 {
354     int i, k;
355     int as;
356     Value_t *to_state2;
357     shifts *sp;
358 
359     putc('\n', verbose_file);
360     sp = shift_table[stateno];
361     to_state2 = sp->shift;
362     for (i = 0; i < sp->nshifts; ++i)
363     {
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);
368     }
369 }
370