Imported Upstream version 20221229
[platform/upstream/byacc.git] / verbose.c
1 /* $Id: verbose.c,v 1.14 2021/05/20 23:57:23 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%ld terminals, %ld nonterminals\n",
40             (long)ntokens, (long)nvars);
41     fprintf(verbose_file, "%ld grammar rules, %ld states\n",
42             (long)(nrules - 2), (long)nstates);
43 #if defined(YYBTYACC)
44     {                           /* print out the grammar symbol # and parser internal symbol # for each
45                                    symbol as an aide to writing the implementation for YYDESTRUCT_CALL()
46                                    and YYSTYPE_TOSTRING() */
47         int maxtok = 0;
48
49         fputs("\ngrammar parser grammar\n", verbose_file);
50         fputs("symbol# value# symbol\n", verbose_file);
51         for (i = 0; i < ntokens; ++i)
52         {
53             fprintf(verbose_file, " %5d  %5d  %s\n",
54                     i, symbol_value[i], symbol_name[i]);
55             if (symbol_value[i] > maxtok)
56                 maxtok = symbol_value[i];
57         }
58         for (i = ntokens; i < nsyms; ++i)
59             fprintf(verbose_file, " %5d  %5d  %s\n",
60                     i, (maxtok + 1) + symbol_value[i] + 1, symbol_name[i]);
61     }
62 #endif
63 }
64
65 static void
66 log_unused(void)
67 {
68     int i;
69     Value_t *p;
70
71     fprintf(verbose_file, "\n\nRules never reduced:\n");
72     for (i = 3; i < nrules; ++i)
73     {
74         if (!rules_used[i])
75         {
76             fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
77             for (p = ritem + rrhs[i]; *p >= 0; ++p)
78                 fprintf(verbose_file, " %s", symbol_name[*p]);
79             fprintf(verbose_file, "  (%d)\n", i - 2);
80         }
81     }
82 }
83
84 static void
85 log_conflicts(void)
86 {
87     int i;
88
89     fprintf(verbose_file, "\n\n");
90     for (i = 0; i < nstates; i++)
91     {
92         if (SRconflicts[i] || RRconflicts[i])
93         {
94             fprintf(verbose_file, "State %d contains ", i);
95             if (SRconflicts[i] > 0)
96                 fprintf(verbose_file, "%ld shift/reduce conflict%s",
97                         (long)SRconflicts[i],
98                         PLURAL(SRconflicts[i]));
99             if (SRconflicts[i] && RRconflicts[i])
100                 fprintf(verbose_file, ", ");
101             if (RRconflicts[i] > 0)
102                 fprintf(verbose_file, "%ld reduce/reduce conflict%s",
103                         (long)RRconflicts[i],
104                         PLURAL(RRconflicts[i]));
105             fprintf(verbose_file, ".\n");
106         }
107     }
108 }
109
110 static void
111 print_state(int state)
112 {
113     if (state)
114         fprintf(verbose_file, "\n\n");
115     if (SRconflicts[state] || RRconflicts[state])
116         print_conflicts(state);
117     fprintf(verbose_file, "state %d\n", state);
118     print_core(state);
119     print_nulls(state);
120     print_actions(state);
121 }
122
123 static void
124 print_conflicts(int state)
125 {
126     int symbol, act, number;
127     action *p;
128
129     act = 0;                    /* not shift/reduce... */
130     number = -1;
131     symbol = -1;
132     for (p = parser[state]; p; p = p->next)
133     {
134         if (p->suppressed == 2)
135             continue;
136
137         if (p->symbol != symbol)
138         {
139             symbol = p->symbol;
140             number = p->number;
141             if (p->action_code == SHIFT)
142                 act = SHIFT;
143             else
144                 act = REDUCE;
145         }
146         else if (p->suppressed == 1)
147         {
148             if (state == final_state && symbol == 0)
149             {
150                 fprintf(verbose_file, "%d: shift/reduce conflict \
151 (accept, reduce %ld) on $end\n", state, (long)(p->number - 2));
152             }
153             else
154             {
155                 if (act == SHIFT)
156                 {
157                     fprintf(verbose_file, "%d: shift/reduce conflict \
158 (shift %ld, reduce %ld) on %s\n", state, (long)number, (long)(p->number - 2),
159                             symbol_name[symbol]);
160                 }
161                 else
162                 {
163                     fprintf(verbose_file, "%d: reduce/reduce conflict \
164 (reduce %ld, reduce %ld) on %s\n", state, (long)(number - 2), (long)(p->number - 2),
165                             symbol_name[symbol]);
166                 }
167             }
168         }
169     }
170 }
171
172 static void
173 print_core(int state)
174 {
175     int i;
176     core *statep = state_table[state];
177     int k = statep->nitems;
178
179     for (i = 0; i < k; i++)
180     {
181         int rule;
182         Value_t *sp = ritem + statep->items[i];
183         Value_t *sp1 = sp;
184
185         while (*sp >= 0)
186             ++sp;
187         rule = -(*sp);
188         fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
189
190         for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
191             fprintf(verbose_file, "%s ", symbol_name[*sp]);
192
193         putc('.', verbose_file);
194
195         while (*sp >= 0)
196         {
197             fprintf(verbose_file, " %s", symbol_name[*sp]);
198             sp++;
199         }
200         fprintf(verbose_file, "  (%ld)\n", (long)(-2 - *sp));
201     }
202 }
203
204 static void
205 print_nulls(int state)
206 {
207     action *p;
208     Value_t i, j, k, nnulls;
209
210     nnulls = 0;
211     for (p = parser[state]; p; p = p->next)
212     {
213         if (p->action_code == REDUCE &&
214             (p->suppressed == 0 || p->suppressed == 1))
215         {
216             i = p->number;
217             if (rrhs[i] + 1 == rrhs[i + 1])
218             {
219                 for (j = 0; j < nnulls && i > null_rules[j]; ++j)
220                     continue;
221
222                 if (j == nnulls)
223                 {
224                     ++nnulls;
225                     null_rules[j] = i;
226                 }
227                 else if (i != null_rules[j])
228                 {
229                     ++nnulls;
230                     for (k = (Value_t)(nnulls - 1); k > j; --k)
231                         null_rules[k] = null_rules[k - 1];
232                     null_rules[j] = i;
233                 }
234             }
235         }
236     }
237
238     for (i = 0; i < nnulls; ++i)
239     {
240         j = null_rules[i];
241         fprintf(verbose_file, "\t%s : .  (%ld)\n", symbol_name[rlhs[j]],
242                 (long)(j - 2));
243     }
244     fprintf(verbose_file, "\n");
245 }
246
247 static void
248 print_actions(int stateno)
249 {
250     action *p;
251     shifts *sp;
252
253     if (stateno == final_state)
254         fprintf(verbose_file, "\t$end  accept\n");
255
256     p = parser[stateno];
257     if (p)
258     {
259         print_shifts(p);
260         print_reductions(p, defred[stateno]);
261     }
262
263     sp = shift_table[stateno];
264     if (sp && sp->nshifts > 0)
265     {
266         int as = accessing_symbol[sp->shift[sp->nshifts - 1]];
267
268         if (ISVAR(as))
269             print_gotos(stateno);
270     }
271 }
272
273 static void
274 print_shifts(action *p)
275 {
276     int count;
277     action *q;
278
279     count = 0;
280     for (q = p; q; q = q->next)
281     {
282         if (q->suppressed < 2 && q->action_code == SHIFT)
283             ++count;
284     }
285
286     if (count > 0)
287     {
288         for (; p; p = p->next)
289         {
290             if (p->action_code == SHIFT && p->suppressed == 0)
291                 fprintf(verbose_file, "\t%s  shift %ld\n",
292                         symbol_name[p->symbol], (long)p->number);
293 #if defined(YYBTYACC)
294             if (backtrack && p->action_code == SHIFT && p->suppressed == 1)
295                 fprintf(verbose_file, "\t%s  [trial] shift %d\n",
296                         symbol_name[p->symbol], p->number);
297 #endif
298         }
299     }
300 }
301
302 static void
303 print_reductions(action *p, int defred2)
304 {
305     int anyreds;
306     action *q;
307
308     anyreds = 0;
309     for (q = p; q; q = q->next)
310     {
311         if (q->action_code == REDUCE && q->suppressed < 2)
312         {
313             anyreds = 1;
314             break;
315         }
316     }
317
318     if (anyreds == 0)
319         fprintf(verbose_file, "\t.  error\n");
320     else
321     {
322         for (; p; p = p->next)
323         {
324             if (p->action_code == REDUCE && p->number != defred2)
325             {
326                 int k = p->number - 2;
327
328                 if (p->suppressed == 0)
329                     fprintf(verbose_file, "\t%s  reduce %d\n",
330                             symbol_name[p->symbol], k);
331 #if defined(YYBTYACC)
332                 if (backtrack && p->suppressed == 1)
333                     fprintf(verbose_file, "\t%s  [trial] reduce %d\n",
334                             symbol_name[p->symbol], k);
335 #endif
336             }
337         }
338
339         if (defred2 > 0)
340             fprintf(verbose_file, "\t.  reduce %d\n", defred2 - 2);
341     }
342 }
343
344 static void
345 print_gotos(int stateno)
346 {
347     int i;
348     Value_t *to_state2;
349     shifts *sp;
350
351     putc('\n', verbose_file);
352     sp = shift_table[stateno];
353     to_state2 = sp->shift;
354     for (i = 0; i < sp->nshifts; ++i)
355     {
356         int k = to_state2[i];
357         int as = accessing_symbol[k];
358
359         if (ISVAR(as))
360             fprintf(verbose_file, "\t%s  goto %d\n", symbol_name[as], k);
361     }
362 }