glr2.cc: require C++11
[bison.git] / src / derives.c
blob4ea5f78bb4c91da63c0de3d730375d2b70f9ab7f
1 /* Match rules with nonterminals for bison,
3 Copyright (C) 1984, 1989, 2000-2003, 2005, 2009-2015, 2018-2021 Free
4 Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
21 #include <config.h>
22 #include "system.h"
24 #include "getargs.h"
26 #include "derives.h"
27 #include "gram.h"
28 #include "reader.h"
29 #include "symtab.h"
31 /* Linked list of rule numbers. */
32 typedef struct rule_list
34 struct rule_list *next;
35 rule *value;
36 } rule_list;
38 rule ***derives;
40 static void
41 print_derives (void)
43 fputs ("DERIVES\n", stderr);
45 for (symbol_number i = ntokens; i < nsyms; ++i)
47 fprintf (stderr, " %s derives\n", symbols[i]->tag);
48 for (rule **rp = derives[i - ntokens]; *rp; ++rp)
50 fprintf (stderr, " %3d ", (*rp)->code);
51 rule_rhs_print (*rp, stderr);
52 fprintf (stderr, "\n");
56 fputs ("\n\n", stderr);
60 void
61 derives_compute (void)
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);
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);
72 for (rule_number r = nrules - 1; r >= 0; --r)
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;
82 /* DSET contains what we need under the form of a linked list. Make
83 it a single array. */
85 derives = xnmalloc (nnterms, sizeof *derives);
86 /* Q is the storage for DERIVES[...] (DERIVES[0] = q). */
87 rule **q = xnmalloc (nnterms + nrules, sizeof *q);
89 for (symbol_number i = ntokens; i < nsyms; ++i)
91 rule_list *p = dset[i - ntokens];
92 derives[i - ntokens] = q;
93 while (p)
95 *q++ = p->value;
96 p = p->next;
98 *q++ = NULL;
101 if (trace_flag & trace_sets)
102 print_derives ();
104 free (dset);
105 free (delts);
109 void
110 derives_free (void)
112 if (derives)
114 free (derives[0]);
115 free (derives);