1*0a6a1f1dSLionel Sambuc /* $NetBSD: verbose.c,v 1.8 2015/01/03 23:22:52 christos Exp $ */
284d9c625SLionel Sambuc
3*0a6a1f1dSLionel Sambuc /* Id: verbose.c,v 1.11 2014/04/01 23:15:59 Tom.Shields Exp */
44a17663cSThomas Veerman
54a17663cSThomas Veerman #include "defs.h"
64a17663cSThomas Veerman
74a17663cSThomas Veerman #include <sys/cdefs.h>
8*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: verbose.c,v 1.8 2015/01/03 23:22:52 christos Exp $");
94a17663cSThomas Veerman
104a17663cSThomas Veerman static void log_conflicts(void);
114a17663cSThomas Veerman static void log_unused(void);
124a17663cSThomas Veerman static void print_actions(int stateno);
134a17663cSThomas Veerman static void print_conflicts(int state);
144a17663cSThomas Veerman static void print_core(int state);
154a17663cSThomas Veerman static void print_gotos(int stateno);
164a17663cSThomas Veerman static void print_nulls(int state);
174a17663cSThomas Veerman static void print_shifts(action *p);
184a17663cSThomas Veerman static void print_state(int state);
194a17663cSThomas Veerman static void print_reductions(action *p, int defred2);
204a17663cSThomas Veerman
21*0a6a1f1dSLionel Sambuc static Value_t *null_rules;
224a17663cSThomas Veerman
234a17663cSThomas Veerman void
verbose(void)244a17663cSThomas Veerman verbose(void)
254a17663cSThomas Veerman {
264a17663cSThomas Veerman int i;
274a17663cSThomas Veerman
284a17663cSThomas Veerman if (!vflag)
294a17663cSThomas Veerman return;
304a17663cSThomas Veerman
31*0a6a1f1dSLionel Sambuc null_rules = TMALLOC(Value_t, nrules);
324a17663cSThomas Veerman NO_SPACE(null_rules);
334a17663cSThomas Veerman
344a17663cSThomas Veerman fprintf(verbose_file, "\f\n");
354a17663cSThomas Veerman for (i = 0; i < nstates; i++)
364a17663cSThomas Veerman print_state(i);
374a17663cSThomas Veerman FREE(null_rules);
384a17663cSThomas Veerman
394a17663cSThomas Veerman if (nunused)
404a17663cSThomas Veerman log_unused();
414a17663cSThomas Veerman if (SRtotal || RRtotal)
424a17663cSThomas Veerman log_conflicts();
434a17663cSThomas Veerman
444a17663cSThomas Veerman fprintf(verbose_file, "\n\n%d terminals, %d nonterminals\n", ntokens,
454a17663cSThomas Veerman nvars);
464a17663cSThomas Veerman fprintf(verbose_file, "%d grammar rules, %d states\n", nrules - 2, nstates);
47*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
48*0a6a1f1dSLionel Sambuc { /* print out the grammar symbol # and parser internal symbol # for each
49*0a6a1f1dSLionel Sambuc symbol as an aide to writing the implementation for YYDESTRUCT_CALL()
50*0a6a1f1dSLionel Sambuc and YYSTYPE_TOSTRING() */
51*0a6a1f1dSLionel Sambuc int maxtok = 0;
52*0a6a1f1dSLionel Sambuc
53*0a6a1f1dSLionel Sambuc fputs("\ngrammar parser grammar\n", verbose_file);
54*0a6a1f1dSLionel Sambuc fputs("symbol# value# symbol\n", verbose_file);
55*0a6a1f1dSLionel Sambuc for (i = 0; i < ntokens; ++i)
56*0a6a1f1dSLionel Sambuc {
57*0a6a1f1dSLionel Sambuc fprintf(verbose_file, " %5d %5d %s\n",
58*0a6a1f1dSLionel Sambuc i, symbol_value[i], symbol_name[i]);
59*0a6a1f1dSLionel Sambuc if (symbol_value[i] > maxtok)
60*0a6a1f1dSLionel Sambuc maxtok = symbol_value[i];
61*0a6a1f1dSLionel Sambuc }
62*0a6a1f1dSLionel Sambuc for (i = ntokens; i < nsyms; ++i)
63*0a6a1f1dSLionel Sambuc fprintf(verbose_file, " %5d %5d %s\n",
64*0a6a1f1dSLionel Sambuc i, (maxtok + 1) + symbol_value[i] + 1, symbol_name[i]);
65*0a6a1f1dSLionel Sambuc }
66*0a6a1f1dSLionel Sambuc #endif
674a17663cSThomas Veerman }
684a17663cSThomas Veerman
694a17663cSThomas Veerman static void
log_unused(void)704a17663cSThomas Veerman log_unused(void)
714a17663cSThomas Veerman {
724a17663cSThomas Veerman int i;
73*0a6a1f1dSLionel Sambuc Value_t *p;
744a17663cSThomas Veerman
754a17663cSThomas Veerman fprintf(verbose_file, "\n\nRules never reduced:\n");
764a17663cSThomas Veerman for (i = 3; i < nrules; ++i)
774a17663cSThomas Veerman {
784a17663cSThomas Veerman if (!rules_used[i])
794a17663cSThomas Veerman {
804a17663cSThomas Veerman fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
814a17663cSThomas Veerman for (p = ritem + rrhs[i]; *p >= 0; ++p)
824a17663cSThomas Veerman fprintf(verbose_file, " %s", symbol_name[*p]);
834a17663cSThomas Veerman fprintf(verbose_file, " (%d)\n", i - 2);
844a17663cSThomas Veerman }
854a17663cSThomas Veerman }
864a17663cSThomas Veerman }
874a17663cSThomas Veerman
884a17663cSThomas Veerman static void
log_conflicts(void)894a17663cSThomas Veerman log_conflicts(void)
904a17663cSThomas Veerman {
914a17663cSThomas Veerman int i;
924a17663cSThomas Veerman
934a17663cSThomas Veerman fprintf(verbose_file, "\n\n");
944a17663cSThomas Veerman for (i = 0; i < nstates; i++)
954a17663cSThomas Veerman {
964a17663cSThomas Veerman if (SRconflicts[i] || RRconflicts[i])
974a17663cSThomas Veerman {
984a17663cSThomas Veerman fprintf(verbose_file, "State %d contains ", i);
994a17663cSThomas Veerman if (SRconflicts[i] > 0)
1004a17663cSThomas Veerman fprintf(verbose_file, "%d shift/reduce conflict%s",
1014a17663cSThomas Veerman SRconflicts[i],
1024a17663cSThomas Veerman PLURAL(SRconflicts[i]));
1034a17663cSThomas Veerman if (SRconflicts[i] && RRconflicts[i])
1044a17663cSThomas Veerman fprintf(verbose_file, ", ");
1054a17663cSThomas Veerman if (RRconflicts[i] > 0)
1064a17663cSThomas Veerman fprintf(verbose_file, "%d reduce/reduce conflict%s",
1074a17663cSThomas Veerman RRconflicts[i],
1084a17663cSThomas Veerman PLURAL(RRconflicts[i]));
1094a17663cSThomas Veerman fprintf(verbose_file, ".\n");
1104a17663cSThomas Veerman }
1114a17663cSThomas Veerman }
1124a17663cSThomas Veerman }
1134a17663cSThomas Veerman
1144a17663cSThomas Veerman static void
print_state(int state)1154a17663cSThomas Veerman print_state(int state)
1164a17663cSThomas Veerman {
1174a17663cSThomas Veerman if (state)
1184a17663cSThomas Veerman fprintf(verbose_file, "\n\n");
1194a17663cSThomas Veerman if (SRconflicts[state] || RRconflicts[state])
1204a17663cSThomas Veerman print_conflicts(state);
1214a17663cSThomas Veerman fprintf(verbose_file, "state %d\n", state);
1224a17663cSThomas Veerman print_core(state);
1234a17663cSThomas Veerman print_nulls(state);
1244a17663cSThomas Veerman print_actions(state);
1254a17663cSThomas Veerman }
1264a17663cSThomas Veerman
1274a17663cSThomas Veerman static void
print_conflicts(int state)1284a17663cSThomas Veerman print_conflicts(int state)
1294a17663cSThomas Veerman {
1304a17663cSThomas Veerman int symbol, act, number;
1314a17663cSThomas Veerman action *p;
1324a17663cSThomas Veerman
1334a17663cSThomas Veerman act = 0; /* not shift/reduce... */
1344a17663cSThomas Veerman number = -1;
1354a17663cSThomas Veerman symbol = -1;
1364a17663cSThomas Veerman for (p = parser[state]; p; p = p->next)
1374a17663cSThomas Veerman {
1384a17663cSThomas Veerman if (p->suppressed == 2)
1394a17663cSThomas Veerman continue;
1404a17663cSThomas Veerman
1414a17663cSThomas Veerman if (p->symbol != symbol)
1424a17663cSThomas Veerman {
1434a17663cSThomas Veerman symbol = p->symbol;
1444a17663cSThomas Veerman number = p->number;
1454a17663cSThomas Veerman if (p->action_code == SHIFT)
1464a17663cSThomas Veerman act = SHIFT;
1474a17663cSThomas Veerman else
1484a17663cSThomas Veerman act = REDUCE;
1494a17663cSThomas Veerman }
1504a17663cSThomas Veerman else if (p->suppressed == 1)
1514a17663cSThomas Veerman {
1524a17663cSThomas Veerman if (state == final_state && symbol == 0)
1534a17663cSThomas Veerman {
1544a17663cSThomas Veerman fprintf(verbose_file, "%d: shift/reduce conflict \
1554a17663cSThomas Veerman (accept, reduce %d) on $end\n", state, p->number - 2);
1564a17663cSThomas Veerman }
1574a17663cSThomas Veerman else
1584a17663cSThomas Veerman {
1594a17663cSThomas Veerman if (act == SHIFT)
1604a17663cSThomas Veerman {
1614a17663cSThomas Veerman fprintf(verbose_file, "%d: shift/reduce conflict \
1624a17663cSThomas Veerman (shift %d, reduce %d) on %s\n", state, number, p->number - 2,
1634a17663cSThomas Veerman symbol_name[symbol]);
1644a17663cSThomas Veerman }
1654a17663cSThomas Veerman else
1664a17663cSThomas Veerman {
1674a17663cSThomas Veerman fprintf(verbose_file, "%d: reduce/reduce conflict \
1684a17663cSThomas Veerman (reduce %d, reduce %d) on %s\n", state, number - 2, p->number - 2,
1694a17663cSThomas Veerman symbol_name[symbol]);
1704a17663cSThomas Veerman }
1714a17663cSThomas Veerman }
1724a17663cSThomas Veerman }
1734a17663cSThomas Veerman }
1744a17663cSThomas Veerman }
1754a17663cSThomas Veerman
1764a17663cSThomas Veerman static void
print_core(int state)1774a17663cSThomas Veerman print_core(int state)
1784a17663cSThomas Veerman {
1794a17663cSThomas Veerman int i;
1804a17663cSThomas Veerman int k;
1814a17663cSThomas Veerman int rule;
1824a17663cSThomas Veerman core *statep;
183*0a6a1f1dSLionel Sambuc Value_t *sp;
184*0a6a1f1dSLionel Sambuc Value_t *sp1;
1854a17663cSThomas Veerman
1864a17663cSThomas Veerman statep = state_table[state];
1874a17663cSThomas Veerman k = statep->nitems;
1884a17663cSThomas Veerman
1894a17663cSThomas Veerman for (i = 0; i < k; i++)
1904a17663cSThomas Veerman {
1914a17663cSThomas Veerman sp1 = sp = ritem + statep->items[i];
1924a17663cSThomas Veerman
1934a17663cSThomas Veerman while (*sp >= 0)
1944a17663cSThomas Veerman ++sp;
1954a17663cSThomas Veerman rule = -(*sp);
1964a17663cSThomas Veerman fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
1974a17663cSThomas Veerman
1984a17663cSThomas Veerman for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
1994a17663cSThomas Veerman fprintf(verbose_file, "%s ", symbol_name[*sp]);
2004a17663cSThomas Veerman
2014a17663cSThomas Veerman putc('.', verbose_file);
2024a17663cSThomas Veerman
2034a17663cSThomas Veerman while (*sp >= 0)
2044a17663cSThomas Veerman {
2054a17663cSThomas Veerman fprintf(verbose_file, " %s", symbol_name[*sp]);
2064a17663cSThomas Veerman sp++;
2074a17663cSThomas Veerman }
2084a17663cSThomas Veerman fprintf(verbose_file, " (%d)\n", -2 - *sp);
2094a17663cSThomas Veerman }
2104a17663cSThomas Veerman }
2114a17663cSThomas Veerman
2124a17663cSThomas Veerman static void
print_nulls(int state)2134a17663cSThomas Veerman print_nulls(int state)
2144a17663cSThomas Veerman {
2154a17663cSThomas Veerman action *p;
2164a17663cSThomas Veerman Value_t i, j, k, nnulls;
2174a17663cSThomas Veerman
2184a17663cSThomas Veerman nnulls = 0;
2194a17663cSThomas Veerman for (p = parser[state]; p; p = p->next)
2204a17663cSThomas Veerman {
2214a17663cSThomas Veerman if (p->action_code == REDUCE &&
2224a17663cSThomas Veerman (p->suppressed == 0 || p->suppressed == 1))
2234a17663cSThomas Veerman {
2244a17663cSThomas Veerman i = p->number;
2254a17663cSThomas Veerman if (rrhs[i] + 1 == rrhs[i + 1])
2264a17663cSThomas Veerman {
2274a17663cSThomas Veerman for (j = 0; j < nnulls && i > null_rules[j]; ++j)
2284a17663cSThomas Veerman continue;
2294a17663cSThomas Veerman
2304a17663cSThomas Veerman if (j == nnulls)
2314a17663cSThomas Veerman {
2324a17663cSThomas Veerman ++nnulls;
2334a17663cSThomas Veerman null_rules[j] = i;
2344a17663cSThomas Veerman }
2354a17663cSThomas Veerman else if (i != null_rules[j])
2364a17663cSThomas Veerman {
2374a17663cSThomas Veerman ++nnulls;
2384a17663cSThomas Veerman for (k = (Value_t) (nnulls - 1); k > j; --k)
2394a17663cSThomas Veerman null_rules[k] = null_rules[k - 1];
2404a17663cSThomas Veerman null_rules[j] = i;
2414a17663cSThomas Veerman }
2424a17663cSThomas Veerman }
2434a17663cSThomas Veerman }
2444a17663cSThomas Veerman }
2454a17663cSThomas Veerman
2464a17663cSThomas Veerman for (i = 0; i < nnulls; ++i)
2474a17663cSThomas Veerman {
2484a17663cSThomas Veerman j = null_rules[i];
2494a17663cSThomas Veerman fprintf(verbose_file, "\t%s : . (%d)\n", symbol_name[rlhs[j]],
2504a17663cSThomas Veerman j - 2);
2514a17663cSThomas Veerman }
2524a17663cSThomas Veerman fprintf(verbose_file, "\n");
2534a17663cSThomas Veerman }
2544a17663cSThomas Veerman
2554a17663cSThomas Veerman static void
print_actions(int stateno)2564a17663cSThomas Veerman print_actions(int stateno)
2574a17663cSThomas Veerman {
2584a17663cSThomas Veerman action *p;
2594a17663cSThomas Veerman shifts *sp;
2604a17663cSThomas Veerman int as;
2614a17663cSThomas Veerman
2624a17663cSThomas Veerman if (stateno == final_state)
2634a17663cSThomas Veerman fprintf(verbose_file, "\t$end accept\n");
2644a17663cSThomas Veerman
2654a17663cSThomas Veerman p = parser[stateno];
2664a17663cSThomas Veerman if (p)
2674a17663cSThomas Veerman {
2684a17663cSThomas Veerman print_shifts(p);
2694a17663cSThomas Veerman print_reductions(p, defred[stateno]);
2704a17663cSThomas Veerman }
2714a17663cSThomas Veerman
2724a17663cSThomas Veerman sp = shift_table[stateno];
2734a17663cSThomas Veerman if (sp && sp->nshifts > 0)
2744a17663cSThomas Veerman {
2754a17663cSThomas Veerman as = accessing_symbol[sp->shift[sp->nshifts - 1]];
2764a17663cSThomas Veerman if (ISVAR(as))
2774a17663cSThomas Veerman print_gotos(stateno);
2784a17663cSThomas Veerman }
2794a17663cSThomas Veerman }
2804a17663cSThomas Veerman
2814a17663cSThomas Veerman static void
print_shifts(action * p)2824a17663cSThomas Veerman print_shifts(action *p)
2834a17663cSThomas Veerman {
2844a17663cSThomas Veerman int count;
2854a17663cSThomas Veerman action *q;
2864a17663cSThomas Veerman
2874a17663cSThomas Veerman count = 0;
2884a17663cSThomas Veerman for (q = p; q; q = q->next)
2894a17663cSThomas Veerman {
2904a17663cSThomas Veerman if (q->suppressed < 2 && q->action_code == SHIFT)
2914a17663cSThomas Veerman ++count;
2924a17663cSThomas Veerman }
2934a17663cSThomas Veerman
2944a17663cSThomas Veerman if (count > 0)
2954a17663cSThomas Veerman {
2964a17663cSThomas Veerman for (; p; p = p->next)
2974a17663cSThomas Veerman {
2984a17663cSThomas Veerman if (p->action_code == SHIFT && p->suppressed == 0)
2994a17663cSThomas Veerman fprintf(verbose_file, "\t%s shift %d\n",
3004a17663cSThomas Veerman symbol_name[p->symbol], p->number);
301*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
302*0a6a1f1dSLionel Sambuc if (backtrack && p->action_code == SHIFT && p->suppressed == 1)
303*0a6a1f1dSLionel Sambuc fprintf(verbose_file, "\t%s [trial] shift %d\n",
304*0a6a1f1dSLionel Sambuc symbol_name[p->symbol], p->number);
305*0a6a1f1dSLionel Sambuc #endif
3064a17663cSThomas Veerman }
3074a17663cSThomas Veerman }
3084a17663cSThomas Veerman }
3094a17663cSThomas Veerman
3104a17663cSThomas Veerman static void
print_reductions(action * p,int defred2)3114a17663cSThomas Veerman print_reductions(action *p, int defred2)
3124a17663cSThomas Veerman {
3134a17663cSThomas Veerman int k, anyreds;
3144a17663cSThomas Veerman action *q;
3154a17663cSThomas Veerman
3164a17663cSThomas Veerman anyreds = 0;
3174a17663cSThomas Veerman for (q = p; q; q = q->next)
3184a17663cSThomas Veerman {
3194a17663cSThomas Veerman if (q->action_code == REDUCE && q->suppressed < 2)
3204a17663cSThomas Veerman {
3214a17663cSThomas Veerman anyreds = 1;
3224a17663cSThomas Veerman break;
3234a17663cSThomas Veerman }
3244a17663cSThomas Veerman }
3254a17663cSThomas Veerman
3264a17663cSThomas Veerman if (anyreds == 0)
3274a17663cSThomas Veerman fprintf(verbose_file, "\t. error\n");
3284a17663cSThomas Veerman else
3294a17663cSThomas Veerman {
3304a17663cSThomas Veerman for (; p; p = p->next)
3314a17663cSThomas Veerman {
3324a17663cSThomas Veerman if (p->action_code == REDUCE && p->number != defred2)
3334a17663cSThomas Veerman {
3344a17663cSThomas Veerman k = p->number - 2;
3354a17663cSThomas Veerman if (p->suppressed == 0)
3364a17663cSThomas Veerman fprintf(verbose_file, "\t%s reduce %d\n",
3374a17663cSThomas Veerman symbol_name[p->symbol], k);
338*0a6a1f1dSLionel Sambuc #if defined(YYBTYACC)
339*0a6a1f1dSLionel Sambuc if (backtrack && p->suppressed == 1)
340*0a6a1f1dSLionel Sambuc fprintf(verbose_file, "\t%s [trial] reduce %d\n",
341*0a6a1f1dSLionel Sambuc symbol_name[p->symbol], k);
342*0a6a1f1dSLionel Sambuc #endif
3434a17663cSThomas Veerman }
3444a17663cSThomas Veerman }
3454a17663cSThomas Veerman
3464a17663cSThomas Veerman if (defred2 > 0)
3474a17663cSThomas Veerman fprintf(verbose_file, "\t. reduce %d\n", defred2 - 2);
3484a17663cSThomas Veerman }
3494a17663cSThomas Veerman }
3504a17663cSThomas Veerman
3514a17663cSThomas Veerman static void
print_gotos(int stateno)3524a17663cSThomas Veerman print_gotos(int stateno)
3534a17663cSThomas Veerman {
3544a17663cSThomas Veerman int i, k;
3554a17663cSThomas Veerman int as;
356*0a6a1f1dSLionel Sambuc Value_t *to_state2;
3574a17663cSThomas Veerman shifts *sp;
3584a17663cSThomas Veerman
3594a17663cSThomas Veerman putc('\n', verbose_file);
3604a17663cSThomas Veerman sp = shift_table[stateno];
3614a17663cSThomas Veerman to_state2 = sp->shift;
3624a17663cSThomas Veerman for (i = 0; i < sp->nshifts; ++i)
3634a17663cSThomas Veerman {
3644a17663cSThomas Veerman k = to_state2[i];
3654a17663cSThomas Veerman as = accessing_symbol[k];
3664a17663cSThomas Veerman if (ISVAR(as))
3674a17663cSThomas Veerman fprintf(verbose_file, "\t%s goto %d\n", symbol_name[as], k);
3684a17663cSThomas Veerman }
3694a17663cSThomas Veerman }
370