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