]> git.saurik.com Git - bison.git/blame - src/derives.c
Some checks for premature EOF.
[bison.git] / src / derives.c
CommitLineData
122c0aac
RS
1/* Match rules with nonterminals for bison,
2 Copyright (C) 1984, 1989 Free Software Foundation, Inc.
3
4This file is part of Bison, the GNU Compiler Compiler.
5
6Bison is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11Bison is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with Bison; see the file COPYING. If not, write to
18the 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"
7612000c 28#include "alloc.h"
122c0aac
RS
29#include "types.h"
30#include "gram.h"
31
d2729d44
JT
32void set_derives PARAMS((void));
33void free_derives PARAMS((void));
122c0aac
RS
34
35short **derives;
36
37void
d2729d44 38set_derives (void)
122c0aac
RS
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
86void
d2729d44 87free_derives (void)
122c0aac
RS
88{
89 FREE(derives[ntokens]);
90 FREE(derives + ntokens);
91}
92
93
94
95#ifdef DEBUG
96
d2729d44
JT
97void
98print_derives (void)
122c0aac
RS
99{
100 register int i;
101 register short *sp;
102
103 extern char **tags;
104
a083fbbf 105 printf(_("\n\n\nDERIVES\n\n"));
122c0aac
RS
106
107 for (i = ntokens; i < nsyms; i++)
108 {
a083fbbf 109 printf(_("%s derives"), tags[i]);
122c0aac
RS
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