]> git.saurik.com Git - bison.git/blame - src/nullable.c
* src/closure.c (print_fderives): Use rule_rhs_print.
[bison.git] / src / nullable.c
CommitLineData
122c0aac 1/* Part of the bison parser generator,
6986fd9e 2 Copyright 1984, 1989, 2000, 2001 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 26#include "system.h"
9bfe901c 27#include "getargs.h"
ad949da9 28#include "symtab.h"
122c0aac
RS
29#include "types.h"
30#include "gram.h"
630e182b 31#include "reduce.h"
3519ec76 32#include "nullable.h"
122c0aac 33
3519ec76 34char *nullable = NULL;
122c0aac 35
6bb1878b
AD
36static void
37nullable_print (FILE *out)
38{
39 int i;
40 fputs ("NULLABLE\n", out);
41 for (i = ntokens; i < nsyms; i++)
ad949da9 42 fprintf (out, "\t%s: %s\n", symbols[i]->tag, nullable[i] ? "yes" : "no");
6bb1878b
AD
43 fputs ("\n\n", out);
44}
45
122c0aac 46void
d2729d44 47set_nullable (void)
122c0aac 48{
9222837b 49 rule_number_t ruleno;
a49aecd5
AD
50 symbol_number_t *s1;
51 symbol_number_t *s2;
3519ec76 52 shorts *p;
122c0aac 53
a49aecd5 54 symbol_number_t *squeue = XCALLOC (symbol_number_t, nvars);
ed8e1f68
AD
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.
9e7f6bbd
AD
60 Supposedly NRITEMS - NRULES is enough. But why take the risk? */
61 shorts *relts = XCALLOC (shorts, nritems + nvars + 1);
122c0aac 62
9bfe901c 63 if (trace_flag)
c87d4863 64 fprintf (stderr, "Entering set_nullable\n");
122c0aac 65
d7913476 66 nullable = XCALLOC (char, nvars) - ntokens;
122c0aac 67
122c0aac 68 s1 = s2 = squeue;
122c0aac
RS
69 p = relts;
70
ed8e1f68 71 for (ruleno = 1; ruleno < nrules + 1; ++ruleno)
1a2b5d37 72 if (rules[ruleno].useful)
ed8e1f68 73 {
9222837b
AD
74 rule_t *rule = &rules[ruleno];
75 if (rule->rhs[0] >= 0)
9c2c67e6
AD
76 {
77 /* This rule has a non empty RHS. */
9222837b 78 item_number_t *r = NULL;
9c2c67e6 79 int any_tokens = 0;
9222837b 80 for (r = rule->rhs; *r >= 0; ++r)
9c2c67e6
AD
81 if (ISTOKEN (*r))
82 any_tokens = 1;
83
84 /* This rule has only nonterminals: schedule it for the second
85 pass. */
86 if (!any_tokens)
9222837b 87 for (r = rule->rhs; *r >= 0; ++r)
9c2c67e6
AD
88 {
89 rcount[ruleno]++;
90 p->next = rsets[*r];
91 p->value = ruleno;
92 rsets[*r] = p;
93 p++;
94 }
95 }
96 else
81b51460 97 {
9c2c67e6 98 /* This rule has an empty RHS. */
9222837b
AD
99 assert (rule_number_of_item_number (rule->rhs[0]) == ruleno);
100 if (rule->useful && !nullable[rule->lhs->number])
9c2c67e6 101 {
9222837b
AD
102 nullable[rule->lhs->number] = 1;
103 *s2++ = rule->lhs->number;
9c2c67e6 104 }
81b51460 105 }
ed8e1f68 106 }
122c0aac
RS
107
108 while (s1 < s2)
e3e4e814
AD
109 for (p = rsets[*s1++]; p; p = p->next)
110 {
ed8e1f68 111 ruleno = p->value;
e3e4e814 112 if (--rcount[ruleno] == 0)
bba97eb2 113 if (rules[ruleno].useful && !nullable[rules[ruleno].lhs->number])
122c0aac 114 {
bba97eb2
AD
115 nullable[rules[ruleno].lhs->number] = 1;
116 *s2++ = rules[ruleno].lhs->number;
122c0aac 117 }
e3e4e814 118 }
122c0aac 119
d7913476
AD
120 XFREE (squeue);
121 XFREE (rcount);
122 XFREE (rsets + ntokens);
123 XFREE (relts);
6bb1878b
AD
124
125 if (trace_flag)
126 nullable_print (stderr);
122c0aac
RS
127}
128
129
130void
d2729d44 131free_nullable (void)
122c0aac 132{
d7913476 133 XFREE (nullable + ntokens);
122c0aac 134}