]>
Commit | Line | Data |
---|---|---|
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 | short *r; | |
50 | short *s1; | |
51 | short *s2; | |
52 | shorts *p; | |
53 | ||
54 | short *squeue; | |
55 | short *rcount; | |
56 | shorts **rsets; | |
57 | shorts *relts; | |
58 | ||
59 | if (trace_flag) | |
60 | fprintf (stderr, "Entering set_nullable\n"); | |
61 | ||
62 | nullable = XCALLOC (char, nvars) - ntokens; | |
63 | ||
64 | squeue = XCALLOC (short, nvars); | |
65 | s1 = s2 = squeue; | |
66 | ||
67 | rcount = XCALLOC (short, nrules + 1); | |
68 | ||
69 | /* RITEM contains all the rules, including useless productions. | |
70 | Hence we must allocate room for useless nonterminals too. */ | |
71 | rsets = XCALLOC (shorts *, nvars + nuseless_nonterminals) - ntokens; | |
72 | /* This is said to be more elements than we actually use. | |
73 | Supposedly nitems - nrules is enough. | |
74 | But why take the risk? */ | |
75 | relts = XCALLOC (shorts, nitems + nvars + nuseless_nonterminals + 1); | |
76 | p = relts; | |
77 | ||
78 | for (r = ritem; *r; ++r) | |
79 | { | |
80 | /* Walk RITEM to find (i), if there are any tokens in the | |
81 | RHS, and (ii), to find RULENO. */ | |
82 | int ruleno; | |
83 | int any_tokens = 0; | |
84 | short *r1; | |
85 | for (r1 = r; *r1 > 0; ++r1) | |
86 | if (ISTOKEN (*r1)) | |
87 | any_tokens = 1; | |
88 | ruleno = -*r1; | |
89 | ||
90 | /* Examine the RHS of the rule. */ | |
91 | if (!any_tokens) | |
92 | for (/* Nothing. */; *r > 0; ++r) | |
93 | { | |
94 | rcount[ruleno]++; | |
95 | p->next = rsets[*r]; | |
96 | p->value = ruleno; | |
97 | rsets[*r] = p; | |
98 | p++; | |
99 | } | |
100 | ||
101 | /* Examine its LHS. */ | |
102 | if (rule_table[ruleno].useful && !nullable[rule_table[ruleno].lhs]) | |
103 | { | |
104 | nullable[rule_table[ruleno].lhs] = 1; | |
105 | *s2++ = rule_table[ruleno].lhs; | |
106 | } | |
107 | } | |
108 | ||
109 | while (s1 < s2) | |
110 | for (p = rsets[*s1++]; p; p = p->next) | |
111 | { | |
112 | int ruleno = p->value; | |
113 | if (--rcount[ruleno] == 0) | |
114 | if (rule_table[ruleno].useful && !nullable[rule_table[ruleno].lhs]) | |
115 | { | |
116 | nullable[rule_table[ruleno].lhs] = 1; | |
117 | *s2++ = rule_table[ruleno].lhs; | |
118 | } | |
119 | } | |
120 | ||
121 | XFREE (squeue); | |
122 | XFREE (rcount); | |
123 | XFREE (rsets + ntokens); | |
124 | XFREE (relts); | |
125 | ||
126 | if (trace_flag) | |
127 | nullable_print (stderr); | |
128 | } | |
129 | ||
130 | ||
131 | void | |
132 | free_nullable (void) | |
133 | { | |
134 | XFREE (nullable + ntokens); | |
135 | } |