1 /* Match rules with nonterminals for bison,
2
3 Copyright (C) 1984, 1989, 2000-2003, 2005, 2009-2015, 2018-2021 Free
4 Software Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <https://www.gnu.org/licenses/>. */
20
21 #include <config.h>
22 #include "system.h"
23
24 #include "getargs.h"
25
26 #include "derives.h"
27 #include "gram.h"
28 #include "reader.h"
29 #include "symtab.h"
30
31 /* Linked list of rule numbers. */
32 typedef struct rule_list
33 {
34 struct rule_list *next;
35 rule *value;
36 } rule_list;
37
38 rule ***derives;
39
40 static void
print_derives(void)41 print_derives (void)
42 {
43 fputs ("DERIVES\n", stderr);
44
45 for (symbol_number i = ntokens; i < nsyms; ++i)
46 {
47 fprintf (stderr, " %s derives\n", symbols[i]->tag);
48 for (rule **rp = derives[i - ntokens]; *rp; ++rp)
49 {
50 fprintf (stderr, " %3d ", (*rp)->code);
51 rule_rhs_print (*rp, stderr);
52 fprintf (stderr, "\n");
53 }
54 }
55
56 fputs ("\n\n", stderr);
57 }
58
59
60 void
derives_compute(void)61 derives_compute (void)
62 {
63 /* DSET[NTERM - NTOKENS] -- A linked list of the numbers of the rules
64 whose LHS is NTERM. */
65 rule_list **dset = xcalloc (nnterms, sizeof *dset);
66
67 /* DELTS[RULE] -- There are NRULES rule number to attach to nterms.
68 Instead of performing NRULES allocations for each, have an array
69 indexed by rule numbers. */
70 rule_list *delts = xnmalloc (nrules, sizeof *delts);
71
72 for (rule_number r = nrules - 1; r >= 0; --r)
73 {
74 symbol_number lhs = rules[r].lhs->number;
75 rule_list *p = &delts[r];
76 /* A new LHS is found. */
77 p->next = dset[lhs - ntokens];
78 p->value = &rules[r];
79 dset[lhs - ntokens] = p;
80 }
81
82 /* DSET contains what we need under the form of a linked list. Make
83 it a single array. */
84
85 derives = xnmalloc (nnterms, sizeof *derives);
86 /* Q is the storage for DERIVES[...] (DERIVES[0] = q). */
87 rule **q = xnmalloc (nnterms + nrules, sizeof *q);
88
89 for (symbol_number i = ntokens; i < nsyms; ++i)
90 {
91 rule_list *p = dset[i - ntokens];
92 derives[i - ntokens] = q;
93 while (p)
94 {
95 *q++ = p->value;
96 p = p->next;
97 }
98 *q++ = NULL;
99 }
100
101 if (trace_flag & trace_sets)
102 print_derives ();
103
104 free (dset);
105 free (delts);
106 }
107
108
109 void
derives_free(void)110 derives_free (void)
111 {
112 if (derives)
113 {
114 free (derives[0]);
115 free (derives);
116 }
117 }
118