1 
2 #include "defs.h"
3 
4 
5 static Yshort *null_rules;
6 
verbose()7 void verbose()
8 {
9     register int i;
10 
11     if (!vflag) return;
12 
13     null_rules = (Yshort *) MALLOC(nrules*sizeof(Yshort));
14     if (null_rules == 0) no_space();
15     fprintf(verbose_file, "\f\n");
16     for (i = 0; i < nstates; i++)
17 	print_state(i);
18     FREE(null_rules);
19 
20     if (nunused)
21 	log_unused();
22     if (SRtotal || RRtotal)
23 	log_conflicts();
24 
25     fprintf(verbose_file, "\n\n%d terminals, %d nonterminals\n", ntokens,
26 	    nvars);
27     fprintf(verbose_file, "%d grammar rules, %d states\n", nrules - 2, nstates);
28 }
29 
30 
log_unused()31 void log_unused()
32 {
33     register int i;
34     register Yshort *p;
35 
36     fprintf(verbose_file, "\n\nRules never reduced:\n");
37     for (i = 3; i < nrules; ++i)
38     {
39 	if (!rules_used[i])
40 	{
41 	    fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
42 	    for (p = ritem + rrhs[i]; *p >= 0; ++p)
43 		fprintf(verbose_file, " %s", symbol_name[*p]);
44 	    fprintf(verbose_file, "  (%d)\n", i - 2);
45 	}
46     }
47 }
48 
49 
log_conflicts()50 void log_conflicts()
51 {
52     register int i;
53 
54     fprintf(verbose_file, "\n\n");
55     for (i = 0; i < nstates; i++)
56     {
57 	if (SRconflicts[i] || RRconflicts[i])
58 	{
59 	    fprintf(verbose_file, "State %d contains ", i);
60 	    if (SRconflicts[i] == 1)
61 		fprintf(verbose_file, "1 shift/reduce conflict");
62 	    else if (SRconflicts[i] > 1)
63 		fprintf(verbose_file, "%d shift/reduce conflicts",
64 			SRconflicts[i]);
65 	    if (SRconflicts[i] && RRconflicts[i])
66 		fprintf(verbose_file, ", ");
67 	    if (RRconflicts[i] == 1)
68 		fprintf(verbose_file, "1 reduce/reduce conflict");
69 	    else if (RRconflicts[i] > 1)
70 		fprintf(verbose_file, "%d reduce/reduce conflicts",
71 			RRconflicts[i]);
72 	    fprintf(verbose_file, ".\n");
73 	}
74     }
75 }
76 
77 
print_state(int state)78 void print_state(int state)
79 {
80     if (state)
81 	fprintf(verbose_file, "\n\n");
82     if (SRconflicts[state] || RRconflicts[state])
83 	print_conflicts(state);
84     fprintf(verbose_file, "state %d\n", state);
85     print_core(state);
86     print_nulls(state);
87     print_actions(state);
88 }
89 
90 
print_conflicts(int state)91 void print_conflicts(int state)
92 {
93     register int symbol, act, number;
94     register action *p;
95 
96     symbol = act = number = -1;
97     for (p = parser[state]; p; p = p->next)
98     {
99 	if (p->suppressed == 2)
100 	    continue;
101 
102 	if (p->symbol != symbol)
103 	{
104 	    symbol = p->symbol;
105 	    number = p->number;
106 	    if (p->action_code == SHIFT)
107 		act = SHIFT;
108 	    else
109 		act = REDUCE;
110 	}
111 	else if (p->suppressed == 1)
112 	{
113 	    if (state == final_state && symbol == 0)
114 	    {
115 		fprintf(verbose_file, "%d: shift/reduce conflict "
116 			"(accept, reduce %d) on $end\n", state, p->number - 2);
117 	    }
118 	    else
119 	    {
120 		if (act == SHIFT)
121 		{
122 		    fprintf(verbose_file, "%d: shift/reduce conflict "
123 			    "(shift %d, reduce %d) on %s\n", state, number,
124 			    p->number - 2, symbol_name[symbol]);
125 		}
126 		else
127 		{
128 		    fprintf(verbose_file, "%d: reduce/reduce conflict "
129 			    "(reduce %d, reduce %d) on %s\n", state,
130 			    number - 2, p->number - 2, symbol_name[symbol]);
131 		}
132 	    }
133 	}
134     }
135 }
136 
137 
print_core(int state)138 void print_core(int state)
139 {
140     register int i;
141     register int k;
142     register int rule;
143     register core *statep;
144     register Yshort *sp;
145     register Yshort *sp1;
146 
147     statep = state_table[state];
148     k = statep->nitems;
149 
150     for (i = 0; i < k; i++)
151     {
152 	sp1 = sp = ritem + statep->items[i];
153 
154 	while (*sp >= 0) ++sp;
155 	rule = -(*sp);
156 	fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
157 
158         for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
159 	    fprintf(verbose_file, "%s ", symbol_name[*sp]);
160 
161 	putc('.', verbose_file);
162 
163 	while (*sp >= 0)
164 	{
165 	    fprintf(verbose_file, " %s", symbol_name[*sp]);
166 	    sp++;
167 	}
168 	fprintf(verbose_file, "  (%d)\n", -2 - *sp);
169     }
170 }
171 
172 
print_nulls(int state)173 void print_nulls(int state)
174 {
175     register action *p;
176     register int i, j, k, nnulls;
177 
178     nnulls = 0;
179     for (p = parser[state]; p; p = p->next)
180     {
181 	if (p->action_code == REDUCE &&
182 		(p->suppressed == 0 || p->suppressed == 1))
183 	{
184 	    i = p->number;
185 	    if (rrhs[i] + 1 == rrhs[i+1])
186 	    {
187 		for (j = 0; j < nnulls && i > null_rules[j]; ++j)
188 		    continue;
189 
190 		if (j == nnulls)
191 		{
192 		    ++nnulls;
193 		    null_rules[j] = i;
194 		}
195 		else if (i != null_rules[j])
196 		{
197 		    ++nnulls;
198 		    for (k = nnulls - 1; k > j; --k)
199 			null_rules[k] = null_rules[k-1];
200 		    null_rules[j] = i;
201 		}
202 	    }
203 	}
204     }
205 
206     for (i = 0; i < nnulls; ++i)
207     {
208 	j = null_rules[i];
209 	fprintf(verbose_file, "\t%s : .  (%d)\n", symbol_name[rlhs[j]],
210 		j - 2);
211     }
212     fprintf(verbose_file, "\n");
213 }
214 
215 
print_actions(int stateno)216 void print_actions(int stateno)
217 {
218     register action *p;
219     register shifts *sp;
220     register int as;
221 
222     if (stateno == final_state)
223 	fprintf(verbose_file, "\t$end  accept\n");
224 
225     p = parser[stateno];
226     if (p)
227     {
228 	print_shifts(p);
229 	print_reductions(p, defred[stateno]);
230     }
231 
232     sp = shift_table[stateno];
233     if (sp && sp->nshifts > 0)
234     {
235 	as = accessing_symbol[sp->shift[sp->nshifts - 1]];
236 	if (ISVAR(as))
237 	    print_gotos(stateno);
238     }
239 }
240 
241 
print_shifts(action * p)242 void print_shifts(action *p)
243 {
244     register int count;
245     register action *q;
246 
247     count = 0;
248     for (q = p; q; q = q->next)
249     {
250 	if (q->suppressed < 2 && q->action_code == SHIFT)
251 	    ++count;
252     }
253 
254     if (count > 0)
255     {
256 	for (; p; p = p->next)
257 	{
258 	    if (p->action_code == SHIFT && p->suppressed == 0)
259 		fprintf(verbose_file, "\t%s  shift %d\n",
260 			    symbol_name[p->symbol], p->number);
261 	}
262     }
263 }
264 
265 
print_reductions(action * p,int defred)266 void print_reductions(action *p, int defred)
267 {
268     register int k, anyreds;
269     register action *q;
270 
271     anyreds = 0;
272     for (q = p; q ; q = q->next)
273     {
274 	if (q->action_code == REDUCE && q->suppressed < 2)
275 	{
276 	    anyreds = 1;
277 	    break;
278 	}
279     }
280 
281     if (anyreds == 0)
282 	fprintf(verbose_file, "\t.  error\n");
283     else
284     {
285 	for (; p; p = p->next)
286 	{
287 	    if (p->action_code == REDUCE && p->number != defred)
288 	    {
289 		k = p->number - 2;
290 		if (p->suppressed == 0)
291 		    fprintf(verbose_file, "\t%s  reduce %d\n",
292 			    symbol_name[p->symbol], k);
293 	    }
294 	}
295 
296         if (defred > 0)
297 	    fprintf(verbose_file, "\t.  reduce %d\n", defred - 2);
298     }
299 }
300 
301 
print_gotos(int stateno)302 void print_gotos(int stateno)
303 {
304     register int i, k;
305     register int as;
306     register Yshort *to_state;
307     register shifts *sp;
308 
309     putc('\n', verbose_file);
310     sp = shift_table[stateno];
311     to_state = sp->shift;
312     for (i = 0; i < sp->nshifts; ++i)
313     {
314 	k = to_state[i];
315 	as = accessing_symbol[k];
316 	if (ISVAR(as))
317 	    fprintf(verbose_file, "\t%s  goto %d\n", symbol_name[as], k);
318     }
319 }
320 
321