17 extern action *parse_actions();
18 extern action *get_shifts();
19 extern action *add_reductions();
20 extern action *add_reduce();
27 parser = NEW2(nstates, action *);
28 for (i = 0; i < nstates; i++)
29 parser[i] = parse_actions(i);
34 if (SRtotal + RRtotal > 0) total_conflicts();
40 parse_actions(stateno)
43 register action *actions;
45 actions = get_shifts(stateno);
46 actions = add_reductions(stateno, actions);
55 register action *actions, *temp;
57 register short *to_state;
62 sp = shift_table[stateno];
66 for (i = sp->nshifts - 1; i >= 0; i--)
69 symbol = accessing_symbol[k];
74 temp->symbol = symbol;
76 temp->prec = symbol_prec[symbol];
77 temp->action_code = SHIFT;
78 temp->assoc = symbol_assoc[symbol];
87 add_reductions(stateno, actions)
89 register action *actions;
91 register int i, j, m, n;
92 register int ruleno, tokensetsize;
93 register unsigned *rowp;
95 tokensetsize = WORDSIZE(ntokens);
96 m = lookaheads[stateno];
97 n = lookaheads[stateno + 1];
98 for (i = m; i < n; i++)
100 ruleno = LAruleno[i];
101 rowp = LA + i * tokensetsize;
102 for (j = ntokens - 1; j >= 0; j--)
105 actions = add_reduce(actions, ruleno, j);
113 add_reduce(actions, ruleno, symbol)
114 register action *actions;
115 register int ruleno, symbol;
117 register action *temp, *prev, *next;
120 for (next = actions; next && next->symbol < symbol; next = next->next)
123 while (next && next->symbol == symbol && next->action_code == SHIFT)
129 while (next && next->symbol == symbol &&
130 next->action_code == REDUCE && next->number < ruleno)
138 temp->symbol = symbol;
139 temp->number = ruleno;
140 temp->prec = rprec[ruleno];
141 temp->action_code = REDUCE;
142 temp->assoc = rassoc[ruleno];
155 register int goal, i;
156 register short *to_state;
162 for (i = p->nshifts - 1; i >= 0; --i)
164 final_state = to_state[i];
165 if (accessing_symbol[final_state] == goal) break;
175 rules_used = (short *) MALLOC(nrules*sizeof(short));
176 if (rules_used == 0) no_space();
178 for (i = 0; i < nrules; ++i)
181 for (i = 0; i < nstates; ++i)
183 for (p = parser[i]; p; p = p->next)
185 if (p->action_code == REDUCE && p->suppressed == 0)
186 rules_used[p->number] = 1;
191 for (i = 3; i < nrules; ++i)
192 if (!rules_used[i]) ++nunused;
196 fprintf(stderr, "%s: 1 rule never reduced\n", myname);
198 fprintf(stderr, "%s: %d rules never reduced\n", myname, nunused);
206 register action *p, *pref;
210 SRconflicts = NEW2(nstates, short);
211 RRconflicts = NEW2(nstates, short);
212 for (i = 0; i < nstates; i++)
217 for (p = parser[i]; p; p = p->next)
219 if (p->symbol != symbol)
224 else if (i == final_state && symbol == 0)
229 else if (pref->action_code == SHIFT)
231 if (pref->prec > 0 && p->prec > 0)
233 if (pref->prec < p->prec)
235 pref->suppressed = 2;
238 else if (pref->prec > p->prec)
242 else if (pref->assoc == LEFT)
244 pref->suppressed = 2;
247 else if (pref->assoc == RIGHT)
253 pref->suppressed = 2;
271 SRconflicts[i] = SRcount;
272 RRconflicts[i] = RRcount;
279 fprintf(stderr, "%s: ", myname);
281 fprintf(stderr, "1 shift/reduce conflict");
282 else if (SRtotal > 1)
283 fprintf(stderr, "%d shift/reduce conflicts", SRtotal);
285 if (SRtotal && RRtotal)
286 fprintf(stderr, ", ");
289 fprintf(stderr, "1 reduce/reduce conflict");
290 else if (RRtotal > 1)
291 fprintf(stderr, "%d reduce/reduce conflicts", RRtotal);
293 fprintf(stderr, ".\n");
298 sole_reduction(stateno)
301 register int count, ruleno;
306 for (p = parser[stateno]; p; p = p->next)
308 if (p->action_code == SHIFT && p->suppressed == 0)
310 else if (p->action_code == REDUCE && p->suppressed == 0)
312 if (ruleno > 0 && p->number != ruleno)
330 defred = NEW2(nstates, short);
331 for (i = 0; i < nstates; i++)
332 defred[i] = sole_reduction(i);
352 for (i = 0; i < nstates; i++)
353 free_action_row(parser[i]);