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