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