]>
git.saurik.com Git - bison.git/blob - src/print.c
1 /* Print information on generated parser, for bison,
2 Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
30 #include "conflicts.h"
39 static bitset shiftset
;
40 static bitset lookaheadset
;
44 print_token (int extnum
, int token
)
46 fprintf (out
, _(" type %d is %s\n"), extnum
, tags
[token
]);
50 static inline const char *
51 escape (const char *s
)
53 return quotearg_n_style (1, escape_quoting_style
, s
);
56 /* Be cautious not to use twice the same slot in a single expression. */
57 static inline const char *
58 escape2 (const char *s
)
60 return quotearg_n_style (2, escape_quoting_style
, s
);
64 /*--------------------------------.
65 | Report information on a state. |
66 `--------------------------------*/
69 print_core (FILE *out
, state_t
*state
)
72 item_number_t
*sitems
= state
->items
;
73 int snritems
= state
->nitems
;
75 /* Output all the items of a state, not only its kernel. */
76 if (report_flag
& report_itemsets
)
78 closure (sitems
, snritems
);
85 for (i
= 0; i
< snritems
; i
++)
91 sp1
= sp
= ritem
+ sitems
[i
];
97 fprintf (out
, " %s -> ", escape (rules
[rule
].lhs
->tag
));
99 for (sp
= rules
[rule
].rhs
; sp
< sp1
; sp
++)
100 fprintf (out
, "%s ", escape (symbols
[*sp
]->tag
));
104 for (/* Nothing */; *sp
>= 0; ++sp
)
105 fprintf (out
, " %s", escape (symbols
[*sp
]->tag
));
107 /* Display the lookaheads? */
108 if (report_flag
& report_lookaheads
)
112 /* Look for lookaheads corresponding to this rule. */
113 for (j
= 0; j
< state
->nlookaheads
; ++j
)
114 for (k
= 0; k
< ntokens
; ++k
)
115 if (bitset_test (LA
[state
->lookaheadsp
+ j
], k
)
116 && LArule
[state
->lookaheadsp
+ j
]->number
== rule
)
121 for (j
= 0; j
< state
->nlookaheads
; ++j
)
122 for (k
= 0; k
< ntokens
; ++k
)
123 if (bitset_test (LA
[state
->lookaheadsp
+ j
], k
)
124 && LArule
[state
->lookaheadsp
+ j
]->number
== rule
)
125 fprintf (out
, "%s%s",
126 quotearg_style (escape_quoting_style
,
128 --nlookaheads
? ", " : "");
133 fprintf (out
, _(" (rule %d)"), rule
- 1);
143 print_shifts (FILE *out
, state_t
*state
)
146 shifts
*shiftp
= state
->shifts
;
148 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
149 if (!SHIFT_IS_DISABLED (shiftp
, i
))
151 int state1
= shiftp
->shifts
[i
];
152 symbol_number_t symbol
= states
[state1
]->accessing_symbol
;
154 _(" %-4s\tshift, and go to state %d\n"),
155 escape (symbols
[symbol
]->tag
), state1
);
164 print_errs (FILE *out
, state_t
*state
)
166 errs
*errp
= state
->errs
;
169 for (i
= 0; i
< errp
->nerrs
; ++i
)
171 fprintf (out
, _(" %-4s\terror (nonassociative)\n"),
172 escape (symbols
[errp
->errs
[i
]]->tag
));
180 print_gotos (FILE *out
, state_t
*state
)
183 shifts
*shiftp
= state
->shifts
;
185 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
186 /* Skip token shifts. */;
188 if (i
< shiftp
->nshifts
)
190 for (; i
< shiftp
->nshifts
; i
++)
191 if (!SHIFT_IS_DISABLED (shiftp
, i
))
193 int state1
= shiftp
->shifts
[i
];
194 symbol_number_t symbol
= states
[state1
]->accessing_symbol
;
195 fprintf (out
, _(" %-4s\tgo to state %d\n"),
196 escape (symbols
[symbol
]->tag
), state1
);
204 print_reductions (FILE *out
, state_t
*state
)
207 shifts
*shiftp
= state
->shifts
;
208 reductions
*redp
= state
->reductions
;
209 errs
*errp
= state
->errs
;
212 if (redp
->nreds
== 0)
215 if (state
->consistent
)
217 int rule
= redp
->rules
[0];
218 symbol_number_t symbol
= rules
[rule
].lhs
->number
;
219 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
220 rule
- 1, escape (symbols
[symbol
]->tag
));
224 bitset_zero (shiftset
);
226 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
227 if (!SHIFT_IS_DISABLED (shiftp
, i
))
229 /* if this state has a shift for the error token, don't use a
231 if (SHIFT_IS_ERROR (shiftp
, i
))
233 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
236 for (i
= 0; i
< errp
->nerrs
; i
++)
238 bitset_set (shiftset
, errp
->errs
[i
]);
240 if (state
->nlookaheads
== 1 && !nodefault
)
242 rule_t
*default_rule
= LArule
[state
->lookaheadsp
];
244 bitset_and (lookaheadset
, LA
[state
->lookaheadsp
], shiftset
);
246 for (i
= 0; i
< ntokens
; i
++)
247 if (bitset_test (lookaheadset
, i
))
248 fprintf (out
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
249 escape (symbols
[i
]->tag
),
250 default_rule
->number
- 1,
251 escape2 (default_rule
->lhs
->tag
));
253 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
254 default_rule
->number
- 1,
255 escape (default_rule
->lhs
->tag
));
257 else if (state
->nlookaheads
>= 1)
261 rule_t
*default_rule
= NULL
;
264 for (i
= 0; i
< state
->nlookaheads
; ++i
)
269 bitset_andn (lookaheadset
, LA
[state
->lookaheadsp
+ i
], shiftset
);
271 for (j
= 0; j
< ntokens
; j
++)
272 if (bitset_test (lookaheadset
, j
))
278 default_LA
= state
->lookaheadsp
+ i
;
279 default_rule
= LArule
[state
->lookaheadsp
+ i
];
282 bitset_or (shiftset
, shiftset
, lookaheadset
);
285 bitset_zero (shiftset
);
287 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
288 if (!SHIFT_IS_DISABLED (shiftp
, i
))
289 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
291 for (i
= 0; i
< ntokens
; i
++)
295 int count
= bitset_test (shiftset
, i
);
297 for (j
= 0; j
< state
->nlookaheads
; ++j
)
298 if (bitset_test (LA
[state
->lookaheadsp
+ j
], i
))
302 if (state
->lookaheadsp
+ j
!= default_LA
)
304 _(" %-4s\treduce using rule %d (%s)\n"),
305 escape (symbols
[i
]->tag
),
306 LArule
[state
->lookaheadsp
+ j
]->number
- 1,
307 escape2 (LArule
[state
->lookaheadsp
+ j
]->lhs
->tag
));
317 _(" %-4s\treduce using rule %d (%s)\n"),
318 escape (symbols
[i
]->tag
),
319 LArule
[default_LA
]->number
- 1,
320 escape2 (LArule
[default_LA
]->lhs
->tag
));
323 _(" %-4s\t[reduce using rule %d (%s)]\n"),
324 escape (symbols
[i
]->tag
),
325 LArule
[state
->lookaheadsp
+ j
]->number
- 1,
326 escape2 (LArule
[state
->lookaheadsp
+ j
]->lhs
->tag
));
332 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
333 default_rule
->number
- 1,
334 escape (default_rule
->lhs
->tag
));
340 print_actions (FILE *out
, state_t
*state
)
342 reductions
*redp
= state
->reductions
;
343 shifts
*shiftp
= state
->shifts
;
345 if (shiftp
->nshifts
== 0 && redp
->nreds
== 0)
347 if (final_state
== state
->number
)
348 fprintf (out
, _(" $default\taccept\n"));
350 fprintf (out
, _(" NO ACTIONS\n"));
354 print_shifts (out
, state
);
355 print_errs (out
, state
);
356 print_reductions (out
, state
);
357 print_gotos (out
, state
);
361 print_state (FILE *out
, state_t
*state
)
363 fprintf (out
, _("state %d"), state
->number
);
365 print_core (out
, state
);
366 print_actions (out
, state
);
367 if ((report_flag
& report_solved_conflicts
)
368 && state
->solved_conflicts
)
369 fputs (state
->solved_conflicts
, out
);
373 /*-----------------------------------------.
374 | Print information on the whole grammar. |
375 `-----------------------------------------*/
377 #define END_TEST(End) \
379 if (column + strlen(buffer) > (End)) \
381 fprintf (out, "%s\n ", buffer); \
389 print_grammar (FILE *out
)
397 /* rule # : LHS -> RHS */
398 fprintf (out
, "%s\n\n", _("Grammar"));
399 fprintf (out
, " %s\n", _("Number, Line, Rule"));
400 for (j
= 1; j
< nrules
+ 1; j
++)
402 fprintf (out
, _(" %3d %3d %s ->"),
403 j
- 1, rules
[j
].line
, escape (rules
[j
].lhs
->tag
));
407 fprintf (out
, " %s", escape (symbols
[*rule
++]->tag
));
409 fprintf (out
, " /* %s */", _("empty"));
415 /* TERMINAL (type #) : rule #s terminal is on RHS */
416 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
417 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
418 if (token_translations
[i
] != undeftoken
->number
)
421 column
= strlen (escape (symbols
[token_translations
[i
]]->tag
));
422 fputs (escape (symbols
[token_translations
[i
]]->tag
), out
);
424 sprintf (buffer
, " (%d)", i
);
426 for (j
= 1; j
< nrules
+ 1; j
++)
427 for (rule
= rules
[j
].rhs
; *rule
>= 0; rule
++)
428 if (item_number_as_symbol_number (*rule
) == token_translations
[i
])
431 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
434 fprintf (out
, "%s\n", buffer
);
439 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
440 for (i
= ntokens
; i
< nsyms
; i
++)
442 int left_count
= 0, right_count
= 0;
444 for (j
= 1; j
< nrules
+ 1; j
++)
446 if (rules
[j
].lhs
->number
== i
)
448 for (rule
= rules
[j
].rhs
; *rule
>= 0; rule
++)
449 if (item_number_as_symbol_number (*rule
) == i
)
457 fputs (escape (symbols
[i
]->tag
), out
);
458 column
= strlen (escape (symbols
[i
]->tag
));
459 sprintf (buffer
, " (%d)", i
);
465 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
467 for (j
= 1; j
< nrules
+ 1; j
++)
470 if (rules
[j
].lhs
->number
== i
)
471 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
478 sprintf (buffer
+ strlen (buffer
), ",");
480 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
481 for (j
= 1; j
< nrules
+ 1; j
++)
483 for (rule
= rules
[j
].rhs
; *rule
>= 0; rule
++)
484 if (item_number_as_symbol_number (*rule
) == i
)
487 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
492 fprintf (out
, "%s\n", buffer
);
502 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
503 that conflicts with Posix. */
504 FILE *out
= xfopen (spec_verbose_file
, "w");
506 size_t size
= obstack_object_size (&output_obstack
);
507 fwrite (obstack_finish (&output_obstack
), 1, size
, out
);
508 obstack_free (&output_obstack
, NULL
);
514 conflicts_output (out
);
518 /* If the whole state item sets, not only the kernels, are wanted,
519 `closure' will be run, which needs memory allocation/deallocation. */
520 if (report_flag
& report_itemsets
)
521 new_closure (nritems
);
522 /* Storage for print_reductions. */
523 shiftset
= bitset_create (ntokens
, BITSET_FIXED
);
524 lookaheadset
= bitset_create (ntokens
, BITSET_FIXED
);
525 for (i
= 0; i
< nstates
; i
++)
526 print_state (out
, states
[i
]);
527 bitset_free (shiftset
);
528 bitset_free (lookaheadset
);
529 if (report_flag
& report_itemsets
)