vm: merge i386 and arm pagetable code
[minix.git] / external / bsd / byacc / dist / lalr.c
blob87350163c9f51b365483dba9a6bec6cb30c15fb5
1 /* $NetBSD: lalr.c,v 1.5 2011/09/10 21:29:04 christos Exp $ */
2 /* Id: lalr.c,v 1.9 2009/10/27 09:49:27 tom Exp */
4 #include "defs.h"
6 #include <sys/cdefs.h>
7 __RCSID("$NetBSD: lalr.c,v 1.5 2011/09/10 21:29:04 christos Exp $");
9 typedef struct shorts
11 struct shorts *next;
12 Value_t value;
14 shorts;
16 static Value_t map_goto(int state, int symbol);
17 static Value_t **transpose(Value_t ** R, int n);
18 static void add_lookback_edge(int stateno, int ruleno, int gotono);
19 static void build_relations(void);
20 static void compute_FOLLOWS(void);
21 static void compute_lookaheads(void);
22 static void digraph(Value_t ** relation);
23 static void initialize_F(void);
24 static void initialize_LA(void);
25 static void set_accessing_symbol(void);
26 static void set_goto_map(void);
27 static void set_maxrhs(void);
28 static void set_reduction_table(void);
29 static void set_shift_table(void);
30 static void set_state_table(void);
31 static void traverse(int i);
33 static int tokensetsize;
34 Value_t *lookaheads;
35 Value_t *LAruleno;
36 unsigned *LA;
37 Value_t *accessing_symbol;
38 core **state_table;
39 shifts **shift_table;
40 reductions **reduction_table;
41 Value_t *goto_map;
42 Value_t *from_state;
43 Value_t *to_state;
45 static Value_t infinity;
46 static int maxrhs;
47 static int ngotos;
48 static unsigned *F;
49 static Value_t **includes;
50 static shorts **lookback;
51 static Value_t **R;
52 static Value_t *INDEX;
53 static Value_t *VERTICES;
54 static Value_t top;
56 void
57 lalr(void)
59 tokensetsize = WORDSIZE(ntokens);
61 set_state_table();
62 set_accessing_symbol();
63 set_shift_table();
64 set_reduction_table();
65 set_maxrhs();
66 initialize_LA();
67 set_goto_map();
68 initialize_F();
69 build_relations();
70 compute_FOLLOWS();
71 compute_lookaheads();
74 static void
75 set_state_table(void)
77 core *sp;
79 state_table = NEW2(nstates, core *);
80 for (sp = first_state; sp; sp = sp->next)
81 state_table[sp->number] = sp;
84 static void
85 set_accessing_symbol(void)
87 core *sp;
89 accessing_symbol = NEW2(nstates, Value_t);
90 for (sp = first_state; sp; sp = sp->next)
91 accessing_symbol[sp->number] = sp->accessing_symbol;
94 static void
95 set_shift_table(void)
97 shifts *sp;
99 shift_table = NEW2(nstates, shifts *);
100 for (sp = first_shift; sp; sp = sp->next)
101 shift_table[sp->number] = sp;
104 static void
105 set_reduction_table(void)
107 reductions *rp;
109 reduction_table = NEW2(nstates, reductions *);
110 for (rp = first_reduction; rp; rp = rp->next)
111 reduction_table[rp->number] = rp;
114 static void
115 set_maxrhs(void)
117 Value_t *itemp;
118 Value_t *item_end;
119 int length;
120 int max;
122 length = 0;
123 max = 0;
124 item_end = ritem + nitems;
125 for (itemp = ritem; itemp < item_end; itemp++)
127 if (*itemp >= 0)
129 length++;
131 else
133 if (length > max)
134 max = length;
135 length = 0;
139 maxrhs = max;
142 static void
143 initialize_LA(void)
145 int i, j, k;
146 reductions *rp;
148 lookaheads = NEW2(nstates + 1, Value_t);
150 k = 0;
151 for (i = 0; i < nstates; i++)
153 lookaheads[i] = (Value_t) k;
154 rp = reduction_table[i];
155 if (rp)
156 k += rp->nreds;
158 lookaheads[nstates] = (Value_t) k;
160 LA = NEW2(k * tokensetsize, unsigned);
161 LAruleno = NEW2(k, Value_t);
162 lookback = NEW2(k, shorts *);
164 k = 0;
165 for (i = 0; i < nstates; i++)
167 rp = reduction_table[i];
168 if (rp)
170 for (j = 0; j < rp->nreds; j++)
172 LAruleno[k] = rp->rules[j];
173 k++;
179 static void
180 set_goto_map(void)
182 shifts *sp;
183 int i;
184 int symbol;
185 int k;
186 Value_t *temp_map;
187 Value_t state2;
188 Value_t state1;
190 goto_map = NEW2(nvars + 1, Value_t) - ntokens;
191 temp_map = NEW2(nvars + 1, Value_t) - ntokens;
193 ngotos = 0;
194 for (sp = first_shift; sp; sp = sp->next)
196 for (i = sp->nshifts - 1; i >= 0; i--)
198 symbol = accessing_symbol[sp->shift[i]];
200 if (ISTOKEN(symbol))
201 break;
203 if (ngotos == MAXSHORT)
204 fatal("too many gotos");
206 ngotos++;
207 goto_map[symbol]++;
211 k = 0;
212 for (i = ntokens; i < nsyms; i++)
214 temp_map[i] = (Value_t) k;
215 k += goto_map[i];
218 for (i = ntokens; i < nsyms; i++)
219 goto_map[i] = temp_map[i];
221 goto_map[nsyms] = (Value_t) ngotos;
222 temp_map[nsyms] = (Value_t) ngotos;
224 from_state = NEW2(ngotos, Value_t);
225 to_state = NEW2(ngotos, Value_t);
227 for (sp = first_shift; sp; sp = sp->next)
229 state1 = sp->number;
230 for (i = sp->nshifts - 1; i >= 0; i--)
232 state2 = sp->shift[i];
233 symbol = accessing_symbol[state2];
235 if (ISTOKEN(symbol))
236 break;
238 k = temp_map[symbol]++;
239 from_state[k] = state1;
240 to_state[k] = state2;
244 FREE(temp_map + ntokens);
247 /* Map_goto maps a state/symbol pair into its numeric representation. */
249 static Value_t
250 map_goto(int state, int symbol)
252 int high;
253 int low;
254 int middle;
255 int s;
257 low = goto_map[symbol];
258 high = goto_map[symbol + 1];
260 for (;;)
262 assert(low <= high);
263 middle = (low + high) >> 1;
264 s = from_state[middle];
265 if (s == state)
266 return (Value_t) (middle);
267 else if (s < state)
268 low = middle + 1;
269 else
270 high = middle - 1;
274 static void
275 initialize_F(void)
277 int i;
278 int j;
279 int k;
280 shifts *sp;
281 Value_t *edge;
282 unsigned *rowp;
283 Value_t *rp;
284 Value_t **reads;
285 int nedges;
286 int stateno;
287 int symbol;
288 int nwords;
290 nwords = ngotos * tokensetsize;
291 F = NEW2(nwords, unsigned);
293 reads = NEW2(ngotos, Value_t *);
294 edge = NEW2(ngotos + 1, Value_t);
295 nedges = 0;
297 rowp = F;
298 for (i = 0; i < ngotos; i++)
300 stateno = to_state[i];
301 sp = shift_table[stateno];
303 if (sp)
305 k = sp->nshifts;
307 for (j = 0; j < k; j++)
309 symbol = accessing_symbol[sp->shift[j]];
310 if (ISVAR(symbol))
311 break;
312 SETBIT(rowp, symbol);
315 for (; j < k; j++)
317 symbol = accessing_symbol[sp->shift[j]];
318 if (nullable[symbol])
319 edge[nedges++] = map_goto(stateno, symbol);
322 if (nedges)
324 reads[i] = rp = NEW2(nedges + 1, Value_t);
326 for (j = 0; j < nedges; j++)
327 rp[j] = edge[j];
329 rp[nedges] = -1;
330 nedges = 0;
334 rowp += tokensetsize;
337 SETBIT(F, 0);
338 digraph(reads);
340 for (i = 0; i < ngotos; i++)
342 if (reads[i])
343 FREE(reads[i]);
346 FREE(reads);
347 FREE(edge);
350 static void
351 build_relations(void)
353 int i;
354 int j;
355 int k;
356 Value_t *rulep;
357 Value_t *rp;
358 shifts *sp;
359 int length;
360 int nedges;
361 int done_flag;
362 Value_t state1;
363 Value_t stateno;
364 int symbol1;
365 int symbol2;
366 Value_t *shortp;
367 Value_t *edge;
368 Value_t *states;
369 Value_t **new_includes;
371 includes = NEW2(ngotos, Value_t *);
372 edge = NEW2(ngotos + 1, Value_t);
373 states = NEW2(maxrhs + 1, Value_t);
375 for (i = 0; i < ngotos; i++)
377 nedges = 0;
378 state1 = from_state[i];
379 symbol1 = accessing_symbol[to_state[i]];
381 for (rulep = derives[symbol1]; *rulep >= 0; rulep++)
383 length = 1;
384 states[0] = state1;
385 stateno = state1;
387 for (rp = ritem + rrhs[*rulep]; *rp >= 0; rp++)
389 symbol2 = *rp;
390 sp = shift_table[stateno];
391 k = sp->nshifts;
393 for (j = 0; j < k; j++)
395 stateno = sp->shift[j];
396 if (accessing_symbol[stateno] == symbol2)
397 break;
400 states[length++] = stateno;
403 add_lookback_edge(stateno, *rulep, i);
405 length--;
406 done_flag = 0;
407 while (!done_flag)
409 done_flag = 1;
410 rp--;
411 if (ISVAR(*rp))
413 stateno = states[--length];
414 edge[nedges++] = map_goto(stateno, *rp);
415 if (nullable[*rp] && length > 0)
416 done_flag = 0;
421 if (nedges)
423 includes[i] = shortp = NEW2(nedges + 1, Value_t);
424 for (j = 0; j < nedges; j++)
425 shortp[j] = edge[j];
426 shortp[nedges] = -1;
430 new_includes = transpose(includes, ngotos);
432 for (i = 0; i < ngotos; i++)
433 if (includes[i])
434 FREE(includes[i]);
436 FREE(includes);
438 includes = new_includes;
440 FREE(edge);
441 FREE(states);
444 static void
445 add_lookback_edge(int stateno, int ruleno, int gotono)
447 int i, k;
448 int found;
449 shorts *sp;
451 i = lookaheads[stateno];
452 k = lookaheads[stateno + 1];
453 found = 0;
454 while (!found && i < k)
456 if (LAruleno[i] == ruleno)
457 found = 1;
458 else
459 ++i;
461 assert(found);
463 sp = NEW(shorts);
464 sp->next = lookback[i];
465 sp->value = (Value_t) gotono;
466 lookback[i] = sp;
469 static Value_t **
470 transpose(Value_t ** R2, int n)
472 Value_t **new_R;
473 Value_t **temp_R;
474 Value_t *nedges;
475 Value_t *sp;
476 int i;
477 int k;
479 nedges = NEW2(n, Value_t);
481 for (i = 0; i < n; i++)
483 sp = R2[i];
484 if (sp)
486 while (*sp >= 0)
487 nedges[*sp++]++;
491 new_R = NEW2(n, Value_t *);
492 temp_R = NEW2(n, Value_t *);
494 for (i = 0; i < n; i++)
496 k = nedges[i];
497 if (k > 0)
499 sp = NEW2(k + 1, Value_t);
500 new_R[i] = sp;
501 temp_R[i] = sp;
502 sp[k] = -1;
506 FREE(nedges);
508 for (i = 0; i < n; i++)
510 sp = R2[i];
511 if (sp)
513 while (*sp >= 0)
514 *temp_R[*sp++]++ = (Value_t) i;
518 FREE(temp_R);
520 return (new_R);
523 static void
524 compute_FOLLOWS(void)
526 digraph(includes);
529 static void
530 compute_lookaheads(void)
532 int i, n;
533 unsigned *fp1, *fp2, *fp3;
534 shorts *sp, *next;
535 unsigned *rowp;
537 rowp = LA;
538 n = lookaheads[nstates];
539 for (i = 0; i < n; i++)
541 fp3 = rowp + tokensetsize;
542 for (sp = lookback[i]; sp; sp = sp->next)
544 fp1 = rowp;
545 fp2 = F + tokensetsize * sp->value;
546 while (fp1 < fp3)
547 *fp1++ |= *fp2++;
549 rowp = fp3;
552 for (i = 0; i < n; i++)
553 for (sp = lookback[i]; sp; sp = next)
555 next = sp->next;
556 FREE(sp);
559 FREE(lookback);
560 FREE(F);
563 static void
564 digraph(Value_t ** relation)
566 int i;
568 infinity = (Value_t) (ngotos + 2);
569 INDEX = NEW2(ngotos + 1, Value_t);
570 VERTICES = NEW2(ngotos + 1, Value_t);
571 top = 0;
573 R = relation;
575 for (i = 0; i < ngotos; i++)
576 INDEX[i] = 0;
578 for (i = 0; i < ngotos; i++)
580 if (INDEX[i] == 0 && R[i])
581 traverse(i);
584 FREE(INDEX);
585 FREE(VERTICES);
588 static void
589 traverse(int i)
591 unsigned *fp1;
592 unsigned *fp2;
593 unsigned *fp3;
594 int j;
595 Value_t *rp;
597 Value_t height;
598 unsigned *base;
600 VERTICES[++top] = (Value_t) i;
601 INDEX[i] = height = top;
603 base = F + i * tokensetsize;
604 fp3 = base + tokensetsize;
606 rp = R[i];
607 if (rp)
609 while ((j = *rp++) >= 0)
611 if (INDEX[j] == 0)
612 traverse(j);
614 if (INDEX[i] > INDEX[j])
615 INDEX[i] = INDEX[j];
617 fp1 = base;
618 fp2 = F + j * tokensetsize;
620 while (fp1 < fp3)
621 *fp1++ |= *fp2++;
625 if (INDEX[i] == height)
627 for (;;)
629 j = VERTICES[top--];
630 INDEX[j] = infinity;
632 if (i == j)
633 break;
635 fp1 = base;
636 fp2 = F + j * tokensetsize;
638 while (fp1 < fp3)
639 *fp2++ = *fp1++;
644 #ifdef NO_LEAKS
645 void
646 lalr_leaks(void)
648 int i;
650 if (includes != 0)
652 for (i = 0; i < ngotos; i++)
654 free(includes[i]);
656 DO_FREE(includes);
659 #endif