1 /* $Id: verbose.c,v 1.11 2014/04/01 23:15:59 Tom.Shields Exp $ */
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
);
16 static Value_t
*null_rules
;
26 null_rules
= TMALLOC(Value_t
, nrules
);
29 fprintf(verbose_file
, "\f\n");
30 for (i
= 0; i
< nstates
; i
++)
36 if (SRtotal
|| RRtotal
)
39 fprintf(verbose_file
, "\n\n%d terminals, %d nonterminals\n", ntokens
,
41 fprintf(verbose_file
, "%d grammar rules, %d states\n", nrules
- 2, nstates
);
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() */
48 fputs("\ngrammar parser grammar\n", verbose_file
);
49 fputs("symbol# value# symbol\n", verbose_file
);
50 for (i
= 0; i
< ntokens
; ++i
)
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
];
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
]);
70 fprintf(verbose_file
, "\n\nRules never reduced:\n");
71 for (i
= 3; i
< nrules
; ++i
)
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);
88 fprintf(verbose_file
, "\n\n");
89 for (i
= 0; i
< nstates
; i
++)
91 if (SRconflicts
[i
] || RRconflicts
[i
])
93 fprintf(verbose_file
, "State %d contains ", i
);
94 if (SRconflicts
[i
] > 0)
95 fprintf(verbose_file
, "%d shift/reduce conflict%s",
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",
103 PLURAL(RRconflicts
[i
]));
104 fprintf(verbose_file
, ".\n");
110 print_state(int 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
);
119 print_actions(state
);
123 print_conflicts(int state
)
125 int symbol
, act
, number
;
128 act
= 0; /* not shift/reduce... */
131 for (p
= parser
[state
]; p
; p
= p
->next
)
133 if (p
->suppressed
== 2)
136 if (p
->symbol
!= symbol
)
140 if (p
->action_code
== SHIFT
)
145 else if (p
->suppressed
== 1)
147 if (state
== final_state
&& symbol
== 0)
149 fprintf(verbose_file
, "%d: shift/reduce conflict \
150 (accept, reduce %d) on $end\n", state
, p
->number
- 2);
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
]);
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
]);
172 print_core(int state
)
181 statep
= state_table
[state
];
184 for (i
= 0; i
< k
; i
++)
186 sp1
= sp
= ritem
+ statep
->items
[i
];
191 fprintf(verbose_file
, "\t%s : ", symbol_name
[rlhs
[rule
]]);
193 for (sp
= ritem
+ rrhs
[rule
]; sp
< sp1
; sp
++)
194 fprintf(verbose_file
, "%s ", symbol_name
[*sp
]);
196 putc('.', verbose_file
);
200 fprintf(verbose_file
, " %s", symbol_name
[*sp
]);
203 fprintf(verbose_file
, " (%d)\n", -2 - *sp
);
208 print_nulls(int state
)
211 Value_t i
, j
, k
, nnulls
;
214 for (p
= parser
[state
]; p
; p
= p
->next
)
216 if (p
->action_code
== REDUCE
&&
217 (p
->suppressed
== 0 || p
->suppressed
== 1))
220 if (rrhs
[i
] + 1 == rrhs
[i
+ 1])
222 for (j
= 0; j
< nnulls
&& i
> null_rules
[j
]; ++j
)
230 else if (i
!= null_rules
[j
])
233 for (k
= (Value_t
) (nnulls
- 1); k
> j
; --k
)
234 null_rules
[k
] = null_rules
[k
- 1];
241 for (i
= 0; i
< nnulls
; ++i
)
244 fprintf(verbose_file
, "\t%s : . (%d)\n", symbol_name
[rlhs
[j
]],
247 fprintf(verbose_file
, "\n");
251 print_actions(int stateno
)
257 if (stateno
== final_state
)
258 fprintf(verbose_file
, "\t$end accept\n");
264 print_reductions(p
, defred
[stateno
]);
267 sp
= shift_table
[stateno
];
268 if (sp
&& sp
->nshifts
> 0)
270 as
= accessing_symbol
[sp
->shift
[sp
->nshifts
- 1]];
272 print_gotos(stateno
);
277 print_shifts(action
*p
)
283 for (q
= p
; q
; q
= q
->next
)
285 if (q
->suppressed
< 2 && q
->action_code
== SHIFT
)
291 for (; p
; p
= p
->next
)
293 if (p
->action_code
== SHIFT
&& p
->suppressed
== 0)
294 fprintf(verbose_file
, "\t%s shift %d\n",
295 symbol_name
[p
->symbol
], p
->number
);
296 #if defined(YYBTYACC)
297 if (backtrack
&& p
->action_code
== SHIFT
&& p
->suppressed
== 1)
298 fprintf(verbose_file
, "\t%s [trial] shift %d\n",
299 symbol_name
[p
->symbol
], p
->number
);
306 print_reductions(action
*p
, int defred2
)
312 for (q
= p
; q
; q
= q
->next
)
314 if (q
->action_code
== REDUCE
&& q
->suppressed
< 2)
322 fprintf(verbose_file
, "\t. error\n");
325 for (; p
; p
= p
->next
)
327 if (p
->action_code
== REDUCE
&& p
->number
!= defred2
)
330 if (p
->suppressed
== 0)
331 fprintf(verbose_file
, "\t%s reduce %d\n",
332 symbol_name
[p
->symbol
], k
);
333 #if defined(YYBTYACC)
334 if (backtrack
&& p
->suppressed
== 1)
335 fprintf(verbose_file
, "\t%s [trial] reduce %d\n",
336 symbol_name
[p
->symbol
], k
);
342 fprintf(verbose_file
, "\t. reduce %d\n", defred2
- 2);
347 print_gotos(int stateno
)
354 putc('\n', verbose_file
);
355 sp
= shift_table
[stateno
];
356 to_state2
= sp
->shift
;
357 for (i
= 0; i
< sp
->nshifts
; ++i
)
360 as
= accessing_symbol
[k
];
362 fprintf(verbose_file
, "\t%s goto %d\n", symbol_name
[as
], k
);