2 * Copyright (c) 1989 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static char sccsid[] = "@(#)mkpar.c 5.3 (Berkeley) 1/20/91";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
62 static action *add_reduce(action *, int, int);
63 static action *add_reductions(int, action *);
64 static void defreds(void);
65 static void find_final_state(void);
66 static void free_action_row(action *);
67 static action *get_shifts(int);
68 static action *parse_actions(int);
69 static void remove_conflicts(void);
70 static int sole_reduction(int);
71 static void total_conflicts(void);
72 static void unused_rules(void);
80 parser = NEW2(nstates, action *);
81 for (i = 0; i < nstates; i++)
82 parser[i] = parse_actions(i);
87 if (SRtotal + RRtotal > 0) total_conflicts();
93 parse_actions(stateno)
98 actions = get_shifts(stateno);
99 actions = add_reductions(stateno, actions);
108 action *actions, *temp;
115 sp = shift_table[stateno];
119 for (i = sp->nshifts - 1; i >= 0; i--)
122 symbol = accessing_symbol[k];
126 temp->next = actions;
127 temp->symbol = symbol;
129 temp->prec = symbol_prec[symbol];
130 temp->action_code = SHIFT;
131 temp->assoc = symbol_assoc[symbol];
140 add_reductions(stateno, actions)
145 int ruleno, tokensetsize;
148 tokensetsize = WORDSIZE(ntokens);
149 m = lookaheads[stateno];
150 n = lookaheads[stateno + 1];
151 for (i = m; i < n; i++)
153 ruleno = LAruleno[i];
154 rowp = LA + i * tokensetsize;
155 for (j = ntokens - 1; j >= 0; j--)
158 actions = add_reduce(actions, ruleno, j);
166 add_reduce(actions, ruleno, symbol)
170 action *temp, *prev, *next;
173 for (next = actions; next && next->symbol < symbol; next = next->next)
176 while (next && next->symbol == symbol && next->action_code == SHIFT)
182 while (next && next->symbol == symbol &&
183 next->action_code == REDUCE && next->number < ruleno)
191 temp->symbol = symbol;
192 temp->number = ruleno;
193 temp->prec = rprec[ruleno];
194 temp->action_code = REDUCE;
195 temp->assoc = rassoc[ruleno];
216 for (i = p->nshifts - 1; i >= 0; --i)
218 final_state = tostate[i];
219 if (accessing_symbol[final_state] == goal) break;
230 rules_used = (short *) MALLOC(nrules*sizeof(short));
231 if (rules_used == 0) no_space();
233 for (i = 0; i < nrules; ++i)
236 for (i = 0; i < nstates; ++i)
238 for (p = parser[i]; p; p = p->next)
240 if (p->action_code == REDUCE && p->suppressed == 0)
241 rules_used[p->number] = 1;
246 for (i = 3; i < nrules; ++i)
247 if (!rules_used[i]) ++nunused;
251 warnx("1 rule never reduced");
253 warnx("%d rules never reduced", nunused);
268 SRconflicts = NEW2(nstates, short);
269 RRconflicts = NEW2(nstates, short);
270 for (i = 0; i < nstates; i++)
275 for (p = parser[i]; p; p = p->next)
277 if (p->symbol != symbol)
282 else if (i == final_state && symbol == 0)
287 else if (pref->action_code == SHIFT)
289 if (pref->prec > 0 && p->prec > 0)
291 if (pref->prec < p->prec)
293 pref->suppressed = 2;
296 else if (pref->prec > p->prec)
300 else if (pref->assoc == LEFT)
302 pref->suppressed = 2;
305 else if (pref->assoc == RIGHT)
311 pref->suppressed = 2;
329 SRconflicts[i] = SRcount;
330 RRconflicts[i] = RRcount;
338 /* Warn if s/r != expect or if any r/r */
339 if ((SRtotal != SRexpect) || RRtotal)
342 warnx("1 shift/reduce conflict");
343 else if (SRtotal > 1)
344 warnx("%d shift/reduce conflicts", SRtotal);
348 warnx("1 reduce/reduce conflict");
349 else if (RRtotal > 1)
350 warnx("%d reduce/reduce conflicts", RRtotal);
355 sole_reduction(stateno)
363 for (p = parser[stateno]; p; p = p->next)
365 if (p->action_code == SHIFT && p->suppressed == 0)
367 else if (p->action_code == REDUCE && p->suppressed == 0)
369 if (ruleno > 0 && p->number != ruleno)
388 defred = NEW2(nstates, short);
389 for (i = 0; i < nstates; i++)
390 defred[i] = sole_reduction(i);
412 for (i = 0; i < nstates; i++)
413 free_action_row(parser[i]);