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