]> git.saurik.com Git - bison.git/blob - src/nullable.c
* src/Makefile.am, lib/Makefile.am: Use $(VAR) instead of @VAR@.
[bison.git] / src / nullable.c
1 /* Calculate which nonterminals can expand into the null string for Bison.
2
3 Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 /* Set up NULLABLE, a vector saying which nonterminals can expand into
24 the null string. NULLABLE[I - NTOKENS] is nonzero if symbol I can
25 do so. */
26
27 #include "system.h"
28
29 #include "getargs.h"
30 #include "gram.h"
31 #include "nullable.h"
32 #include "reduce.h"
33 #include "symtab.h"
34
35 /* Linked list of rules. */
36 typedef struct rule_list
37 {
38 struct rule_list *next;
39 rule *value;
40 } rule_list;
41
42 bool *nullable = NULL;
43
44 static void
45 nullable_print (FILE *out)
46 {
47 int i;
48 fputs ("NULLABLE\n", out);
49 for (i = ntokens; i < nsyms; i++)
50 fprintf (out, "\t%s: %s\n", symbols[i]->tag,
51 nullable[i - ntokens] ? "yes" : "no");
52 fputs ("\n\n", out);
53 }
54
55 void
56 nullable_compute (void)
57 {
58 rule_number ruleno;
59 symbol_number *s1;
60 symbol_number *s2;
61 rule_list *p;
62
63 symbol_number *squeue = CALLOC (squeue, nvars);
64 short *rcount = CALLOC (rcount, nrules);
65 /* RITEM contains all the rules, including useless productions.
66 Hence we must allocate room for useless nonterminals too. */
67 rule_list **rsets = CALLOC (rsets, nvars);
68 /* This is said to be more elements than we actually use.
69 Supposedly NRITEMS - NRULES is enough. But why take the risk? */
70 rule_list *relts = CALLOC (relts, nritems + nvars + 1);
71
72 CALLOC (nullable, nvars);
73
74 s1 = s2 = squeue;
75 p = relts;
76
77 for (ruleno = 0; ruleno < nrules; ++ruleno)
78 if (rules[ruleno].useful)
79 {
80 rule *rules_ruleno = &rules[ruleno];
81 if (rules_ruleno->rhs[0] >= 0)
82 {
83 /* This rule has a non empty RHS. */
84 item_number *r = NULL;
85 int any_tokens = 0;
86 for (r = rules_ruleno->rhs; *r >= 0; ++r)
87 if (ISTOKEN (*r))
88 any_tokens = 1;
89
90 /* This rule has only nonterminals: schedule it for the second
91 pass. */
92 if (!any_tokens)
93 for (r = rules_ruleno->rhs; *r >= 0; ++r)
94 {
95 rcount[ruleno]++;
96 p->next = rsets[*r - ntokens];
97 p->value = rules_ruleno;
98 rsets[*r - ntokens] = p;
99 p++;
100 }
101 }
102 else
103 {
104 /* This rule has an empty RHS. */
105 if (item_number_as_rule_number (rules_ruleno->rhs[0]) != ruleno)
106 abort ();
107 if (rules_ruleno->useful
108 && ! nullable[rules_ruleno->lhs->number - ntokens])
109 {
110 nullable[rules_ruleno->lhs->number - ntokens] = 1;
111 *s2++ = rules_ruleno->lhs->number;
112 }
113 }
114 }
115
116 while (s1 < s2)
117 for (p = rsets[*s1++ - ntokens]; p; p = p->next)
118 {
119 rule *r = p->value;
120 if (--rcount[r->number] == 0)
121 if (r->useful && ! nullable[r->lhs->number - ntokens])
122 {
123 nullable[r->lhs->number - ntokens] = 1;
124 *s2++ = r->lhs->number;
125 }
126 }
127
128 XFREE (squeue);
129 XFREE (rcount);
130 XFREE (rsets);
131 XFREE (relts);
132
133 if (trace_flag & trace_sets)
134 nullable_print (stderr);
135 }
136
137
138 void
139 nullable_free (void)
140 {
141 XFREE (nullable);
142 }