1319297Sdelphij/* $Id: verbose.c,v 1.12 2016/06/07 00:22:05 tom Exp $ */
2234949Sbapt
3234949Sbapt#include "defs.h"
4234949Sbapt
5234949Sbaptstatic void log_conflicts(void);
6234949Sbaptstatic void log_unused(void);
7234949Sbaptstatic void print_actions(int stateno);
8234949Sbaptstatic void print_conflicts(int state);
9234949Sbaptstatic void print_core(int state);
10234949Sbaptstatic void print_gotos(int stateno);
11234949Sbaptstatic void print_nulls(int state);
12234949Sbaptstatic void print_shifts(action *p);
13234949Sbaptstatic void print_state(int state);
14234949Sbaptstatic void print_reductions(action *p, int defred2);
15234949Sbapt
16264803Sbaptstatic Value_t *null_rules;
17234949Sbapt
18234949Sbaptvoid
19234949Sbaptverbose(void)
20234949Sbapt{
21234949Sbapt    int i;
22234949Sbapt
23234949Sbapt    if (!vflag)
24234949Sbapt	return;
25234949Sbapt
26264803Sbapt    null_rules = TMALLOC(Value_t, nrules);
27234949Sbapt    NO_SPACE(null_rules);
28234949Sbapt
29234949Sbapt    fprintf(verbose_file, "\f\n");
30234949Sbapt    for (i = 0; i < nstates; i++)
31234949Sbapt	print_state(i);
32234949Sbapt    FREE(null_rules);
33234949Sbapt
34234949Sbapt    if (nunused)
35234949Sbapt	log_unused();
36234949Sbapt    if (SRtotal || RRtotal)
37234949Sbapt	log_conflicts();
38234949Sbapt
39234949Sbapt    fprintf(verbose_file, "\n\n%d terminals, %d nonterminals\n", ntokens,
40234949Sbapt	    nvars);
41234949Sbapt    fprintf(verbose_file, "%d grammar rules, %d states\n", nrules - 2, nstates);
42264803Sbapt#if defined(YYBTYACC)
43264803Sbapt    {				/* print out the grammar symbol # and parser internal symbol # for each
44264803Sbapt				   symbol as an aide to writing the implementation for YYDESTRUCT_CALL()
45264803Sbapt				   and YYSTYPE_TOSTRING() */
46264803Sbapt	int maxtok = 0;
47264803Sbapt
48264803Sbapt	fputs("\ngrammar parser grammar\n", verbose_file);
49264803Sbapt	fputs("symbol# value# symbol\n", verbose_file);
50264803Sbapt	for (i = 0; i < ntokens; ++i)
51264803Sbapt	{
52264803Sbapt	    fprintf(verbose_file, " %5d  %5d  %s\n",
53264803Sbapt		    i, symbol_value[i], symbol_name[i]);
54264803Sbapt	    if (symbol_value[i] > maxtok)
55264803Sbapt		maxtok = symbol_value[i];
56264803Sbapt	}
57264803Sbapt	for (i = ntokens; i < nsyms; ++i)
58264803Sbapt	    fprintf(verbose_file, " %5d  %5d  %s\n",
59264803Sbapt		    i, (maxtok + 1) + symbol_value[i] + 1, symbol_name[i]);
60264803Sbapt    }
61264803Sbapt#endif
62234949Sbapt}
63234949Sbapt
64234949Sbaptstatic void
65234949Sbaptlog_unused(void)
66234949Sbapt{
67234949Sbapt    int i;
68264803Sbapt    Value_t *p;
69234949Sbapt
70234949Sbapt    fprintf(verbose_file, "\n\nRules never reduced:\n");
71234949Sbapt    for (i = 3; i < nrules; ++i)
72234949Sbapt    {
73234949Sbapt	if (!rules_used[i])
74234949Sbapt	{
75234949Sbapt	    fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
76234949Sbapt	    for (p = ritem + rrhs[i]; *p >= 0; ++p)
77234949Sbapt		fprintf(verbose_file, " %s", symbol_name[*p]);
78234949Sbapt	    fprintf(verbose_file, "  (%d)\n", i - 2);
79234949Sbapt	}
80234949Sbapt    }
81234949Sbapt}
82234949Sbapt
83234949Sbaptstatic void
84234949Sbaptlog_conflicts(void)
85234949Sbapt{
86234949Sbapt    int i;
87234949Sbapt
88234949Sbapt    fprintf(verbose_file, "\n\n");
89234949Sbapt    for (i = 0; i < nstates; i++)
90234949Sbapt    {
91234949Sbapt	if (SRconflicts[i] || RRconflicts[i])
92234949Sbapt	{
93234949Sbapt	    fprintf(verbose_file, "State %d contains ", i);
94234949Sbapt	    if (SRconflicts[i] > 0)
95234949Sbapt		fprintf(verbose_file, "%d shift/reduce conflict%s",
96234949Sbapt			SRconflicts[i],
97234949Sbapt			PLURAL(SRconflicts[i]));
98234949Sbapt	    if (SRconflicts[i] && RRconflicts[i])
99234949Sbapt		fprintf(verbose_file, ", ");
100234949Sbapt	    if (RRconflicts[i] > 0)
101234949Sbapt		fprintf(verbose_file, "%d reduce/reduce conflict%s",
102234949Sbapt			RRconflicts[i],
103234949Sbapt			PLURAL(RRconflicts[i]));
104234949Sbapt	    fprintf(verbose_file, ".\n");
105234949Sbapt	}
106234949Sbapt    }
107234949Sbapt}
108234949Sbapt
109234949Sbaptstatic void
110234949Sbaptprint_state(int state)
111234949Sbapt{
112234949Sbapt    if (state)
113234949Sbapt	fprintf(verbose_file, "\n\n");
114234949Sbapt    if (SRconflicts[state] || RRconflicts[state])
115234949Sbapt	print_conflicts(state);
116234949Sbapt    fprintf(verbose_file, "state %d\n", state);
117234949Sbapt    print_core(state);
118234949Sbapt    print_nulls(state);
119234949Sbapt    print_actions(state);
120234949Sbapt}
121234949Sbapt
122234949Sbaptstatic void
123234949Sbaptprint_conflicts(int state)
124234949Sbapt{
125234949Sbapt    int symbol, act, number;
126234949Sbapt    action *p;
127234949Sbapt
128234949Sbapt    act = 0;			/* not shift/reduce... */
129234949Sbapt    number = -1;
130234949Sbapt    symbol = -1;
131234949Sbapt    for (p = parser[state]; p; p = p->next)
132234949Sbapt    {
133234949Sbapt	if (p->suppressed == 2)
134234949Sbapt	    continue;
135234949Sbapt
136234949Sbapt	if (p->symbol != symbol)
137234949Sbapt	{
138234949Sbapt	    symbol = p->symbol;
139234949Sbapt	    number = p->number;
140234949Sbapt	    if (p->action_code == SHIFT)
141234949Sbapt		act = SHIFT;
142234949Sbapt	    else
143234949Sbapt		act = REDUCE;
144234949Sbapt	}
145234949Sbapt	else if (p->suppressed == 1)
146234949Sbapt	{
147234949Sbapt	    if (state == final_state && symbol == 0)
148234949Sbapt	    {
149234949Sbapt		fprintf(verbose_file, "%d: shift/reduce conflict \
150234949Sbapt(accept, reduce %d) on $end\n", state, p->number - 2);
151234949Sbapt	    }
152234949Sbapt	    else
153234949Sbapt	    {
154234949Sbapt		if (act == SHIFT)
155234949Sbapt		{
156234949Sbapt		    fprintf(verbose_file, "%d: shift/reduce conflict \
157234949Sbapt(shift %d, reduce %d) on %s\n", state, number, p->number - 2,
158234949Sbapt			    symbol_name[symbol]);
159234949Sbapt		}
160234949Sbapt		else
161234949Sbapt		{
162234949Sbapt		    fprintf(verbose_file, "%d: reduce/reduce conflict \
163234949Sbapt(reduce %d, reduce %d) on %s\n", state, number - 2, p->number - 2,
164234949Sbapt			    symbol_name[symbol]);
165234949Sbapt		}
166234949Sbapt	    }
167234949Sbapt	}
168234949Sbapt    }
169234949Sbapt}
170234949Sbapt
171234949Sbaptstatic void
172234949Sbaptprint_core(int state)
173234949Sbapt{
174234949Sbapt    int i;
175234949Sbapt    int k;
176234949Sbapt    int rule;
177234949Sbapt    core *statep;
178264803Sbapt    Value_t *sp;
179264803Sbapt    Value_t *sp1;
180234949Sbapt
181234949Sbapt    statep = state_table[state];
182234949Sbapt    k = statep->nitems;
183234949Sbapt
184234949Sbapt    for (i = 0; i < k; i++)
185234949Sbapt    {
186234949Sbapt	sp1 = sp = ritem + statep->items[i];
187234949Sbapt
188234949Sbapt	while (*sp >= 0)
189234949Sbapt	    ++sp;
190234949Sbapt	rule = -(*sp);
191234949Sbapt	fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
192234949Sbapt
193234949Sbapt	for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
194234949Sbapt	    fprintf(verbose_file, "%s ", symbol_name[*sp]);
195234949Sbapt
196234949Sbapt	putc('.', verbose_file);
197234949Sbapt
198234949Sbapt	while (*sp >= 0)
199234949Sbapt	{
200234949Sbapt	    fprintf(verbose_file, " %s", symbol_name[*sp]);
201234949Sbapt	    sp++;
202234949Sbapt	}
203234949Sbapt	fprintf(verbose_file, "  (%d)\n", -2 - *sp);
204234949Sbapt    }
205234949Sbapt}
206234949Sbapt
207234949Sbaptstatic void
208234949Sbaptprint_nulls(int state)
209234949Sbapt{
210234949Sbapt    action *p;
211234949Sbapt    Value_t i, j, k, nnulls;
212234949Sbapt
213234949Sbapt    nnulls = 0;
214234949Sbapt    for (p = parser[state]; p; p = p->next)
215234949Sbapt    {
216234949Sbapt	if (p->action_code == REDUCE &&
217234949Sbapt	    (p->suppressed == 0 || p->suppressed == 1))
218234949Sbapt	{
219234949Sbapt	    i = p->number;
220234949Sbapt	    if (rrhs[i] + 1 == rrhs[i + 1])
221234949Sbapt	    {
222234949Sbapt		for (j = 0; j < nnulls && i > null_rules[j]; ++j)
223234949Sbapt		    continue;
224234949Sbapt
225234949Sbapt		if (j == nnulls)
226234949Sbapt		{
227234949Sbapt		    ++nnulls;
228234949Sbapt		    null_rules[j] = i;
229234949Sbapt		}
230234949Sbapt		else if (i != null_rules[j])
231234949Sbapt		{
232234949Sbapt		    ++nnulls;
233319297Sdelphij		    for (k = (Value_t)(nnulls - 1); k > j; --k)
234234949Sbapt			null_rules[k] = null_rules[k - 1];
235234949Sbapt		    null_rules[j] = i;
236234949Sbapt		}
237234949Sbapt	    }
238234949Sbapt	}
239234949Sbapt    }
240234949Sbapt
241234949Sbapt    for (i = 0; i < nnulls; ++i)
242234949Sbapt    {
243234949Sbapt	j = null_rules[i];
244234949Sbapt	fprintf(verbose_file, "\t%s : .  (%d)\n", symbol_name[rlhs[j]],
245234949Sbapt		j - 2);
246234949Sbapt    }
247234949Sbapt    fprintf(verbose_file, "\n");
248234949Sbapt}
249234949Sbapt
250234949Sbaptstatic void
251234949Sbaptprint_actions(int stateno)
252234949Sbapt{
253234949Sbapt    action *p;
254234949Sbapt    shifts *sp;
255234949Sbapt    int as;
256234949Sbapt
257234949Sbapt    if (stateno == final_state)
258234949Sbapt	fprintf(verbose_file, "\t$end  accept\n");
259234949Sbapt
260234949Sbapt    p = parser[stateno];
261234949Sbapt    if (p)
262234949Sbapt    {
263234949Sbapt	print_shifts(p);
264234949Sbapt	print_reductions(p, defred[stateno]);
265234949Sbapt    }
266234949Sbapt
267234949Sbapt    sp = shift_table[stateno];
268234949Sbapt    if (sp && sp->nshifts > 0)
269234949Sbapt    {
270234949Sbapt	as = accessing_symbol[sp->shift[sp->nshifts - 1]];
271234949Sbapt	if (ISVAR(as))
272234949Sbapt	    print_gotos(stateno);
273234949Sbapt    }
274234949Sbapt}
275234949Sbapt
276234949Sbaptstatic void
277234949Sbaptprint_shifts(action *p)
278234949Sbapt{
279234949Sbapt    int count;
280234949Sbapt    action *q;
281234949Sbapt
282234949Sbapt    count = 0;
283234949Sbapt    for (q = p; q; q = q->next)
284234949Sbapt    {
285234949Sbapt	if (q->suppressed < 2 && q->action_code == SHIFT)
286234949Sbapt	    ++count;
287234949Sbapt    }
288234949Sbapt
289234949Sbapt    if (count > 0)
290234949Sbapt    {
291234949Sbapt	for (; p; p = p->next)
292234949Sbapt	{
293234949Sbapt	    if (p->action_code == SHIFT && p->suppressed == 0)
294234949Sbapt		fprintf(verbose_file, "\t%s  shift %d\n",
295234949Sbapt			symbol_name[p->symbol], p->number);
296264803Sbapt#if defined(YYBTYACC)
297264803Sbapt	    if (backtrack && p->action_code == SHIFT && p->suppressed == 1)
298264803Sbapt		fprintf(verbose_file, "\t%s  [trial] shift %d\n",
299264803Sbapt			symbol_name[p->symbol], p->number);
300264803Sbapt#endif
301234949Sbapt	}
302234949Sbapt    }
303234949Sbapt}
304234949Sbapt
305234949Sbaptstatic void
306234949Sbaptprint_reductions(action *p, int defred2)
307234949Sbapt{
308234949Sbapt    int k, anyreds;
309234949Sbapt    action *q;
310234949Sbapt
311234949Sbapt    anyreds = 0;
312234949Sbapt    for (q = p; q; q = q->next)
313234949Sbapt    {
314234949Sbapt	if (q->action_code == REDUCE && q->suppressed < 2)
315234949Sbapt	{
316234949Sbapt	    anyreds = 1;
317234949Sbapt	    break;
318234949Sbapt	}
319234949Sbapt    }
320234949Sbapt
321234949Sbapt    if (anyreds == 0)
322234949Sbapt	fprintf(verbose_file, "\t.  error\n");
323234949Sbapt    else
324234949Sbapt    {
325234949Sbapt	for (; p; p = p->next)
326234949Sbapt	{
327234949Sbapt	    if (p->action_code == REDUCE && p->number != defred2)
328234949Sbapt	    {
329234949Sbapt		k = p->number - 2;
330234949Sbapt		if (p->suppressed == 0)
331234949Sbapt		    fprintf(verbose_file, "\t%s  reduce %d\n",
332234949Sbapt			    symbol_name[p->symbol], k);
333264803Sbapt#if defined(YYBTYACC)
334264803Sbapt		if (backtrack && p->suppressed == 1)
335264803Sbapt		    fprintf(verbose_file, "\t%s  [trial] reduce %d\n",
336264803Sbapt			    symbol_name[p->symbol], k);
337264803Sbapt#endif
338234949Sbapt	    }
339234949Sbapt	}
340234949Sbapt
341234949Sbapt	if (defred2 > 0)
342234949Sbapt	    fprintf(verbose_file, "\t.  reduce %d\n", defred2 - 2);
343234949Sbapt    }
344234949Sbapt}
345234949Sbapt
346234949Sbaptstatic void
347234949Sbaptprint_gotos(int stateno)
348234949Sbapt{
349234949Sbapt    int i, k;
350234949Sbapt    int as;
351264803Sbapt    Value_t *to_state2;
352234949Sbapt    shifts *sp;
353234949Sbapt
354234949Sbapt    putc('\n', verbose_file);
355234949Sbapt    sp = shift_table[stateno];
356234949Sbapt    to_state2 = sp->shift;
357234949Sbapt    for (i = 0; i < sp->nshifts; ++i)
358234949Sbapt    {
359234949Sbapt	k = to_state2[i];
360234949Sbapt	as = accessing_symbol[k];
361234949Sbapt	if (ISVAR(as))
362234949Sbapt	    fprintf(verbose_file, "\t%s  goto %d\n", symbol_name[as], k);
363234949Sbapt    }
364234949Sbapt}
365