]>
git.saurik.com Git - bison.git/blob - src/print.c
1 /* Print information on generated parser, for bison,
2 Copyright 1984, 1986, 1989, 2000, 2001 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. */
27 #include "conflicts.h"
35 static unsigned *shiftset
= NULL
;
36 static unsigned *lookaheadset
= NULL
;
40 print_token (int extnum
, int token
)
42 fprintf (out
, _(" type %d is %s\n"), extnum
, tags
[token
]);
47 /*--------------------------------.
48 | Report information on a state. |
49 `--------------------------------*/
52 print_core (FILE *out
, state_t
*state
)
55 short *sitems
= state
->items
;
56 int snitems
= state
->nitems
;
58 /* New experimental feature: if TRACE_FLAGS output all the items of
59 a state, not only its kernel. */
62 closure (sitems
, snitems
);
69 for (i
= 0; i
< snitems
; i
++)
75 sp1
= sp
= ritem
+ sitems
[i
];
81 fprintf (out
, " %s -> ", tags
[rule_table
[rule
].lhs
]);
83 for (sp
= ritem
+ rule_table
[rule
].rhs
; sp
< sp1
; sp
++)
84 fprintf (out
, "%s ", tags
[*sp
]);
88 for (/* Nothing */; *sp
> 0; ++sp
)
89 fprintf (out
, " %s", tags
[*sp
]);
91 fprintf (out
, _(" (rule %d)"), rule
);
101 print_shifts (FILE *out
, state_t
*state
)
104 shifts
*shiftp
= state
->shifts
;
106 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
107 if (!SHIFT_IS_DISABLED (shiftp
, i
))
109 int state1
= shiftp
->shifts
[i
];
110 int symbol
= state_table
[state1
]->accessing_symbol
;
112 _(" %-4s\tshift, and go to state %d\n"),
113 tags
[symbol
], state1
);
122 print_errs (FILE *out
, state_t
*state
)
124 errs
*errp
= state
->errs
;
127 for (i
= 0; i
< errp
->nerrs
; ++i
)
129 fprintf (out
, _(" %-4s\terror (nonassociative)\n"),
130 tags
[errp
->errs
[i
]]);
138 print_gotos (FILE *out
, state_t
*state
)
141 shifts
*shiftp
= state
->shifts
;
143 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
144 /* Skip token shifts. */;
146 if (i
< shiftp
->nshifts
)
148 for (; i
< shiftp
->nshifts
; i
++)
149 if (!SHIFT_IS_DISABLED (shiftp
, i
))
151 int state1
= shiftp
->shifts
[i
];
152 int symbol
= state_table
[state1
]->accessing_symbol
;
153 fprintf (out
, _(" %-4s\tgo to state %d\n"),
154 tags
[symbol
], state1
);
162 print_reductions (FILE *out
, state_t
*state
)
165 shifts
*shiftp
= state
->shifts
;
166 reductions
*redp
= state
->reductions
;
167 errs
*errp
= state
->errs
;
170 if (redp
->nreds
== 0)
173 if (state
->consistent
)
175 int rule
= redp
->rules
[0];
176 int symbol
= rule_table
[rule
].lhs
;
177 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
182 for (i
= 0; i
< tokensetsize
; i
++)
185 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
186 if (!SHIFT_IS_DISABLED (shiftp
, i
))
188 /* if this state has a shift for the error token, don't use a
190 if (SHIFT_IS_ERROR (shiftp
, i
))
192 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
195 for (i
= 0; i
< errp
->nerrs
; i
++)
197 SETBIT (shiftset
, errp
->errs
[i
]);
199 if (state
->nlookaheads
== 1 && !nodefault
)
202 int default_rule
= LAruleno
[state
->lookaheadsp
];
204 for (k
= 0; k
< tokensetsize
; ++k
)
205 lookaheadset
[k
] = LA (state
->lookaheadsp
)[k
] & shiftset
[k
];
207 for (i
= 0; i
< ntokens
; i
++)
208 if (BITISSET (lookaheadset
, i
))
209 fprintf (out
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
210 tags
[i
], default_rule
,
211 tags
[rule_table
[default_rule
].lhs
]);
213 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
214 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
216 else if (state
->nlookaheads
>= 1)
220 int default_rule
= 0;
223 for (i
= 0; i
< state
->nlookaheads
; ++i
)
228 for (k
= 0; k
< tokensetsize
; ++k
)
229 lookaheadset
[k
] = LA (state
->lookaheadsp
+ i
)[k
] & ~shiftset
[k
];
231 for (j
= 0; j
< ntokens
; j
++)
232 if (BITISSET (lookaheadset
, j
))
238 default_LA
= state
->lookaheadsp
+ i
;
239 default_rule
= LAruleno
[state
->lookaheadsp
+ i
];
242 for (k
= 0; k
< tokensetsize
; ++k
)
243 shiftset
[k
] |= lookaheadset
[k
];
246 for (i
= 0; i
< tokensetsize
; i
++)
249 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
250 if (!SHIFT_IS_DISABLED (shiftp
, i
))
251 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
253 for (i
= 0; i
< ntokens
; i
++)
257 int count
= BITISSET (shiftset
, i
);
259 for (j
= 0; j
< state
->nlookaheads
; ++j
)
261 if (BITISSET (LA (state
->lookaheadsp
+ j
), i
))
265 if (state
->lookaheadsp
+ j
!= default_LA
)
267 _(" %-4s\treduce using rule %d (%s)\n"),
269 LAruleno
[state
->lookaheadsp
+ j
],
270 tags
[rule_table
[LAruleno
[state
->lookaheadsp
+ j
]].lhs
]);
280 _(" %-4s\treduce using rule %d (%s)\n"),
282 LAruleno
[default_LA
],
283 tags
[rule_table
[LAruleno
[default_LA
]].lhs
]);
286 _(" %-4s\t[reduce using rule %d (%s)]\n"),
288 LAruleno
[state
->lookaheadsp
+ j
],
289 tags
[rule_table
[LAruleno
[state
->lookaheadsp
+ j
]].lhs
]);
296 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
297 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
303 print_actions (FILE *out
, state_t
*state
)
305 reductions
*redp
= state
->reductions
;
306 shifts
*shiftp
= state
->shifts
;
308 if (shiftp
->nshifts
== 0 && redp
->nreds
== 0)
310 if (final_state
== state
->number
)
311 fprintf (out
, _(" $default\taccept\n"));
313 fprintf (out
, _(" NO ACTIONS\n"));
317 print_shifts (out
, state
);
318 print_errs (out
, state
);
319 print_reductions (out
, state
);
320 print_gotos (out
, state
);
324 print_state (FILE *out
, state_t
*state
)
326 fprintf (out
, _("state %d"), state
->number
);
328 print_core (out
, state
);
329 print_actions (out
, state
);
333 /*-----------------------------------------.
334 | Print information on the whole grammar. |
335 `-----------------------------------------*/
337 #define END_TEST(End) \
339 if (column + strlen(buffer) > (End)) \
341 fprintf (out, "%s\n ", buffer); \
349 print_grammar (FILE *out
)
356 /* rule # : LHS -> RHS */
357 fprintf (out
, "%s\n\n", _("Grammar"));
358 fprintf (out
, " %s\n", _("Number, Line, Rule"));
359 for (i
= 1; i
<= nrules
; i
++)
360 /* Don't print rules disabled in reduce_grammar_tables. */
361 if (rule_table
[i
].useful
)
363 fprintf (out
, _(" %3d %3d %s ->"),
364 i
, rule_table
[i
].line
, tags
[rule_table
[i
].lhs
]);
365 rule
= &ritem
[rule_table
[i
].rhs
];
368 fprintf (out
, " %s", tags
[*rule
++]);
370 fprintf (out
, " /* %s */", _("empty"));
376 /* TERMINAL (type #) : rule #s terminal is on RHS */
377 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
378 fprintf (out
, "%s (-1)\n", tags
[0]);
380 for (i
= 0; i
<= max_user_token_number
; i
++)
381 if (token_translations
[i
] != 2)
384 column
= strlen (tags
[token_translations
[i
]]);
385 fputs (tags
[token_translations
[i
]], out
);
387 sprintf (buffer
, " (%d)", i
);
389 for (j
= 1; j
<= nrules
; j
++)
390 for (rule
= &ritem
[rule_table
[j
].rhs
]; *rule
> 0; rule
++)
391 if (*rule
== token_translations
[i
])
394 sprintf (buffer
+ strlen (buffer
), " %d", j
);
397 fprintf (out
, "%s\n", buffer
);
402 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
403 for (i
= ntokens
; i
<= nsyms
- 1; i
++)
405 int left_count
= 0, right_count
= 0;
407 for (j
= 1; j
<= nrules
; j
++)
409 if (rule_table
[j
].lhs
== i
)
411 for (rule
= &ritem
[rule_table
[j
].rhs
]; *rule
> 0; rule
++)
420 fputs (tags
[i
], out
);
421 column
= strlen (tags
[i
]);
422 sprintf (buffer
, " (%d)", i
);
428 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
430 for (j
= 1; j
<= nrules
; j
++)
433 if (rule_table
[j
].lhs
== i
)
434 sprintf (buffer
+ strlen (buffer
), " %d", j
);
441 sprintf (buffer
+ strlen (buffer
), ",");
443 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
444 for (j
= 1; j
<= nrules
; j
++)
446 for (rule
= &ritem
[rule_table
[j
].rhs
]; *rule
> 0; rule
++)
450 sprintf (buffer
+ strlen (buffer
), " %d", j
);
455 fprintf (out
, "%s\n", buffer
);
465 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
466 that conflicts with Posix. */
467 FILE *out
= xfopen (spec_verbose_file
, "w");
469 size_t size
= obstack_object_size (&output_obstack
);
470 fwrite (obstack_finish (&output_obstack
), 1, size
, out
);
471 obstack_free (&output_obstack
, NULL
);
477 conflicts_output (out
);
481 /* New experimental feature: output all the items of a state, not
482 only its kernel. Requires to run closure, which need memory
483 allocation/deallocation. */
485 new_closure (nitems
);
486 /* Storage for print_reductions. */
487 shiftset
= XCALLOC (unsigned, tokensetsize
);
488 lookaheadset
= XCALLOC (unsigned, tokensetsize
);
489 for (i
= 0; i
< nstates
; i
++)
490 print_state (out
, state_table
[i
]);