]> git.saurik.com Git - bison.git/blob - src/derives.c
Use prototypes if the compiler understands them.
[bison.git] / src / derives.c
1 /* Match rules with nonterminals for bison,
2 Copyright (C) 1984, 1989 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
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.
10
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.
15
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21 /* set_derives finds, for each variable (nonterminal), which rules can derive it.
22 It sets up the value of derives so that
23 derives[i - ntokens] points to a vector of rule numbers,
24 terminated with -1. */
25
26 #include <stdio.h>
27 #include "system.h"
28 #include "alloc.h"
29 #include "types.h"
30 #include "gram.h"
31
32 void set_derives PARAMS((void));
33 void free_derives PARAMS((void));
34
35 short **derives;
36
37 void
38 set_derives (void)
39 {
40 register int i;
41 register int lhs;
42 register shorts *p;
43 register short *q;
44 register shorts **dset;
45 register shorts *delts;
46
47 dset = NEW2(nvars, shorts *) - ntokens;
48 delts = NEW2(nrules + 1, shorts);
49
50 p = delts;
51 for (i = nrules; i > 0; i--)
52 {
53 lhs = rlhs[i];
54 if (lhs >= 0)
55 {
56 p->next = dset[lhs];
57 p->value = i;
58 dset[lhs] = p;
59 p++;
60 }
61 }
62
63 derives = NEW2(nvars, short *) - ntokens;
64 q = NEW2(nvars + nrules, short);
65
66 for (i = ntokens; i < nsyms; i++)
67 {
68 derives[i] = q;
69 p = dset[i];
70 while (p)
71 {
72 *q++ = p->value;
73 p = p->next;
74 }
75 *q++ = -1;
76 }
77
78 #ifdef DEBUG
79 print_derives();
80 #endif
81
82 FREE(dset + ntokens);
83 FREE(delts);
84 }
85
86 void
87 free_derives (void)
88 {
89 FREE(derives[ntokens]);
90 FREE(derives + ntokens);
91 }
92
93
94
95 #ifdef DEBUG
96
97 void
98 print_derives (void)
99 {
100 register int i;
101 register short *sp;
102
103 extern char **tags;
104
105 printf(_("\n\n\nDERIVES\n\n"));
106
107 for (i = ntokens; i < nsyms; i++)
108 {
109 printf(_("%s derives"), tags[i]);
110 for (sp = derives[i]; *sp > 0; sp++)
111 {
112 printf(" %d", *sp);
113 }
114 putchar('\n');
115 }
116
117 putchar('\n');
118 }
119
120 #endif