]>
Commit | Line | Data |
---|---|---|
1 | /* Print information on generated parser, for bison, | |
2 | Copyright 1984, 1986, 1989, 2000 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of Bison, the GNU Compiler Compiler. | |
5 | ||
6 | Bison is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | Bison is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with Bison; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | ||
22 | #include "system.h" | |
23 | #include "xalloc.h" | |
24 | #include "files.h" | |
25 | #include "gram.h" | |
26 | #include "LR0.h" | |
27 | #include "lalr.h" | |
28 | #include "conflicts.h" | |
29 | #include "getargs.h" | |
30 | #include "state.h" | |
31 | #include "reader.h" | |
32 | #include "print.h" | |
33 | ||
34 | #if 0 | |
35 | static void | |
36 | print_token (int extnum, int token) | |
37 | { | |
38 | obstack_fgrow2 (&output_obstack, _(" type %d is %s\n"), extnum, tags[token]); | |
39 | } | |
40 | #endif | |
41 | ||
42 | \f | |
43 | /*================================\ | |
44 | | Report information on a state. | | |
45 | \================================*/ | |
46 | ||
47 | static void | |
48 | print_core (int state) | |
49 | { | |
50 | int i; | |
51 | int k; | |
52 | int rule; | |
53 | core *statep; | |
54 | short *sp; | |
55 | short *sp1; | |
56 | ||
57 | statep = state_table[state]; | |
58 | k = statep->nitems; | |
59 | ||
60 | if (k == 0) | |
61 | return; | |
62 | ||
63 | for (i = 0; i < k; i++) | |
64 | { | |
65 | sp1 = sp = ritem + statep->items[i]; | |
66 | ||
67 | while (*sp > 0) | |
68 | sp++; | |
69 | ||
70 | rule = -(*sp); | |
71 | obstack_fgrow1 (&output_obstack, " %s -> ", tags[rlhs[rule]]); | |
72 | ||
73 | for (sp = ritem + rrhs[rule]; sp < sp1; sp++) | |
74 | { | |
75 | obstack_fgrow1 (&output_obstack, "%s ", tags[*sp]); | |
76 | } | |
77 | ||
78 | obstack_1grow (&output_obstack, '.'); | |
79 | ||
80 | while (*sp > 0) | |
81 | { | |
82 | obstack_fgrow1 (&output_obstack, " %s", tags[*sp]); | |
83 | sp++; | |
84 | } | |
85 | ||
86 | obstack_fgrow1 (&output_obstack, _(" (rule %d)"), rule); | |
87 | obstack_1grow (&output_obstack, '\n'); | |
88 | } | |
89 | ||
90 | obstack_1grow (&output_obstack, '\n'); | |
91 | } | |
92 | ||
93 | static void | |
94 | print_actions (int state) | |
95 | { | |
96 | int i; | |
97 | int k; | |
98 | int state1; | |
99 | int symbol; | |
100 | shifts *shiftp; | |
101 | errs *errp; | |
102 | reductions *redp; | |
103 | int rule; | |
104 | ||
105 | shiftp = shift_table[state]; | |
106 | redp = reduction_table[state]; | |
107 | errp = err_table[state]; | |
108 | ||
109 | if (!shiftp && !redp) | |
110 | { | |
111 | if (final_state == state) | |
112 | obstack_sgrow (&output_obstack, _(" $default\taccept\n")); | |
113 | else | |
114 | obstack_sgrow (&output_obstack, _(" NO ACTIONS\n")); | |
115 | return; | |
116 | } | |
117 | ||
118 | if (shiftp) | |
119 | { | |
120 | k = shiftp->nshifts; | |
121 | ||
122 | for (i = 0; i < k; i++) | |
123 | { | |
124 | if (!shiftp->shifts[i]) | |
125 | continue; | |
126 | state1 = shiftp->shifts[i]; | |
127 | symbol = accessing_symbol[state1]; | |
128 | /* The following line used to be turned off. */ | |
129 | if (ISVAR (symbol)) | |
130 | break; | |
131 | if (symbol == 0) /* I.e. strcmp(tags[symbol],"$")==0 */ | |
132 | obstack_fgrow1 (&output_obstack, | |
133 | _(" $ \tgo to state %d\n"), state1); | |
134 | else | |
135 | obstack_fgrow2 (&output_obstack, | |
136 | _(" %-4s\tshift, and go to state %d\n"), | |
137 | tags[symbol], state1); | |
138 | } | |
139 | ||
140 | if (i > 0) | |
141 | obstack_1grow (&output_obstack, '\n'); | |
142 | } | |
143 | else | |
144 | { | |
145 | i = 0; | |
146 | k = 0; | |
147 | } | |
148 | ||
149 | if (errp) | |
150 | { | |
151 | int j, nerrs; | |
152 | ||
153 | nerrs = errp->nerrs; | |
154 | ||
155 | for (j = 0; j < nerrs; j++) | |
156 | { | |
157 | if (!errp->errs[j]) | |
158 | continue; | |
159 | symbol = errp->errs[j]; | |
160 | obstack_fgrow1 (&output_obstack, _(" %-4s\terror (nonassociative)\n"), | |
161 | tags[symbol]); | |
162 | } | |
163 | ||
164 | if (j > 0) | |
165 | obstack_1grow (&output_obstack, '\n'); | |
166 | } | |
167 | ||
168 | if (consistent[state] && redp) | |
169 | { | |
170 | rule = redp->rules[0]; | |
171 | symbol = rlhs[rule]; | |
172 | obstack_fgrow2 (&output_obstack, | |
173 | _(" $default\treduce using rule %d (%s)\n\n"), | |
174 | rule, tags[symbol]); | |
175 | } | |
176 | else if (redp) | |
177 | { | |
178 | print_reductions (state); | |
179 | } | |
180 | ||
181 | if (i < k) | |
182 | { | |
183 | for (; i < k; i++) | |
184 | { | |
185 | if (!shiftp->shifts[i]) | |
186 | continue; | |
187 | state1 = shiftp->shifts[i]; | |
188 | symbol = accessing_symbol[state1]; | |
189 | obstack_fgrow2 (&output_obstack, | |
190 | _(" %-4s\tgo to state %d\n"), | |
191 | tags[symbol], state1); | |
192 | } | |
193 | ||
194 | obstack_1grow (&output_obstack, '\n'); | |
195 | } | |
196 | } | |
197 | ||
198 | static void | |
199 | print_state (int state) | |
200 | { | |
201 | obstack_sgrow (&output_obstack, "\n\n"); | |
202 | obstack_fgrow1 (&output_obstack, _("state %d"), state); | |
203 | obstack_sgrow (&output_obstack, "\n\n"); | |
204 | print_core (state); | |
205 | print_actions (state); | |
206 | } | |
207 | \f | |
208 | /*-----------------------------------------. | |
209 | | Print information on the whole grammar. | | |
210 | `-----------------------------------------*/ | |
211 | ||
212 | #define END_TEST(End) \ | |
213 | do { \ | |
214 | if (column + strlen(buffer) > (End)) \ | |
215 | { \ | |
216 | obstack_fgrow1 (&output_obstack, "%s\n ", buffer); \ | |
217 | column = 3; \ | |
218 | buffer[0] = 0; \ | |
219 | } \ | |
220 | } while (0) | |
221 | ||
222 | ||
223 | static void | |
224 | print_grammar (void) | |
225 | { | |
226 | int i, j; | |
227 | short *rule; | |
228 | char buffer[90]; | |
229 | int column = 0; | |
230 | ||
231 | /* rule # : LHS -> RHS */ | |
232 | obstack_1grow (&output_obstack, '\n'); | |
233 | obstack_sgrow (&output_obstack, _("Grammar")); | |
234 | obstack_1grow (&output_obstack, '\n'); | |
235 | for (i = 1; i <= nrules; i++) | |
236 | /* Don't print rules disabled in reduce_grammar_tables. */ | |
237 | if (rlhs[i] >= 0) | |
238 | { | |
239 | obstack_fgrow2 (&output_obstack, | |
240 | _("rule %-4d %s ->"), i, tags[rlhs[i]]); | |
241 | rule = &ritem[rrhs[i]]; | |
242 | if (*rule > 0) | |
243 | while (*rule > 0) | |
244 | obstack_fgrow1 (&output_obstack, " %s", tags[*rule++]); | |
245 | else | |
246 | obstack_sgrow (&output_obstack, _(" /* empty */")); | |
247 | obstack_1grow (&output_obstack, '\n'); | |
248 | } | |
249 | ||
250 | /* TERMINAL (type #) : rule #s terminal is on RHS */ | |
251 | obstack_sgrow (&output_obstack, "\n"); | |
252 | obstack_sgrow (&output_obstack, | |
253 | _("Terminals, with rules where they appear")); | |
254 | obstack_sgrow (&output_obstack, "\n\n"); | |
255 | obstack_fgrow1 (&output_obstack, "%s (-1)\n", tags[0]); | |
256 | if (translations) | |
257 | { | |
258 | for (i = 0; i <= max_user_token_number; i++) | |
259 | if (token_translations[i] != 2) | |
260 | { | |
261 | buffer[0] = 0; | |
262 | column = strlen (tags[token_translations[i]]); | |
263 | obstack_sgrow (&output_obstack, tags[token_translations[i]]); | |
264 | END_TEST (50); | |
265 | sprintf (buffer, " (%d)", i); | |
266 | ||
267 | for (j = 1; j <= nrules; j++) | |
268 | for (rule = &ritem[rrhs[j]]; *rule > 0; rule++) | |
269 | if (*rule == token_translations[i]) | |
270 | { | |
271 | END_TEST (65); | |
272 | sprintf (buffer + strlen (buffer), " %d", j); | |
273 | break; | |
274 | } | |
275 | obstack_fgrow1 (&output_obstack, "%s\n", buffer); | |
276 | } | |
277 | } | |
278 | else | |
279 | { | |
280 | for (i = 1; i < ntokens; i++) | |
281 | { | |
282 | buffer[0] = 0; | |
283 | column = strlen (tags[i]); | |
284 | obstack_sgrow (&output_obstack, tags[i]); | |
285 | END_TEST (50); | |
286 | sprintf (buffer, " (%d)", i); | |
287 | ||
288 | for (j = 1; j <= nrules; j++) | |
289 | for (rule = &ritem[rrhs[j]]; *rule > 0; rule++) | |
290 | if (*rule == i) | |
291 | { | |
292 | END_TEST (65); | |
293 | sprintf (buffer + strlen (buffer), " %d", j); | |
294 | break; | |
295 | } | |
296 | obstack_fgrow1 (&output_obstack, "%s\n", buffer); | |
297 | } | |
298 | } | |
299 | ||
300 | obstack_sgrow (&output_obstack, "\n"); | |
301 | obstack_sgrow (&output_obstack, | |
302 | _("Nonterminals, with rules where they appear")); | |
303 | obstack_sgrow (&output_obstack, "\n\n"); | |
304 | for (i = ntokens; i <= nsyms - 1; i++) | |
305 | { | |
306 | int left_count = 0, right_count = 0; | |
307 | ||
308 | for (j = 1; j <= nrules; j++) | |
309 | { | |
310 | if (rlhs[j] == i) | |
311 | left_count++; | |
312 | for (rule = &ritem[rrhs[j]]; *rule > 0; rule++) | |
313 | if (*rule == i) | |
314 | { | |
315 | right_count++; | |
316 | break; | |
317 | } | |
318 | } | |
319 | ||
320 | buffer[0] = 0; | |
321 | obstack_sgrow (&output_obstack, tags[i]); | |
322 | column = strlen (tags[i]); | |
323 | sprintf (buffer, " (%d)", i); | |
324 | END_TEST (0); | |
325 | ||
326 | if (left_count > 0) | |
327 | { | |
328 | END_TEST (50); | |
329 | sprintf (buffer + strlen (buffer), _(" on left:")); | |
330 | ||
331 | for (j = 1; j <= nrules; j++) | |
332 | { | |
333 | END_TEST (65); | |
334 | if (rlhs[j] == i) | |
335 | sprintf (buffer + strlen (buffer), " %d", j); | |
336 | } | |
337 | } | |
338 | ||
339 | if (right_count > 0) | |
340 | { | |
341 | if (left_count > 0) | |
342 | sprintf (buffer + strlen (buffer), ","); | |
343 | END_TEST (50); | |
344 | sprintf (buffer + strlen (buffer), _(" on right:")); | |
345 | for (j = 1; j <= nrules; j++) | |
346 | { | |
347 | for (rule = &ritem[rrhs[j]]; *rule > 0; rule++) | |
348 | if (*rule == i) | |
349 | { | |
350 | END_TEST (65); | |
351 | sprintf (buffer + strlen (buffer), " %d", j); | |
352 | break; | |
353 | } | |
354 | } | |
355 | } | |
356 | obstack_fgrow1 (&output_obstack, "%s\n", buffer); | |
357 | } | |
358 | } | |
359 | \f | |
360 | void | |
361 | print_results (void) | |
362 | { | |
363 | int i; | |
364 | ||
365 | if (any_conflicts) | |
366 | print_conflicts (); | |
367 | ||
368 | if (verbose_flag) | |
369 | print_grammar (); | |
370 | ||
371 | if (verbose_flag) | |
372 | for (i = 0; i < nstates; i++) | |
373 | print_state (i); | |
374 | } |