1 /* $Id: mkpar.c,v 1.17 2020/09/10 17:37:33 tom Exp $ */
5 #define NotSuppressed(p) ((p)->suppressed == 0)
8 #define MaySuppress(p) ((backtrack ? ((p)->suppressed <= 1) : (p)->suppressed == 0))
9 /* suppress the preferred action => enable backtracking */
10 #define StartBacktrack(p) if (backtrack && (p) != NULL && NotSuppressed(p)) (p)->suppressed = 1
12 #define MaySuppress(p) ((p)->suppressed == 0)
13 #define StartBacktrack(p) /*nothing */
16 static action *add_reduce(action *actions, int ruleno, int symbol);
17 static action *add_reductions(int stateno, action *actions);
18 static action *get_shifts(int stateno);
19 static action *parse_actions(int stateno);
20 static int sole_reduction(int stateno);
21 static void defreds(void);
22 static void find_final_state(void);
23 static void free_action_row(action *p);
24 static void remove_conflicts(void);
25 static void total_conflicts(void);
26 static void unused_rules(void);
43 static Value_t SRcount;
44 static Value_t RRcount;
51 parser = NEW2(nstates, action *);
52 for (i = 0; i < nstates; i++)
53 parser[i] = parse_actions(i);
58 if (SRtotal + RRtotal > 0)
64 parse_actions(int stateno)
68 actions = get_shifts(stateno);
69 actions = add_reductions(stateno, actions);
74 get_shifts(int stateno)
76 action *actions, *temp;
81 sp = shift_table[stateno];
86 to_state2 = sp->shift;
87 for (i = (Value_t)(sp->nshifts - 1); i >= 0; i--)
89 Value_t k = to_state2[i];
90 Value_t symbol = accessing_symbol[k];
96 temp->symbol = symbol;
98 temp->prec = symbol_prec[symbol];
99 temp->action_code = SHIFT;
100 temp->assoc = symbol_assoc[symbol];
109 add_reductions(int stateno, action *actions)
114 tokensetsize = WORDSIZE(ntokens);
115 m = lookaheads[stateno];
116 n = lookaheads[stateno + 1];
117 for (i = m; i < n; i++)
119 int ruleno = LAruleno[i];
120 unsigned *rowp = LA + i * tokensetsize;
122 for (j = ntokens - 1; j >= 0; j--)
125 actions = add_reduce(actions, ruleno, j);
132 add_reduce(action *actions,
136 action *temp, *prev, *next;
139 for (next = actions; next && next->symbol < symbol; next = next->next)
142 while (next && next->symbol == symbol && next->action_code == SHIFT)
148 while (next && next->symbol == symbol &&
149 next->action_code == REDUCE && next->number < ruleno)
157 temp->symbol = (Value_t)symbol;
158 temp->number = (Value_t)ruleno;
159 temp->prec = rprec[ruleno];
160 temp->action_code = REDUCE;
161 temp->assoc = rassoc[ruleno];
172 find_final_state(void)
177 if ((p = shift_table[0]) != 0)
182 to_state2 = p->shift;
183 for (i = p->nshifts - 1; i >= 0; --i)
185 final_state = to_state2[i];
186 if (accessing_symbol[final_state] == goal)
198 rules_used = TMALLOC(Value_t, nrules);
199 NO_SPACE(rules_used);
201 for (i = 0; i < nrules; ++i)
204 for (i = 0; i < nstates; ++i)
206 for (p = parser[i]; p; p = p->next)
208 if ((p->action_code == REDUCE) && MaySuppress(p))
209 rules_used[p->number] = 1;
214 for (i = 3; i < nrules; ++i)
221 fprintf(stderr, "%s: 1 rule never reduced\n", myname);
223 fprintf(stderr, "%s: %d rules never reduced\n", myname, nunused);
228 remove_conflicts(void)
231 action *p, *pref = 0;
235 SRconflicts = NEW2(nstates, Value_t);
236 RRconflicts = NEW2(nstates, Value_t);
237 for (i = 0; i < nstates; i++)
243 #if defined(YYBTYACC)
246 for (p = parser[i]; p; p = p->next)
248 if (p->symbol != symbol)
250 /* the first parse action for each symbol is the preferred action */
254 /* following conditions handle multiple, i.e., conflicting, parse actions */
255 else if (i == final_state && symbol == 0)
259 StartBacktrack(pref);
261 else if (pref != 0 && pref->action_code == SHIFT)
263 if (pref->prec > 0 && p->prec > 0)
265 if (pref->prec < p->prec)
267 pref->suppressed = 2;
270 else if (pref->prec > p->prec)
274 else if (pref->assoc == LEFT)
276 pref->suppressed = 2;
279 else if (pref->assoc == RIGHT)
285 pref->suppressed = 2;
293 StartBacktrack(pref);
300 StartBacktrack(pref);
305 SRconflicts[i] = SRcount;
306 RRconflicts[i] = RRcount;
311 total_conflicts(void)
313 fprintf(stderr, "%s: ", myname);
315 fprintf(stderr, "1 shift/reduce conflict");
316 else if (SRtotal > 1)
317 fprintf(stderr, "%d shift/reduce conflicts", SRtotal);
319 if (SRtotal && RRtotal)
320 fprintf(stderr, ", ");
323 fprintf(stderr, "1 reduce/reduce conflict");
324 else if (RRtotal > 1)
325 fprintf(stderr, "%d reduce/reduce conflicts", RRtotal);
327 fprintf(stderr, ".\n");
329 if (SRexpect >= 0 && SRtotal != SRexpect)
331 fprintf(stderr, "%s: ", myname);
332 fprintf(stderr, "expected %d shift/reduce conflict%s.\n",
333 SRexpect, PLURAL(SRexpect));
334 exit_code = EXIT_FAILURE;
336 if (RRexpect >= 0 && RRtotal != RRexpect)
338 fprintf(stderr, "%s: ", myname);
339 fprintf(stderr, "expected %d reduce/reduce conflict%s.\n",
340 RRexpect, PLURAL(RRexpect));
341 exit_code = EXIT_FAILURE;
346 sole_reduction(int stateno)
353 for (p = parser[stateno]; p; p = p->next)
355 if (p->action_code == SHIFT && MaySuppress(p))
357 else if ((p->action_code == REDUCE) && MaySuppress(p))
359 if (ruleno > 0 && p->number != ruleno)
377 defred = NEW2(nstates, Value_t);
378 for (i = 0; i < nstates; i++)
379 defred[i] = (Value_t)sole_reduction(i);
383 free_action_row(action *p)
400 for (i = 0; i < nstates; i++)
401 free_action_row(parser[i]);
412 DO_FREE(SRconflicts);
413 DO_FREE(RRconflicts);