]> git.saurik.com Git - bison.git/blame - src/nullable.c
(b4_yysymprint_generate): yyout -> yyoutput,
[bison.git] / src / nullable.c
CommitLineData
122c0aac 1/* Part of the bison parser generator,
e68e0410 2 Copyright (C) 1984, 1989, 2000, 2001, 2002 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 29#include "gram.h"
630e182b 30#include "reduce.h"
3519ec76 31#include "nullable.h"
122c0aac 32
bb0027a9 33/* Linked list of rules. */
e68e0410
AD
34typedef struct rule_list_s
35{
36 struct rule_list_s *next;
bb0027a9 37 rule_t *value;
e68e0410
AD
38} rule_list_t;
39
bb0027a9 40bool *nullable = NULL;
122c0aac 41
6bb1878b
AD
42static void
43nullable_print (FILE *out)
44{
45 int i;
46 fputs ("NULLABLE\n", out);
47 for (i = ntokens; i < nsyms; i++)
ad949da9 48 fprintf (out, "\t%s: %s\n", symbols[i]->tag, nullable[i] ? "yes" : "no");
6bb1878b
AD
49 fputs ("\n\n", out);
50}
51
122c0aac 52void
bb0027a9 53nullable_compute (void)
122c0aac 54{
9222837b 55 rule_number_t ruleno;
a49aecd5
AD
56 symbol_number_t *s1;
57 symbol_number_t *s2;
e68e0410 58 rule_list_t *p;
122c0aac 59
a49aecd5 60 symbol_number_t *squeue = XCALLOC (symbol_number_t, nvars);
4b3d3a8e 61 short *rcount = XCALLOC (short, nrules);
ed8e1f68
AD
62 /* RITEM contains all the rules, including useless productions.
63 Hence we must allocate room for useless nonterminals too. */
e68e0410 64 rule_list_t **rsets = XCALLOC (rule_list_t *, nvars) - ntokens;
ed8e1f68 65 /* This is said to be more elements than we actually use.
9e7f6bbd 66 Supposedly NRITEMS - NRULES is enough. But why take the risk? */
e68e0410 67 rule_list_t *relts = XCALLOC (rule_list_t, nritems + nvars + 1);
122c0aac 68
bb0027a9 69 nullable = XCALLOC (bool, nvars) - ntokens;
122c0aac 70
122c0aac 71 s1 = s2 = squeue;
122c0aac
RS
72 p = relts;
73
4b3d3a8e 74 for (ruleno = 0; ruleno < nrules; ++ruleno)
1a2b5d37 75 if (rules[ruleno].useful)
ed8e1f68 76 {
9222837b
AD
77 rule_t *rule = &rules[ruleno];
78 if (rule->rhs[0] >= 0)
9c2c67e6
AD
79 {
80 /* This rule has a non empty RHS. */
9222837b 81 item_number_t *r = NULL;
9c2c67e6 82 int any_tokens = 0;
9222837b 83 for (r = rule->rhs; *r >= 0; ++r)
9c2c67e6
AD
84 if (ISTOKEN (*r))
85 any_tokens = 1;
86
87 /* This rule has only nonterminals: schedule it for the second
88 pass. */
89 if (!any_tokens)
9222837b 90 for (r = rule->rhs; *r >= 0; ++r)
9c2c67e6
AD
91 {
92 rcount[ruleno]++;
93 p->next = rsets[*r];
bb0027a9 94 p->value = rule;
9c2c67e6
AD
95 rsets[*r] = p;
96 p++;
97 }
98 }
99 else
81b51460 100 {
9c2c67e6 101 /* This rule has an empty RHS. */
b475e0cc
PE
102 if (item_number_as_rule_number (rule->rhs[0]) != ruleno)
103 abort ();
9222837b 104 if (rule->useful && !nullable[rule->lhs->number])
9c2c67e6 105 {
9222837b
AD
106 nullable[rule->lhs->number] = 1;
107 *s2++ = rule->lhs->number;
9c2c67e6 108 }
81b51460 109 }
ed8e1f68 110 }
122c0aac
RS
111
112 while (s1 < s2)
e3e4e814
AD
113 for (p = rsets[*s1++]; p; p = p->next)
114 {
bb0027a9
AD
115 rule_t *rule = p->value;
116 if (--rcount[rule->number] == 0)
117 if (rule->useful && !nullable[rule->lhs->number])
122c0aac 118 {
bb0027a9
AD
119 nullable[rule->lhs->number] = 1;
120 *s2++ = rule->lhs->number;
122c0aac 121 }
e3e4e814 122 }
122c0aac 123
d7913476
AD
124 XFREE (squeue);
125 XFREE (rcount);
126 XFREE (rsets + ntokens);
127 XFREE (relts);
6bb1878b 128
273a74fa 129 if (trace_flag & trace_sets)
6bb1878b 130 nullable_print (stderr);
122c0aac
RS
131}
132
133
134void
bb0027a9 135nullable_free (void)
122c0aac 136{
d7913476 137 XFREE (nullable + ntokens);
122c0aac 138}