xref: /freebsd/contrib/byacc/verbose.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /* $Id: verbose.c,v 1.11 2014/04/01 23:15:59 Tom.Shields Exp $ */
2 
3 #include "defs.h"
4 
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);
15 
16 static Value_t *null_rules;
17 
18 void
19 verbose(void)
20 {
21     int i;
22 
23     if (!vflag)
24 	return;
25 
26     null_rules = TMALLOC(Value_t, nrules);
27     NO_SPACE(null_rules);
28 
29     fprintf(verbose_file, "\f\n");
30     for (i = 0; i < nstates; i++)
31 	print_state(i);
32     FREE(null_rules);
33 
34     if (nunused)
35 	log_unused();
36     if (SRtotal || RRtotal)
37 	log_conflicts();
38 
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;
47 
48 	fputs("\ngrammar parser grammar\n", verbose_file);
49 	fputs("symbol# value# symbol\n", verbose_file);
50 	for (i = 0; i < ntokens; ++i)
51 	{
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];
56 	}
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]);
60     }
61 #endif
62 }
63 
64 static void
65 log_unused(void)
66 {
67     int i;
68     Value_t *p;
69 
70     fprintf(verbose_file, "\n\nRules never reduced:\n");
71     for (i = 3; i < nrules; ++i)
72     {
73 	if (!rules_used[i])
74 	{
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);
79 	}
80     }
81 }
82 
83 static void
84 log_conflicts(void)
85 {
86     int i;
87 
88     fprintf(verbose_file, "\n\n");
89     for (i = 0; i < nstates; i++)
90     {
91 	if (SRconflicts[i] || RRconflicts[i])
92 	{
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");
105 	}
106     }
107 }
108 
109 static void
110 print_state(int state)
111 {
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);
120 }
121 
122 static void
123 print_conflicts(int state)
124 {
125     int symbol, act, number;
126     action *p;
127 
128     act = 0;			/* not shift/reduce... */
129     number = -1;
130     symbol = -1;
131     for (p = parser[state]; p; p = p->next)
132     {
133 	if (p->suppressed == 2)
134 	    continue;
135 
136 	if (p->symbol != symbol)
137 	{
138 	    symbol = p->symbol;
139 	    number = p->number;
140 	    if (p->action_code == SHIFT)
141 		act = SHIFT;
142 	    else
143 		act = REDUCE;
144 	}
145 	else if (p->suppressed == 1)
146 	{
147 	    if (state == final_state && symbol == 0)
148 	    {
149 		fprintf(verbose_file, "%d: shift/reduce conflict \
150 (accept, reduce %d) on $end\n", state, p->number - 2);
151 	    }
152 	    else
153 	    {
154 		if (act == SHIFT)
155 		{
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]);
159 		}
160 		else
161 		{
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]);
165 		}
166 	    }
167 	}
168     }
169 }
170 
171 static void
172 print_core(int state)
173 {
174     int i;
175     int k;
176     int rule;
177     core *statep;
178     Value_t *sp;
179     Value_t *sp1;
180 
181     statep = state_table[state];
182     k = statep->nitems;
183 
184     for (i = 0; i < k; i++)
185     {
186 	sp1 = sp = ritem + statep->items[i];
187 
188 	while (*sp >= 0)
189 	    ++sp;
190 	rule = -(*sp);
191 	fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
192 
193 	for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
194 	    fprintf(verbose_file, "%s ", symbol_name[*sp]);
195 
196 	putc('.', verbose_file);
197 
198 	while (*sp >= 0)
199 	{
200 	    fprintf(verbose_file, " %s", symbol_name[*sp]);
201 	    sp++;
202 	}
203 	fprintf(verbose_file, "  (%d)\n", -2 - *sp);
204     }
205 }
206 
207 static void
208 print_nulls(int state)
209 {
210     action *p;
211     Value_t i, j, k, nnulls;
212 
213     nnulls = 0;
214     for (p = parser[state]; p; p = p->next)
215     {
216 	if (p->action_code == REDUCE &&
217 	    (p->suppressed == 0 || p->suppressed == 1))
218 	{
219 	    i = p->number;
220 	    if (rrhs[i] + 1 == rrhs[i + 1])
221 	    {
222 		for (j = 0; j < nnulls && i > null_rules[j]; ++j)
223 		    continue;
224 
225 		if (j == nnulls)
226 		{
227 		    ++nnulls;
228 		    null_rules[j] = i;
229 		}
230 		else if (i != null_rules[j])
231 		{
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;
236 		}
237 	    }
238 	}
239     }
240 
241     for (i = 0; i < nnulls; ++i)
242     {
243 	j = null_rules[i];
244 	fprintf(verbose_file, "\t%s : .  (%d)\n", symbol_name[rlhs[j]],
245 		j - 2);
246     }
247     fprintf(verbose_file, "\n");
248 }
249 
250 static void
251 print_actions(int stateno)
252 {
253     action *p;
254     shifts *sp;
255     int as;
256 
257     if (stateno == final_state)
258 	fprintf(verbose_file, "\t$end  accept\n");
259 
260     p = parser[stateno];
261     if (p)
262     {
263 	print_shifts(p);
264 	print_reductions(p, defred[stateno]);
265     }
266 
267     sp = shift_table[stateno];
268     if (sp && sp->nshifts > 0)
269     {
270 	as = accessing_symbol[sp->shift[sp->nshifts - 1]];
271 	if (ISVAR(as))
272 	    print_gotos(stateno);
273     }
274 }
275 
276 static void
277 print_shifts(action *p)
278 {
279     int count;
280     action *q;
281 
282     count = 0;
283     for (q = p; q; q = q->next)
284     {
285 	if (q->suppressed < 2 && q->action_code == SHIFT)
286 	    ++count;
287     }
288 
289     if (count > 0)
290     {
291 	for (; p; p = p->next)
292 	{
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
301 	}
302     }
303 }
304 
305 static void
306 print_reductions(action *p, int defred2)
307 {
308     int k, anyreds;
309     action *q;
310 
311     anyreds = 0;
312     for (q = p; q; q = q->next)
313     {
314 	if (q->action_code == REDUCE && q->suppressed < 2)
315 	{
316 	    anyreds = 1;
317 	    break;
318 	}
319     }
320 
321     if (anyreds == 0)
322 	fprintf(verbose_file, "\t.  error\n");
323     else
324     {
325 	for (; p; p = p->next)
326 	{
327 	    if (p->action_code == REDUCE && p->number != defred2)
328 	    {
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
338 	    }
339 	}
340 
341 	if (defred2 > 0)
342 	    fprintf(verbose_file, "\t.  reduce %d\n", defred2 - 2);
343     }
344 }
345 
346 static void
347 print_gotos(int stateno)
348 {
349     int i, k;
350     int as;
351     Value_t *to_state2;
352     shifts *sp;
353 
354     putc('\n', verbose_file);
355     sp = shift_table[stateno];
356     to_state2 = sp->shift;
357     for (i = 0; i < sp->nshifts; ++i)
358     {
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);
363     }
364 }
365