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