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: lr0.c 3573 2001-07-12 12:54:24Z doligez $ */
20 extern short *itemset;
21 extern short *itemsetend;
22 extern unsigned *ruleset;
27 reductions *first_reduction;
29 int get_state(int symbol);
30 core *new_state(int symbol);
32 static core **state_set;
33 static core *this_state;
34 static core *last_state;
35 static shifts *last_shift;
36 static reductions *last_reduction;
39 static short *shift_symbol;
42 static short *shiftset;
44 static short **kernel_base;
45 static short **kernel_end;
46 static short *kernel_items;
50 void initialize_states (void);
51 void save_reductions (void);
52 void new_itemsets (void);
53 void save_shifts (void);
54 void print_derives ();
55 void show_cores (void), show_ritems (void), show_rrhs (void), show_shifts (void);
57 void allocate_itemsets(void)
59 register short *itemp;
60 register short *item_end;
65 register short *symbol_count;
68 symbol_count = NEW2(nsyms, short);
70 item_end = ritem + nitems;
71 for (itemp = ritem; itemp < item_end; itemp++)
77 symbol_count[symbol]++;
81 kernel_base = NEW2(nsyms, short *);
82 kernel_items = NEW2(count, short);
86 for (i = 0; i < nsyms; i++)
88 kernel_base[i] = kernel_items + count;
89 count += symbol_count[i];
90 if (max < symbol_count[i])
91 max = symbol_count[i];
94 shift_symbol = symbol_count;
95 kernel_end = NEW2(nsyms, short *);
99 void allocate_storage(void)
102 shiftset = NEW2(nsyms, short);
103 redset = NEW2(nrules + 1, short);
104 state_set = NEW2(nitems, core *);
108 void append_states(void)
115 fprintf(stderr, "Entering append_states()\n");
117 for (i = 1; i < nshifts; i++)
119 symbol = shift_symbol[i];
121 while (j > 0 && shift_symbol[j - 1] > symbol)
123 shift_symbol[j] = shift_symbol[j - 1];
126 shift_symbol[j] = symbol;
129 for (i = 0; i < nshifts; i++)
131 symbol = shift_symbol[i];
132 shiftset[i] = get_state(symbol);
137 void free_storage(void)
150 void generate_states(void)
153 itemset = NEW2(nitems, short);
154 ruleset = NEW2(WORDSIZE(nrules), unsigned);
160 closure(this_state->items, this_state->nitems);
168 this_state = this_state->next;
178 get_state(int symbol)
181 register short *isp1;
182 register short *isp2;
183 register short *iend;
189 fprintf(stderr, "Entering get_state(%d)\n", symbol);
192 isp1 = kernel_base[symbol];
193 iend = kernel_end[symbol];
197 assert(0 <= key && key < nitems);
207 isp1 = kernel_base[symbol];
210 while (found && isp1 < iend)
212 if (*isp1++ != *isp2++)
225 sp = sp->link = new_state(symbol);
233 state_set[key] = sp = new_state(symbol);
241 void initialize_states(void)
244 register short *start_derives;
247 start_derives = derives[start_symbol];
248 for (i = 0; start_derives[i] >= 0; ++i)
251 p = (core *) MALLOC(sizeof(core) + i*sizeof(short));
252 if (p == 0) no_space();
257 p->accessing_symbol = 0;
260 for (i = 0; start_derives[i] >= 0; ++i)
261 p->items[i] = rrhs[start_derives[i]];
263 first_state = last_state = this_state = p;
268 void new_itemsets(void)
271 register int shiftcount;
276 for (i = 0; i < nsyms; i++)
281 while (isp < itemsetend)
287 ksp = kernel_end[symbol];
290 shift_symbol[shiftcount++] = symbol;
291 ksp = kernel_base[symbol];
295 kernel_end[symbol] = ksp;
299 nshifts = shiftcount;
305 new_state(int symbol)
309 register short *isp1;
310 register short *isp2;
311 register short *iend;
314 fprintf(stderr, "Entering new_state(%d)\n", symbol);
317 if (nstates >= MAXSHORT)
318 fatal("too many states");
320 isp1 = kernel_base[symbol];
321 iend = kernel_end[symbol];
324 p = (core *) allocate((unsigned) (sizeof(core) + (n - 1) * sizeof(short)));
325 p->accessing_symbol = symbol;
333 last_state->next = p;
342 /* show_cores is used for debugging */
344 void show_cores(void)
351 for (p = first_state; p; ++k, p = p->next)
354 printf("state %d, number = %d, accessing symbol = %s\n",
355 k, p->number, symbol_name[p->accessing_symbol]);
357 for (i = 0; i < n; ++i)
359 itemno = p->items[i];
360 printf("%4d ", itemno);
362 while (ritem[j] >= 0) ++j;
363 printf("%s :", symbol_name[rlhs[-ritem[j]]]);
366 printf(" %s", symbol_name[ritem[j++]]);
368 while (ritem[j] >= 0)
369 printf(" %s", symbol_name[ritem[j++]]);
377 /* show_ritems is used for debugging */
379 void show_ritems(void)
383 for (i = 0; i < nitems; ++i)
384 printf("ritem[%d] = %d\n", i, ritem[i]);
388 /* show_rrhs is used for debugging */
394 for (i = 0; i < nrules; ++i)
395 printf("rrhs[%d] = %d\n", i, rrhs[i]);
399 /* show_shifts is used for debugging */
401 void show_shifts(void)
407 for (p = first_shift; p; ++k, p = p->next)
410 printf("shift %d, number = %d, nshifts = %d\n", k, p->number,
413 for (i = 0; i < j; ++i)
414 printf("\t%d\n", p->shift[i]);
419 void save_shifts(void)
424 register short *send;
426 p = (shifts *) allocate((unsigned) (sizeof(shifts) +
427 (nshifts - 1) * sizeof(short)));
429 p->number = this_state->number;
430 p->nshifts = nshifts;
434 send = shiftset + nshifts;
441 last_shift->next = p;
453 void save_reductions(void)
460 register reductions *p;
461 register short *rend;
464 for (isp = itemset; isp < itemsetend; isp++)
469 redset[count++] = -item;
475 p = (reductions *) allocate((unsigned) (sizeof(reductions) +
476 (count - 1) * sizeof(short)));
478 p->number = this_state->number;
490 last_reduction->next = p;
502 void set_derives(void)
506 register short *rules;
508 derives = NEW2(nsyms, short *);
509 rules = NEW2(nvars + nrules, short);
512 for (lhs = start_symbol; lhs < nsyms; lhs++)
514 derives[lhs] = rules + k;
515 for (i = 0; i < nrules; i++)
532 void free_derives(void)
534 FREE(derives[start_symbol]);
539 void print_derives(void)
544 printf("\nDERIVES\n\n");
546 for (i = start_symbol; i < nsyms; i++)
548 printf("%s derives ", symbol_name[i]);
549 for (sp = derives[i]; *sp >= 0; sp++)
561 void set_nullable(void)
567 nullable = MALLOC(nsyms);
568 if (nullable == 0) no_space();
570 for (i = 0; i < nsyms; ++i)
577 for (i = 1; i < nitems; i++)
580 while ((j = ritem[i]) >= 0)
599 for (i = 0; i < nsyms; i++)
602 printf("%s is nullable\n", symbol_name[i]);
604 printf("%s is not nullable\n", symbol_name[i]);
610 void free_nullable(void)