Fix typo in OIDs corresponding to SHA256, SHA384, and SHA512 (#21707)
[mono-project.git] / mcs / jay / lalr.c
blob81656753bc544ad9b5fdc4bb983a1efc1cdb7aa0
1 /*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Robert Paul Corbett.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
37 #ifndef lint
38 static char sccsid[] = "@(#)lalr.c 5.3 (Berkeley) 6/1/90";
39 #endif /* not lint */
41 #include "defs.h"
43 typedef
44 struct shorts
46 struct shorts *next;
47 short value;
49 shorts;
51 int tokensetsize;
52 short *lookaheads;
53 short *LAruleno;
54 unsigned *LA;
55 short *accessing_symbol;
56 core **state_table;
57 shifts **shift_table;
58 reductions **reduction_table;
59 short *goto_map;
60 short *from_state;
61 short *to_state;
63 static short **
64 transpose (short **R, int n);
66 static int infinity;
67 static int maxrhs;
68 static int ngotos;
69 static unsigned *F;
70 static short **includes;
71 static shorts **lookback;
72 static short **R;
73 static short *INDEX;
74 static short *VERTICES;
75 static int top;
77 static void
78 set_state_table (void);
80 static void
81 set_accessing_symbol (void);
83 static void
84 set_shift_table (void);
86 static void
87 set_reduction_table (void);
89 static void
90 set_maxrhs (void);
92 static void
93 initialize_LA (void);
95 static void
96 set_goto_map (void);
98 static void
99 initialize_F (void);
101 static void
102 build_relations (void);
104 static void
105 compute_FOLLOWS (void);
107 static void
108 compute_lookaheads (void);
110 static void
111 traverse (int i);
113 static void
114 digraph (short **relation);
116 static void
117 add_lookback_edge (int stateno, int ruleno, int gotono);
119 void
120 lalr (void)
122 tokensetsize = WORDSIZE(ntokens);
124 set_state_table();
125 set_accessing_symbol();
126 set_shift_table();
127 set_reduction_table();
128 set_maxrhs();
129 initialize_LA();
130 set_goto_map();
131 initialize_F();
132 build_relations();
133 compute_FOLLOWS();
134 compute_lookaheads();
137 static void
138 set_state_table (void)
140 register core *sp;
142 state_table = NEW2(nstates, core *);
143 for (sp = first_state; sp; sp = sp->next)
144 state_table[sp->number] = sp;
147 static void
148 set_accessing_symbol (void)
150 register core *sp;
152 accessing_symbol = NEW2(nstates, short);
153 for (sp = first_state; sp; sp = sp->next)
154 accessing_symbol[sp->number] = sp->accessing_symbol;
157 static void
158 set_shift_table (void)
160 register shifts *sp;
162 shift_table = NEW2(nstates, shifts *);
163 for (sp = first_shift; sp; sp = sp->next)
164 shift_table[sp->number] = sp;
167 static void
168 set_reduction_table (void)
170 register reductions *rp;
172 reduction_table = NEW2(nstates, reductions *);
173 for (rp = first_reduction; rp; rp = rp->next)
174 reduction_table[rp->number] = rp;
177 static void
178 set_maxrhs (void)
180 register short *itemp;
181 register short *item_end;
182 register int length;
183 register int max;
185 length = 0;
186 max = 0;
187 item_end = ritem + nitems;
188 for (itemp = ritem; itemp < item_end; itemp++)
190 if (*itemp >= 0)
192 length++;
194 else
196 if (length > max) max = length;
197 length = 0;
201 maxrhs = max;
204 static void
205 initialize_LA (void)
207 register int i, j, k;
208 register reductions *rp;
210 lookaheads = NEW2(nstates + 1, short);
212 k = 0;
213 for (i = 0; i < nstates; i++)
215 lookaheads[i] = k;
216 rp = reduction_table[i];
217 if (rp)
218 k += rp->nreds;
220 lookaheads[nstates] = k;
222 LA = NEW2(k * tokensetsize, unsigned);
223 LAruleno = NEW2(k, short);
224 lookback = NEW2(k, shorts *);
226 k = 0;
227 for (i = 0; i < nstates; i++)
229 rp = reduction_table[i];
230 if (rp)
232 for (j = 0; j < rp->nreds; j++)
234 LAruleno[k] = rp->rules[j];
235 k++;
241 static void
242 set_goto_map (void)
244 register shifts *sp;
245 register int i;
246 register int symbol;
247 register int k;
248 register short *temp_map;
249 register int state2;
250 register int state1;
252 goto_map = NEW2(nvars + 1, short) - ntokens;
253 temp_map = NEW2(nvars + 1, short) - ntokens;
255 ngotos = 0;
256 for (sp = first_shift; sp; sp = sp->next)
258 for (i = sp->nshifts - 1; i >= 0; i--)
260 symbol = accessing_symbol[sp->shift[i]];
262 if (ISTOKEN(symbol)) break;
264 if (ngotos == MAXSHORT)
265 fatal("too many gotos");
267 ngotos++;
268 goto_map[symbol]++;
272 k = 0;
273 for (i = ntokens; i < nsyms; i++)
275 temp_map[i] = k;
276 k += goto_map[i];
279 for (i = ntokens; i < nsyms; i++)
280 goto_map[i] = temp_map[i];
282 goto_map[nsyms] = ngotos;
283 temp_map[nsyms] = ngotos;
285 from_state = NEW2(ngotos, short);
286 to_state = NEW2(ngotos, short);
288 for (sp = first_shift; sp; sp = sp->next)
290 state1 = sp->number;
291 for (i = sp->nshifts - 1; i >= 0; i--)
293 state2 = sp->shift[i];
294 symbol = accessing_symbol[state2];
296 if (ISTOKEN(symbol)) break;
298 k = temp_map[symbol]++;
299 from_state[k] = state1;
300 to_state[k] = state2;
304 FREE(temp_map + ntokens);
309 /* Map_goto maps a state/symbol pair into its numeric representation. */
311 static int
312 map_goto (int state, int symbol)
314 register int high;
315 register int low;
316 register int middle;
317 register int s;
319 low = goto_map[symbol];
320 high = goto_map[symbol + 1];
322 for (;;)
324 assert(low <= high);
325 middle = (low + high) >> 1;
326 s = from_state[middle];
327 if (s == state)
328 return (middle);
329 else if (s < state)
330 low = middle + 1;
331 else
332 high = middle - 1;
336 static void
337 initialize_F (void)
339 register int i;
340 register int j;
341 register int k;
342 register shifts *sp;
343 register short *edge;
344 register unsigned *rowp;
345 register short *rp;
346 register short **reads;
347 register int nedges;
348 register int stateno;
349 register int symbol;
350 register int nwords;
352 nwords = ngotos * tokensetsize;
353 F = NEW2(nwords, unsigned);
355 reads = NEW2(ngotos, short *);
356 edge = NEW2(ngotos + 1, short);
357 nedges = 0;
359 rowp = F;
360 for (i = 0; i < ngotos; i++)
362 stateno = to_state[i];
363 sp = shift_table[stateno];
365 if (sp)
367 k = sp->nshifts;
369 for (j = 0; j < k; j++)
371 symbol = accessing_symbol[sp->shift[j]];
372 if (ISVAR(symbol))
373 break;
374 SETBIT(rowp, symbol);
377 for (; j < k; j++)
379 symbol = accessing_symbol[sp->shift[j]];
380 if (nullable[symbol])
381 edge[nedges++] = map_goto(stateno, symbol);
384 if (nedges)
386 reads[i] = rp = NEW2(nedges + 1, short);
388 for (j = 0; j < nedges; j++)
389 rp[j] = edge[j];
391 rp[nedges] = -1;
392 nedges = 0;
396 rowp += tokensetsize;
399 SETBIT(F, 0);
400 digraph(reads);
402 for (i = 0; i < ngotos; i++)
404 if (reads[i])
405 FREE(reads[i]);
408 FREE(reads);
409 FREE(edge);
412 static void
413 build_relations (void)
415 register int i;
416 register int j;
417 register int k;
418 register short *rulep;
419 register short *rp;
420 register shifts *sp;
421 register int length;
422 register int nedges;
423 register int done;
424 register int state1;
425 register int stateno;
426 register int symbol1;
427 register int symbol2;
428 register short *shortp;
429 register short *edge;
430 register short *states;
431 register short **new_includes;
433 includes = NEW2(ngotos, short *);
434 edge = NEW2(ngotos + 1, short);
435 states = NEW2(maxrhs + 1, short);
437 for (i = 0; i < ngotos; i++)
439 nedges = 0;
440 state1 = from_state[i];
441 symbol1 = accessing_symbol[to_state[i]];
443 for (rulep = derives[symbol1]; *rulep >= 0; rulep++)
445 length = 1;
446 states[0] = state1;
447 stateno = state1;
449 for (rp = ritem + rrhs[*rulep]; *rp >= 0; rp++)
451 symbol2 = *rp;
452 sp = shift_table[stateno];
453 k = sp->nshifts;
455 for (j = 0; j < k; j++)
457 stateno = sp->shift[j];
458 if (accessing_symbol[stateno] == symbol2) break;
461 states[length++] = stateno;
464 add_lookback_edge(stateno, *rulep, i);
466 length--;
467 done = 0;
468 while (!done)
470 done = 1;
471 rp--;
472 if (ISVAR(*rp))
474 stateno = states[--length];
475 edge[nedges++] = map_goto(stateno, *rp);
476 if (nullable[*rp] && length > 0) done = 0;
481 if (nedges)
483 includes[i] = shortp = NEW2(nedges + 1, short);
484 for (j = 0; j < nedges; j++)
485 shortp[j] = edge[j];
486 shortp[nedges] = -1;
490 new_includes = transpose(includes, ngotos);
492 for (i = 0; i < ngotos; i++)
493 if (includes[i])
494 FREE(includes[i]);
496 FREE(includes);
498 includes = new_includes;
500 FREE(edge);
501 FREE(states);
504 static void
505 add_lookback_edge (int stateno, int ruleno, int gotono)
507 register int i, k;
508 register int found;
509 register shorts *sp;
511 i = lookaheads[stateno];
512 k = lookaheads[stateno + 1];
513 found = 0;
514 while (!found && i < k)
516 if (LAruleno[i] == ruleno)
517 found = 1;
518 else
519 ++i;
521 assert(found);
523 sp = NEW(shorts);
524 sp->next = lookback[i];
525 sp->value = gotono;
526 lookback[i] = sp;
529 static short **
530 transpose (short **R, int n)
532 register short **new_R;
533 register short **temp_R;
534 register short *nedges;
535 register short *sp;
536 register int i;
537 register int k;
539 nedges = NEW2(n, short);
541 for (i = 0; i < n; i++)
543 sp = R[i];
544 if (sp)
546 while (*sp >= 0)
547 nedges[*sp++]++;
551 new_R = NEW2(n, short *);
552 temp_R = NEW2(n, short *);
554 for (i = 0; i < n; i++)
556 k = nedges[i];
557 if (k > 0)
559 sp = NEW2(k + 1, short);
560 new_R[i] = sp;
561 temp_R[i] = sp;
562 sp[k] = -1;
566 FREE(nedges);
568 for (i = 0; i < n; i++)
570 sp = R[i];
571 if (sp)
573 while (*sp >= 0)
574 *temp_R[*sp++]++ = i;
578 FREE(temp_R);
580 return (new_R);
583 static void
584 compute_FOLLOWS (void)
586 digraph(includes);
589 static void
590 compute_lookaheads (void)
592 register int i, n;
593 register unsigned *fp1, *fp2, *fp3;
594 register shorts *sp, *next;
595 register unsigned *rowp;
597 rowp = LA;
598 n = lookaheads[nstates];
599 for (i = 0; i < n; i++)
601 fp3 = rowp + tokensetsize;
602 for (sp = lookback[i]; sp; sp = sp->next)
604 fp1 = rowp;
605 fp2 = F + tokensetsize * sp->value;
606 while (fp1 < fp3)
607 *fp1++ |= *fp2++;
609 rowp = fp3;
612 for (i = 0; i < n; i++)
613 for (sp = lookback[i]; sp; sp = next)
615 next = sp->next;
616 FREE(sp);
619 FREE(lookback);
620 FREE(F);
623 static void
624 digraph (short **relation)
626 register int i;
628 infinity = ngotos + 2;
629 INDEX = NEW2(ngotos + 1, short);
630 VERTICES = NEW2(ngotos + 1, short);
631 top = 0;
633 R = relation;
635 for (i = 0; i < ngotos; i++)
636 INDEX[i] = 0;
638 for (i = 0; i < ngotos; i++)
640 if (INDEX[i] == 0 && R[i])
641 traverse(i);
644 FREE(INDEX);
645 FREE(VERTICES);
648 static void
649 traverse (int i)
651 register unsigned *fp1;
652 register unsigned *fp2;
653 register unsigned *fp3;
654 register int j;
655 register short *rp;
657 int height;
658 unsigned *base;
660 VERTICES[++top] = i;
661 INDEX[i] = height = top;
663 base = F + i * tokensetsize;
664 fp3 = base + tokensetsize;
666 rp = R[i];
667 if (rp)
669 while ((j = *rp++) >= 0)
671 if (INDEX[j] == 0)
672 traverse(j);
674 if (INDEX[i] > INDEX[j])
675 INDEX[i] = INDEX[j];
677 fp1 = base;
678 fp2 = F + j * tokensetsize;
680 while (fp1 < fp3)
681 *fp1++ |= *fp2++;
685 if (INDEX[i] == height)
687 for (;;)
689 j = VERTICES[top--];
690 INDEX[j] = infinity;
692 if (i == j)
693 break;
695 fp1 = base;
696 fp2 = F + j * tokensetsize;
698 while (fp1 < fp3)
699 *fp2++ = *fp1++;