Remove building with NOCRYPTO option
[minix3.git] / external / bsd / byacc / dist / closure.c
blobb7aae8f311f697ba23dc429c8a9dbf4e2f2a6f36
1 /* $NetBSD: closure.c,v 1.8 2015/01/03 23:22:52 christos Exp $ */
3 /* Id: closure.c,v 1.11 2014/09/18 00:40:07 tom Exp */
5 #include "defs.h"
7 #include <sys/cdefs.h>
8 __RCSID("$NetBSD: closure.c,v 1.8 2015/01/03 23:22:52 christos Exp $");
10 Value_t *itemset;
11 Value_t *itemsetend;
12 unsigned *ruleset;
14 static unsigned *first_base;
15 static unsigned *first_derives;
16 static unsigned *EFF;
18 #ifdef DEBUG
19 static void print_closure(int);
20 static void print_EFF(void);
21 static void print_first_derives(void);
22 #endif
24 static void
25 set_EFF(void)
27 unsigned *row;
28 int symbol;
29 Value_t *sp;
30 int rowsize;
31 int i;
32 int rule;
34 rowsize = WORDSIZE(nvars);
35 EFF = NEW2(nvars * rowsize, unsigned);
37 row = EFF;
38 for (i = start_symbol; i < nsyms; i++)
40 sp = derives[i];
41 for (rule = *sp; rule > 0; rule = *++sp)
43 symbol = ritem[rrhs[rule]];
44 if (ISVAR(symbol))
46 symbol -= start_symbol;
47 SETBIT(row, symbol);
50 row += rowsize;
53 reflexive_transitive_closure(EFF, nvars);
55 #ifdef DEBUG
56 print_EFF();
57 #endif
60 void
61 set_first_derives(void)
63 unsigned *rrow;
64 unsigned *vrow;
65 int j;
66 unsigned k;
67 unsigned cword = 0;
68 Value_t *rp;
70 int rule;
71 int i;
72 int rulesetsize;
73 int varsetsize;
75 rulesetsize = WORDSIZE(nrules);
76 varsetsize = WORDSIZE(nvars);
77 first_base = NEW2(nvars * rulesetsize, unsigned);
78 first_derives = first_base - ntokens * rulesetsize;
80 set_EFF();
82 rrow = first_derives + ntokens * rulesetsize;
83 for (i = start_symbol; i < nsyms; i++)
85 vrow = EFF + ((i - ntokens) * varsetsize);
86 k = BITS_PER_WORD;
87 for (j = start_symbol; j < nsyms; k++, j++)
89 if (k >= BITS_PER_WORD)
91 cword = *vrow++;
92 k = 0;
95 if (cword & (unsigned)(1 << k))
97 rp = derives[j];
98 while ((rule = *rp++) >= 0)
100 SETBIT(rrow, rule);
105 rrow += rulesetsize;
108 #ifdef DEBUG
109 print_first_derives();
110 #endif
112 FREE(EFF);
115 void
116 closure(Value_t *nucleus, int n)
118 unsigned ruleno;
119 unsigned word;
120 unsigned i;
121 Value_t *csp;
122 unsigned *dsp;
123 unsigned *rsp;
124 int rulesetsize;
126 Value_t *csend;
127 unsigned *rsend;
128 int symbol;
129 Value_t itemno;
131 rulesetsize = WORDSIZE(nrules);
132 rsend = ruleset + rulesetsize;
133 for (rsp = ruleset; rsp < rsend; rsp++)
134 *rsp = 0;
136 csend = nucleus + n;
137 for (csp = nucleus; csp < csend; ++csp)
139 symbol = ritem[*csp];
140 if (ISVAR(symbol))
142 dsp = first_derives + symbol * rulesetsize;
143 rsp = ruleset;
144 while (rsp < rsend)
145 *rsp++ |= *dsp++;
149 ruleno = 0;
150 itemsetend = itemset;
151 csp = nucleus;
152 for (rsp = ruleset; rsp < rsend; ++rsp)
154 word = *rsp;
155 if (word)
157 for (i = 0; i < BITS_PER_WORD; ++i)
159 if (word & (unsigned)(1 << i))
161 itemno = rrhs[ruleno + i];
162 while (csp < csend && *csp < itemno)
163 *itemsetend++ = *csp++;
164 *itemsetend++ = itemno;
165 while (csp < csend && *csp == itemno)
166 ++csp;
170 ruleno += BITS_PER_WORD;
173 while (csp < csend)
174 *itemsetend++ = *csp++;
176 #ifdef DEBUG
177 print_closure(n);
178 #endif
181 void
182 finalize_closure(void)
184 FREE(itemset);
185 FREE(ruleset);
186 FREE(first_base);
189 #ifdef DEBUG
191 static void
192 print_closure(int n)
194 Value_t *isp;
196 printf("\n\nn = %d\n\n", n);
197 for (isp = itemset; isp < itemsetend; isp++)
198 printf(" %d\n", *isp);
201 static void
202 print_EFF(void)
204 int i, j;
205 unsigned *rowp;
206 unsigned word;
207 unsigned k;
209 printf("\n\nEpsilon Free Firsts\n");
211 for (i = start_symbol; i < nsyms; i++)
213 printf("\n%s", symbol_name[i]);
214 rowp = EFF + ((i - start_symbol) * WORDSIZE(nvars));
215 word = *rowp++;
217 k = BITS_PER_WORD;
218 for (j = 0; j < nvars; k++, j++)
220 if (k >= BITS_PER_WORD)
222 word = *rowp++;
223 k = 0;
226 if (word & (1 << k))
227 printf(" %s", symbol_name[start_symbol + j]);
232 static void
233 print_first_derives(void)
235 int i;
236 int j;
237 unsigned *rp;
238 unsigned cword = 0;
239 unsigned k;
241 printf("\n\n\nFirst Derives\n");
243 for (i = start_symbol; i < nsyms; i++)
245 printf("\n%s derives\n", symbol_name[i]);
246 rp = first_derives + i * WORDSIZE(nrules);
247 k = BITS_PER_WORD;
248 for (j = 0; j <= nrules; k++, j++)
250 if (k >= BITS_PER_WORD)
252 cword = *rp++;
253 k = 0;
256 if (cword & (1 << k))
257 printf(" %d\n", j);
261 fflush(stdout);
264 #endif