]> git.saurik.com Git - bison.git/blame_incremental - src/derives.c
style: use %code for local function declarations in our parser
[bison.git] / src / derives.c
... / ...
CommitLineData
1/* Match rules with nonterminals for bison,
2
3 Copyright (C) 1984, 1989, 2000-2003, 2005, 2009-2013 Free Software
4 Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
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.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21#include <config.h>
22#include "system.h"
23
24#include "getargs.h"
25
26#include "derives.h"
27#include "gram.h"
28#include "reader.h"
29#include "symtab.h"
30
31/* Linked list of rule numbers. */
32typedef struct rule_list
33{
34 struct rule_list *next;
35 rule *value;
36} rule_list;
37
38rule ***derives;
39
40static void
41print_derives (void)
42{
43 int i;
44
45 fputs ("DERIVES\n", stderr);
46
47 for (i = ntokens; i < nsyms; i++)
48 {
49 rule **rp;
50 fprintf (stderr, " %s derives\n", symbols[i]->tag);
51 for (rp = derives[i - ntokens]; *rp; ++rp)
52 {
53 fprintf (stderr, " %3d ", (*rp)->user_number);
54 rule_rhs_print (*rp, stderr);
55 fprintf (stderr, "\n");
56 }
57 }
58
59 fputs ("\n\n", stderr);
60}
61
62
63void
64derives_compute (void)
65{
66 symbol_number i;
67 rule_number r;
68 rule **q;
69
70 /* DSET[NTERM - NTOKENS] -- A linked list of the numbers of the rules
71 whose LHS is NTERM. */
72 rule_list **dset = xcalloc (nvars, sizeof *dset);
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. */
77 rule_list *delts = xnmalloc (nrules, sizeof *delts);
78
79 for (r = nrules - 1; r >= 0; --r)
80 {
81 symbol_number lhs = rules[r].lhs->number;
82 rule_list *p = &delts[r];
83 /* A new LHS is found. */
84 p->next = dset[lhs - ntokens];
85 p->value = &rules[r];
86 dset[lhs - ntokens] = p;
87 }
88
89 /* DSET contains what we need under the form of a linked list. Make
90 it a single array. */
91
92 derives = xnmalloc (nvars, sizeof *derives);
93 q = xnmalloc (nvars + nrules, sizeof *q);
94
95 for (i = ntokens; i < nsyms; i++)
96 {
97 rule_list *p = dset[i - ntokens];
98 derives[i - ntokens] = q;
99 while (p)
100 {
101 *q++ = p->value;
102 p = p->next;
103 }
104 *q++ = NULL;
105 }
106
107 if (trace_flag & trace_sets)
108 print_derives ();
109
110 free (dset);
111 free (delts);
112}
113
114
115void
116derives_free (void)
117{
118 free (derives[0]);
119 free (derives);
120}