Annotation of src/usr.bin/yacc/verbose.c, Revision 1.10
1.10 ! deraadt 1: /* $OpenBSD: verbose.c,v 1.9 2005/06/10 16:40:45 pvalchev Exp $ */
1.2 deraadt 2: /* $NetBSD: verbose.c,v 1.4 1996/03/19 03:21:50 jtc Exp $ */
3:
4: /*
5: * Copyright (c) 1989 The Regents of the University of California.
6: * All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Robert Paul Corbett.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
1.7 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.2 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
1.1 deraadt 35:
36: #include "defs.h"
37:
38: static short *null_rules;
39:
1.6 millert 40: void log_unused(void);
41: void log_conflicts(void);
42: void print_state(int);
43: void print_conflicts(int);
44: void print_core(int);
45: void print_nulls(int);
46: void print_actions(int);
47: void print_shifts(action *);
48: void print_reductions(action *, int);
49: void print_gotos(int);
1.4 pvalchev 50:
51: void
1.8 pvalchev 52: verbose(void)
1.1 deraadt 53: {
1.5 mpech 54: int i;
1.1 deraadt 55:
56: if (!vflag) return;
57:
58: null_rules = (short *) MALLOC(nrules*sizeof(short));
59: if (null_rules == 0) no_space();
60: fprintf(verbose_file, "\f\n");
61: for (i = 0; i < nstates; i++)
62: print_state(i);
63: FREE(null_rules);
64:
65: if (nunused)
66: log_unused();
67: if (SRtotal || RRtotal)
68: log_conflicts();
69:
70: fprintf(verbose_file, "\n\n%d terminals, %d nonterminals\n", ntokens,
71: nvars);
72: fprintf(verbose_file, "%d grammar rules, %d states\n", nrules - 2, nstates);
73: }
74:
75:
1.4 pvalchev 76: void
1.8 pvalchev 77: log_unused(void)
1.1 deraadt 78: {
1.5 mpech 79: int i;
80: short *p;
1.1 deraadt 81:
82: fprintf(verbose_file, "\n\nRules never reduced:\n");
83: for (i = 3; i < nrules; ++i)
84: {
85: if (!rules_used[i])
86: {
87: fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
88: for (p = ritem + rrhs[i]; *p >= 0; ++p)
89: fprintf(verbose_file, " %s", symbol_name[*p]);
90: fprintf(verbose_file, " (%d)\n", i - 2);
91: }
92: }
93: }
94:
95:
1.4 pvalchev 96: void
1.8 pvalchev 97: log_conflicts(void)
1.1 deraadt 98: {
1.5 mpech 99: int i;
1.1 deraadt 100:
101: fprintf(verbose_file, "\n\n");
102: for (i = 0; i < nstates; i++)
103: {
104: if (SRconflicts[i] || RRconflicts[i])
105: {
106: fprintf(verbose_file, "State %d contains ", i);
107: if (SRconflicts[i] == 1)
108: fprintf(verbose_file, "1 shift/reduce conflict");
109: else if (SRconflicts[i] > 1)
110: fprintf(verbose_file, "%d shift/reduce conflicts",
111: SRconflicts[i]);
112: if (SRconflicts[i] && RRconflicts[i])
113: fprintf(verbose_file, ", ");
114: if (RRconflicts[i] == 1)
115: fprintf(verbose_file, "1 reduce/reduce conflict");
116: else if (RRconflicts[i] > 1)
117: fprintf(verbose_file, "%d reduce/reduce conflicts",
118: RRconflicts[i]);
119: fprintf(verbose_file, ".\n");
120: }
121: }
122: }
123:
124:
1.4 pvalchev 125: void
1.8 pvalchev 126: print_state(int state)
1.1 deraadt 127: {
128: if (state)
129: fprintf(verbose_file, "\n\n");
130: if (SRconflicts[state] || RRconflicts[state])
131: print_conflicts(state);
132: fprintf(verbose_file, "state %d\n", state);
133: print_core(state);
134: print_nulls(state);
135: print_actions(state);
136: }
137:
138:
1.4 pvalchev 139: void
1.8 pvalchev 140: print_conflicts(int state)
1.1 deraadt 141: {
1.9 pvalchev 142: int symbol, act = REDUCE, number = 0;
1.5 mpech 143: action *p;
1.1 deraadt 144:
145: symbol = -1;
146: for (p = parser[state]; p; p = p->next)
147: {
148: if (p->suppressed == 2)
149: continue;
150:
151: if (p->symbol != symbol)
152: {
153: symbol = p->symbol;
154: number = p->number;
155: if (p->action_code == SHIFT)
156: act = SHIFT;
157: else
158: act = REDUCE;
159: }
160: else if (p->suppressed == 1)
161: {
162: if (state == final_state && symbol == 0)
163: {
164: fprintf(verbose_file, "%d: shift/reduce conflict \
165: (accept, reduce %d) on $end\n", state, p->number - 2);
166: }
167: else
168: {
169: if (act == SHIFT)
170: {
171: fprintf(verbose_file, "%d: shift/reduce conflict \
172: (shift %d, reduce %d) on %s\n", state, number, p->number - 2,
173: symbol_name[symbol]);
174: }
175: else
176: {
177: fprintf(verbose_file, "%d: reduce/reduce conflict \
178: (reduce %d, reduce %d) on %s\n", state, number - 2, p->number - 2,
179: symbol_name[symbol]);
180: }
181: }
182: }
183: }
184: }
185:
186:
1.4 pvalchev 187: void
1.8 pvalchev 188: print_core(int state)
1.1 deraadt 189: {
1.5 mpech 190: int i;
191: int k;
192: int rule;
193: core *statep;
194: short *sp;
195: short *sp1;
1.1 deraadt 196:
197: statep = state_table[state];
198: k = statep->nitems;
199:
200: for (i = 0; i < k; i++)
201: {
202: sp1 = sp = ritem + statep->items[i];
203:
204: while (*sp >= 0) ++sp;
205: rule = -(*sp);
206: fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
207:
208: for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
209: fprintf(verbose_file, "%s ", symbol_name[*sp]);
210:
211: putc('.', verbose_file);
212:
213: while (*sp >= 0)
214: {
215: fprintf(verbose_file, " %s", symbol_name[*sp]);
216: sp++;
217: }
218: fprintf(verbose_file, " (%d)\n", -2 - *sp);
219: }
220: }
221:
222:
1.4 pvalchev 223: void
1.8 pvalchev 224: print_nulls(int state)
1.1 deraadt 225: {
1.5 mpech 226: action *p;
227: int i, j, k, nnulls;
1.1 deraadt 228:
229: nnulls = 0;
230: for (p = parser[state]; p; p = p->next)
231: {
232: if (p->action_code == REDUCE &&
233: (p->suppressed == 0 || p->suppressed == 1))
234: {
235: i = p->number;
236: if (rrhs[i] + 1 == rrhs[i+1])
237: {
238: for (j = 0; j < nnulls && i > null_rules[j]; ++j)
239: continue;
240:
241: if (j == nnulls)
242: {
243: ++nnulls;
244: null_rules[j] = i;
245: }
246: else if (i != null_rules[j])
247: {
248: ++nnulls;
249: for (k = nnulls - 1; k > j; --k)
250: null_rules[k] = null_rules[k-1];
251: null_rules[j] = i;
252: }
253: }
254: }
255: }
256:
257: for (i = 0; i < nnulls; ++i)
258: {
259: j = null_rules[i];
260: fprintf(verbose_file, "\t%s : . (%d)\n", symbol_name[rlhs[j]],
261: j - 2);
262: }
263: fprintf(verbose_file, "\n");
264: }
265:
266:
1.4 pvalchev 267: void
1.8 pvalchev 268: print_actions(int stateno)
1.1 deraadt 269: {
1.5 mpech 270: action *p;
271: shifts *sp;
272: int as;
1.1 deraadt 273:
274: if (stateno == final_state)
275: fprintf(verbose_file, "\t$end accept\n");
276:
277: p = parser[stateno];
278: if (p)
279: {
280: print_shifts(p);
281: print_reductions(p, defred[stateno]);
282: }
283:
284: sp = shift_table[stateno];
285: if (sp && sp->nshifts > 0)
286: {
287: as = accessing_symbol[sp->shift[sp->nshifts - 1]];
288: if (ISVAR(as))
289: print_gotos(stateno);
290: }
291: }
292:
293:
1.4 pvalchev 294: void
1.8 pvalchev 295: print_shifts(action *p)
1.1 deraadt 296: {
1.5 mpech 297: int count;
298: action *q;
1.1 deraadt 299:
300: count = 0;
301: for (q = p; q; q = q->next)
302: {
303: if (q->suppressed < 2 && q->action_code == SHIFT)
304: ++count;
305: }
306:
307: if (count > 0)
308: {
309: for (; p; p = p->next)
310: {
311: if (p->action_code == SHIFT && p->suppressed == 0)
312: fprintf(verbose_file, "\t%s shift %d\n",
313: symbol_name[p->symbol], p->number);
314: }
315: }
316: }
317:
318:
1.4 pvalchev 319: void
1.8 pvalchev 320: print_reductions(action *p, int defred)
1.1 deraadt 321: {
1.5 mpech 322: int k, anyreds;
323: action *q;
1.1 deraadt 324:
325: anyreds = 0;
326: for (q = p; q ; q = q->next)
327: {
328: if (q->action_code == REDUCE && q->suppressed < 2)
329: {
330: anyreds = 1;
331: break;
332: }
333: }
334:
335: if (anyreds == 0)
336: fprintf(verbose_file, "\t. error\n");
337: else
338: {
339: for (; p; p = p->next)
340: {
341: if (p->action_code == REDUCE && p->number != defred)
342: {
343: k = p->number - 2;
344: if (p->suppressed == 0)
345: fprintf(verbose_file, "\t%s reduce %d\n",
346: symbol_name[p->symbol], k);
347: }
348: }
349:
350: if (defred > 0)
351: fprintf(verbose_file, "\t. reduce %d\n", defred - 2);
352: }
353: }
354:
355:
1.4 pvalchev 356: void
1.8 pvalchev 357: print_gotos(int stateno)
1.1 deraadt 358: {
1.5 mpech 359: int i, k;
360: int as;
361: short *to_state;
362: shifts *sp;
1.1 deraadt 363:
364: putc('\n', verbose_file);
365: sp = shift_table[stateno];
366: to_state = sp->shift;
367: for (i = 0; i < sp->nshifts; ++i)
368: {
369: k = to_state[i];
370: as = accessing_symbol[k];
371: if (ISVAR(as))
372: fprintf(verbose_file, "\t%s goto %d\n", symbol_name[as], k);
373: }
374: }