]> git.saurik.com Git - bison.git/blame - src/derives.c
* src/derives.h, src/derives.c (derives): A `rule_t***' instead of
[bison.git] / src / derives.c
CommitLineData
122c0aac 1/* Match rules with nonterminals for bison,
bb0027a9 2 Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
122c0aac 3
cc84fd5d 4 This file is part of Bison, the GNU Compiler Compiler.
122c0aac 5
cc84fd5d
AD
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
122c0aac 10
cc84fd5d
AD
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
122c0aac 15
cc84fd5d
AD
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
122c0aac
RS
20
21
122c0aac 22#include "system.h"
9bfe901c 23#include "getargs.h"
ad949da9 24#include "symtab.h"
2c5f66ed 25#include "reader.h"
122c0aac 26#include "gram.h"
cc84fd5d 27#include "derives.h"
4a120d45 28
e1a4f3a4
AD
29/* Linked list of rule numbers. */
30typedef struct rule_list_s
31{
32 struct rule_list_s *next;
bb0027a9 33 rule_t *value;
e1a4f3a4
AD
34} rule_list_t;
35
bb0027a9 36rule_t ***derives = NULL;
cc84fd5d
AD
37
38static void
39print_derives (void)
40{
d7913476 41 int i;
cc84fd5d 42
c87d4863 43 fputs ("DERIVES\n", stderr);
cc84fd5d
AD
44
45 for (i = ntokens; i < nsyms; i++)
46 {
bb0027a9 47 rule_t **rp;
ad949da9 48 fprintf (stderr, "\t%s derives\n", symbols[i]->tag);
bb0027a9 49 for (rp = derives[i]; *rp; ++rp)
720e5c1b 50 {
bb0027a9
AD
51 fprintf (stderr, "\t\t%3d ", (*rp)->user_number);
52 rule_rhs_print (*rp, stderr);
720e5c1b 53 }
cc84fd5d
AD
54 }
55
c87d4863 56 fputs ("\n\n", stderr);
cc84fd5d
AD
57}
58
122c0aac 59
122c0aac 60void
bb0027a9 61derives_compute (void)
122c0aac 62{
9222837b 63 symbol_number_t i;
4b3d3a8e 64 int r;
bb0027a9 65 rule_t **q;
122c0aac 66
e1a4f3a4
AD
67 /* DSET[NTERM] -- A linked list of the numbers of the rules whose
68 LHS is NTERM. */
69 rule_list_t **dset = XCALLOC (rule_list_t *, nvars) - ntokens;
70
71 /* DELTS[RULE] -- There are NRULES rule number to attach to nterms.
72 Instead of performing NRULES allocations for each, have an array
73 indexed by rule numbers. */
4b3d3a8e 74 rule_list_t *delts = XCALLOC (rule_list_t, nrules);
122c0aac 75
4b3d3a8e 76 for (r = nrules - 1; r >= 0; --r)
bba97eb2 77 {
9222837b 78 symbol_number_t lhs = rules[r].lhs->number;
e1a4f3a4
AD
79 rule_list_t *p = &delts[r];
80 /* A new LHS is found. */
bba97eb2 81 p->next = dset[lhs];
bb0027a9 82 p->value = &rules[r];
bba97eb2 83 dset[lhs] = p;
bba97eb2 84 }
122c0aac 85
e1a4f3a4
AD
86 /* DSET contains what we need under the form of a linked list. Make
87 it a single array. */
88
bb0027a9
AD
89 derives = XCALLOC (rule_t **, nvars) - ntokens;
90 q = XCALLOC (rule_t *, nvars + int_of_rule_number (nrules));
122c0aac
RS
91
92 for (i = ntokens; i < nsyms; i++)
93 {
e1a4f3a4 94 rule_list_t *p = dset[i];
122c0aac 95 derives[i] = q;
122c0aac
RS
96 while (p)
97 {
98 *q++ = p->value;
99 p = p->next;
100 }
bb0027a9 101 *q++ = NULL;
122c0aac
RS
102 }
103
273a74fa 104 if (trace_flag & trace_sets)
9bfe901c 105 print_derives ();
122c0aac 106
e1a4f3a4
AD
107 free (dset + ntokens);
108 free (delts);
122c0aac
RS
109}
110
e1a4f3a4 111
122c0aac 112void
bb0027a9 113derives_free (void)
122c0aac 114{
d7913476
AD
115 XFREE (derives[ntokens]);
116 XFREE (derives + ntokens);
122c0aac 117}