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