]> git.saurik.com Git - bison.git/blame - src/print.c
* src/lalr.c (set_goto_map, initialize_F): Use SHIFT_SYMBOL.
[bison.git] / src / print.c
CommitLineData
e06f0c34 1/* Print information on generated parser, for bison,
09b503c8 2 Copyright 1984, 1986, 1989, 2000, 2001 Free Software Foundation, Inc.
e06f0c34 3
c29240e7 4 This file is part of Bison, the GNU Compiler Compiler.
e06f0c34 5
c29240e7
AD
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.
e06f0c34 10
c29240e7
AD
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.
e06f0c34 15
c29240e7
AD
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. */
e06f0c34
RS
20
21
e06f0c34 22#include "system.h"
e06f0c34
RS
23#include "files.h"
24#include "gram.h"
b2ca4022 25#include "LR0.h"
720d742f 26#include "lalr.h"
0619caf0 27#include "conflicts.h"
07a58c13
AD
28#include "getargs.h"
29#include "state.h"
b2ca4022 30#include "reader.h"
d7913476 31#include "print.h"
09b503c8 32#include "reduce.h"
43168960 33#include "closure.h"
e06f0c34 34
5092aba5
AD
35static unsigned *shiftset = NULL;
36static unsigned *lookaheadset = NULL;
37
07a58c13 38#if 0
4a120d45 39static void
d2729d44 40print_token (int extnum, int token)
e06f0c34 41{
342b8b6e 42 fprintf (out, _(" type %d is %s\n"), extnum, tags[token]);
e06f0c34 43}
4a120d45 44#endif
e06f0c34 45
07a58c13 46\f
342b8b6e 47/*--------------------------------.
07a58c13 48| Report information on a state. |
342b8b6e 49`--------------------------------*/
e06f0c34 50
4a120d45 51static void
065fbd27 52print_core (FILE *out, state_t *state)
e06f0c34 53{
c29240e7 54 int i;
065fbd27
AD
55 short *sitems = state->items;
56 int snitems = state->nitems;
e06f0c34 57
43168960
AD
58 /* New experimental feature: if TRACE_FLAGS output all the items of
59 a state, not only its kernel. */
60 if (trace_flag)
61 {
62 closure (sitems, snitems);
63 sitems = itemset;
64 snitems = nitemset;
65 }
e06f0c34 66
43168960 67 if (snitems)
e06f0c34 68 {
43168960
AD
69 for (i = 0; i < snitems; i++)
70 {
71 short *sp;
72 short *sp1;
73 int rule;
4bc30f78 74
43168960 75 sp1 = sp = ritem + sitems[i];
e06f0c34 76
43168960
AD
77 while (*sp > 0)
78 sp++;
e06f0c34 79
43168960
AD
80 rule = -(*sp);
81 fprintf (out, " %s -> ", tags[rule_table[rule].lhs]);
e06f0c34 82
43168960
AD
83 for (sp = ritem + rule_table[rule].rhs; sp < sp1; sp++)
84 fprintf (out, "%s ", tags[*sp]);
e06f0c34 85
43168960 86 fputc ('.', out);
e06f0c34 87
43168960
AD
88 for (/* Nothing */; *sp > 0; ++sp)
89 fprintf (out, " %s", tags[*sp]);
90
91 fprintf (out, _(" (rule %d)"), rule);
92 fputc ('\n', out);
93 }
e06f0c34 94
342b8b6e 95 fputc ('\n', out);
e06f0c34 96 }
e06f0c34
RS
97}
98
5092aba5 99
4a120d45 100static void
5092aba5 101print_shifts (FILE *out, state_t *state)
e06f0c34 102{
c29240e7 103 int i;
5092aba5 104 shifts *shiftp = state->shifts;
e06f0c34 105
2e729273 106 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
d954473d
AD
107 if (!SHIFT_IS_DISABLED (shiftp, i))
108 {
109 int state1 = shiftp->shifts[i];
f693ad14 110 int symbol = state_table[state1]->accessing_symbol;
2e729273
AD
111 fprintf (out,
112 _(" %-4s\tshift, and go to state %d\n"),
113 tags[symbol], state1);
d954473d 114 }
e06f0c34 115
d954473d
AD
116 if (i > 0)
117 fputc ('\n', out);
5092aba5 118}
e06f0c34 119
e06f0c34 120
5092aba5
AD
121static void
122print_errs (FILE *out, state_t *state)
123{
124 errs *errp = state->errs;
125 int i;
126
127 if (!errp)
128 return;
129
130 for (i = 0; i < errp->nerrs; ++i)
131 if (errp->errs[i])
132 fprintf (out, _(" %-4s\terror (nonassociative)\n"),
133 tags[errp->errs[i]]);
134
135 if (i > 0)
342b8b6e 136 fputc ('\n', out);
5092aba5 137}
e06f0c34 138
5092aba5
AD
139
140static void
141print_gotos (FILE *out, state_t *state)
142{
143 int i;
144 shifts *shiftp = state->shifts;
145
146 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
147 /* Skip token shifts. */;
e06f0c34 148
d954473d 149 if (i < shiftp->nshifts)
e06f0c34 150 {
d954473d
AD
151 for (; i < shiftp->nshifts; i++)
152 if (!SHIFT_IS_DISABLED (shiftp, i))
153 {
154 int state1 = shiftp->shifts[i];
f693ad14 155 int symbol = state_table[state1]->accessing_symbol;
d954473d
AD
156 fprintf (out, _(" %-4s\tgo to state %d\n"),
157 tags[symbol], state1);
158 }
e06f0c34 159
342b8b6e 160 fputc ('\n', out);
e06f0c34
RS
161 }
162}
163
5092aba5
AD
164static void
165print_reductions (FILE *out, state_t *state)
166{
167 int i;
168 shifts *shiftp = state->shifts;
169 reductions *redp = state->reductions;
170 errs *errp = state->errs;
171 int nodefault = 0;
172
173 if (state->consistent)
174 {
175 int rule = redp->rules[0];
176 int symbol = rule_table[rule].lhs;
177 fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"),
178 rule, tags[symbol]);
179 return;
180 }
181
182 for (i = 0; i < tokensetsize; i++)
183 shiftset[i] = 0;
184
185 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
186 if (!SHIFT_IS_DISABLED (shiftp, i))
187 {
188 /* if this state has a shift for the error token, don't use a
189 default rule. */
190 if (SHIFT_IS_ERROR (shiftp, i))
191 nodefault = 1;
192 SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i));
193 }
194
195 if (errp)
196 for (i = 0; i < errp->nerrs; i++)
197 if (errp->errs[i])
198 SETBIT (shiftset, errp->errs[i]);
199
200 if (state->nlookaheads == 1 && !nodefault)
201 {
202 int k;
203 int default_rule = LAruleno[state->lookaheadsp];
204
205 for (k = 0; k < tokensetsize; ++k)
206 lookaheadset[k] = LA (state->lookaheadsp)[k] & shiftset[k];
207
208 for (i = 0; i < ntokens; i++)
209 if (BITISSET (lookaheadset, i))
210 fprintf (out, _(" %-4s\t[reduce using rule %d (%s)]\n"),
211 tags[i], default_rule,
212 tags[rule_table[default_rule].lhs]);
213
214 fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"),
215 default_rule, tags[rule_table[default_rule].lhs]);
216 }
217 else if (state->nlookaheads >= 1)
218 {
219 int cmax = 0;
220 int default_LA = -1;
221 int default_rule = 0;
222
223 if (!nodefault)
224 for (i = 0; i < state->nlookaheads; ++i)
225 {
226 int count = 0;
227 int j, k;
228
229 for (k = 0; k < tokensetsize; ++k)
230 lookaheadset[k] = LA (state->lookaheadsp + i)[k] & ~shiftset[k];
231
232 for (j = 0; j < ntokens; j++)
233 if (BITISSET (lookaheadset, j))
234 count++;
235
236 if (count > cmax)
237 {
238 cmax = count;
239 default_LA = state->lookaheadsp + i;
240 default_rule = LAruleno[state->lookaheadsp + i];
241 }
242
243 for (k = 0; k < tokensetsize; ++k)
244 shiftset[k] |= lookaheadset[k];
245 }
246
247 for (i = 0; i < tokensetsize; i++)
248 shiftset[i] = 0;
249
250 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
251 if (!SHIFT_IS_DISABLED (shiftp, i))
252 SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i));
253
254 for (i = 0; i < ntokens; i++)
255 {
256 int j;
257 int defaulted = 0;
258 int count = BITISSET (shiftset, i);
259
260 for (j = 0; j < state->nlookaheads; ++j)
261 {
262 if (BITISSET (LA (state->lookaheadsp + j), i))
263 {
264 if (count == 0)
265 {
266 if (state->lookaheadsp + j != default_LA)
267 fprintf (out,
268 _(" %-4s\treduce using rule %d (%s)\n"),
269 tags[i],
270 LAruleno[state->lookaheadsp + j],
271 tags[rule_table[LAruleno[state->lookaheadsp + j]].lhs]);
272 else
273 defaulted = 1;
274
275 count++;
276 }
277 else
278 {
279 if (defaulted)
280 fprintf (out,
281 _(" %-4s\treduce using rule %d (%s)\n"),
282 tags[i],
283 LAruleno[default_LA],
284 tags[rule_table[LAruleno[default_LA]].lhs]);
285 defaulted = 0;
286 fprintf (out,
287 _(" %-4s\t[reduce using rule %d (%s)]\n"),
288 tags[i],
289 LAruleno[state->lookaheadsp + j],
290 tags[rule_table[LAruleno[state->lookaheadsp + j]].lhs]);
291 }
292 }
293 }
294 }
295
296 if (default_LA >= 0)
297 fprintf (out, _(" $default\treduce using rule %d (%s)\n"),
298 default_rule, tags[rule_table[default_rule].lhs]);
299 }
300}
301
302
303static void
304print_actions (FILE *out, state_t *state)
305{
306 reductions *redp = state->reductions;
307 shifts *shiftp = state->shifts;
308
309 if (!shiftp->nshifts && !redp)
310 {
311 if (final_state == state->number)
312 fprintf (out, _(" $default\taccept\n"));
313 else
314 fprintf (out, _(" NO ACTIONS\n"));
315 return;
316 }
317
318 print_shifts (out, state);
319 print_errs (out, state);
320 if (redp)
321 print_reductions (out, state);
322 print_gotos (out, state);
323}
324
07a58c13 325static void
065fbd27 326print_state (FILE *out, state_t *state)
07a58c13 327{
065fbd27 328 fprintf (out, _("state %d"), state->number);
342b8b6e
AD
329 fputs ("\n\n", out);
330 print_core (out, state);
331 print_actions (out, state);
d2d1b42b 332 fputs ("\n\n", out);
07a58c13
AD
333}
334\f
335/*-----------------------------------------.
336| Print information on the whole grammar. |
337`-----------------------------------------*/
338
342b8b6e
AD
339#define END_TEST(End) \
340do { \
341 if (column + strlen(buffer) > (End)) \
342 { \
343 fprintf (out, "%s\n ", buffer); \
344 column = 3; \
345 buffer[0] = 0; \
346 } \
ff4423cc 347} while (0)
e06f0c34 348
07a58c13 349
4a120d45 350static void
342b8b6e 351print_grammar (FILE *out)
e06f0c34
RS
352{
353 int i, j;
c29240e7 354 short *rule;
e06f0c34
RS
355 char buffer[90];
356 int column = 0;
357
358 /* rule # : LHS -> RHS */
d2d1b42b 359 fprintf (out, "%s\n\n", _("Grammar"));
b29b2ed5 360 fprintf (out, " %s\n", _("Number, Line, Rule"));
e06f0c34
RS
361 for (i = 1; i <= nrules; i++)
362 /* Don't print rules disabled in reduce_grammar_tables. */
68f1e3ed 363 if (rule_table[i].useful)
e06f0c34 364 {
b29b2ed5
AD
365 fprintf (out, _(" %3d %3d %s ->"),
366 i, rule_table[i].line, tags[rule_table[i].lhs]);
b2ed6e58 367 rule = &ritem[rule_table[i].rhs];
e06f0c34
RS
368 if (*rule > 0)
369 while (*rule > 0)
342b8b6e 370 fprintf (out, " %s", tags[*rule++]);
e06f0c34 371 else
b29b2ed5 372 fprintf (out, " /* %s */", _("empty"));
0df87bb6 373 fputc ('\n', out);
e06f0c34 374 }
d2d1b42b
AD
375 fputs ("\n\n", out);
376
e06f0c34
RS
377
378 /* TERMINAL (type #) : rule #s terminal is on RHS */
d2d1b42b 379 fprintf (out, "%s\n\n", _("Terminals, with rules where they appear"));
342b8b6e 380 fprintf (out, "%s (-1)\n", tags[0]);
e06f0c34 381
342b8b6e
AD
382 for (i = 0; i <= max_user_token_number; i++)
383 if (token_translations[i] != 2)
384 {
385 buffer[0] = 0;
386 column = strlen (tags[token_translations[i]]);
387 fputs (tags[token_translations[i]], out);
388 END_TEST (50);
389 sprintf (buffer, " (%d)", i);
e06f0c34 390
342b8b6e 391 for (j = 1; j <= nrules; j++)
b2ed6e58 392 for (rule = &ritem[rule_table[j].rhs]; *rule > 0; rule++)
342b8b6e
AD
393 if (*rule == token_translations[i])
394 {
395 END_TEST (65);
396 sprintf (buffer + strlen (buffer), " %d", j);
397 break;
398 }
399 fprintf (out, "%s\n", buffer);
400 }
d2d1b42b
AD
401 fputs ("\n\n", out);
402
342b8b6e 403
d2d1b42b 404 fprintf (out, "%s\n\n", _("Nonterminals, with rules where they appear"));
e06f0c34
RS
405 for (i = ntokens; i <= nsyms - 1; i++)
406 {
407 int left_count = 0, right_count = 0;
408
409 for (j = 1; j <= nrules; j++)
410 {
b2ed6e58 411 if (rule_table[j].lhs == i)
e06f0c34 412 left_count++;
b2ed6e58 413 for (rule = &ritem[rule_table[j].rhs]; *rule > 0; rule++)
e06f0c34
RS
414 if (*rule == i)
415 {
416 right_count++;
417 break;
418 }
419 }
420
421 buffer[0] = 0;
342b8b6e 422 fputs (tags[i], out);
e06f0c34
RS
423 column = strlen (tags[i]);
424 sprintf (buffer, " (%d)", i);
425 END_TEST (0);
426
427 if (left_count > 0)
428 {
429 END_TEST (50);
c29240e7 430 sprintf (buffer + strlen (buffer), _(" on left:"));
e06f0c34
RS
431
432 for (j = 1; j <= nrules; j++)
433 {
434 END_TEST (65);
b2ed6e58 435 if (rule_table[j].lhs == i)
c29240e7 436 sprintf (buffer + strlen (buffer), " %d", j);
e06f0c34
RS
437 }
438 }
439
440 if (right_count > 0)
441 {
442 if (left_count > 0)
c29240e7 443 sprintf (buffer + strlen (buffer), ",");
e06f0c34 444 END_TEST (50);
c29240e7 445 sprintf (buffer + strlen (buffer), _(" on right:"));
e06f0c34
RS
446 for (j = 1; j <= nrules; j++)
447 {
b2ed6e58 448 for (rule = &ritem[rule_table[j].rhs]; *rule > 0; rule++)
e06f0c34
RS
449 if (*rule == i)
450 {
451 END_TEST (65);
c29240e7 452 sprintf (buffer + strlen (buffer), " %d", j);
e06f0c34
RS
453 break;
454 }
455 }
456 }
342b8b6e 457 fprintf (out, "%s\n", buffer);
e06f0c34 458 }
d2d1b42b 459 fputs ("\n\n", out);
e06f0c34 460}
07a58c13
AD
461\f
462void
463print_results (void)
464{
64d15509 465 int i;
07a58c13 466
64d15509
AD
467 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
468 that conflicts with Posix. */
469 FILE *out = xfopen (spec_verbose_file, "w");
07a58c13 470
64d15509
AD
471 size_t size = obstack_object_size (&output_obstack);
472 fwrite (obstack_finish (&output_obstack), 1, size, out);
473 obstack_free (&output_obstack, NULL);
07a58c13 474
64d15509
AD
475 if (size)
476 fputs ("\n\n", out);
342b8b6e 477
64d15509
AD
478 reduce_output (out);
479 conflicts_output (out);
342b8b6e 480
64d15509 481 print_grammar (out);
342b8b6e 482
5092aba5
AD
483 /* New experimental feature: output all the items of a state, not
484 only its kernel. Requires to run closure, which need memory
485 allocation/deallocation. */
64d15509
AD
486 if (trace_flag)
487 new_closure (nitems);
5092aba5
AD
488 /* Storage for print_reductions. */
489 shiftset = XCALLOC (unsigned, tokensetsize);
490 lookaheadset = XCALLOC (unsigned, tokensetsize);
64d15509 491 for (i = 0; i < nstates; i++)
065fbd27 492 print_state (out, state_table[i]);
5092aba5
AD
493 free (shiftset);
494 free (lookaheadset);
64d15509
AD
495 if (trace_flag)
496 free_closure ();
497
498 xfclose (out);
07a58c13 499}