]> git.saurik.com Git - bison.git/blame - src/derives.c
Merge remote-tracking branch 'origin/maint'
[bison.git] / src / derives.c
CommitLineData
122c0aac 1/* Match rules with nonterminals for bison,
a0b76449 2
7d6bad19 3 Copyright (C) 1984, 1989, 2000-2003, 2005, 2009-2013 Free Software
575619af 4 Foundation, Inc.
122c0aac 5
cc84fd5d 6 This file is part of Bison, the GNU Compiler Compiler.
122c0aac 7
f16b0819 8 This program is free software: you can redistribute it and/or modify
cc84fd5d 9 it under the terms of the GNU General Public License as published by
f16b0819
PE
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
122c0aac 12
f16b0819 13 This program is distributed in the hope that it will be useful,
cc84fd5d
AD
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.
122c0aac 17
cc84fd5d 18 You should have received a copy of the GNU General Public License
f16b0819 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
122c0aac 20
2cec9080 21#include <config.h>
122c0aac 22#include "system.h"
a0b76449 23
9bfe901c 24#include "getargs.h"
a0b76449 25
cc84fd5d 26#include "derives.h"
a0b76449
PE
27#include "gram.h"
28#include "reader.h"
29#include "symtab.h"
4a120d45 30
e1a4f3a4 31/* Linked list of rule numbers. */
a0b76449 32typedef struct rule_list
e1a4f3a4 33{
a0b76449
PE
34 struct rule_list *next;
35 rule *value;
36} rule_list;
e1a4f3a4 37
da2a7671 38rule ***derives;
cc84fd5d
AD
39
40static void
41print_derives (void)
42{
d7913476 43 int i;
cc84fd5d 44
c87d4863 45 fputs ("DERIVES\n", stderr);
cc84fd5d
AD
46
47 for (i = ntokens; i < nsyms; i++)
48 {
a0b76449 49 rule **rp;
5cd907c4 50 fprintf (stderr, " %s derives\n", symbols[i]->tag);
69cecfeb 51 for (rp = derives[i - ntokens]; *rp; ++rp)
e9690142 52 {
5cd907c4 53 fprintf (stderr, " %3d ", (*rp)->user_number);
e9690142 54 rule_rhs_print (*rp, stderr);
3e153163 55 fprintf (stderr, "\n");
e9690142 56 }
cc84fd5d
AD
57 }
58
c87d4863 59 fputs ("\n\n", stderr);
cc84fd5d
AD
60}
61
122c0aac 62
122c0aac 63void
bb0027a9 64derives_compute (void)
122c0aac 65{
a0b76449 66 symbol_number i;
a737b216 67 rule_number r;
a0b76449 68 rule **q;
122c0aac 69
69cecfeb
PE
70 /* DSET[NTERM - NTOKENS] -- A linked list of the numbers of the rules
71 whose LHS is NTERM. */
da2a7671 72 rule_list **dset = xcalloc (nvars, sizeof *dset);
e1a4f3a4
AD
73
74 /* DELTS[RULE] -- There are NRULES rule number to attach to nterms.
75 Instead of performing NRULES allocations for each, have an array
76 indexed by rule numbers. */
da2a7671 77 rule_list *delts = xnmalloc (nrules, sizeof *delts);
122c0aac 78
4b3d3a8e 79 for (r = nrules - 1; r >= 0; --r)
bba97eb2 80 {
a0b76449
PE
81 symbol_number lhs = rules[r].lhs->number;
82 rule_list *p = &delts[r];
e1a4f3a4 83 /* A new LHS is found. */
69cecfeb 84 p->next = dset[lhs - ntokens];
bb0027a9 85 p->value = &rules[r];
69cecfeb 86 dset[lhs - ntokens] = p;
bba97eb2 87 }
122c0aac 88
e1a4f3a4
AD
89 /* DSET contains what we need under the form of a linked list. Make
90 it a single array. */
91
da2a7671
PE
92 derives = xnmalloc (nvars, sizeof *derives);
93 q = xnmalloc (nvars + nrules, sizeof *q);
122c0aac
RS
94
95 for (i = ntokens; i < nsyms; i++)
96 {
69cecfeb
PE
97 rule_list *p = dset[i - ntokens];
98 derives[i - ntokens] = q;
122c0aac 99 while (p)
e9690142
JD
100 {
101 *q++ = p->value;
102 p = p->next;
103 }
bb0027a9 104 *q++ = NULL;
122c0aac
RS
105 }
106
273a74fa 107 if (trace_flag & trace_sets)
9bfe901c 108 print_derives ();
122c0aac 109
69cecfeb 110 free (dset);
e1a4f3a4 111 free (delts);
122c0aac
RS
112}
113
e1a4f3a4 114
122c0aac 115void
bb0027a9 116derives_free (void)
122c0aac 117{
afbb696d
PE
118 free (derives[0]);
119 free (derives);
122c0aac 120}