Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / byacc / dist / closure.c
blob785fe07c4f0acff5eef5df5032acc72f630ec753
1 /* $NetBSD$ */
2 /* Id: closure.c,v 1.7 2009/10/27 09:30:14 tom Exp */
4 #include "defs.h"
6 #include <sys/cdefs.h>
7 __RCSID("$NetBSD: closure.c,v 1.8 2006/05/24 18:01:43 christos Exp $");
9 Value_t *itemset;
10 Value_t *itemsetend;
11 unsigned *ruleset;
13 static unsigned *first_derives;
14 static unsigned *EFF;
16 static void
17 set_EFF(void)
19 unsigned *row;
20 int symbol;
21 short *sp;
22 int rowsize;
23 int i;
24 int rule;
26 rowsize = WORDSIZE(nvars);
27 EFF = NEW2(nvars * rowsize, unsigned);
29 row = EFF;
30 for (i = start_symbol; i < nsyms; i++)
32 sp = derives[i];
33 for (rule = *sp; rule > 0; rule = *++sp)
35 symbol = ritem[rrhs[rule]];
36 if (ISVAR(symbol))
38 symbol -= start_symbol;
39 SETBIT(row, symbol);
42 row += rowsize;
45 reflexive_transitive_closure(EFF, nvars);
47 #ifdef DEBUG
48 print_EFF();
49 #endif
52 void
53 set_first_derives(void)
55 unsigned *rrow;
56 unsigned *vrow;
57 int j;
58 unsigned k;
59 unsigned cword = 0;
60 short *rp;
62 int rule;
63 int i;
64 int rulesetsize;
65 int varsetsize;
67 rulesetsize = WORDSIZE(nrules);
68 varsetsize = WORDSIZE(nvars);
69 first_derives = NEW2(nvars * rulesetsize, unsigned) - ntokens * rulesetsize;
71 set_EFF();
73 rrow = first_derives + ntokens * rulesetsize;
74 for (i = start_symbol; i < nsyms; i++)
76 vrow = EFF + ((i - ntokens) * varsetsize);
77 k = BITS_PER_WORD;
78 for (j = start_symbol; j < nsyms; k++, j++)
80 if (k >= BITS_PER_WORD)
82 cword = *vrow++;
83 k = 0;
86 if (cword & (1 << k))
88 rp = derives[j];
89 while ((rule = *rp++) >= 0)
91 SETBIT(rrow, rule);
96 vrow += varsetsize;
97 rrow += rulesetsize;
100 #ifdef DEBUG
101 print_first_derives();
102 #endif
104 FREE(EFF);
107 void
108 closure(short *nucleus, int n)
110 unsigned ruleno;
111 unsigned word;
112 unsigned i;
113 Value_t *csp;
114 unsigned *dsp;
115 unsigned *rsp;
116 int rulesetsize;
118 Value_t *csend;
119 unsigned *rsend;
120 int symbol;
121 Value_t itemno;
123 rulesetsize = WORDSIZE(nrules);
124 rsp = ruleset;
125 rsend = ruleset + rulesetsize;
126 for (rsp = ruleset; rsp < rsend; rsp++)
127 *rsp = 0;
129 csend = nucleus + n;
130 for (csp = nucleus; csp < csend; ++csp)
132 symbol = ritem[*csp];
133 if (ISVAR(symbol))
135 dsp = first_derives + symbol * rulesetsize;
136 rsp = ruleset;
137 while (rsp < rsend)
138 *rsp++ |= *dsp++;
142 ruleno = 0;
143 itemsetend = itemset;
144 csp = nucleus;
145 for (rsp = ruleset; rsp < rsend; ++rsp)
147 word = *rsp;
148 if (word)
150 for (i = 0; i < BITS_PER_WORD; ++i)
152 if (word & (1 << i))
154 itemno = rrhs[ruleno + i];
155 while (csp < csend && *csp < itemno)
156 *itemsetend++ = *csp++;
157 *itemsetend++ = itemno;
158 while (csp < csend && *csp == itemno)
159 ++csp;
163 ruleno += BITS_PER_WORD;
166 while (csp < csend)
167 *itemsetend++ = *csp++;
169 #ifdef DEBUG
170 print_closure(n);
171 #endif
174 void
175 finalize_closure(void)
177 FREE(itemset);
178 FREE(ruleset);
179 FREE(first_derives + ntokens * WORDSIZE(nrules));
182 #ifdef DEBUG
184 void
185 print_closure(int n)
187 short *isp;
189 printf("\n\nn = %d\n\n", n);
190 for (isp = itemset; isp < itemsetend; isp++)
191 printf(" %d\n", *isp);
194 void
195 print_EFF(void)
197 int i, j;
198 unsigned *rowp;
199 unsigned word;
200 unsigned k;
202 printf("\n\nEpsilon Free Firsts\n");
204 for (i = start_symbol; i < nsyms; i++)
206 printf("\n%s", symbol_name[i]);
207 rowp = EFF + ((i - start_symbol) * WORDSIZE(nvars));
208 word = *rowp++;
210 k = BITS_PER_WORD;
211 for (j = 0; j < nvars; k++, j++)
213 if (k >= BITS_PER_WORD)
215 word = *rowp++;
216 k = 0;
219 if (word & (1 << k))
220 printf(" %s", symbol_name[start_symbol + j]);
225 void
226 print_first_derives(void)
228 int i;
229 int j;
230 unsigned *rp;
231 unsigned cword = 0;
232 unsigned k;
234 printf("\n\n\nFirst Derives\n");
236 for (i = start_symbol; i < nsyms; i++)
238 printf("\n%s derives\n", symbol_name[i]);
239 rp = first_derives + i * WORDSIZE(nrules);
240 k = BITS_PER_WORD;
241 for (j = 0; j <= nrules; k++, j++)
243 if (k >= BITS_PER_WORD)
245 cword = *rp++;
246 k = 0;
249 if (cword & (1 << k))
250 printf(" %d\n", j);
254 fflush(stdout);
257 #endif