]>
git.saurik.com Git - bison.git/blob - src/print.c
ddaa332813a364b011fe8548654215e6271d1946
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 token_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 token_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 token_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
);
370 /*-----------------------------------------.
371 | Print information on the whole grammar. |
372 `-----------------------------------------*/
374 #define END_TEST(End) \
376 if (column + strlen(buffer) > (End)) \
378 fprintf (out, "%s\n ", buffer); \
386 print_grammar (FILE *out
)
394 /* rule # : LHS -> RHS */
395 fprintf (out
, "%s\n\n", _("Grammar"));
396 fprintf (out
, " %s\n", _("Number, Line, Rule"));
397 for (j
= 1; j
< nrules
+ 1; j
++)
399 fprintf (out
, _(" %3d %3d %s ->"),
400 j
- 1, rules
[j
].line
, escape (rules
[j
].lhs
->tag
));
404 fprintf (out
, " %s", escape (symbols
[*rule
++]->tag
));
406 fprintf (out
, " /* %s */", _("empty"));
412 /* TERMINAL (type #) : rule #s terminal is on RHS */
413 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
414 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
415 if (token_translations
[i
] != undeftoken
->number
)
418 column
= strlen (escape (symbols
[token_translations
[i
]]->tag
));
419 fputs (escape (symbols
[token_translations
[i
]]->tag
), out
);
421 sprintf (buffer
, " (%d)", i
);
423 for (j
= 1; j
< nrules
+ 1; j
++)
424 for (rule
= rules
[j
].rhs
; *rule
>= 0; rule
++)
425 if (item_number_as_token_number (*rule
) == token_translations
[i
])
428 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
431 fprintf (out
, "%s\n", buffer
);
436 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
437 for (i
= ntokens
; i
< nsyms
; i
++)
439 int left_count
= 0, right_count
= 0;
441 for (j
= 1; j
< nrules
+ 1; j
++)
443 if (rules
[j
].lhs
->number
== i
)
445 for (rule
= rules
[j
].rhs
; *rule
>= 0; rule
++)
446 if (item_number_as_token_number (*rule
) == i
)
454 fputs (escape (symbols
[i
]->tag
), out
);
455 column
= strlen (escape (symbols
[i
]->tag
));
456 sprintf (buffer
, " (%d)", i
);
462 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
464 for (j
= 1; j
< nrules
+ 1; j
++)
467 if (rules
[j
].lhs
->number
== i
)
468 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
475 sprintf (buffer
+ strlen (buffer
), ",");
477 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
478 for (j
= 1; j
< nrules
+ 1; j
++)
480 for (rule
= rules
[j
].rhs
; *rule
>= 0; rule
++)
481 if (item_number_as_token_number (*rule
) == i
)
484 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
489 fprintf (out
, "%s\n", buffer
);
499 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
500 that conflicts with Posix. */
501 FILE *out
= xfopen (spec_verbose_file
, "w");
503 size_t size
= obstack_object_size (&output_obstack
);
504 fwrite (obstack_finish (&output_obstack
), 1, size
, out
);
505 obstack_free (&output_obstack
, NULL
);
511 conflicts_output (out
);
515 /* If the whole state item sets, not only the kernels, are wanted,
516 `closure' will be run, which needs memory allocation/deallocation. */
517 if (report_flag
& report_itemsets
)
518 new_closure (nritems
);
519 /* Storage for print_reductions. */
520 shiftset
= bitset_create (ntokens
, BITSET_FIXED
);
521 lookaheadset
= bitset_create (ntokens
, BITSET_FIXED
);
522 for (i
= 0; i
< nstates
; i
++)
523 print_state (out
, states
[i
]);
524 bitset_free (shiftset
);
525 bitset_free (lookaheadset
);
526 if (report_flag
& report_itemsets
)