]>
Commit | Line | Data |
---|---|---|
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 | |
e68e0410 AD |
33 | /* Linked list of rule numbers. */ |
34 | typedef struct rule_list_s | |
35 | { | |
36 | struct rule_list_s *next; | |
37 | rule_number_t value; | |
38 | } rule_list_t; | |
39 | ||
3519ec76 | 40 | char *nullable = NULL; |
122c0aac | 41 | |
6bb1878b AD |
42 | static void |
43 | nullable_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 | 52 | void |
d2729d44 | 53 | set_nullable (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); |
ed8e1f68 AD |
61 | short *rcount = XCALLOC (short, nrules + 1); |
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 | |
9bfe901c | 69 | if (trace_flag) |
c87d4863 | 70 | fprintf (stderr, "Entering set_nullable\n"); |
122c0aac | 71 | |
d7913476 | 72 | nullable = XCALLOC (char, nvars) - ntokens; |
122c0aac | 73 | |
122c0aac | 74 | s1 = s2 = squeue; |
122c0aac RS |
75 | p = relts; |
76 | ||
ed8e1f68 | 77 | for (ruleno = 1; ruleno < nrules + 1; ++ruleno) |
1a2b5d37 | 78 | if (rules[ruleno].useful) |
ed8e1f68 | 79 | { |
9222837b AD |
80 | rule_t *rule = &rules[ruleno]; |
81 | if (rule->rhs[0] >= 0) | |
9c2c67e6 AD |
82 | { |
83 | /* This rule has a non empty RHS. */ | |
9222837b | 84 | item_number_t *r = NULL; |
9c2c67e6 | 85 | int any_tokens = 0; |
9222837b | 86 | for (r = rule->rhs; *r >= 0; ++r) |
9c2c67e6 AD |
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) | |
9222837b | 93 | for (r = rule->rhs; *r >= 0; ++r) |
9c2c67e6 AD |
94 | { |
95 | rcount[ruleno]++; | |
96 | p->next = rsets[*r]; | |
97 | p->value = ruleno; | |
98 | rsets[*r] = p; | |
99 | p++; | |
100 | } | |
101 | } | |
102 | else | |
81b51460 | 103 | { |
9c2c67e6 | 104 | /* This rule has an empty RHS. */ |
9222837b AD |
105 | assert (rule_number_of_item_number (rule->rhs[0]) == ruleno); |
106 | if (rule->useful && !nullable[rule->lhs->number]) | |
9c2c67e6 | 107 | { |
9222837b AD |
108 | nullable[rule->lhs->number] = 1; |
109 | *s2++ = rule->lhs->number; | |
9c2c67e6 | 110 | } |
81b51460 | 111 | } |
ed8e1f68 | 112 | } |
122c0aac RS |
113 | |
114 | while (s1 < s2) | |
e3e4e814 AD |
115 | for (p = rsets[*s1++]; p; p = p->next) |
116 | { | |
ed8e1f68 | 117 | ruleno = p->value; |
e3e4e814 | 118 | if (--rcount[ruleno] == 0) |
bba97eb2 | 119 | if (rules[ruleno].useful && !nullable[rules[ruleno].lhs->number]) |
122c0aac | 120 | { |
bba97eb2 AD |
121 | nullable[rules[ruleno].lhs->number] = 1; |
122 | *s2++ = rules[ruleno].lhs->number; | |
122c0aac | 123 | } |
e3e4e814 | 124 | } |
122c0aac | 125 | |
d7913476 AD |
126 | XFREE (squeue); |
127 | XFREE (rcount); | |
128 | XFREE (rsets + ntokens); | |
129 | XFREE (relts); | |
6bb1878b AD |
130 | |
131 | if (trace_flag) | |
132 | nullable_print (stderr); | |
122c0aac RS |
133 | } |
134 | ||
135 | ||
136 | void | |
d2729d44 | 137 | free_nullable (void) |
122c0aac | 138 | { |
d7913476 | 139 | XFREE (nullable + ntokens); |
122c0aac | 140 | } |