1 /* $NetBSD: verbose.c,v 1.6 2011/09/10 21:29:04 christos Exp $ */
2 /* Id: verbose.c,v 1.9 2010/06/09 08:58:29 tom Exp */
7 __RCSID("$NetBSD: verbose.c,v 1.6 2011/09/10 21:29:04 christos Exp $");
9 static void log_conflicts(void);
10 static void log_unused(void);
11 static void print_actions(int stateno
);
12 static void print_conflicts(int state
);
13 static void print_core(int state
);
14 static void print_gotos(int stateno
);
15 static void print_nulls(int state
);
16 static void print_shifts(action
*p
);
17 static void print_state(int state
);
18 static void print_reductions(action
*p
, int defred2
);
20 static short *null_rules
;
30 null_rules
= (short *)MALLOC((unsigned)nrules
* sizeof(short));
33 fprintf(verbose_file
, "\f\n");
34 for (i
= 0; i
< nstates
; i
++)
40 if (SRtotal
|| RRtotal
)
43 fprintf(verbose_file
, "\n\n%d terminals, %d nonterminals\n", ntokens
,
45 fprintf(verbose_file
, "%d grammar rules, %d states\n", nrules
- 2, nstates
);
54 fprintf(verbose_file
, "\n\nRules never reduced:\n");
55 for (i
= 3; i
< nrules
; ++i
)
59 fprintf(verbose_file
, "\t%s :", symbol_name
[rlhs
[i
]]);
60 for (p
= ritem
+ rrhs
[i
]; *p
>= 0; ++p
)
61 fprintf(verbose_file
, " %s", symbol_name
[*p
]);
62 fprintf(verbose_file
, " (%d)\n", i
- 2);
72 fprintf(verbose_file
, "\n\n");
73 for (i
= 0; i
< nstates
; i
++)
75 if (SRconflicts
[i
] || RRconflicts
[i
])
77 fprintf(verbose_file
, "State %d contains ", i
);
78 if (SRconflicts
[i
] > 0)
79 fprintf(verbose_file
, "%d shift/reduce conflict%s",
81 PLURAL(SRconflicts
[i
]));
82 if (SRconflicts
[i
] && RRconflicts
[i
])
83 fprintf(verbose_file
, ", ");
84 if (RRconflicts
[i
] > 0)
85 fprintf(verbose_file
, "%d reduce/reduce conflict%s",
87 PLURAL(RRconflicts
[i
]));
88 fprintf(verbose_file
, ".\n");
94 print_state(int state
)
97 fprintf(verbose_file
, "\n\n");
98 if (SRconflicts
[state
] || RRconflicts
[state
])
99 print_conflicts(state
);
100 fprintf(verbose_file
, "state %d\n", state
);
103 print_actions(state
);
107 print_conflicts(int state
)
109 int symbol
, act
, number
;
112 act
= 0; /* not shift/reduce... */
115 for (p
= parser
[state
]; p
; p
= p
->next
)
117 if (p
->suppressed
== 2)
120 if (p
->symbol
!= symbol
)
124 if (p
->action_code
== SHIFT
)
129 else if (p
->suppressed
== 1)
131 if (state
== final_state
&& symbol
== 0)
133 fprintf(verbose_file
, "%d: shift/reduce conflict \
134 (accept, reduce %d) on $end\n", state
, p
->number
- 2);
140 fprintf(verbose_file
, "%d: shift/reduce conflict \
141 (shift %d, reduce %d) on %s\n", state
, number
, p
->number
- 2,
142 symbol_name
[symbol
]);
146 fprintf(verbose_file
, "%d: reduce/reduce conflict \
147 (reduce %d, reduce %d) on %s\n", state
, number
- 2, p
->number
- 2,
148 symbol_name
[symbol
]);
156 print_core(int state
)
165 statep
= state_table
[state
];
168 for (i
= 0; i
< k
; i
++)
170 sp1
= sp
= ritem
+ statep
->items
[i
];
175 fprintf(verbose_file
, "\t%s : ", symbol_name
[rlhs
[rule
]]);
177 for (sp
= ritem
+ rrhs
[rule
]; sp
< sp1
; sp
++)
178 fprintf(verbose_file
, "%s ", symbol_name
[*sp
]);
180 putc('.', verbose_file
);
184 fprintf(verbose_file
, " %s", symbol_name
[*sp
]);
187 fprintf(verbose_file
, " (%d)\n", -2 - *sp
);
192 print_nulls(int state
)
195 Value_t i
, j
, k
, nnulls
;
198 for (p
= parser
[state
]; p
; p
= p
->next
)
200 if (p
->action_code
== REDUCE
&&
201 (p
->suppressed
== 0 || p
->suppressed
== 1))
204 if (rrhs
[i
] + 1 == rrhs
[i
+ 1])
206 for (j
= 0; j
< nnulls
&& i
> null_rules
[j
]; ++j
)
214 else if (i
!= null_rules
[j
])
217 for (k
= (Value_t
) (nnulls
- 1); k
> j
; --k
)
218 null_rules
[k
] = null_rules
[k
- 1];
225 for (i
= 0; i
< nnulls
; ++i
)
228 fprintf(verbose_file
, "\t%s : . (%d)\n", symbol_name
[rlhs
[j
]],
231 fprintf(verbose_file
, "\n");
235 print_actions(int stateno
)
241 if (stateno
== final_state
)
242 fprintf(verbose_file
, "\t$end accept\n");
248 print_reductions(p
, defred
[stateno
]);
251 sp
= shift_table
[stateno
];
252 if (sp
&& sp
->nshifts
> 0)
254 as
= accessing_symbol
[sp
->shift
[sp
->nshifts
- 1]];
256 print_gotos(stateno
);
261 print_shifts(action
*p
)
267 for (q
= p
; q
; q
= q
->next
)
269 if (q
->suppressed
< 2 && q
->action_code
== SHIFT
)
275 for (; p
; p
= p
->next
)
277 if (p
->action_code
== SHIFT
&& p
->suppressed
== 0)
278 fprintf(verbose_file
, "\t%s shift %d\n",
279 symbol_name
[p
->symbol
], p
->number
);
285 print_reductions(action
*p
, int defred2
)
291 for (q
= p
; q
; q
= q
->next
)
293 if (q
->action_code
== REDUCE
&& q
->suppressed
< 2)
301 fprintf(verbose_file
, "\t. error\n");
304 for (; p
; p
= p
->next
)
306 if (p
->action_code
== REDUCE
&& p
->number
!= defred2
)
309 if (p
->suppressed
== 0)
310 fprintf(verbose_file
, "\t%s reduce %d\n",
311 symbol_name
[p
->symbol
], k
);
316 fprintf(verbose_file
, "\t. reduce %d\n", defred2
- 2);
321 print_gotos(int stateno
)
328 putc('\n', verbose_file
);
329 sp
= shift_table
[stateno
];
330 to_state2
= sp
->shift
;
331 for (i
= 0; i
< sp
->nshifts
; ++i
)
334 as
= accessing_symbol
[k
];
336 fprintf(verbose_file
, "\t%s goto %d\n", symbol_name
[as
], k
);