]> git.saurik.com Git - bison.git/blob - src/nullable.c
74164937f7296d020152b99dc04315ff900a9068
[bison.git] / src / nullable.c
1 /* Part of the bison parser generator,
2 Copyright 1984, 1989, 2000, 2001 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, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
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
24 do so. */
25
26 #include "system.h"
27 #include "getargs.h"
28 #include "reader.h"
29 #include "types.h"
30 #include "gram.h"
31 #include "nullable.h"
32
33 char *nullable = NULL;
34
35 static void
36 nullable_print (FILE *out)
37 {
38 int i;
39 fputs ("NULLABLE\n", out);
40 for (i = ntokens; i < nsyms; i++)
41 fprintf (out, "\t%s: %s\n", tags[i], nullable[i] ? "yes" : "no");
42 fputs ("\n\n", out);
43 }
44
45 void
46 set_nullable (void)
47 {
48 short *r;
49 short *s1;
50 short *s2;
51 shorts *p;
52
53 short *squeue;
54 short *rcount;
55 shorts **rsets;
56 shorts *relts;
57
58 if (trace_flag)
59 fprintf (stderr, "Entering set_nullable\n");
60
61 nullable = XCALLOC (char, nvars) - ntokens;
62
63 squeue = XCALLOC (short, nvars);
64 s1 = s2 = squeue;
65
66 rcount = XCALLOC (short, nrules + 1);
67 rsets = XCALLOC (shorts *, nvars) - ntokens;
68 /* This is said to be more elements than we actually use.
69 Supposedly nitems - nrules is enough.
70 But why take the risk? */
71 relts = XCALLOC (shorts, nitems + nvars + 1);
72 p = relts;
73
74 for (r = ritem; *r; ++r)
75 {
76 /* Walk RITEM to find (i), if there are any tokens in the
77 RHS, and (ii), to find RULENO. */
78 int ruleno;
79 int any_tokens = 0;
80 short *r1;
81 for (r1 = r; *r1 > 0; ++r1)
82 if (ISTOKEN (*r1))
83 any_tokens = 1;
84 ruleno = -*r1;
85
86 /* Examine the RHS of the rule. */
87 if (!any_tokens)
88 for (/* Nothing. */; *r > 0; ++r)
89 {
90 rcount[ruleno]++;
91 p->next = rsets[*r];
92 p->value = ruleno;
93 rsets[*r] = p;
94 p++;
95 }
96
97 /* Examine its LHS. */
98 if (rule_table[ruleno].useful && !nullable[rule_table[ruleno].lhs])
99 {
100 nullable[rule_table[ruleno].lhs] = 1;
101 *s2++ = rule_table[ruleno].lhs;
102 }
103 }
104
105 while (s1 < s2)
106 {
107 p = rsets[*s1++];
108 while (p)
109 {
110 int ruleno = p->value;
111 p = p->next;
112 if (--rcount[ruleno] == 0)
113 {
114 int symbol = rule_table[ruleno].lhs;
115 if (symbol >= 0 && !nullable[symbol])
116 {
117 nullable[symbol] = 1;
118 *s2++ = symbol;
119 }
120 }
121 }
122 }
123
124 XFREE (squeue);
125 XFREE (rcount);
126 XFREE (rsets + ntokens);
127 XFREE (relts);
128
129 if (trace_flag)
130 nullable_print (stderr);
131 }
132
133
134 void
135 free_nullable (void)
136 {
137 XFREE (nullable + ntokens);
138 }