]> git.saurik.com Git - bison.git/blob - src/nullable.c
* src/gram.h, src/gram.c (symbols): New, similar to state_table
[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 "reduce.h"
32 #include "nullable.h"
33
34 char *nullable = NULL;
35
36 static void
37 nullable_print (FILE *out)
38 {
39 int i;
40 fputs ("NULLABLE\n", out);
41 for (i = ntokens; i < nsyms; i++)
42 fprintf (out, "\t%s: %s\n", tags[i], nullable[i] ? "yes" : "no");
43 fputs ("\n\n", out);
44 }
45
46 void
47 set_nullable (void)
48 {
49 int ruleno;
50 short *s1;
51 short *s2;
52 shorts *p;
53
54 short *squeue = XCALLOC (short, nvars);
55 short *rcount = XCALLOC (short, nrules + 1);
56 /* RITEM contains all the rules, including useless productions.
57 Hence we must allocate room for useless nonterminals too. */
58 shorts **rsets = XCALLOC (shorts *, nvars) - ntokens;
59 /* This is said to be more elements than we actually use.
60 Supposedly NRITEMS - NRULES is enough. But why take the risk? */
61 shorts *relts = XCALLOC (shorts, nritems + nvars + 1);
62
63 if (trace_flag)
64 fprintf (stderr, "Entering set_nullable\n");
65
66 nullable = XCALLOC (char, nvars) - ntokens;
67
68 s1 = s2 = squeue;
69 p = relts;
70
71 for (ruleno = 1; ruleno < nrules + 1; ++ruleno)
72 if (rule_table[ruleno].useful)
73 {
74 if (ritem[rule_table[ruleno].rhs] >= 0)
75 {
76 /* This rule has a non empty RHS. */
77 short *r;
78 int any_tokens = 0;
79 for (r = &ritem[rule_table[ruleno].rhs]; *r >= 0; ++r)
80 if (ISTOKEN (*r))
81 any_tokens = 1;
82
83 /* This rule has only nonterminals: schedule it for the second
84 pass. */
85 if (!any_tokens)
86 for (r = &ritem[rule_table[ruleno].rhs]; *r >= 0; ++r)
87 {
88 rcount[ruleno]++;
89 p->next = rsets[*r];
90 p->value = ruleno;
91 rsets[*r] = p;
92 p++;
93 }
94 }
95 else
96 {
97 /* This rule has an empty RHS. */
98 assert (ritem[rule_table[ruleno].rhs] == -ruleno);
99 if (rule_table[ruleno].useful && !nullable[rule_table[ruleno].lhs])
100 {
101 nullable[rule_table[ruleno].lhs] = 1;
102 *s2++ = rule_table[ruleno].lhs;
103 }
104 }
105 }
106
107 while (s1 < s2)
108 for (p = rsets[*s1++]; p; p = p->next)
109 {
110 ruleno = p->value;
111 if (--rcount[ruleno] == 0)
112 if (rule_table[ruleno].useful && !nullable[rule_table[ruleno].lhs])
113 {
114 nullable[rule_table[ruleno].lhs] = 1;
115 *s2++ = rule_table[ruleno].lhs;
116 }
117 }
118
119 XFREE (squeue);
120 XFREE (rcount);
121 XFREE (rsets + ntokens);
122 XFREE (relts);
123
124 if (trace_flag)
125 nullable_print (stderr);
126 }
127
128
129 void
130 free_nullable (void)
131 {
132 XFREE (nullable + ntokens);
133 }