1 /***********************************************************************/
5 /* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
7 /* Copyright 1996 Institut National de Recherche en Informatique et */
8 /* en Automatique. All rights reserved. This file is distributed */
9 /* under the terms of the Q Public License version 1.0. */
11 /***********************************************************************/
13 /* Based on public-domain code from Berkeley Yacc */
15 /* $Id: mkpar.c 3573 2001-07-12 12:54:24Z doligez $ */
33 void find_final_state (void);
34 void remove_conflicts (void);
35 void unused_rules (void);
36 void total_conflicts (void);
39 void make_parser(void)
43 parser = NEW2(nstates, action *);
44 for (i = 0; i < nstates; i++)
45 parser[i] = parse_actions(i);
50 if (SRtotal + RRtotal > 0) total_conflicts();
56 parse_actions(register int stateno)
58 register action *actions;
60 actions = get_shifts(stateno);
61 actions = add_reductions(stateno, actions);
67 get_shifts(int stateno)
69 register action *actions, *temp;
71 register short *to_state;
76 sp = shift_table[stateno];
80 for (i = sp->nshifts - 1; i >= 0; i--)
83 symbol = accessing_symbol[k];
88 temp->symbol = symbol;
90 temp->prec = symbol_prec[symbol];
91 temp->action_code = SHIFT;
92 temp->assoc = symbol_assoc[symbol];
101 add_reductions(int stateno, register action *actions)
103 register int i, j, m, n;
104 register int ruleno, tokensetsize;
105 register unsigned *rowp;
107 tokensetsize = WORDSIZE(ntokens);
108 m = lookaheads[stateno];
109 n = lookaheads[stateno + 1];
110 for (i = m; i < n; i++)
112 ruleno = LAruleno[i];
113 rowp = LA + i * tokensetsize;
114 for (j = ntokens - 1; j >= 0; j--)
117 actions = add_reduce(actions, ruleno, j);
125 add_reduce(register action *actions, register int ruleno, register int symbol)
127 register action *temp, *prev, *next;
130 for (next = actions; next && next->symbol < symbol; next = next->next)
133 while (next && next->symbol == symbol && next->action_code == SHIFT)
139 while (next && next->symbol == symbol &&
140 next->action_code == REDUCE && next->number < ruleno)
148 temp->symbol = symbol;
149 temp->number = ruleno;
150 temp->prec = rprec[ruleno];
151 temp->action_code = REDUCE;
152 temp->assoc = rassoc[ruleno];
163 void find_final_state(void)
165 register int goal, i;
166 register short *to_state;
172 for (i = p->nshifts - 1; i >= 0; --i)
174 final_state = to_state[i];
175 if (accessing_symbol[final_state] == goal) break;
180 void unused_rules(void)
185 rules_used = (short *) MALLOC(nrules*sizeof(short));
186 if (rules_used == 0) no_space();
188 for (i = 0; i < nrules; ++i)
191 for (i = 0; i < nstates; ++i)
193 for (p = parser[i]; p; p = p->next)
195 if (p->action_code == REDUCE && p->suppressed == 0)
196 rules_used[p->number] = 1;
201 for (i = 3; i < nrules; ++i)
202 if (!rules_used[i]) ++nunused;
206 fprintf(stderr, "1 rule never reduced\n");
208 fprintf(stderr, "%d rules never reduced\n", nunused);
213 void remove_conflicts(void)
217 register action *p, *pref;
221 SRconflicts = NEW2(nstates, short);
222 RRconflicts = NEW2(nstates, short);
224 for (i = 0; i < nstates; i++)
229 for (p = parser[i]; p; p = p->next)
231 if (p->symbol != symbol)
236 else if (i == final_state && symbol == 0)
241 else if (pref->action_code == SHIFT)
243 if (pref->prec > 0 && p->prec > 0)
245 if (pref->prec < p->prec)
247 pref->suppressed = 2;
250 else if (pref->prec > p->prec)
254 else if (pref->assoc == LEFT)
256 pref->suppressed = 2;
259 else if (pref->assoc == RIGHT)
265 pref->suppressed = 2;
283 SRconflicts[i] = SRcount;
284 RRconflicts[i] = RRcount;
289 void total_conflicts(void)
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, ", ");
300 fprintf(stderr, "1 reduce/reduce conflict");
301 else if (RRtotal > 1)
302 fprintf(stderr, "%d reduce/reduce conflicts", RRtotal);
304 fprintf(stderr, ".\n");
309 sole_reduction(int stateno)
311 register int count, ruleno;
316 for (p = parser[stateno]; p; p = p->next)
318 if (p->action_code == SHIFT && p->suppressed == 0)
320 else if (p->action_code == REDUCE && p->suppressed == 0)
322 if (ruleno > 0 && p->number != ruleno)
340 defred = NEW2(nstates, short);
341 for (i = 0; i < nstates; i++)
342 defred[i] = sole_reduction(i);
345 void free_action_row(register action *p)
357 void free_parser(void)
361 for (i = 0; i < nstates; i++)
362 free_action_row(parser[i]);