]>
git.saurik.com Git - bison.git/blob - src/print.c
7be9f63df48eb70d2d868aff23bda3bf4385b7aa
1 /* Print information on generated parser, for bison,
2 Copyright 1984, 1986, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
29 #include "conflicts.h"
38 static bitset shiftset
;
39 static bitset lookaheadset
;
43 print_token (int extnum
, int token
)
45 fprintf (out
, _(" type %d is %s\n"), extnum
, tags
[token
]);
49 static inline const char *
50 escape (const char *s
)
52 return quotearg_n_style (1, escape_quoting_style
, s
);
55 /* Be cautious not to use twice the same slot in a single expression. */
56 static inline const char *
57 escape2 (const char *s
)
59 return quotearg_n_style (2, escape_quoting_style
, s
);
63 /*--------------------------------.
64 | Report information on a state. |
65 `--------------------------------*/
68 print_core (FILE *out
, state_t
*state
)
71 short *sitems
= state
->items
;
72 int snitems
= state
->nitems
;
74 /* New experimental feature: if TRACE_FLAGS output all the items of
75 a state, not only its kernel. */
78 closure (sitems
, snitems
);
85 for (i
= 0; i
< snitems
; i
++)
91 sp1
= sp
= ritem
+ sitems
[i
];
97 fprintf (out
, " %s -> ", escape (symbols
[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 fprintf (out
, _(" (rule %d)"), rule
- 1);
117 print_shifts (FILE *out
, state_t
*state
)
120 shifts
*shiftp
= state
->shifts
;
122 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
123 if (!SHIFT_IS_DISABLED (shiftp
, i
))
125 int state1
= shiftp
->shifts
[i
];
126 int symbol
= states
[state1
]->accessing_symbol
;
128 _(" %-4s\tshift, and go to state %d\n"),
129 escape (symbols
[symbol
]->tag
), state1
);
138 print_errs (FILE *out
, state_t
*state
)
140 errs
*errp
= state
->errs
;
143 for (i
= 0; i
< errp
->nerrs
; ++i
)
145 fprintf (out
, _(" %-4s\terror (nonassociative)\n"),
146 escape (symbols
[errp
->errs
[i
]]->tag
));
154 print_gotos (FILE *out
, state_t
*state
)
157 shifts
*shiftp
= state
->shifts
;
159 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
160 /* Skip token shifts. */;
162 if (i
< shiftp
->nshifts
)
164 for (; i
< shiftp
->nshifts
; i
++)
165 if (!SHIFT_IS_DISABLED (shiftp
, i
))
167 int state1
= shiftp
->shifts
[i
];
168 int symbol
= states
[state1
]->accessing_symbol
;
169 fprintf (out
, _(" %-4s\tgo to state %d\n"),
170 escape (symbols
[symbol
]->tag
), state1
);
178 print_reductions (FILE *out
, state_t
*state
)
181 shifts
*shiftp
= state
->shifts
;
182 reductions
*redp
= state
->reductions
;
183 errs
*errp
= state
->errs
;
186 if (redp
->nreds
== 0)
189 if (state
->consistent
)
191 int rule
= redp
->rules
[0];
192 int symbol
= rules
[rule
].lhs
;
193 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
194 rule
- 1, escape (symbols
[symbol
]->tag
));
198 bitset_zero (shiftset
);
200 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
201 if (!SHIFT_IS_DISABLED (shiftp
, i
))
203 /* if this state has a shift for the error token, don't use a
205 if (SHIFT_IS_ERROR (shiftp
, i
))
207 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
210 for (i
= 0; i
< errp
->nerrs
; i
++)
212 bitset_set (shiftset
, errp
->errs
[i
]);
214 if (state
->nlookaheads
== 1 && !nodefault
)
216 int default_rule
= LAruleno
[state
->lookaheadsp
];
218 bitset_and (lookaheadset
, LA
[state
->lookaheadsp
], shiftset
);
220 for (i
= 0; i
< ntokens
; i
++)
221 if (bitset_test (lookaheadset
, i
))
222 fprintf (out
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
223 escape (symbols
[i
]->tag
), default_rule
- 1,
224 escape2 (symbols
[rules
[default_rule
].lhs
]->tag
));
226 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
227 default_rule
- 1, escape (symbols
[rules
[default_rule
].lhs
]->tag
));
229 else if (state
->nlookaheads
>= 1)
233 int default_rule
= 0;
236 for (i
= 0; i
< state
->nlookaheads
; ++i
)
241 bitset_andn (lookaheadset
, LA
[state
->lookaheadsp
+ i
], shiftset
);
243 for (j
= 0; j
< ntokens
; j
++)
244 if (bitset_test (lookaheadset
, j
))
250 default_LA
= state
->lookaheadsp
+ i
;
251 default_rule
= LAruleno
[state
->lookaheadsp
+ i
];
254 bitset_or (shiftset
, shiftset
, lookaheadset
);
257 bitset_zero (shiftset
);
259 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
260 if (!SHIFT_IS_DISABLED (shiftp
, i
))
261 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
263 for (i
= 0; i
< ntokens
; i
++)
267 int count
= bitset_test (shiftset
, i
);
269 for (j
= 0; j
< state
->nlookaheads
; ++j
)
270 if (bitset_test (LA
[state
->lookaheadsp
+ j
], i
))
274 if (state
->lookaheadsp
+ j
!= default_LA
)
276 _(" %-4s\treduce using rule %d (%s)\n"),
277 escape (symbols
[i
]->tag
),
278 LAruleno
[state
->lookaheadsp
+ j
] - 1,
279 escape2 (symbols
[rules
[LAruleno
[state
->lookaheadsp
+ j
]].lhs
]->tag
));
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
));
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
));
304 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
306 escape (symbols
[rules
[default_rule
].lhs
]->tag
));
312 print_actions (FILE *out
, state_t
*state
)
314 reductions
*redp
= state
->reductions
;
315 shifts
*shiftp
= state
->shifts
;
317 if (shiftp
->nshifts
== 0 && redp
->nreds
== 0)
319 if (final_state
== state
->number
)
320 fprintf (out
, _(" $default\taccept\n"));
322 fprintf (out
, _(" NO ACTIONS\n"));
326 print_shifts (out
, state
);
327 print_errs (out
, state
);
328 print_reductions (out
, state
);
329 print_gotos (out
, state
);
333 print_state (FILE *out
, state_t
*state
)
335 fprintf (out
, _("state %d"), state
->number
);
337 print_core (out
, state
);
338 print_actions (out
, state
);
342 /*-----------------------------------------.
343 | Print information on the whole grammar. |
344 `-----------------------------------------*/
346 #define END_TEST(End) \
348 if (column + strlen(buffer) > (End)) \
350 fprintf (out, "%s\n ", buffer); \
358 print_grammar (FILE *out
)
365 /* rule # : LHS -> RHS */
366 fprintf (out
, "%s\n\n", _("Grammar"));
367 fprintf (out
, " %s\n", _("Number, Line, Rule"));
368 for (i
= 1; i
< nrules
+ 1; i
++)
369 /* Don't print rules disabled in reduce_grammar_tables. */
372 fprintf (out
, _(" %3d %3d %s ->"),
373 i
- 1, rules
[i
].line
, escape (symbols
[rules
[i
].lhs
]->tag
));
377 fprintf (out
, " %s", escape (symbols
[*rule
++]->tag
));
379 fprintf (out
, " /* %s */", _("empty"));
385 /* TERMINAL (type #) : rule #s terminal is on RHS */
386 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
387 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
388 if (token_translations
[i
] != 2)
391 column
= strlen (escape (symbols
[token_translations
[i
]]->tag
));
392 fputs (escape (symbols
[token_translations
[i
]]->tag
), out
);
394 sprintf (buffer
, " (%d)", i
);
396 for (j
= 1; j
< nrules
+ 1; j
++)
397 for (rule
= rules
[j
].rhs
; *rule
>= 0; rule
++)
398 if (*rule
== token_translations
[i
])
401 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
404 fprintf (out
, "%s\n", buffer
);
409 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
410 for (i
= ntokens
; i
< nsyms
; i
++)
412 int left_count
= 0, right_count
= 0;
414 for (j
= 1; j
< nrules
+ 1; j
++)
416 if (rules
[j
].lhs
== i
)
418 for (rule
= rules
[j
].rhs
; *rule
>= 0; rule
++)
427 fputs (escape (symbols
[i
]->tag
), out
);
428 column
= strlen (escape (symbols
[i
]->tag
));
429 sprintf (buffer
, " (%d)", i
);
435 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
437 for (j
= 1; j
< nrules
+ 1; j
++)
440 if (rules
[j
].lhs
== i
)
441 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
448 sprintf (buffer
+ strlen (buffer
), ",");
450 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
451 for (j
= 1; j
< nrules
+ 1; j
++)
453 for (rule
= rules
[j
].rhs
; *rule
>= 0; rule
++)
457 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
462 fprintf (out
, "%s\n", buffer
);
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");
476 size_t size
= obstack_object_size (&output_obstack
);
477 fwrite (obstack_finish (&output_obstack
), 1, size
, out
);
478 obstack_free (&output_obstack
, NULL
);
484 conflicts_output (out
);
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. */
492 new_closure (nritems
);
493 /* Storage for print_reductions. */
494 shiftset
= bitset_create (ntokens
, BITSET_FIXED
);
495 lookaheadset
= bitset_create (ntokens
, BITSET_FIXED
);
496 for (i
= 0; i
< nstates
; i
++)
497 print_state (out
, states
[i
]);
498 bitset_free (shiftset
);
499 bitset_free (lookaheadset
);