]>
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 /* New experimental feature: if TRACE_FLAGS output all the items of
76 a state, not only its kernel. */
79 closure (sitems
, snritems
);
86 for (i
= 0; i
< snritems
; i
++)
92 sp1
= sp
= ritem
+ sitems
[i
];
98 fprintf (out
, " %s -> ", escape (rules
[rule
].lhs
->tag
));
100 for (sp
= rules
[rule
].rhs
; sp
< sp1
; sp
++)
101 fprintf (out
, "%s ", escape (symbols
[*sp
]->tag
));
105 for (/* Nothing */; *sp
>= 0; ++sp
)
106 fprintf (out
, " %s", escape (symbols
[*sp
]->tag
));
108 fprintf (out
, _(" (rule %d)"), rule
- 1);
118 print_shifts (FILE *out
, state_t
*state
)
121 shifts
*shiftp
= state
->shifts
;
123 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
124 if (!SHIFT_IS_DISABLED (shiftp
, i
))
126 int state1
= shiftp
->shifts
[i
];
127 int symbol
= states
[state1
]->accessing_symbol
;
129 _(" %-4s\tshift, and go to state %d\n"),
130 escape (symbols
[symbol
]->tag
), state1
);
139 print_errs (FILE *out
, state_t
*state
)
141 errs
*errp
= state
->errs
;
144 for (i
= 0; i
< errp
->nerrs
; ++i
)
146 fprintf (out
, _(" %-4s\terror (nonassociative)\n"),
147 escape (symbols
[errp
->errs
[i
]]->tag
));
155 print_gotos (FILE *out
, state_t
*state
)
158 shifts
*shiftp
= state
->shifts
;
160 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
161 /* Skip token shifts. */;
163 if (i
< shiftp
->nshifts
)
165 for (; i
< shiftp
->nshifts
; i
++)
166 if (!SHIFT_IS_DISABLED (shiftp
, i
))
168 int state1
= shiftp
->shifts
[i
];
169 int symbol
= states
[state1
]->accessing_symbol
;
170 fprintf (out
, _(" %-4s\tgo to state %d\n"),
171 escape (symbols
[symbol
]->tag
), state1
);
179 print_reductions (FILE *out
, state_t
*state
)
182 shifts
*shiftp
= state
->shifts
;
183 reductions
*redp
= state
->reductions
;
184 errs
*errp
= state
->errs
;
187 if (redp
->nreds
== 0)
190 if (state
->consistent
)
192 int rule
= redp
->rules
[0];
193 int symbol
= rules
[rule
].lhs
->number
;
194 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
195 rule
- 1, escape (symbols
[symbol
]->tag
));
199 bitset_zero (shiftset
);
201 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
202 if (!SHIFT_IS_DISABLED (shiftp
, i
))
204 /* if this state has a shift for the error token, don't use a
206 if (SHIFT_IS_ERROR (shiftp
, i
))
208 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
211 for (i
= 0; i
< errp
->nerrs
; i
++)
213 bitset_set (shiftset
, errp
->errs
[i
]);
215 if (state
->nlookaheads
== 1 && !nodefault
)
217 rule_t
*default_rule
= LArule
[state
->lookaheadsp
];
219 bitset_and (lookaheadset
, LA
[state
->lookaheadsp
], shiftset
);
221 for (i
= 0; i
< ntokens
; i
++)
222 if (bitset_test (lookaheadset
, i
))
223 fprintf (out
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
224 escape (symbols
[i
]->tag
),
225 default_rule
->number
- 1,
226 escape2 (default_rule
->lhs
->tag
));
228 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
229 default_rule
->number
- 1,
230 escape (default_rule
->lhs
->tag
));
232 else if (state
->nlookaheads
>= 1)
236 rule_t
*default_rule
= NULL
;
239 for (i
= 0; i
< state
->nlookaheads
; ++i
)
244 bitset_andn (lookaheadset
, LA
[state
->lookaheadsp
+ i
], shiftset
);
246 for (j
= 0; j
< ntokens
; j
++)
247 if (bitset_test (lookaheadset
, j
))
253 default_LA
= state
->lookaheadsp
+ i
;
254 default_rule
= LArule
[state
->lookaheadsp
+ i
];
257 bitset_or (shiftset
, shiftset
, lookaheadset
);
260 bitset_zero (shiftset
);
262 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
263 if (!SHIFT_IS_DISABLED (shiftp
, i
))
264 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
266 for (i
= 0; i
< ntokens
; i
++)
270 int count
= bitset_test (shiftset
, i
);
272 for (j
= 0; j
< state
->nlookaheads
; ++j
)
273 if (bitset_test (LA
[state
->lookaheadsp
+ j
], i
))
277 if (state
->lookaheadsp
+ j
!= default_LA
)
279 _(" %-4s\treduce using rule %d (%s)\n"),
280 escape (symbols
[i
]->tag
),
281 LArule
[state
->lookaheadsp
+ j
]->number
- 1,
282 escape2 (LArule
[state
->lookaheadsp
+ j
]->lhs
->tag
));
292 _(" %-4s\treduce using rule %d (%s)\n"),
293 escape (symbols
[i
]->tag
),
294 LArule
[default_LA
]->number
- 1,
295 escape2 (LArule
[default_LA
]->lhs
->tag
));
298 _(" %-4s\t[reduce using rule %d (%s)]\n"),
299 escape (symbols
[i
]->tag
),
300 LArule
[state
->lookaheadsp
+ j
]->number
- 1,
301 escape2 (LArule
[state
->lookaheadsp
+ j
]->lhs
->tag
));
307 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
308 default_rule
->number
- 1,
309 escape (default_rule
->lhs
->tag
));
315 print_actions (FILE *out
, state_t
*state
)
317 reductions
*redp
= state
->reductions
;
318 shifts
*shiftp
= state
->shifts
;
320 if (shiftp
->nshifts
== 0 && redp
->nreds
== 0)
322 if (final_state
== state
->number
)
323 fprintf (out
, _(" $default\taccept\n"));
325 fprintf (out
, _(" NO ACTIONS\n"));
329 print_shifts (out
, state
);
330 print_errs (out
, state
);
331 print_reductions (out
, state
);
332 print_gotos (out
, state
);
336 print_state (FILE *out
, state_t
*state
)
338 fprintf (out
, _("state %d"), state
->number
);
340 print_core (out
, state
);
341 print_actions (out
, state
);
345 /*-----------------------------------------.
346 | Print information on the whole grammar. |
347 `-----------------------------------------*/
349 #define END_TEST(End) \
351 if (column + strlen(buffer) > (End)) \
353 fprintf (out, "%s\n ", buffer); \
361 print_grammar (FILE *out
)
368 /* rule # : LHS -> RHS */
369 fprintf (out
, "%s\n\n", _("Grammar"));
370 fprintf (out
, " %s\n", _("Number, Line, Rule"));
371 for (i
= 1; i
< nrules
+ 1; i
++)
373 fprintf (out
, _(" %3d %3d %s ->"),
374 i
- 1, rules
[i
].line
, escape (rules
[i
].lhs
->tag
));
378 fprintf (out
, " %s", escape (symbols
[*rule
++]->tag
));
380 fprintf (out
, " /* %s */", _("empty"));
386 /* TERMINAL (type #) : rule #s terminal is on RHS */
387 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
388 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
389 if (token_translations
[i
] != undeftoken
->number
)
392 column
= strlen (escape (symbols
[token_translations
[i
]]->tag
));
393 fputs (escape (symbols
[token_translations
[i
]]->tag
), out
);
395 sprintf (buffer
, " (%d)", i
);
397 for (j
= 1; j
< nrules
+ 1; j
++)
398 for (rule
= rules
[j
].rhs
; *rule
>= 0; rule
++)
399 if (*rule
== token_translations
[i
])
402 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
405 fprintf (out
, "%s\n", buffer
);
410 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
411 for (i
= ntokens
; i
< nsyms
; i
++)
413 int left_count
= 0, right_count
= 0;
415 for (j
= 1; j
< nrules
+ 1; j
++)
417 if (rules
[j
].lhs
->number
== i
)
419 for (rule
= rules
[j
].rhs
; *rule
>= 0; rule
++)
428 fputs (escape (symbols
[i
]->tag
), out
);
429 column
= strlen (escape (symbols
[i
]->tag
));
430 sprintf (buffer
, " (%d)", i
);
436 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
438 for (j
= 1; j
< nrules
+ 1; j
++)
441 if (rules
[j
].lhs
->number
== i
)
442 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
449 sprintf (buffer
+ strlen (buffer
), ",");
451 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
452 for (j
= 1; j
< nrules
+ 1; j
++)
454 for (rule
= rules
[j
].rhs
; *rule
>= 0; rule
++)
458 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
463 fprintf (out
, "%s\n", buffer
);
473 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
474 that conflicts with Posix. */
475 FILE *out
= xfopen (spec_verbose_file
, "w");
477 size_t size
= obstack_object_size (&output_obstack
);
478 fwrite (obstack_finish (&output_obstack
), 1, size
, out
);
479 obstack_free (&output_obstack
, NULL
);
485 conflicts_output (out
);
489 /* New experimental feature: output all the items of a state, not
490 only its kernel. Requires to run closure, which need memory
491 allocation/deallocation. */
493 new_closure (nritems
);
494 /* Storage for print_reductions. */
495 shiftset
= bitset_create (ntokens
, BITSET_FIXED
);
496 lookaheadset
= bitset_create (ntokens
, BITSET_FIXED
);
497 for (i
= 0; i
< nstates
; i
++)
498 print_state (out
, states
[i
]);
499 bitset_free (shiftset
);
500 bitset_free (lookaheadset
);