loader: remove shouting from ORB's variable name
[hvf.git] / build / byacc / mkpar.c
blobf9f2b5c2e85da0a06cdb1f81338d6e45de0cead0
1 /* $Id: mkpar.c,v 1.11 2010/06/09 08:53:17 tom Exp $ */
3 #include "defs.h"
5 static action *add_reduce(action *actions, int ruleno, int symbol);
6 static action *add_reductions(int stateno, action *actions);
7 static action *get_shifts(int stateno);
8 static action *parse_actions(int stateno);
9 static int sole_reduction(int stateno);
10 static void defreds(void);
11 static void find_final_state(void);
12 static void free_action_row(action *p);
13 static void remove_conflicts(void);
14 static void total_conflicts(void);
15 static void unused_rules(void);
17 action **parser;
19 int SRexpect;
20 int RRexpect;
22 int SRtotal;
23 int RRtotal;
25 Value_t *SRconflicts;
26 Value_t *RRconflicts;
27 Value_t *defred;
28 Value_t *rules_used;
29 Value_t nunused;
30 Value_t final_state;
32 static Value_t SRcount;
33 static Value_t RRcount;
35 void
36 make_parser(void)
38 int i;
40 parser = NEW2(nstates, action *);
41 for (i = 0; i < nstates; i++)
42 parser[i] = parse_actions(i);
44 find_final_state();
45 remove_conflicts();
46 unused_rules();
47 if (SRtotal + RRtotal > 0)
48 total_conflicts();
49 defreds();
52 static action *
53 parse_actions(int stateno)
55 action *actions;
57 actions = get_shifts(stateno);
58 actions = add_reductions(stateno, actions);
59 return (actions);
62 static action *
63 get_shifts(int stateno)
65 action *actions, *temp;
66 shifts *sp;
67 Value_t *to_state2;
68 Value_t i, k;
69 Value_t symbol;
71 actions = 0;
72 sp = shift_table[stateno];
73 if (sp)
75 to_state2 = sp->shift;
76 for (i = (Value_t) (sp->nshifts - 1); i >= 0; i--)
78 k = to_state2[i];
79 symbol = accessing_symbol[k];
80 if (ISTOKEN(symbol))
82 temp = NEW(action);
83 temp->next = actions;
84 temp->symbol = symbol;
85 temp->number = k;
86 temp->prec = symbol_prec[symbol];
87 temp->action_code = SHIFT;
88 temp->assoc = symbol_assoc[symbol];
89 actions = temp;
93 return (actions);
96 static action *
97 add_reductions(int stateno, action *actions)
99 int i, j, m, n;
100 int ruleno, tokensetsize;
101 unsigned *rowp;
103 tokensetsize = WORDSIZE(ntokens);
104 m = lookaheads[stateno];
105 n = lookaheads[stateno + 1];
106 for (i = m; i < n; i++)
108 ruleno = LAruleno[i];
109 rowp = LA + i * tokensetsize;
110 for (j = ntokens - 1; j >= 0; j--)
112 if (BIT(rowp, j))
113 actions = add_reduce(actions, ruleno, j);
116 return (actions);
119 static action *
120 add_reduce(action *actions,
121 int ruleno,
122 int symbol)
124 action *temp, *prev, *next;
126 prev = 0;
127 for (next = actions; next && next->symbol < symbol; next = next->next)
128 prev = next;
130 while (next && next->symbol == symbol && next->action_code == SHIFT)
132 prev = next;
133 next = next->next;
136 while (next && next->symbol == symbol &&
137 next->action_code == REDUCE && next->number < ruleno)
139 prev = next;
140 next = next->next;
143 temp = NEW(action);
144 temp->next = next;
145 temp->symbol = (Value_t) symbol;
146 temp->number = (Value_t) ruleno;
147 temp->prec = rprec[ruleno];
148 temp->action_code = REDUCE;
149 temp->assoc = rassoc[ruleno];
151 if (prev)
152 prev->next = temp;
153 else
154 actions = temp;
156 return (actions);
159 static void
160 find_final_state(void)
162 int goal, i;
163 Value_t *to_state2;
164 shifts *p;
166 p = shift_table[0];
167 to_state2 = p->shift;
168 goal = ritem[1];
169 for (i = p->nshifts - 1; i >= 0; --i)
171 final_state = to_state2[i];
172 if (accessing_symbol[final_state] == goal)
173 break;
177 static void
178 unused_rules(void)
180 int i;
181 action *p;
183 rules_used = (Value_t *) MALLOC((unsigned)nrules * sizeof(Value_t));
184 NO_SPACE(rules_used);
186 for (i = 0; i < nrules; ++i)
187 rules_used[i] = 0;
189 for (i = 0; i < nstates; ++i)
191 for (p = parser[i]; p; p = p->next)
193 if (p->action_code == REDUCE && p->suppressed == 0)
194 rules_used[p->number] = 1;
198 nunused = 0;
199 for (i = 3; i < nrules; ++i)
200 if (!rules_used[i])
201 ++nunused;
203 if (nunused)
205 if (nunused == 1)
206 fprintf(stderr, "%s: 1 rule never reduced\n", myname);
207 else
208 fprintf(stderr, "%s: %d rules never reduced\n", myname, nunused);
212 static void
213 remove_conflicts(void)
215 int i;
216 int symbol;
217 action *p, *pref = 0;
219 SRtotal = 0;
220 RRtotal = 0;
221 SRconflicts = NEW2(nstates, Value_t);
222 RRconflicts = NEW2(nstates, Value_t);
223 for (i = 0; i < nstates; i++)
225 SRcount = 0;
226 RRcount = 0;
227 symbol = -1;
228 for (p = parser[i]; p; p = p->next)
230 if (p->symbol != symbol)
232 pref = p;
233 symbol = p->symbol;
235 else if (i == final_state && symbol == 0)
237 SRcount++;
238 p->suppressed = 1;
240 else if (pref != 0 && pref->action_code == SHIFT)
242 if (pref->prec > 0 && p->prec > 0)
244 if (pref->prec < p->prec)
246 pref->suppressed = 2;
247 pref = p;
249 else if (pref->prec > p->prec)
251 p->suppressed = 2;
253 else if (pref->assoc == LEFT)
255 pref->suppressed = 2;
256 pref = p;
258 else if (pref->assoc == RIGHT)
260 p->suppressed = 2;
262 else
264 pref->suppressed = 2;
265 p->suppressed = 2;
268 else
270 SRcount++;
271 p->suppressed = 1;
274 else
276 RRcount++;
277 p->suppressed = 1;
280 SRtotal += SRcount;
281 RRtotal += RRcount;
282 SRconflicts[i] = SRcount;
283 RRconflicts[i] = RRcount;
287 static void
288 total_conflicts(void)
290 fprintf(stderr, "%s: ", myname);
291 if (SRtotal == 1)
292 fprintf(stderr, "1 shift/reduce conflict");
293 else if (SRtotal > 1)
294 fprintf(stderr, "%d shift/reduce conflicts", SRtotal);
296 if (SRtotal && RRtotal)
297 fprintf(stderr, ", ");
299 if (RRtotal == 1)
300 fprintf(stderr, "1 reduce/reduce conflict");
301 else if (RRtotal > 1)
302 fprintf(stderr, "%d reduce/reduce conflicts", RRtotal);
304 fprintf(stderr, ".\n");
306 if (SRexpect >= 0 && SRtotal != SRexpect)
308 fprintf(stderr, "%s: ", myname);
309 fprintf(stderr, "expected %d shift/reduce conflict%s.\n",
310 SRexpect, PLURAL(SRexpect));
311 exit_code = EXIT_FAILURE;
313 if (RRexpect >= 0 && RRtotal != RRexpect)
315 fprintf(stderr, "%s: ", myname);
316 fprintf(stderr, "expected %d reduce/reduce conflict%s.\n",
317 RRexpect, PLURAL(RRexpect));
318 exit_code = EXIT_FAILURE;
322 static int
323 sole_reduction(int stateno)
325 int count, ruleno;
326 action *p;
328 count = 0;
329 ruleno = 0;
330 for (p = parser[stateno]; p; p = p->next)
332 if (p->action_code == SHIFT && p->suppressed == 0)
333 return (0);
334 else if (p->action_code == REDUCE && p->suppressed == 0)
336 if (ruleno > 0 && p->number != ruleno)
337 return (0);
338 if (p->symbol != 1)
339 ++count;
340 ruleno = p->number;
344 if (count == 0)
345 return (0);
346 return (ruleno);
349 static void
350 defreds(void)
352 int i;
354 defred = NEW2(nstates, Value_t);
355 for (i = 0; i < nstates; i++)
356 defred[i] = (Value_t) sole_reduction(i);
359 static void
360 free_action_row(action *p)
362 action *q;
364 while (p)
366 q = p->next;
367 FREE(p);
368 p = q;
372 void
373 free_parser(void)
375 int i;
377 for (i = 0; i < nstates; i++)
378 free_action_row(parser[i]);
380 FREE(parser);
383 #ifdef NO_LEAKS
384 void
385 mkpar_leaks(void)
387 DO_FREE(defred);
388 DO_FREE(rules_used);
389 DO_FREE(SRconflicts);
390 DO_FREE(RRconflicts);
392 #endif