sys/subr_rman: fix some issues
[dragonfly.git] / contrib / byacc / lr0.c
blob641f9f800748e6b6a8c2e2de3674ca368d5cb177
1 /* $Id: lr0.c,v 1.13 2012/05/26 00:40:47 tom Exp $ */
3 #include "defs.h"
5 static core *new_state(int symbol);
6 static Value_t get_state(int symbol);
7 static void allocate_itemsets(void);
8 static void allocate_storage(void);
9 static void append_states(void);
10 static void free_storage(void);
11 static void generate_states(void);
12 static void initialize_states(void);
13 static void new_itemsets(void);
14 static void save_reductions(void);
15 static void save_shifts(void);
16 static void set_derives(void);
17 static void set_nullable(void);
19 int nstates;
20 core *first_state;
21 shifts *first_shift;
22 reductions *first_reduction;
24 static core **state_set;
25 static core *this_state;
26 static core *last_state;
27 static shifts *last_shift;
28 static reductions *last_reduction;
30 static int nshifts;
31 static short *shift_symbol;
33 static Value_t *redset;
34 static Value_t *shiftset;
36 static Value_t **kernel_base;
37 static Value_t **kernel_end;
38 static Value_t *kernel_items;
40 static void
41 allocate_itemsets(void)
43 short *itemp;
44 short *item_end;
45 int symbol;
46 int i;
47 int count;
48 int max;
49 short *symbol_count;
51 count = 0;
52 symbol_count = NEW2(nsyms, short);
54 item_end = ritem + nitems;
55 for (itemp = ritem; itemp < item_end; itemp++)
57 symbol = *itemp;
58 if (symbol >= 0)
60 count++;
61 symbol_count[symbol]++;
65 kernel_base = NEW2(nsyms, short *);
66 kernel_items = NEW2(count, short);
68 count = 0;
69 max = 0;
70 for (i = 0; i < nsyms; i++)
72 kernel_base[i] = kernel_items + count;
73 count += symbol_count[i];
74 if (max < symbol_count[i])
75 max = symbol_count[i];
78 shift_symbol = symbol_count;
79 kernel_end = NEW2(nsyms, short *);
82 static void
83 allocate_storage(void)
85 allocate_itemsets();
86 shiftset = NEW2(nsyms, short);
87 redset = NEW2(nrules + 1, short);
88 state_set = NEW2(nitems, core *);
91 static void
92 append_states(void)
94 int i;
95 int j;
96 Value_t symbol;
98 #ifdef TRACE
99 fprintf(stderr, "Entering append_states()\n");
100 #endif
101 for (i = 1; i < nshifts; i++)
103 symbol = shift_symbol[i];
104 j = i;
105 while (j > 0 && shift_symbol[j - 1] > symbol)
107 shift_symbol[j] = shift_symbol[j - 1];
108 j--;
110 shift_symbol[j] = symbol;
113 for (i = 0; i < nshifts; i++)
115 symbol = shift_symbol[i];
116 shiftset[i] = get_state(symbol);
120 static void
121 free_storage(void)
123 FREE(shift_symbol);
124 FREE(redset);
125 FREE(shiftset);
126 FREE(kernel_base);
127 FREE(kernel_end);
128 FREE(kernel_items);
129 FREE(state_set);
132 static void
133 generate_states(void)
135 allocate_storage();
136 itemset = NEW2(nitems, short);
137 ruleset = NEW2(WORDSIZE(nrules), unsigned);
138 set_first_derives();
139 initialize_states();
141 while (this_state)
143 closure(this_state->items, this_state->nitems);
144 save_reductions();
145 new_itemsets();
146 append_states();
148 if (nshifts > 0)
149 save_shifts();
151 this_state = this_state->next;
154 free_storage();
157 static Value_t
158 get_state(int symbol)
160 int key;
161 short *isp1;
162 short *isp2;
163 short *iend;
164 core *sp;
165 int found;
166 int n;
168 #ifdef TRACE
169 fprintf(stderr, "Entering get_state(%d)\n", symbol);
170 #endif
172 isp1 = kernel_base[symbol];
173 iend = kernel_end[symbol];
174 n = (int)(iend - isp1);
176 key = *isp1;
177 assert(0 <= key && key < nitems);
178 sp = state_set[key];
179 if (sp)
181 found = 0;
182 while (!found)
184 if (sp->nitems == n)
186 found = 1;
187 isp1 = kernel_base[symbol];
188 isp2 = sp->items;
190 while (found && isp1 < iend)
192 if (*isp1++ != *isp2++)
193 found = 0;
197 if (!found)
199 if (sp->link)
201 sp = sp->link;
203 else
205 sp = sp->link = new_state(symbol);
206 found = 1;
211 else
213 state_set[key] = sp = new_state(symbol);
216 return (sp->number);
219 static void
220 initialize_states(void)
222 unsigned i;
223 short *start_derives;
224 core *p;
226 start_derives = derives[start_symbol];
227 for (i = 0; start_derives[i] >= 0; ++i)
228 continue;
230 p = (core *)MALLOC(sizeof(core) + i * sizeof(short));
231 NO_SPACE(p);
233 p->next = 0;
234 p->link = 0;
235 p->number = 0;
236 p->accessing_symbol = 0;
237 p->nitems = (Value_t) i;
239 for (i = 0; start_derives[i] >= 0; ++i)
240 p->items[i] = rrhs[start_derives[i]];
242 first_state = last_state = this_state = p;
243 nstates = 1;
246 static void
247 new_itemsets(void)
249 Value_t i;
250 int shiftcount;
251 short *isp;
252 short *ksp;
253 Value_t symbol;
255 for (i = 0; i < nsyms; i++)
256 kernel_end[i] = 0;
258 shiftcount = 0;
259 isp = itemset;
260 while (isp < itemsetend)
262 i = *isp++;
263 symbol = ritem[i];
264 if (symbol > 0)
266 ksp = kernel_end[symbol];
267 if (!ksp)
269 shift_symbol[shiftcount++] = symbol;
270 ksp = kernel_base[symbol];
273 *ksp++ = (Value_t) (i + 1);
274 kernel_end[symbol] = ksp;
278 nshifts = shiftcount;
281 static core *
282 new_state(int symbol)
284 unsigned n;
285 core *p;
286 short *isp1;
287 short *isp2;
288 short *iend;
290 #ifdef TRACE
291 fprintf(stderr, "Entering new_state(%d)\n", symbol);
292 #endif
294 if (nstates >= MAXSHORT)
295 fatal("too many states");
297 isp1 = kernel_base[symbol];
298 iend = kernel_end[symbol];
299 n = (unsigned)(iend - isp1);
301 p = (core *)allocate((sizeof(core) + (n - 1) * sizeof(short)));
302 p->accessing_symbol = (Value_t) symbol;
303 p->number = (Value_t) nstates;
304 p->nitems = (Value_t) n;
306 isp2 = p->items;
307 while (isp1 < iend)
308 *isp2++ = *isp1++;
310 last_state->next = p;
311 last_state = p;
313 nstates++;
315 return (p);
318 /* show_cores is used for debugging */
320 void
321 show_cores(void)
323 core *p;
324 int i, j, k, n;
325 int itemno;
327 k = 0;
328 for (p = first_state; p; ++k, p = p->next)
330 if (k)
331 printf("\n");
332 printf("state %d, number = %d, accessing symbol = %s\n",
333 k, p->number, symbol_name[p->accessing_symbol]);
334 n = p->nitems;
335 for (i = 0; i < n; ++i)
337 itemno = p->items[i];
338 printf("%4d ", itemno);
339 j = itemno;
340 while (ritem[j] >= 0)
341 ++j;
342 printf("%s :", symbol_name[rlhs[-ritem[j]]]);
343 j = rrhs[-ritem[j]];
344 while (j < itemno)
345 printf(" %s", symbol_name[ritem[j++]]);
346 printf(" .");
347 while (ritem[j] >= 0)
348 printf(" %s", symbol_name[ritem[j++]]);
349 printf("\n");
350 fflush(stdout);
355 /* show_ritems is used for debugging */
357 void
358 show_ritems(void)
360 int i;
362 for (i = 0; i < nitems; ++i)
363 printf("ritem[%d] = %d\n", i, ritem[i]);
366 /* show_rrhs is used for debugging */
367 void
368 show_rrhs(void)
370 int i;
372 for (i = 0; i < nrules; ++i)
373 printf("rrhs[%d] = %d\n", i, rrhs[i]);
376 /* show_shifts is used for debugging */
378 void
379 show_shifts(void)
381 shifts *p;
382 int i, j, k;
384 k = 0;
385 for (p = first_shift; p; ++k, p = p->next)
387 if (k)
388 printf("\n");
389 printf("shift %d, number = %d, nshifts = %d\n", k, p->number,
390 p->nshifts);
391 j = p->nshifts;
392 for (i = 0; i < j; ++i)
393 printf("\t%d\n", p->shift[i]);
397 static void
398 save_shifts(void)
400 shifts *p;
401 short *sp1;
402 short *sp2;
403 short *send;
405 p = (shifts *)allocate((sizeof(shifts) +
406 (unsigned)(nshifts - 1) * sizeof(short)));
408 p->number = this_state->number;
409 p->nshifts = (Value_t) nshifts;
411 sp1 = shiftset;
412 sp2 = p->shift;
413 send = shiftset + nshifts;
415 while (sp1 < send)
416 *sp2++ = *sp1++;
418 if (last_shift)
420 last_shift->next = p;
421 last_shift = p;
423 else
425 first_shift = p;
426 last_shift = p;
430 static void
431 save_reductions(void)
433 short *isp;
434 short *rp1;
435 short *rp2;
436 int item;
437 Value_t count;
438 reductions *p;
439 short *rend;
441 count = 0;
442 for (isp = itemset; isp < itemsetend; isp++)
444 item = ritem[*isp];
445 if (item < 0)
447 redset[count++] = (Value_t) - item;
451 if (count)
453 p = (reductions *)allocate((sizeof(reductions) +
454 (unsigned)(count - 1) *
455 sizeof(short)));
457 p->number = this_state->number;
458 p->nreds = count;
460 rp1 = redset;
461 rp2 = p->rules;
462 rend = rp1 + count;
464 while (rp1 < rend)
465 *rp2++ = *rp1++;
467 if (last_reduction)
469 last_reduction->next = p;
470 last_reduction = p;
472 else
474 first_reduction = p;
475 last_reduction = p;
480 static void
481 set_derives(void)
483 Value_t i, k;
484 int lhs;
485 short *rules;
487 derives = NEW2(nsyms, short *);
488 rules = NEW2(nvars + nrules, short);
490 k = 0;
491 for (lhs = start_symbol; lhs < nsyms; lhs++)
493 derives[lhs] = rules + k;
494 for (i = 0; i < nrules; i++)
496 if (rlhs[i] == lhs)
498 rules[k] = i;
499 k++;
502 rules[k] = -1;
503 k++;
506 #ifdef DEBUG
507 print_derives();
508 #endif
511 #ifdef DEBUG
512 void
513 print_derives(void)
515 int i;
516 short *sp;
518 printf("\nDERIVES\n\n");
520 for (i = start_symbol; i < nsyms; i++)
522 printf("%s derives ", symbol_name[i]);
523 for (sp = derives[i]; *sp >= 0; sp++)
525 printf(" %d", *sp);
527 putchar('\n');
530 putchar('\n');
532 #endif
534 static void
535 set_nullable(void)
537 int i, j;
538 int empty;
539 int done_flag;
541 nullable = TMALLOC(char, nsyms);
542 NO_SPACE(nullable);
544 for (i = 0; i < nsyms; ++i)
545 nullable[i] = 0;
547 done_flag = 0;
548 while (!done_flag)
550 done_flag = 1;
551 for (i = 1; i < nitems; i++)
553 empty = 1;
554 while ((j = ritem[i]) >= 0)
556 if (!nullable[j])
557 empty = 0;
558 ++i;
560 if (empty)
562 j = rlhs[-j];
563 if (!nullable[j])
565 nullable[j] = 1;
566 done_flag = 0;
572 #ifdef DEBUG
573 for (i = 0; i < nsyms; i++)
575 if (nullable[i])
576 printf("%s is nullable\n", symbol_name[i]);
577 else
578 printf("%s is not nullable\n", symbol_name[i]);
580 #endif
583 void
584 lr0(void)
586 set_derives();
587 set_nullable();
588 generate_states();
591 #ifdef NO_LEAKS
592 void
593 lr0_leaks(void)
595 DO_FREE(derives[start_symbol]);
596 DO_FREE(derives);
597 DO_FREE(nullable);
599 #endif