]> git.saurik.com Git - bison.git/blame - src/nullable.c
Merge in branch-1_29.
[bison.git] / src / nullable.c
CommitLineData
122c0aac 1/* Part of the bison parser generator,
aa7815f5 2 Copyright 1984, 1989, 2000 Free Software Foundation, Inc.
122c0aac 3
3519ec76 4 This file is part of Bison, the GNU Compiler Compiler.
122c0aac 5
3519ec76
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
3519ec76
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
3519ec76
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
3519ec76
AD
22/* Set up NULLABLE, a vector saying which nonterminals can expand into
23 the null string. NULLABLE[I - NTOKENS] is nonzero if symbol I can
ceed8467 24 do so. */
122c0aac 25
122c0aac
RS
26#include "system.h"
27#include "types.h"
28#include "gram.h"
3519ec76 29#include "nullable.h"
122c0aac 30
3519ec76 31char *nullable = NULL;
122c0aac
RS
32
33void
d2729d44 34set_nullable (void)
122c0aac 35{
3519ec76
AD
36 short *r;
37 short *s1;
38 short *s2;
39 int ruleno;
40 int symbol;
41 shorts *p;
122c0aac
RS
42
43 short *squeue;
44 short *rcount;
45 shorts **rsets;
46 shorts *relts;
47 char any_tokens;
48 short *r1;
49
50#ifdef TRACE
3519ec76 51 fprintf (stderr, _("Entering set_nullable"));
122c0aac
RS
52#endif
53
d7913476 54 nullable = XCALLOC (char, nvars) - ntokens;
122c0aac 55
d7913476 56 squeue = XCALLOC (short, nvars);
122c0aac
RS
57 s1 = s2 = squeue;
58
d7913476
AD
59 rcount = XCALLOC (short, nrules + 1);
60 rsets = XCALLOC (shorts *, nvars) - ntokens;
122c0aac
RS
61 /* This is said to be more elements than we actually use.
62 Supposedly nitems - nrules is enough.
63 But why take the risk? */
d7913476 64 relts = XCALLOC (shorts, nitems + nvars + 1);
122c0aac
RS
65 p = relts;
66
67 r = ritem;
68 while (*r)
69 {
70 if (*r < 0)
71 {
72 symbol = rlhs[-(*r++)];
73 if (symbol >= 0 && !nullable[symbol])
74 {
75 nullable[symbol] = 1;
76 *s2++ = symbol;
77 }
78 }
79 else
80 {
81 r1 = r;
82 any_tokens = 0;
83 for (symbol = *r++; symbol > 0; symbol = *r++)
84 {
3519ec76 85 if (ISTOKEN (symbol))
122c0aac
RS
86 any_tokens = 1;
87 }
88
89 if (!any_tokens)
90 {
91 ruleno = -symbol;
92 r = r1;
93 for (symbol = *r++; symbol > 0; symbol = *r++)
94 {
95 rcount[ruleno]++;
96 p->next = rsets[symbol];
97 p->value = ruleno;
98 rsets[symbol] = p;
99 p++;
100 }
101 }
102 }
103 }
104
105 while (s1 < s2)
106 {
107 p = rsets[*s1++];
108 while (p)
109 {
110 ruleno = p->value;
111 p = p->next;
112 if (--rcount[ruleno] == 0)
113 {
114 symbol = rlhs[ruleno];
115 if (symbol >= 0 && !nullable[symbol])
116 {
117 nullable[symbol] = 1;
118 *s2++ = symbol;
119 }
120 }
121 }
122 }
123
d7913476
AD
124 XFREE (squeue);
125 XFREE (rcount);
126 XFREE (rsets + ntokens);
127 XFREE (relts);
122c0aac
RS
128}
129
130
131void
d2729d44 132free_nullable (void)
122c0aac 133{
d7913476 134 XFREE (nullable + ntokens);
122c0aac 135}