]> git.saurik.com Git - bison.git/blame - src/derives.c
Provide an additional sub-message for clarity.
[bison.git] / src / derives.c
CommitLineData
122c0aac 1/* Match rules with nonterminals for bison,
a0b76449 2
401b73af 3 Copyright (C) 1984, 1989, 2000, 2001, 2002, 2003, 2005, 2009 Free
2cec9080 4 Software 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;
ad949da9 50 fprintf (stderr, "\t%s derives\n", symbols[i]->tag);
69cecfeb 51 for (rp = derives[i - ntokens]; *rp; ++rp)
720e5c1b 52 {
bb0027a9
AD
53 fprintf (stderr, "\t\t%3d ", (*rp)->user_number);
54 rule_rhs_print (*rp, stderr);
720e5c1b 55 }
cc84fd5d
AD
56 }
57
c87d4863 58 fputs ("\n\n", stderr);
cc84fd5d
AD
59}
60
122c0aac 61
122c0aac 62void
bb0027a9 63derives_compute (void)
122c0aac 64{
a0b76449 65 symbol_number i;
a737b216 66 rule_number r;
a0b76449 67 rule **q;
122c0aac 68
69cecfeb
PE
69 /* DSET[NTERM - NTOKENS] -- A linked list of the numbers of the rules
70 whose LHS is NTERM. */
da2a7671 71 rule_list **dset = xcalloc (nvars, sizeof *dset);
e1a4f3a4
AD
72
73 /* DELTS[RULE] -- There are NRULES rule number to attach to nterms.
74 Instead of performing NRULES allocations for each, have an array
75 indexed by rule numbers. */
da2a7671 76 rule_list *delts = xnmalloc (nrules, sizeof *delts);
122c0aac 77
4b3d3a8e 78 for (r = nrules - 1; r >= 0; --r)
bba97eb2 79 {
a0b76449
PE
80 symbol_number lhs = rules[r].lhs->number;
81 rule_list *p = &delts[r];
e1a4f3a4 82 /* A new LHS is found. */
69cecfeb 83 p->next = dset[lhs - ntokens];
bb0027a9 84 p->value = &rules[r];
69cecfeb 85 dset[lhs - ntokens] = p;
bba97eb2 86 }
122c0aac 87
e1a4f3a4
AD
88 /* DSET contains what we need under the form of a linked list. Make
89 it a single array. */
90
da2a7671
PE
91 derives = xnmalloc (nvars, sizeof *derives);
92 q = xnmalloc (nvars + nrules, sizeof *q);
122c0aac
RS
93
94 for (i = ntokens; i < nsyms; i++)
95 {
69cecfeb
PE
96 rule_list *p = dset[i - ntokens];
97 derives[i - ntokens] = q;
122c0aac
RS
98 while (p)
99 {
100 *q++ = p->value;
101 p = p->next;
102 }
bb0027a9 103 *q++ = NULL;
122c0aac
RS
104 }
105
273a74fa 106 if (trace_flag & trace_sets)
9bfe901c 107 print_derives ();
122c0aac 108
69cecfeb 109 free (dset);
e1a4f3a4 110 free (delts);
122c0aac
RS
111}
112
e1a4f3a4 113
122c0aac 114void
bb0027a9 115derives_free (void)
122c0aac 116{
afbb696d
PE
117 free (derives[0]);
118 free (derives);
122c0aac 119}