1 /* $NetBSD: verbose.c,v 1.8 2015/01/03 23:22:52 christos Exp $ */
3 /* Id: verbose.c,v 1.11 2014/04/01 23:15:59 Tom.Shields Exp */
8 __RCSID("$NetBSD: verbose.c,v 1.8 2015/01/03 23:22:52 christos Exp $");
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
);
21 static Value_t
*null_rules
;
31 null_rules
= TMALLOC(Value_t
, nrules
);
34 fprintf(verbose_file
, "\f\n");
35 for (i
= 0; i
< nstates
; i
++)
41 if (SRtotal
|| RRtotal
)
44 fprintf(verbose_file
, "\n\n%d terminals, %d nonterminals\n", ntokens
,
46 fprintf(verbose_file
, "%d grammar rules, %d states\n", nrules
- 2, nstates
);
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() */
53 fputs("\ngrammar parser grammar\n", verbose_file
);
54 fputs("symbol# value# symbol\n", verbose_file
);
55 for (i
= 0; i
< ntokens
; ++i
)
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
];
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
]);
75 fprintf(verbose_file
, "\n\nRules never reduced:\n");
76 for (i
= 3; i
< nrules
; ++i
)
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);
93 fprintf(verbose_file
, "\n\n");
94 for (i
= 0; i
< nstates
; i
++)
96 if (SRconflicts
[i
] || RRconflicts
[i
])
98 fprintf(verbose_file
, "State %d contains ", i
);
99 if (SRconflicts
[i
] > 0)
100 fprintf(verbose_file
, "%d shift/reduce conflict%s",
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",
108 PLURAL(RRconflicts
[i
]));
109 fprintf(verbose_file
, ".\n");
115 print_state(int 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
);
124 print_actions(state
);
128 print_conflicts(int state
)
130 int symbol
, act
, number
;
133 act
= 0; /* not shift/reduce... */
136 for (p
= parser
[state
]; p
; p
= p
->next
)
138 if (p
->suppressed
== 2)
141 if (p
->symbol
!= symbol
)
145 if (p
->action_code
== SHIFT
)
150 else if (p
->suppressed
== 1)
152 if (state
== final_state
&& symbol
== 0)
154 fprintf(verbose_file
, "%d: shift/reduce conflict \
155 (accept, reduce %d) on $end\n", state
, p
->number
- 2);
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
]);
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
]);
177 print_core(int state
)
186 statep
= state_table
[state
];
189 for (i
= 0; i
< k
; i
++)
191 sp1
= sp
= ritem
+ statep
->items
[i
];
196 fprintf(verbose_file
, "\t%s : ", symbol_name
[rlhs
[rule
]]);
198 for (sp
= ritem
+ rrhs
[rule
]; sp
< sp1
; sp
++)
199 fprintf(verbose_file
, "%s ", symbol_name
[*sp
]);
201 putc('.', verbose_file
);
205 fprintf(verbose_file
, " %s", symbol_name
[*sp
]);
208 fprintf(verbose_file
, " (%d)\n", -2 - *sp
);
213 print_nulls(int state
)
216 Value_t i
, j
, k
, nnulls
;
219 for (p
= parser
[state
]; p
; p
= p
->next
)
221 if (p
->action_code
== REDUCE
&&
222 (p
->suppressed
== 0 || p
->suppressed
== 1))
225 if (rrhs
[i
] + 1 == rrhs
[i
+ 1])
227 for (j
= 0; j
< nnulls
&& i
> null_rules
[j
]; ++j
)
235 else if (i
!= null_rules
[j
])
238 for (k
= (Value_t
) (nnulls
- 1); k
> j
; --k
)
239 null_rules
[k
] = null_rules
[k
- 1];
246 for (i
= 0; i
< nnulls
; ++i
)
249 fprintf(verbose_file
, "\t%s : . (%d)\n", symbol_name
[rlhs
[j
]],
252 fprintf(verbose_file
, "\n");
256 print_actions(int stateno
)
262 if (stateno
== final_state
)
263 fprintf(verbose_file
, "\t$end accept\n");
269 print_reductions(p
, defred
[stateno
]);
272 sp
= shift_table
[stateno
];
273 if (sp
&& sp
->nshifts
> 0)
275 as
= accessing_symbol
[sp
->shift
[sp
->nshifts
- 1]];
277 print_gotos(stateno
);
282 print_shifts(action
*p
)
288 for (q
= p
; q
; q
= q
->next
)
290 if (q
->suppressed
< 2 && q
->action_code
== SHIFT
)
296 for (; p
; p
= p
->next
)
298 if (p
->action_code
== SHIFT
&& p
->suppressed
== 0)
299 fprintf(verbose_file
, "\t%s shift %d\n",
300 symbol_name
[p
->symbol
], p
->number
);
301 #if defined(YYBTYACC)
302 if (backtrack
&& p
->action_code
== SHIFT
&& p
->suppressed
== 1)
303 fprintf(verbose_file
, "\t%s [trial] shift %d\n",
304 symbol_name
[p
->symbol
], p
->number
);
311 print_reductions(action
*p
, int defred2
)
317 for (q
= p
; q
; q
= q
->next
)
319 if (q
->action_code
== REDUCE
&& q
->suppressed
< 2)
327 fprintf(verbose_file
, "\t. error\n");
330 for (; p
; p
= p
->next
)
332 if (p
->action_code
== REDUCE
&& p
->number
!= defred2
)
335 if (p
->suppressed
== 0)
336 fprintf(verbose_file
, "\t%s reduce %d\n",
337 symbol_name
[p
->symbol
], k
);
338 #if defined(YYBTYACC)
339 if (backtrack
&& p
->suppressed
== 1)
340 fprintf(verbose_file
, "\t%s [trial] reduce %d\n",
341 symbol_name
[p
->symbol
], k
);
347 fprintf(verbose_file
, "\t. reduce %d\n", defred2
- 2);
352 print_gotos(int stateno
)
359 putc('\n', verbose_file
);
360 sp
= shift_table
[stateno
];
361 to_state2
= sp
->shift
;
362 for (i
= 0; i
< sp
->nshifts
; ++i
)
365 as
= accessing_symbol
[k
];
367 fprintf(verbose_file
, "\t%s goto %d\n", symbol_name
[as
], k
);