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