Imported Upstream version 20200910
[platform/upstream/byacc.git] / verbose.c
1 /* $Id: verbose.c,v 1.13 2020/09/10 17:57:34 tom 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     core *statep = state_table[state];
176     int k = statep->nitems;
177
178     for (i = 0; i < k; i++)
179     {
180         int rule;
181         Value_t *sp = ritem + statep->items[i];
182         Value_t *sp1 = sp;
183
184         while (*sp >= 0)
185             ++sp;
186         rule = -(*sp);
187         fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
188
189         for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
190             fprintf(verbose_file, "%s ", symbol_name[*sp]);
191
192         putc('.', verbose_file);
193
194         while (*sp >= 0)
195         {
196             fprintf(verbose_file, " %s", symbol_name[*sp]);
197             sp++;
198         }
199         fprintf(verbose_file, "  (%d)\n", -2 - *sp);
200     }
201 }
202
203 static void
204 print_nulls(int state)
205 {
206     action *p;
207     Value_t i, j, k, nnulls;
208
209     nnulls = 0;
210     for (p = parser[state]; p; p = p->next)
211     {
212         if (p->action_code == REDUCE &&
213             (p->suppressed == 0 || p->suppressed == 1))
214         {
215             i = p->number;
216             if (rrhs[i] + 1 == rrhs[i + 1])
217             {
218                 for (j = 0; j < nnulls && i > null_rules[j]; ++j)
219                     continue;
220
221                 if (j == nnulls)
222                 {
223                     ++nnulls;
224                     null_rules[j] = i;
225                 }
226                 else if (i != null_rules[j])
227                 {
228                     ++nnulls;
229                     for (k = (Value_t)(nnulls - 1); k > j; --k)
230                         null_rules[k] = null_rules[k - 1];
231                     null_rules[j] = i;
232                 }
233             }
234         }
235     }
236
237     for (i = 0; i < nnulls; ++i)
238     {
239         j = null_rules[i];
240         fprintf(verbose_file, "\t%s : .  (%d)\n", symbol_name[rlhs[j]],
241                 j - 2);
242     }
243     fprintf(verbose_file, "\n");
244 }
245
246 static void
247 print_actions(int stateno)
248 {
249     action *p;
250     shifts *sp;
251
252     if (stateno == final_state)
253         fprintf(verbose_file, "\t$end  accept\n");
254
255     p = parser[stateno];
256     if (p)
257     {
258         print_shifts(p);
259         print_reductions(p, defred[stateno]);
260     }
261
262     sp = shift_table[stateno];
263     if (sp && sp->nshifts > 0)
264     {
265         int as = accessing_symbol[sp->shift[sp->nshifts - 1]];
266
267         if (ISVAR(as))
268             print_gotos(stateno);
269     }
270 }
271
272 static void
273 print_shifts(action *p)
274 {
275     int count;
276     action *q;
277
278     count = 0;
279     for (q = p; q; q = q->next)
280     {
281         if (q->suppressed < 2 && q->action_code == SHIFT)
282             ++count;
283     }
284
285     if (count > 0)
286     {
287         for (; p; p = p->next)
288         {
289             if (p->action_code == SHIFT && p->suppressed == 0)
290                 fprintf(verbose_file, "\t%s  shift %d\n",
291                         symbol_name[p->symbol], p->number);
292 #if defined(YYBTYACC)
293             if (backtrack && p->action_code == SHIFT && p->suppressed == 1)
294                 fprintf(verbose_file, "\t%s  [trial] shift %d\n",
295                         symbol_name[p->symbol], p->number);
296 #endif
297         }
298     }
299 }
300
301 static void
302 print_reductions(action *p, int defred2)
303 {
304     int anyreds;
305     action *q;
306
307     anyreds = 0;
308     for (q = p; q; q = q->next)
309     {
310         if (q->action_code == REDUCE && q->suppressed < 2)
311         {
312             anyreds = 1;
313             break;
314         }
315     }
316
317     if (anyreds == 0)
318         fprintf(verbose_file, "\t.  error\n");
319     else
320     {
321         for (; p; p = p->next)
322         {
323             if (p->action_code == REDUCE && p->number != defred2)
324             {
325                 int k = p->number - 2;
326
327                 if (p->suppressed == 0)
328                     fprintf(verbose_file, "\t%s  reduce %d\n",
329                             symbol_name[p->symbol], k);
330 #if defined(YYBTYACC)
331                 if (backtrack && p->suppressed == 1)
332                     fprintf(verbose_file, "\t%s  [trial] reduce %d\n",
333                             symbol_name[p->symbol], k);
334 #endif
335             }
336         }
337
338         if (defred2 > 0)
339             fprintf(verbose_file, "\t.  reduce %d\n", defred2 - 2);
340     }
341 }
342
343 static void
344 print_gotos(int stateno)
345 {
346     int i;
347     Value_t *to_state2;
348     shifts *sp;
349
350     putc('\n', verbose_file);
351     sp = shift_table[stateno];
352     to_state2 = sp->shift;
353     for (i = 0; i < sp->nshifts; ++i)
354     {
355         int k = to_state2[i];
356         int as = accessing_symbol[k];
357
358         if (ISVAR(as))
359             fprintf(verbose_file, "\t%s  goto %d\n", symbol_name[as], k);
360     }
361 }