]> git.saurik.com Git - bison.git/blame - src/print.c
* src/gram.h, src/gram.c (rules_rhs_length): New.
[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
99013900 99 for (sp = 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
f9abaa2c 218 bitset_and (lookaheadset, LA[state->lookaheadsp], shiftset);
5092aba5
AD
219
220 for (i = 0; i < ntokens; i++)
34ba9743 221 if (bitset_test (lookaheadset, i))
5092aba5 222 fprintf (out, _(" %-4s\t[reduce using rule %d (%s)]\n"),
ad949da9 223 escape (symbols[i]->tag), default_rule - 1,
1a2b5d37 224 escape2 (symbols[rules[default_rule].lhs]->tag));
5092aba5
AD
225
226 fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"),
1a2b5d37 227 default_rule - 1, escape (symbols[rules[default_rule].lhs]->tag));
5092aba5
AD
228 }
229 else if (state->nlookaheads >= 1)
230 {
231 int cmax = 0;
232 int default_LA = -1;
233 int default_rule = 0;
234
235 if (!nodefault)
236 for (i = 0; i < state->nlookaheads; ++i)
237 {
238 int count = 0;
f9abaa2c 239 int j;
5092aba5 240
f9abaa2c 241 bitset_andn (lookaheadset, LA[state->lookaheadsp + i], shiftset);
5092aba5
AD
242
243 for (j = 0; j < ntokens; j++)
34ba9743 244 if (bitset_test (lookaheadset, j))
5092aba5
AD
245 count++;
246
247 if (count > cmax)
248 {
249 cmax = count;
250 default_LA = state->lookaheadsp + i;
251 default_rule = LAruleno[state->lookaheadsp + i];
252 }
253
34ba9743 254 bitset_or (shiftset, shiftset, lookaheadset);
5092aba5
AD
255 }
256
34ba9743 257 bitset_zero (shiftset);
5092aba5
AD
258
259 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
260 if (!SHIFT_IS_DISABLED (shiftp, i))
34ba9743 261 bitset_set (shiftset, SHIFT_SYMBOL (shiftp, i));
5092aba5
AD
262
263 for (i = 0; i < ntokens; i++)
264 {
265 int j;
266 int defaulted = 0;
34ba9743 267 int count = bitset_test (shiftset, i);
5092aba5
AD
268
269 for (j = 0; j < state->nlookaheads; ++j)
f9abaa2c
AD
270 if (bitset_test (LA[state->lookaheadsp + j], i))
271 {
272 if (count == 0)
273 {
274 if (state->lookaheadsp + j != default_LA)
5092aba5 275 fprintf (out,
f9abaa2c 276 _(" %-4s\treduce using rule %d (%s)\n"),
ad949da9 277 escape (symbols[i]->tag),
30171f79 278 LAruleno[state->lookaheadsp + j] - 1,
1a2b5d37 279 escape2 (symbols[rules[LAruleno[state->lookaheadsp + j]].lhs]->tag));
f9abaa2c
AD
280 else
281 defaulted = 1;
282
283 count++;
284 }
285 else
286 {
287 if (defaulted)
288 fprintf (out,
289 _(" %-4s\treduce using rule %d (%s)\n"),
290 escape (symbols[i]->tag),
291 LAruleno[default_LA] - 1,
292 escape2 (symbols[rules[LAruleno[default_LA]].lhs]->tag));
293 defaulted = 0;
294 fprintf (out,
295 _(" %-4s\t[reduce using rule %d (%s)]\n"),
296 escape (symbols[i]->tag),
297 LAruleno[state->lookaheadsp + j] - 1,
298 escape2 (symbols[rules[LAruleno[state->lookaheadsp + j]].lhs]->tag));
299 }
300 }
5092aba5
AD
301 }
302
303 if (default_LA >= 0)
304 fprintf (out, _(" $default\treduce using rule %d (%s)\n"),
30171f79 305 default_rule - 1,
1a2b5d37 306 escape (symbols[rules[default_rule].lhs]->tag));
5092aba5
AD
307 }
308}
309
310
311static void
312print_actions (FILE *out, state_t *state)
313{
314 reductions *redp = state->reductions;
315 shifts *shiftp = state->shifts;
316
80dac38c 317 if (shiftp->nshifts == 0 && redp->nreds == 0)
5092aba5
AD
318 {
319 if (final_state == state->number)
30171f79 320 fprintf (out, _(" $default\taccept\n"));
5092aba5 321 else
30171f79 322 fprintf (out, _(" NO ACTIONS\n"));
5092aba5
AD
323 return;
324 }
325
326 print_shifts (out, state);
327 print_errs (out, state);
80dac38c 328 print_reductions (out, state);
5092aba5
AD
329 print_gotos (out, state);
330}
331
07a58c13 332static void
065fbd27 333print_state (FILE *out, state_t *state)
07a58c13 334{
065fbd27 335 fprintf (out, _("state %d"), state->number);
342b8b6e
AD
336 fputs ("\n\n", out);
337 print_core (out, state);
338 print_actions (out, state);
d2d1b42b 339 fputs ("\n\n", out);
07a58c13
AD
340}
341\f
342/*-----------------------------------------.
343| Print information on the whole grammar. |
344`-----------------------------------------*/
345
342b8b6e
AD
346#define END_TEST(End) \
347do { \
348 if (column + strlen(buffer) > (End)) \
349 { \
350 fprintf (out, "%s\n ", buffer); \
351 column = 3; \
352 buffer[0] = 0; \
353 } \
ff4423cc 354} while (0)
e06f0c34 355
07a58c13 356
4a120d45 357static void
342b8b6e 358print_grammar (FILE *out)
e06f0c34
RS
359{
360 int i, j;
c29240e7 361 short *rule;
e06f0c34
RS
362 char buffer[90];
363 int column = 0;
364
365 /* rule # : LHS -> RHS */
d2d1b42b 366 fprintf (out, "%s\n\n", _("Grammar"));
b29b2ed5 367 fprintf (out, " %s\n", _("Number, Line, Rule"));
18bcecb0 368 for (i = 1; i < nrules + 1; i++)
c3b407f4
AD
369 {
370 fprintf (out, _(" %3d %3d %s ->"),
371 i - 1, rules[i].line, escape (symbols[rules[i].lhs]->tag));
372 rule = rules[i].rhs;
373 if (*rule >= 0)
374 while (*rule >= 0)
375 fprintf (out, " %s", escape (symbols[*rule++]->tag));
376 else
377 fprintf (out, " /* %s */", _("empty"));
378 fputc ('\n', out);
379 }
d2d1b42b
AD
380 fputs ("\n\n", out);
381
e06f0c34
RS
382
383 /* TERMINAL (type #) : rule #s terminal is on RHS */
d2d1b42b 384 fprintf (out, "%s\n\n", _("Terminals, with rules where they appear"));
18bcecb0 385 for (i = 0; i < max_user_token_number + 1; i++)
342b8b6e
AD
386 if (token_translations[i] != 2)
387 {
388 buffer[0] = 0;
ad949da9
AD
389 column = strlen (escape (symbols[token_translations[i]]->tag));
390 fputs (escape (symbols[token_translations[i]]->tag), out);
342b8b6e
AD
391 END_TEST (50);
392 sprintf (buffer, " (%d)", i);
e06f0c34 393
18bcecb0 394 for (j = 1; j < nrules + 1; j++)
99013900 395 for (rule = rules[j].rhs; *rule >= 0; rule++)
342b8b6e
AD
396 if (*rule == token_translations[i])
397 {
398 END_TEST (65);
30171f79 399 sprintf (buffer + strlen (buffer), " %d", j - 1);
342b8b6e
AD
400 break;
401 }
402 fprintf (out, "%s\n", buffer);
403 }
d2d1b42b
AD
404 fputs ("\n\n", out);
405
342b8b6e 406
d2d1b42b 407 fprintf (out, "%s\n\n", _("Nonterminals, with rules where they appear"));
18bcecb0 408 for (i = ntokens; i < nsyms; i++)
e06f0c34
RS
409 {
410 int left_count = 0, right_count = 0;
411
18bcecb0 412 for (j = 1; j < nrules + 1; j++)
e06f0c34 413 {
1a2b5d37 414 if (rules[j].lhs == i)
e06f0c34 415 left_count++;
99013900 416 for (rule = rules[j].rhs; *rule >= 0; rule++)
e06f0c34
RS
417 if (*rule == i)
418 {
419 right_count++;
420 break;
421 }
422 }
423
424 buffer[0] = 0;
ad949da9
AD
425 fputs (escape (symbols[i]->tag), out);
426 column = strlen (escape (symbols[i]->tag));
e06f0c34
RS
427 sprintf (buffer, " (%d)", i);
428 END_TEST (0);
429
430 if (left_count > 0)
431 {
432 END_TEST (50);
c29240e7 433 sprintf (buffer + strlen (buffer), _(" on left:"));
e06f0c34 434
18bcecb0 435 for (j = 1; j < nrules + 1; j++)
e06f0c34
RS
436 {
437 END_TEST (65);
1a2b5d37 438 if (rules[j].lhs == i)
30171f79 439 sprintf (buffer + strlen (buffer), " %d", j - 1);
e06f0c34
RS
440 }
441 }
442
443 if (right_count > 0)
444 {
445 if (left_count > 0)
c29240e7 446 sprintf (buffer + strlen (buffer), ",");
e06f0c34 447 END_TEST (50);
c29240e7 448 sprintf (buffer + strlen (buffer), _(" on right:"));
18bcecb0 449 for (j = 1; j < nrules + 1; j++)
e06f0c34 450 {
99013900 451 for (rule = rules[j].rhs; *rule >= 0; rule++)
e06f0c34
RS
452 if (*rule == i)
453 {
454 END_TEST (65);
30171f79 455 sprintf (buffer + strlen (buffer), " %d", j - 1);
e06f0c34
RS
456 break;
457 }
458 }
459 }
342b8b6e 460 fprintf (out, "%s\n", buffer);
e06f0c34 461 }
d2d1b42b 462 fputs ("\n\n", out);
e06f0c34 463}
07a58c13
AD
464\f
465void
466print_results (void)
467{
602bbf31 468 size_t i;
07a58c13 469
64d15509
AD
470 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
471 that conflicts with Posix. */
472 FILE *out = xfopen (spec_verbose_file, "w");
07a58c13 473
64d15509
AD
474 size_t size = obstack_object_size (&output_obstack);
475 fwrite (obstack_finish (&output_obstack), 1, size, out);
476 obstack_free (&output_obstack, NULL);
07a58c13 477
64d15509
AD
478 if (size)
479 fputs ("\n\n", out);
342b8b6e 480
64d15509
AD
481 reduce_output (out);
482 conflicts_output (out);
342b8b6e 483
64d15509 484 print_grammar (out);
342b8b6e 485
5092aba5
AD
486 /* New experimental feature: output all the items of a state, not
487 only its kernel. Requires to run closure, which need memory
488 allocation/deallocation. */
64d15509 489 if (trace_flag)
9e7f6bbd 490 new_closure (nritems);
5092aba5 491 /* Storage for print_reductions. */
34ba9743 492 shiftset = bitset_create (ntokens, BITSET_FIXED);
34ba9743 493 lookaheadset = bitset_create (ntokens, BITSET_FIXED);
64d15509 494 for (i = 0; i < nstates; i++)
29e88316 495 print_state (out, states[i]);
34ba9743
AD
496 bitset_free (shiftset);
497 bitset_free (lookaheadset);
64d15509
AD
498 if (trace_flag)
499 free_closure ();
500
501 xfclose (out);
07a58c13 502}