32 |
#include "reduce.h" |
#include "reduce.h" |
33 |
#include "closure.h" |
#include "closure.h" |
34 |
|
|
35 |
|
static unsigned *shiftset = NULL; |
36 |
|
static unsigned *lookaheadset = NULL; |
37 |
|
|
38 |
#if 0 |
#if 0 |
39 |
static void |
static void |
40 |
print_token (int extnum, int token) |
print_token (int extnum, int token) |
96 |
} |
} |
97 |
} |
} |
98 |
|
|
99 |
|
|
100 |
static void |
static void |
101 |
print_actions (FILE *out, state_t *state) |
print_shifts (FILE *out, state_t *state) |
102 |
{ |
{ |
103 |
int i; |
int i; |
104 |
|
shifts *shiftp = state->shifts; |
|
shifts *shiftp = state->shifts; |
|
|
reductions *redp = state->reductions; |
|
|
errs *errp = state->errs; |
|
|
|
|
|
if (!shiftp->nshifts && !redp) |
|
|
{ |
|
|
if (final_state == state->number) |
|
|
fprintf (out, _(" $default\taccept\n")); |
|
|
else |
|
|
fprintf (out, _(" NO ACTIONS\n")); |
|
|
return; |
|
|
} |
|
105 |
|
|
106 |
for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++) |
for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++) |
107 |
if (!SHIFT_IS_DISABLED (shiftp, i)) |
if (!SHIFT_IS_DISABLED (shiftp, i)) |
115 |
|
|
116 |
if (i > 0) |
if (i > 0) |
117 |
fputc ('\n', out); |
fputc ('\n', out); |
118 |
|
} |
119 |
|
|
|
if (errp) |
|
|
{ |
|
|
int j; |
|
|
for (j = 0; j < errp->nerrs; j++) |
|
|
{ |
|
|
int symbol = errp->errs[j]; |
|
|
if (!symbol) |
|
|
continue; |
|
|
fprintf (out, _(" %-4s\terror (nonassociative)\n"), |
|
|
tags[symbol]); |
|
|
} |
|
120 |
|
|
121 |
if (j > 0) |
static void |
122 |
|
print_errs (FILE *out, state_t *state) |
123 |
|
{ |
124 |
|
errs *errp = state->errs; |
125 |
|
int i; |
126 |
|
|
127 |
|
if (!errp) |
128 |
|
return; |
129 |
|
|
130 |
|
for (i = 0; i < errp->nerrs; ++i) |
131 |
|
if (errp->errs[i]) |
132 |
|
fprintf (out, _(" %-4s\terror (nonassociative)\n"), |
133 |
|
tags[errp->errs[i]]); |
134 |
|
|
135 |
|
if (i > 0) |
136 |
fputc ('\n', out); |
fputc ('\n', out); |
137 |
} |
} |
138 |
|
|
139 |
if (state->consistent && redp) |
|
140 |
{ |
static void |
141 |
int rule = redp->rules[0]; |
print_gotos (FILE *out, state_t *state) |
142 |
int symbol = rule_table[rule].lhs; |
{ |
143 |
fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"), |
int i; |
144 |
rule, tags[symbol]); |
shifts *shiftp = state->shifts; |
145 |
} |
|
146 |
else if (redp) |
for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++) |
147 |
{ |
/* Skip token shifts. */; |
|
print_reductions (out, state); |
|
|
} |
|
148 |
|
|
149 |
if (i < shiftp->nshifts) |
if (i < shiftp->nshifts) |
150 |
{ |
{ |
162 |
} |
} |
163 |
|
|
164 |
static void |
static void |
165 |
|
print_reductions (FILE *out, state_t *state) |
166 |
|
{ |
167 |
|
int i; |
168 |
|
shifts *shiftp = state->shifts; |
169 |
|
reductions *redp = state->reductions; |
170 |
|
errs *errp = state->errs; |
171 |
|
int nodefault = 0; |
172 |
|
|
173 |
|
if (state->consistent) |
174 |
|
{ |
175 |
|
int rule = redp->rules[0]; |
176 |
|
int symbol = rule_table[rule].lhs; |
177 |
|
fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"), |
178 |
|
rule, tags[symbol]); |
179 |
|
return; |
180 |
|
} |
181 |
|
|
182 |
|
for (i = 0; i < tokensetsize; i++) |
183 |
|
shiftset[i] = 0; |
184 |
|
|
185 |
|
for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++) |
186 |
|
if (!SHIFT_IS_DISABLED (shiftp, i)) |
187 |
|
{ |
188 |
|
/* if this state has a shift for the error token, don't use a |
189 |
|
default rule. */ |
190 |
|
if (SHIFT_IS_ERROR (shiftp, i)) |
191 |
|
nodefault = 1; |
192 |
|
SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i)); |
193 |
|
} |
194 |
|
|
195 |
|
if (errp) |
196 |
|
for (i = 0; i < errp->nerrs; i++) |
197 |
|
if (errp->errs[i]) |
198 |
|
SETBIT (shiftset, errp->errs[i]); |
199 |
|
|
200 |
|
if (state->nlookaheads == 1 && !nodefault) |
201 |
|
{ |
202 |
|
int k; |
203 |
|
int default_rule = LAruleno[state->lookaheadsp]; |
204 |
|
|
205 |
|
for (k = 0; k < tokensetsize; ++k) |
206 |
|
lookaheadset[k] = LA (state->lookaheadsp)[k] & shiftset[k]; |
207 |
|
|
208 |
|
for (i = 0; i < ntokens; i++) |
209 |
|
if (BITISSET (lookaheadset, i)) |
210 |
|
fprintf (out, _(" %-4s\t[reduce using rule %d (%s)]\n"), |
211 |
|
tags[i], default_rule, |
212 |
|
tags[rule_table[default_rule].lhs]); |
213 |
|
|
214 |
|
fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"), |
215 |
|
default_rule, tags[rule_table[default_rule].lhs]); |
216 |
|
} |
217 |
|
else if (state->nlookaheads >= 1) |
218 |
|
{ |
219 |
|
int cmax = 0; |
220 |
|
int default_LA = -1; |
221 |
|
int default_rule = 0; |
222 |
|
|
223 |
|
if (!nodefault) |
224 |
|
for (i = 0; i < state->nlookaheads; ++i) |
225 |
|
{ |
226 |
|
int count = 0; |
227 |
|
int j, k; |
228 |
|
|
229 |
|
for (k = 0; k < tokensetsize; ++k) |
230 |
|
lookaheadset[k] = LA (state->lookaheadsp + i)[k] & ~shiftset[k]; |
231 |
|
|
232 |
|
for (j = 0; j < ntokens; j++) |
233 |
|
if (BITISSET (lookaheadset, j)) |
234 |
|
count++; |
235 |
|
|
236 |
|
if (count > cmax) |
237 |
|
{ |
238 |
|
cmax = count; |
239 |
|
default_LA = state->lookaheadsp + i; |
240 |
|
default_rule = LAruleno[state->lookaheadsp + i]; |
241 |
|
} |
242 |
|
|
243 |
|
for (k = 0; k < tokensetsize; ++k) |
244 |
|
shiftset[k] |= lookaheadset[k]; |
245 |
|
} |
246 |
|
|
247 |
|
for (i = 0; i < tokensetsize; i++) |
248 |
|
shiftset[i] = 0; |
249 |
|
|
250 |
|
for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++) |
251 |
|
if (!SHIFT_IS_DISABLED (shiftp, i)) |
252 |
|
SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i)); |
253 |
|
|
254 |
|
for (i = 0; i < ntokens; i++) |
255 |
|
{ |
256 |
|
int j; |
257 |
|
int defaulted = 0; |
258 |
|
int count = BITISSET (shiftset, i); |
259 |
|
|
260 |
|
for (j = 0; j < state->nlookaheads; ++j) |
261 |
|
{ |
262 |
|
if (BITISSET (LA (state->lookaheadsp + j), i)) |
263 |
|
{ |
264 |
|
if (count == 0) |
265 |
|
{ |
266 |
|
if (state->lookaheadsp + j != default_LA) |
267 |
|
fprintf (out, |
268 |
|
_(" %-4s\treduce using rule %d (%s)\n"), |
269 |
|
tags[i], |
270 |
|
LAruleno[state->lookaheadsp + j], |
271 |
|
tags[rule_table[LAruleno[state->lookaheadsp + j]].lhs]); |
272 |
|
else |
273 |
|
defaulted = 1; |
274 |
|
|
275 |
|
count++; |
276 |
|
} |
277 |
|
else |
278 |
|
{ |
279 |
|
if (defaulted) |
280 |
|
fprintf (out, |
281 |
|
_(" %-4s\treduce using rule %d (%s)\n"), |
282 |
|
tags[i], |
283 |
|
LAruleno[default_LA], |
284 |
|
tags[rule_table[LAruleno[default_LA]].lhs]); |
285 |
|
defaulted = 0; |
286 |
|
fprintf (out, |
287 |
|
_(" %-4s\t[reduce using rule %d (%s)]\n"), |
288 |
|
tags[i], |
289 |
|
LAruleno[state->lookaheadsp + j], |
290 |
|
tags[rule_table[LAruleno[state->lookaheadsp + j]].lhs]); |
291 |
|
} |
292 |
|
} |
293 |
|
} |
294 |
|
} |
295 |
|
|
296 |
|
if (default_LA >= 0) |
297 |
|
fprintf (out, _(" $default\treduce using rule %d (%s)\n"), |
298 |
|
default_rule, tags[rule_table[default_rule].lhs]); |
299 |
|
} |
300 |
|
} |
301 |
|
|
302 |
|
|
303 |
|
static void |
304 |
|
print_actions (FILE *out, state_t *state) |
305 |
|
{ |
306 |
|
reductions *redp = state->reductions; |
307 |
|
shifts *shiftp = state->shifts; |
308 |
|
|
309 |
|
if (!shiftp->nshifts && !redp) |
310 |
|
{ |
311 |
|
if (final_state == state->number) |
312 |
|
fprintf (out, _(" $default\taccept\n")); |
313 |
|
else |
314 |
|
fprintf (out, _(" NO ACTIONS\n")); |
315 |
|
return; |
316 |
|
} |
317 |
|
|
318 |
|
print_shifts (out, state); |
319 |
|
print_errs (out, state); |
320 |
|
if (redp) |
321 |
|
print_reductions (out, state); |
322 |
|
print_gotos (out, state); |
323 |
|
} |
324 |
|
|
325 |
|
static void |
326 |
print_state (FILE *out, state_t *state) |
print_state (FILE *out, state_t *state) |
327 |
{ |
{ |
328 |
fprintf (out, _("state %d"), state->number); |
fprintf (out, _("state %d"), state->number); |
480 |
|
|
481 |
print_grammar (out); |
print_grammar (out); |
482 |
|
|
483 |
/* New experimental feature: output all the items of a state, |
/* New experimental feature: output all the items of a state, not |
484 |
not only its kernel. Requires to run closure, which need |
only its kernel. Requires to run closure, which need memory |
485 |
memory allocation/deallocation. */ |
allocation/deallocation. */ |
486 |
if (trace_flag) |
if (trace_flag) |
487 |
new_closure (nitems); |
new_closure (nitems); |
488 |
|
/* Storage for print_reductions. */ |
489 |
|
shiftset = XCALLOC (unsigned, tokensetsize); |
490 |
|
lookaheadset = XCALLOC (unsigned, tokensetsize); |
491 |
for (i = 0; i < nstates; i++) |
for (i = 0; i < nstates; i++) |
492 |
print_state (out, state_table[i]); |
print_state (out, state_table[i]); |
493 |
|
free (shiftset); |
494 |
|
free (lookaheadset); |
495 |
if (trace_flag) |
if (trace_flag) |
496 |
free_closure (); |
free_closure (); |
497 |
|
|