]>
Commit | Line | Data |
---|---|---|
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 | 35 | static void |
d2729d44 | 36 | print_token (int extnum, int token) |
e06f0c34 | 37 | { |
c29240e7 | 38 | fprintf (foutput, _(" 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 | 47 | static void |
d2729d44 | 48 | print_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); | |
c29240e7 | 71 | fprintf (foutput, " %s -> ", tags[rlhs[rule]]); |
e06f0c34 RS |
72 | |
73 | for (sp = ritem + rrhs[rule]; sp < sp1; sp++) | |
74 | { | |
c29240e7 | 75 | fprintf (foutput, "%s ", tags[*sp]); |
e06f0c34 RS |
76 | } |
77 | ||
c29240e7 | 78 | putc ('.', foutput); |
e06f0c34 RS |
79 | |
80 | while (*sp > 0) | |
81 | { | |
c29240e7 | 82 | fprintf (foutput, " %s", tags[*sp]); |
e06f0c34 RS |
83 | sp++; |
84 | } | |
85 | ||
a083fbbf | 86 | fprintf (foutput, _(" (rule %d)"), rule); |
c29240e7 | 87 | putc ('\n', foutput); |
e06f0c34 RS |
88 | } |
89 | ||
c29240e7 | 90 | putc ('\n', foutput); |
e06f0c34 RS |
91 | } |
92 | ||
4a120d45 | 93 | static void |
d2729d44 | 94 | print_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) | |
c29240e7 | 112 | fprintf (foutput, _(" $default\taccept\n")); |
e06f0c34 | 113 | else |
c29240e7 | 114 | fprintf (foutput, _(" 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 */ | |
132 | fprintf (foutput, _(" $ \tgo to state %d\n"), state1); | |
133 | else | |
134 | fprintf (foutput, _(" %-4s\tshift, and go to state %d\n"), | |
135 | tags[symbol], state1); | |
e06f0c34 RS |
136 | } |
137 | ||
138 | if (i > 0) | |
c29240e7 | 139 | putc ('\n', foutput); |
e06f0c34 RS |
140 | } |
141 | else | |
142 | { | |
143 | i = 0; | |
144 | k = 0; | |
145 | } | |
146 | ||
147 | if (errp) | |
148 | { | |
149 | int j, nerrs; | |
150 | ||
151 | nerrs = errp->nerrs; | |
152 | ||
153 | for (j = 0; j < nerrs; j++) | |
154 | { | |
c29240e7 AD |
155 | if (!errp->errs[j]) |
156 | continue; | |
e06f0c34 | 157 | symbol = errp->errs[j]; |
c29240e7 AD |
158 | fprintf (foutput, _(" %-4s\terror (nonassociative)\n"), |
159 | tags[symbol]); | |
e06f0c34 RS |
160 | } |
161 | ||
162 | if (j > 0) | |
c29240e7 | 163 | putc ('\n', foutput); |
e06f0c34 RS |
164 | } |
165 | ||
166 | if (consistent[state] && redp) | |
167 | { | |
168 | rule = redp->rules[0]; | |
169 | symbol = rlhs[rule]; | |
c29240e7 AD |
170 | fprintf (foutput, _(" $default\treduce using rule %d (%s)\n\n"), |
171 | rule, tags[symbol]); | |
e06f0c34 RS |
172 | } |
173 | else if (redp) | |
174 | { | |
c29240e7 | 175 | print_reductions (state); |
e06f0c34 RS |
176 | } |
177 | ||
178 | if (i < k) | |
179 | { | |
180 | for (; i < k; i++) | |
181 | { | |
c29240e7 AD |
182 | if (!shiftp->shifts[i]) |
183 | continue; | |
e06f0c34 RS |
184 | state1 = shiftp->shifts[i]; |
185 | symbol = accessing_symbol[state1]; | |
c29240e7 AD |
186 | fprintf (foutput, _(" %-4s\tgo to state %d\n"), tags[symbol], |
187 | state1); | |
e06f0c34 RS |
188 | } |
189 | ||
c29240e7 | 190 | putc ('\n', foutput); |
e06f0c34 RS |
191 | } |
192 | } | |
193 | ||
07a58c13 AD |
194 | static void |
195 | print_state (int state) | |
196 | { | |
444c570a AD |
197 | fputs ("\n\n", foutput); |
198 | fprintf (foutput, _("state %d"), state); | |
199 | fputs ("\n\n", foutput); | |
07a58c13 AD |
200 | print_core (state); |
201 | print_actions (state); | |
202 | } | |
203 | \f | |
204 | /*-----------------------------------------. | |
205 | | Print information on the whole grammar. | | |
206 | `-----------------------------------------*/ | |
207 | ||
4a120d45 JT |
208 | #define END_TEST(end) \ |
209 | do { \ | |
210 | if (column + strlen(buffer) > (end)) { \ | |
211 | fprintf (foutput, "%s\n ", buffer); \ | |
212 | column = 3; \ | |
213 | buffer[0] = 0; \ | |
214 | } \ | |
215 | } while (0) | |
e06f0c34 | 216 | |
07a58c13 | 217 | |
4a120d45 | 218 | static void |
d2729d44 | 219 | print_grammar (void) |
e06f0c34 RS |
220 | { |
221 | int i, j; | |
c29240e7 | 222 | short *rule; |
e06f0c34 RS |
223 | char buffer[90]; |
224 | int column = 0; | |
225 | ||
226 | /* rule # : LHS -> RHS */ | |
444c570a AD |
227 | putc ('\n', foutput); |
228 | fputs (_("Grammar"), foutput); | |
229 | putc ('\n', foutput); | |
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 | { | |
c29240e7 | 234 | fprintf (foutput, _("rule %-4d %s ->"), i, tags[rlhs[i]]); |
e06f0c34 RS |
235 | rule = &ritem[rrhs[i]]; |
236 | if (*rule > 0) | |
237 | while (*rule > 0) | |
c29240e7 | 238 | fprintf (foutput, " %s", tags[*rule++]); |
e06f0c34 | 239 | else |
a083fbbf | 240 | fputs (_(" /* empty */"), foutput); |
c29240e7 | 241 | putc ('\n', foutput); |
e06f0c34 RS |
242 | } |
243 | ||
244 | /* TERMINAL (type #) : rule #s terminal is on RHS */ | |
444c570a AD |
245 | fputs ("\n", foutput); |
246 | fputs (_("Terminals, with rules where they appear"), foutput); | |
247 | fputs ("\n\n", foutput); | |
c29240e7 | 248 | fprintf (foutput, "%s (-1)\n", tags[0]); |
e06f0c34 RS |
249 | if (translations) |
250 | { | |
251 | for (i = 0; i <= max_user_token_number; i++) | |
252 | if (token_translations[i] != 2) | |
253 | { | |
254 | buffer[0] = 0; | |
255 | column = strlen (tags[token_translations[i]]); | |
c29240e7 | 256 | fprintf (foutput, "%s", tags[token_translations[i]]); |
e06f0c34 RS |
257 | END_TEST (50); |
258 | sprintf (buffer, " (%d)", i); | |
259 | ||
260 | for (j = 1; j <= nrules; j++) | |
261 | { | |
262 | for (rule = &ritem[rrhs[j]]; *rule > 0; rule++) | |
263 | if (*rule == token_translations[i]) | |
264 | { | |
265 | END_TEST (65); | |
c29240e7 | 266 | sprintf (buffer + strlen (buffer), " %d", j); |
e06f0c34 RS |
267 | break; |
268 | } | |
269 | } | |
270 | fprintf (foutput, "%s\n", buffer); | |
271 | } | |
272 | } | |
273 | else | |
274 | for (i = 1; i < ntokens; i++) | |
275 | { | |
276 | buffer[0] = 0; | |
277 | column = strlen (tags[i]); | |
c29240e7 | 278 | fprintf (foutput, "%s", tags[i]); |
e06f0c34 RS |
279 | END_TEST (50); |
280 | sprintf (buffer, " (%d)", i); | |
281 | ||
282 | for (j = 1; j <= nrules; j++) | |
283 | { | |
284 | for (rule = &ritem[rrhs[j]]; *rule > 0; rule++) | |
285 | if (*rule == i) | |
286 | { | |
287 | END_TEST (65); | |
c29240e7 | 288 | sprintf (buffer + strlen (buffer), " %d", j); |
e06f0c34 RS |
289 | break; |
290 | } | |
291 | } | |
292 | fprintf (foutput, "%s\n", buffer); | |
293 | } | |
294 | ||
444c570a AD |
295 | fputs ("\n", foutput); |
296 | fputs (_("Nonterminals, with rules where they appear"), foutput); | |
297 | fputs ("\n\n", foutput); | |
e06f0c34 RS |
298 | for (i = ntokens; i <= nsyms - 1; i++) |
299 | { | |
300 | int left_count = 0, right_count = 0; | |
301 | ||
302 | for (j = 1; j <= nrules; j++) | |
303 | { | |
304 | if (rlhs[j] == i) | |
305 | left_count++; | |
306 | for (rule = &ritem[rrhs[j]]; *rule > 0; rule++) | |
307 | if (*rule == i) | |
308 | { | |
309 | right_count++; | |
310 | break; | |
311 | } | |
312 | } | |
313 | ||
314 | buffer[0] = 0; | |
c29240e7 | 315 | fprintf (foutput, "%s", tags[i]); |
e06f0c34 RS |
316 | column = strlen (tags[i]); |
317 | sprintf (buffer, " (%d)", i); | |
318 | END_TEST (0); | |
319 | ||
320 | if (left_count > 0) | |
321 | { | |
322 | END_TEST (50); | |
c29240e7 | 323 | sprintf (buffer + strlen (buffer), _(" on left:")); |
e06f0c34 RS |
324 | |
325 | for (j = 1; j <= nrules; j++) | |
326 | { | |
327 | END_TEST (65); | |
328 | if (rlhs[j] == i) | |
c29240e7 | 329 | sprintf (buffer + strlen (buffer), " %d", j); |
e06f0c34 RS |
330 | } |
331 | } | |
332 | ||
333 | if (right_count > 0) | |
334 | { | |
335 | if (left_count > 0) | |
c29240e7 | 336 | sprintf (buffer + strlen (buffer), ","); |
e06f0c34 | 337 | END_TEST (50); |
c29240e7 | 338 | sprintf (buffer + strlen (buffer), _(" on right:")); |
e06f0c34 RS |
339 | for (j = 1; j <= nrules; j++) |
340 | { | |
341 | for (rule = &ritem[rrhs[j]]; *rule > 0; rule++) | |
342 | if (*rule == i) | |
343 | { | |
344 | END_TEST (65); | |
c29240e7 | 345 | sprintf (buffer + strlen (buffer), " %d", j); |
e06f0c34 RS |
346 | break; |
347 | } | |
348 | } | |
349 | } | |
350 | fprintf (foutput, "%s\n", buffer); | |
351 | } | |
352 | } | |
07a58c13 AD |
353 | \f |
354 | void | |
355 | print_results (void) | |
356 | { | |
357 | int i; | |
358 | ||
359 | if (any_conflicts) | |
360 | print_conflicts (); | |
361 | ||
89cab50d | 362 | if (verbose_flag) |
07a58c13 AD |
363 | print_grammar (); |
364 | ||
89cab50d | 365 | if (verbose_flag) |
07a58c13 AD |
366 | for (i = 0; i < nstates; i++) |
367 | print_state (i); | |
368 | } |