]> git.saurik.com Git - bison.git/blob - src/nullable.c
Don't define PACKAGE here, since config.h defines it.
[bison.git] / src / nullable.c
1 /* Part of the bison parser generator,
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 up nullable, a vector saying which nonterminals can expand into the null string.
22 nullable[i - ntokens] is nonzero if symbol i can do so. */
23
24 #include <stdio.h>
25 #include "system.h"
26 #include "types.h"
27 #include "gram.h"
28 #include "alloc.h"
29
30
31 char *nullable;
32
33 void free_nullable PARAMS((void));
34 void set_nullable PARAMS((void));
35
36 void
37 set_nullable (void)
38 {
39 register short *r;
40 register short *s1;
41 register short *s2;
42 register int ruleno;
43 register int symbol;
44 register shorts *p;
45
46 short *squeue;
47 short *rcount;
48 shorts **rsets;
49 shorts *relts;
50 char any_tokens;
51 short *r1;
52
53 #ifdef TRACE
54 fprintf(stderr, _("Entering set_nullable"));
55 #endif
56
57 nullable = NEW2(nvars, char) - ntokens;
58
59 squeue = NEW2(nvars, short);
60 s1 = s2 = squeue;
61
62 rcount = NEW2(nrules + 1, short);
63 rsets = NEW2(nvars, shorts *) - ntokens;
64 /* This is said to be more elements than we actually use.
65 Supposedly nitems - nrules is enough.
66 But why take the risk? */
67 relts = NEW2(nitems + nvars + 1, shorts);
68 p = relts;
69
70 r = ritem;
71 while (*r)
72 {
73 if (*r < 0)
74 {
75 symbol = rlhs[-(*r++)];
76 if (symbol >= 0 && !nullable[symbol])
77 {
78 nullable[symbol] = 1;
79 *s2++ = symbol;
80 }
81 }
82 else
83 {
84 r1 = r;
85 any_tokens = 0;
86 for (symbol = *r++; symbol > 0; symbol = *r++)
87 {
88 if (ISTOKEN(symbol))
89 any_tokens = 1;
90 }
91
92 if (!any_tokens)
93 {
94 ruleno = -symbol;
95 r = r1;
96 for (symbol = *r++; symbol > 0; symbol = *r++)
97 {
98 rcount[ruleno]++;
99 p->next = rsets[symbol];
100 p->value = ruleno;
101 rsets[symbol] = p;
102 p++;
103 }
104 }
105 }
106 }
107
108 while (s1 < s2)
109 {
110 p = rsets[*s1++];
111 while (p)
112 {
113 ruleno = p->value;
114 p = p->next;
115 if (--rcount[ruleno] == 0)
116 {
117 symbol = rlhs[ruleno];
118 if (symbol >= 0 && !nullable[symbol])
119 {
120 nullable[symbol] = 1;
121 *s2++ = symbol;
122 }
123 }
124 }
125 }
126
127 FREE(squeue);
128 FREE(rcount);
129 FREE(rsets + ntokens);
130 FREE(relts);
131 }
132
133
134 void
135 free_nullable (void)
136 {
137 FREE(nullable + ntokens);
138 }