]> git.saurik.com Git - bison.git/blame - src/print.c
Some checks for premature EOF.
[bison.git] / src / print.c
CommitLineData
e06f0c34
RS
1/* Print information on generated parser, for bison,
2 Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
3
4This file is part of Bison, the GNU Compiler Compiler.
5
6Bison is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11Bison is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with Bison; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include <stdio.h>
22#include "system.h"
23#include "machine.h"
7612000c 24#include "alloc.h"
e06f0c34
RS
25#include "files.h"
26#include "gram.h"
27#include "state.h"
28
29
30extern char **tags;
31extern int nstates;
32extern short *accessing_symbol;
33extern core **state_table;
34extern shifts **shift_table;
35extern errs **err_table;
36extern reductions **reduction_table;
37extern char *consistent;
38extern char any_conflicts;
39extern char *conflicts;
40extern int final_state;
41
d2729d44
JT
42extern void conflict_log PARAMS((void));
43extern void verbose_conflict_log PARAMS((void));
44extern void print_reductions PARAMS((int));
45
46void terse PARAMS((void));
47void verbose PARAMS((void));
48void print_token PARAMS((int, int));
49void print_state PARAMS((int));
50void print_core PARAMS((int));
51void print_actions PARAMS((int));
52void print_grammar PARAMS((void));
e06f0c34
RS
53
54void
d2729d44 55terse (void)
e06f0c34
RS
56{
57 if (any_conflicts)
58 {
59 conflict_log();
60 }
61}
62
63
64void
d2729d44 65verbose (void)
e06f0c34
RS
66{
67 register int i;
68
69 if (any_conflicts)
70 verbose_conflict_log();
71
72 print_grammar();
73
74 for (i = 0; i < nstates; i++)
75 {
76 print_state(i);
77 }
78}
79
80
81void
d2729d44 82print_token (int extnum, int token)
e06f0c34 83{
a083fbbf 84 fprintf(foutput, _(" type %d is %s\n"), extnum, tags[token]);
e06f0c34
RS
85}
86
87
88void
d2729d44 89print_state (int state)
e06f0c34 90{
a083fbbf 91 fprintf(foutput, _("\n\nstate %d\n\n"), state);
e06f0c34
RS
92 print_core(state);
93 print_actions(state);
94}
95
96
97void
d2729d44 98print_core (int state)
e06f0c34
RS
99{
100 register int i;
101 register int k;
102 register int rule;
103 register core *statep;
104 register short *sp;
105 register short *sp1;
106
107 statep = state_table[state];
108 k = statep->nitems;
109
110 if (k == 0) return;
111
112 for (i = 0; i < k; i++)
113 {
114 sp1 = sp = ritem + statep->items[i];
115
116 while (*sp > 0)
117 sp++;
118
119 rule = -(*sp);
120 fprintf(foutput, " %s -> ", tags[rlhs[rule]]);
121
122 for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
123 {
124 fprintf(foutput, "%s ", tags[*sp]);
125 }
126
127 putc('.', foutput);
128
129 while (*sp > 0)
130 {
131 fprintf(foutput, " %s", tags[*sp]);
132 sp++;
133 }
134
a083fbbf 135 fprintf (foutput, _(" (rule %d)"), rule);
e06f0c34
RS
136 putc('\n', foutput);
137 }
138
139 putc('\n', foutput);
140}
141
142
143void
d2729d44 144print_actions (int state)
e06f0c34
RS
145{
146 register int i;
147 register int k;
148 register int state1;
149 register int symbol;
150 register shifts *shiftp;
151 register errs *errp;
152 register reductions *redp;
153 register int rule;
154
155 shiftp = shift_table[state];
156 redp = reduction_table[state];
157 errp = err_table[state];
158
159 if (!shiftp && !redp)
160 {
161 if (final_state == state)
a083fbbf 162 fprintf(foutput, _(" $default\taccept\n"));
e06f0c34 163 else
a083fbbf 164 fprintf(foutput, _(" NO ACTIONS\n"));
e06f0c34
RS
165 return;
166 }
167
168 if (shiftp)
169 {
170 k = shiftp->nshifts;
171
172 for (i = 0; i < k; i++)
173 {
174 if (! shiftp->shifts[i]) continue;
175 state1 = shiftp->shifts[i];
176 symbol = accessing_symbol[state1];
177 /* The following line used to be turned off. */
178 if (ISVAR(symbol)) break;
179 if (symbol==0) /* I.e. strcmp(tags[symbol],"$")==0 */
a083fbbf 180 fprintf(foutput, _(" $ \tgo to state %d\n"), state1);
e06f0c34 181 else
a083fbbf 182 fprintf(foutput, _(" %-4s\tshift, and go to state %d\n"),
e06f0c34
RS
183 tags[symbol], state1);
184 }
185
186 if (i > 0)
187 putc('\n', foutput);
188 }
189 else
190 {
191 i = 0;
192 k = 0;
193 }
194
195 if (errp)
196 {
197 int j, nerrs;
198
199 nerrs = errp->nerrs;
200
201 for (j = 0; j < nerrs; j++)
202 {
203 if (! errp->errs[j]) continue;
204 symbol = errp->errs[j];
a083fbbf 205 fprintf(foutput, _(" %-4s\terror (nonassociative)\n"), tags[symbol]);
e06f0c34
RS
206 }
207
208 if (j > 0)
209 putc('\n', foutput);
210 }
211
212 if (consistent[state] && redp)
213 {
214 rule = redp->rules[0];
215 symbol = rlhs[rule];
a083fbbf 216 fprintf(foutput, _(" $default\treduce using rule %d (%s)\n\n"),
e06f0c34
RS
217 rule, tags[symbol]);
218 }
219 else if (redp)
220 {
221 print_reductions(state);
222 }
223
224 if (i < k)
225 {
226 for (; i < k; i++)
227 {
228 if (! shiftp->shifts[i]) continue;
229 state1 = shiftp->shifts[i];
230 symbol = accessing_symbol[state1];
a083fbbf 231 fprintf(foutput, _(" %-4s\tgo to state %d\n"), tags[symbol], state1);
e06f0c34
RS
232 }
233
234 putc('\n', foutput);
235 }
236}
237
238#define END_TEST(end) \
239 if (column + strlen(buffer) > (end)) \
240 { fprintf (foutput, "%s\n ", buffer); column = 3; buffer[0] = 0; } \
241 else
242
243void
d2729d44 244print_grammar (void)
e06f0c34
RS
245{
246 int i, j;
247 short* rule;
248 char buffer[90];
249 int column = 0;
250
251 /* rule # : LHS -> RHS */
a083fbbf 252 fputs(_("\nGrammar\n"), foutput);
e06f0c34
RS
253 for (i = 1; i <= nrules; i++)
254 /* Don't print rules disabled in reduce_grammar_tables. */
255 if (rlhs[i] >= 0)
256 {
a083fbbf 257 fprintf(foutput, _("rule %-4d %s ->"), i, tags[rlhs[i]]);
e06f0c34
RS
258 rule = &ritem[rrhs[i]];
259 if (*rule > 0)
260 while (*rule > 0)
261 fprintf(foutput, " %s", tags[*rule++]);
262 else
a083fbbf 263 fputs (_(" /* empty */"), foutput);
e06f0c34
RS
264 putc('\n', foutput);
265 }
266
267 /* TERMINAL (type #) : rule #s terminal is on RHS */
a083fbbf 268 fputs(_("\nTerminals, with rules where they appear\n\n"), foutput);
e06f0c34
RS
269 fprintf(foutput, "%s (-1)\n", tags[0]);
270 if (translations)
271 {
272 for (i = 0; i <= max_user_token_number; i++)
273 if (token_translations[i] != 2)
274 {
275 buffer[0] = 0;
276 column = strlen (tags[token_translations[i]]);
277 fprintf(foutput, "%s", tags[token_translations[i]]);
278 END_TEST (50);
279 sprintf (buffer, " (%d)", i);
280
281 for (j = 1; j <= nrules; j++)
282 {
283 for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
284 if (*rule == token_translations[i])
285 {
286 END_TEST (65);
287 sprintf (buffer + strlen(buffer), " %d", j);
288 break;
289 }
290 }
291 fprintf (foutput, "%s\n", buffer);
292 }
293 }
294 else
295 for (i = 1; i < ntokens; i++)
296 {
297 buffer[0] = 0;
298 column = strlen (tags[i]);
299 fprintf(foutput, "%s", tags[i]);
300 END_TEST (50);
301 sprintf (buffer, " (%d)", i);
302
303 for (j = 1; j <= nrules; j++)
304 {
305 for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
306 if (*rule == i)
307 {
308 END_TEST (65);
309 sprintf (buffer + strlen(buffer), " %d", j);
310 break;
311 }
312 }
313 fprintf (foutput, "%s\n", buffer);
314 }
315
a083fbbf 316 fputs(_("\nNonterminals, with rules where they appear\n\n"), foutput);
e06f0c34
RS
317 for (i = ntokens; i <= nsyms - 1; i++)
318 {
319 int left_count = 0, right_count = 0;
320
321 for (j = 1; j <= nrules; j++)
322 {
323 if (rlhs[j] == i)
324 left_count++;
325 for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
326 if (*rule == i)
327 {
328 right_count++;
329 break;
330 }
331 }
332
333 buffer[0] = 0;
334 fprintf(foutput, "%s", tags[i]);
335 column = strlen (tags[i]);
336 sprintf (buffer, " (%d)", i);
337 END_TEST (0);
338
339 if (left_count > 0)
340 {
341 END_TEST (50);
a083fbbf 342 sprintf (buffer + strlen(buffer), _(" on left:"));
e06f0c34
RS
343
344 for (j = 1; j <= nrules; j++)
345 {
346 END_TEST (65);
347 if (rlhs[j] == i)
348 sprintf (buffer + strlen(buffer), " %d", j);
349 }
350 }
351
352 if (right_count > 0)
353 {
354 if (left_count > 0)
355 sprintf (buffer + strlen(buffer), ",");
356 END_TEST (50);
a083fbbf 357 sprintf (buffer + strlen(buffer), _(" on right:"));
e06f0c34
RS
358 for (j = 1; j <= nrules; j++)
359 {
360 for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
361 if (*rule == i)
362 {
363 END_TEST (65);
364 sprintf (buffer + strlen(buffer), " %d", j);
365 break;
366 }
367 }
368 }
369 fprintf (foutput, "%s\n", buffer);
370 }
371}