]> git.saurik.com Git - bison.git/blame - src/print.c
Use Gettext 0.11.1.
[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
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"));
e06f0c34
RS
368 for (i = 1; i <= nrules; i++)
369 /* Don't print rules disabled in reduce_grammar_tables. */
1a2b5d37 370 if (rules[i].useful)
e06f0c34 371 {
b29b2ed5 372 fprintf (out, _(" %3d %3d %s ->"),
1a2b5d37
AD
373 i - 1, rules[i].line, escape (symbols[rules[i].lhs]->tag));
374 rule = &ritem[rules[i].rhs];
75142d45
AD
375 if (*rule >= 0)
376 while (*rule >= 0)
ad949da9 377 fprintf (out, " %s", escape (symbols[*rule++]->tag));
e06f0c34 378 else
b29b2ed5 379 fprintf (out, " /* %s */", _("empty"));
0df87bb6 380 fputc ('\n', out);
e06f0c34 381 }
d2d1b42b
AD
382 fputs ("\n\n", out);
383
e06f0c34
RS
384
385 /* TERMINAL (type #) : rule #s terminal is on RHS */
d2d1b42b 386 fprintf (out, "%s\n\n", _("Terminals, with rules where they appear"));
342b8b6e
AD
387 for (i = 0; i <= max_user_token_number; i++)
388 if (token_translations[i] != 2)
389 {
390 buffer[0] = 0;
ad949da9
AD
391 column = strlen (escape (symbols[token_translations[i]]->tag));
392 fputs (escape (symbols[token_translations[i]]->tag), out);
342b8b6e
AD
393 END_TEST (50);
394 sprintf (buffer, " (%d)", i);
e06f0c34 395
342b8b6e 396 for (j = 1; j <= nrules; j++)
1a2b5d37 397 for (rule = &ritem[rules[j].rhs]; *rule >= 0; rule++)
342b8b6e
AD
398 if (*rule == token_translations[i])
399 {
400 END_TEST (65);
30171f79 401 sprintf (buffer + strlen (buffer), " %d", j - 1);
342b8b6e
AD
402 break;
403 }
404 fprintf (out, "%s\n", buffer);
405 }
d2d1b42b
AD
406 fputs ("\n\n", out);
407
342b8b6e 408
d2d1b42b 409 fprintf (out, "%s\n\n", _("Nonterminals, with rules where they appear"));
e06f0c34
RS
410 for (i = ntokens; i <= nsyms - 1; i++)
411 {
412 int left_count = 0, right_count = 0;
413
414 for (j = 1; j <= nrules; j++)
415 {
1a2b5d37 416 if (rules[j].lhs == i)
e06f0c34 417 left_count++;
1a2b5d37 418 for (rule = &ritem[rules[j].rhs]; *rule >= 0; rule++)
e06f0c34
RS
419 if (*rule == i)
420 {
421 right_count++;
422 break;
423 }
424 }
425
426 buffer[0] = 0;
ad949da9
AD
427 fputs (escape (symbols[i]->tag), out);
428 column = strlen (escape (symbols[i]->tag));
e06f0c34
RS
429 sprintf (buffer, " (%d)", i);
430 END_TEST (0);
431
432 if (left_count > 0)
433 {
434 END_TEST (50);
c29240e7 435 sprintf (buffer + strlen (buffer), _(" on left:"));
e06f0c34
RS
436
437 for (j = 1; j <= nrules; j++)
438 {
439 END_TEST (65);
1a2b5d37 440 if (rules[j].lhs == i)
30171f79 441 sprintf (buffer + strlen (buffer), " %d", j - 1);
e06f0c34
RS
442 }
443 }
444
445 if (right_count > 0)
446 {
447 if (left_count > 0)
c29240e7 448 sprintf (buffer + strlen (buffer), ",");
e06f0c34 449 END_TEST (50);
c29240e7 450 sprintf (buffer + strlen (buffer), _(" on right:"));
e06f0c34
RS
451 for (j = 1; j <= nrules; j++)
452 {
1a2b5d37 453 for (rule = &ritem[rules[j].rhs]; *rule >= 0; rule++)
e06f0c34
RS
454 if (*rule == i)
455 {
456 END_TEST (65);
30171f79 457 sprintf (buffer + strlen (buffer), " %d", j - 1);
e06f0c34
RS
458 break;
459 }
460 }
461 }
342b8b6e 462 fprintf (out, "%s\n", buffer);
e06f0c34 463 }
d2d1b42b 464 fputs ("\n\n", out);
e06f0c34 465}
07a58c13
AD
466\f
467void
468print_results (void)
469{
602bbf31 470 size_t i;
07a58c13 471
64d15509
AD
472 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
473 that conflicts with Posix. */
474 FILE *out = xfopen (spec_verbose_file, "w");
07a58c13 475
64d15509
AD
476 size_t size = obstack_object_size (&output_obstack);
477 fwrite (obstack_finish (&output_obstack), 1, size, out);
478 obstack_free (&output_obstack, NULL);
07a58c13 479
64d15509
AD
480 if (size)
481 fputs ("\n\n", out);
342b8b6e 482
64d15509
AD
483 reduce_output (out);
484 conflicts_output (out);
342b8b6e 485
64d15509 486 print_grammar (out);
342b8b6e 487
5092aba5
AD
488 /* New experimental feature: output all the items of a state, not
489 only its kernel. Requires to run closure, which need memory
490 allocation/deallocation. */
64d15509 491 if (trace_flag)
9e7f6bbd 492 new_closure (nritems);
5092aba5 493 /* Storage for print_reductions. */
34ba9743 494 shiftset = bitset_create (ntokens, BITSET_FIXED);
34ba9743 495 lookaheadset = bitset_create (ntokens, BITSET_FIXED);
64d15509 496 for (i = 0; i < nstates; i++)
29e88316 497 print_state (out, states[i]);
34ba9743
AD
498 bitset_free (shiftset);
499 bitset_free (lookaheadset);
64d15509
AD
500 if (trace_flag)
501 free_closure ();
502
503 xfclose (out);
07a58c13 504}