]> git.saurik.com Git - bison.git/blame - src/print.c
Merge in branch-1_29.
[bison.git] / src / print.c
CommitLineData
e06f0c34 1/* Print information on generated parser, for bison,
aa7815f5 2 Copyright 1984, 1986, 1989, 2000 Free Software Foundation, Inc.
e06f0c34 3
c29240e7 4 This file is part of Bison, the GNU Compiler Compiler.
e06f0c34 5
c29240e7
AD
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)
9 any later version.
e06f0c34 10
c29240e7
AD
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.
e06f0c34 15
c29240e7
AD
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. */
e06f0c34
RS
20
21
e06f0c34 22#include "system.h"
e06f0c34
RS
23#include "files.h"
24#include "gram.h"
b2ca4022 25#include "LR0.h"
720d742f 26#include "lalr.h"
0619caf0 27#include "conflicts.h"
07a58c13
AD
28#include "getargs.h"
29#include "state.h"
b2ca4022 30#include "reader.h"
d7913476 31#include "print.h"
e06f0c34 32
07a58c13 33#if 0
4a120d45 34static void
d2729d44 35print_token (int extnum, int token)
e06f0c34 36{
342b8b6e 37 fprintf (out, _(" type %d is %s\n"), extnum, tags[token]);
e06f0c34 38}
4a120d45 39#endif
e06f0c34 40
07a58c13 41\f
342b8b6e 42/*--------------------------------.
07a58c13 43| Report information on a state. |
342b8b6e 44`--------------------------------*/
e06f0c34 45
4a120d45 46static void
342b8b6e 47print_core (FILE *out, int state)
e06f0c34 48{
c29240e7
AD
49 int i;
50 int k;
51 int rule;
52 core *statep;
53 short *sp;
54 short *sp1;
e06f0c34
RS
55
56 statep = state_table[state];
57 k = statep->nitems;
58
c29240e7
AD
59 if (k == 0)
60 return;
e06f0c34
RS
61
62 for (i = 0; i < k; i++)
63 {
64 sp1 = sp = ritem + statep->items[i];
65
66 while (*sp > 0)
67 sp++;
68
69 rule = -(*sp);
342b8b6e 70 fprintf (out, " %s -> ", tags[rlhs[rule]]);
e06f0c34
RS
71
72 for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
73 {
342b8b6e 74 fprintf (out, "%s ", tags[*sp]);
e06f0c34
RS
75 }
76
342b8b6e 77 fputc ('.', out);
e06f0c34
RS
78
79 while (*sp > 0)
80 {
342b8b6e 81 fprintf (out, " %s", tags[*sp]);
e06f0c34
RS
82 sp++;
83 }
84
342b8b6e
AD
85 fprintf (out, _(" (rule %d)"), rule);
86 fputc ('\n', out);
e06f0c34
RS
87 }
88
342b8b6e 89 fputc ('\n', out);
e06f0c34
RS
90}
91
4a120d45 92static void
342b8b6e 93print_actions (FILE *out, int state)
e06f0c34 94{
c29240e7
AD
95 int i;
96 int k;
97 int state1;
98 int symbol;
99 shifts *shiftp;
100 errs *errp;
101 reductions *redp;
102 int rule;
e06f0c34
RS
103
104 shiftp = shift_table[state];
105 redp = reduction_table[state];
106 errp = err_table[state];
107
108 if (!shiftp && !redp)
109 {
110 if (final_state == state)
342b8b6e 111 fprintf (out, _(" $default\taccept\n"));
e06f0c34 112 else
342b8b6e 113 fprintf (out, _(" NO ACTIONS\n"));
e06f0c34
RS
114 return;
115 }
116
117 if (shiftp)
118 {
119 k = shiftp->nshifts;
120
121 for (i = 0; i < k; i++)
122 {
c29240e7
AD
123 if (!shiftp->shifts[i])
124 continue;
e06f0c34
RS
125 state1 = shiftp->shifts[i];
126 symbol = accessing_symbol[state1];
127 /* The following line used to be turned off. */
c29240e7
AD
128 if (ISVAR (symbol))
129 break;
130 if (symbol == 0) /* I.e. strcmp(tags[symbol],"$")==0 */
342b8b6e
AD
131 fprintf (out,
132 _(" $ \tgo to state %d\n"), state1);
c29240e7 133 else
342b8b6e
AD
134 fprintf (out,
135 _(" %-4s\tshift, and go to state %d\n"),
136 tags[symbol], state1);
e06f0c34
RS
137 }
138
139 if (i > 0)
342b8b6e 140 fputc ('\n', out);
e06f0c34
RS
141 }
142 else
143 {
144 i = 0;
145 k = 0;
146 }
147
148 if (errp)
149 {
150 int j, nerrs;
151
152 nerrs = errp->nerrs;
153
154 for (j = 0; j < nerrs; j++)
155 {
c29240e7
AD
156 if (!errp->errs[j])
157 continue;
e06f0c34 158 symbol = errp->errs[j];
342b8b6e 159 fprintf (out, _(" %-4s\terror (nonassociative)\n"),
c29240e7 160 tags[symbol]);
e06f0c34
RS
161 }
162
163 if (j > 0)
342b8b6e 164 fputc ('\n', out);
e06f0c34
RS
165 }
166
167 if (consistent[state] && redp)
168 {
169 rule = redp->rules[0];
170 symbol = rlhs[rule];
342b8b6e
AD
171 fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"),
172 rule, tags[symbol]);
e06f0c34
RS
173 }
174 else if (redp)
175 {
c29240e7 176 print_reductions (state);
e06f0c34
RS
177 }
178
179 if (i < k)
180 {
181 for (; i < k; i++)
182 {
c29240e7
AD
183 if (!shiftp->shifts[i])
184 continue;
e06f0c34
RS
185 state1 = shiftp->shifts[i];
186 symbol = accessing_symbol[state1];
342b8b6e
AD
187 fprintf (out, _(" %-4s\tgo to state %d\n"),
188 tags[symbol], state1);
e06f0c34
RS
189 }
190
342b8b6e 191 fputc ('\n', out);
e06f0c34
RS
192 }
193}
194
07a58c13 195static void
342b8b6e 196print_state (FILE *out, int state)
07a58c13 197{
342b8b6e
AD
198 fputs ("\n\n", out);
199 fprintf (out, _("state %d"), state);
200 fputs ("\n\n", out);
201 print_core (out, state);
202 print_actions (out, state);
07a58c13
AD
203}
204\f
205/*-----------------------------------------.
206| Print information on the whole grammar. |
207`-----------------------------------------*/
208
342b8b6e
AD
209#define END_TEST(End) \
210do { \
211 if (column + strlen(buffer) > (End)) \
212 { \
213 fprintf (out, "%s\n ", buffer); \
214 column = 3; \
215 buffer[0] = 0; \
216 } \
ff4423cc 217} while (0)
e06f0c34 218
07a58c13 219
4a120d45 220static void
342b8b6e 221print_grammar (FILE *out)
e06f0c34
RS
222{
223 int i, j;
c29240e7 224 short *rule;
e06f0c34
RS
225 char buffer[90];
226 int column = 0;
227
228 /* rule # : LHS -> RHS */
342b8b6e 229 fprintf (out, "\n%s\n", _("Grammar"));
e06f0c34
RS
230 for (i = 1; i <= nrules; i++)
231 /* Don't print rules disabled in reduce_grammar_tables. */
232 if (rlhs[i] >= 0)
233 {
342b8b6e 234 fprintf (out, _("rule %-4d %s ->"), i, tags[rlhs[i]]);
e06f0c34
RS
235 rule = &ritem[rrhs[i]];
236 if (*rule > 0)
237 while (*rule > 0)
342b8b6e 238 fprintf (out, " %s", tags[*rule++]);
e06f0c34 239 else
342b8b6e 240 fprintf (out, " /* %s */\n", _("empty"));
e06f0c34
RS
241 }
242
243 /* TERMINAL (type #) : rule #s terminal is on RHS */
342b8b6e
AD
244 fprintf (out, "\n%s\n\n", _("Terminals, with rules where they appear"));
245 fprintf (out, "%s (-1)\n", tags[0]);
e06f0c34 246
342b8b6e
AD
247 for (i = 0; i <= max_user_token_number; i++)
248 if (token_translations[i] != 2)
249 {
250 buffer[0] = 0;
251 column = strlen (tags[token_translations[i]]);
252 fputs (tags[token_translations[i]], out);
253 END_TEST (50);
254 sprintf (buffer, " (%d)", i);
e06f0c34 255
342b8b6e
AD
256 for (j = 1; j <= nrules; j++)
257 for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
258 if (*rule == token_translations[i])
259 {
260 END_TEST (65);
261 sprintf (buffer + strlen (buffer), " %d", j);
262 break;
263 }
264 fprintf (out, "%s\n", buffer);
265 }
266
267 fprintf (out, "\n%s\n\n",
268 _("Nonterminals, with rules where they appear"));
e06f0c34
RS
269 for (i = ntokens; i <= nsyms - 1; i++)
270 {
271 int left_count = 0, right_count = 0;
272
273 for (j = 1; j <= nrules; j++)
274 {
275 if (rlhs[j] == i)
276 left_count++;
277 for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
278 if (*rule == i)
279 {
280 right_count++;
281 break;
282 }
283 }
284
285 buffer[0] = 0;
342b8b6e 286 fputs (tags[i], out);
e06f0c34
RS
287 column = strlen (tags[i]);
288 sprintf (buffer, " (%d)", i);
289 END_TEST (0);
290
291 if (left_count > 0)
292 {
293 END_TEST (50);
c29240e7 294 sprintf (buffer + strlen (buffer), _(" on left:"));
e06f0c34
RS
295
296 for (j = 1; j <= nrules; j++)
297 {
298 END_TEST (65);
299 if (rlhs[j] == i)
c29240e7 300 sprintf (buffer + strlen (buffer), " %d", j);
e06f0c34
RS
301 }
302 }
303
304 if (right_count > 0)
305 {
306 if (left_count > 0)
c29240e7 307 sprintf (buffer + strlen (buffer), ",");
e06f0c34 308 END_TEST (50);
c29240e7 309 sprintf (buffer + strlen (buffer), _(" on right:"));
e06f0c34
RS
310 for (j = 1; j <= nrules; j++)
311 {
312 for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
313 if (*rule == i)
314 {
315 END_TEST (65);
c29240e7 316 sprintf (buffer + strlen (buffer), " %d", j);
e06f0c34
RS
317 break;
318 }
319 }
320 }
342b8b6e 321 fprintf (out, "%s\n", buffer);
e06f0c34
RS
322 }
323}
07a58c13
AD
324\f
325void
326print_results (void)
327{
342b8b6e
AD
328 if (verbose_flag)
329 {
330 int i;
07a58c13 331
342b8b6e
AD
332 /* We used to use just .out if spec_name_prefix (-p) was used, but
333 that conflicts with Posix. */
334 FILE *out = xfopen (spec_verbose_file, "w");
07a58c13 335
342b8b6e
AD
336 size_t size = obstack_object_size (&output_obstack);
337 fwrite (obstack_finish (&output_obstack), 1, size, out);
07a58c13 338
342b8b6e
AD
339 if (any_conflicts)
340 print_conflicts (out);
341
342 print_grammar (out);
343
344 for (i = 0; i < nstates; i++)
345 print_state (out, i);
346
347 xfclose (out);
348 }
349 obstack_free (&output_obstack, NULL);
07a58c13 350}