| 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 "nullable.h" |
| 32 | |
| 33 | char *nullable = NULL; |
| 34 | |
| 35 | static void |
| 36 | nullable_print (FILE *out) |
| 37 | { |
| 38 | int i; |
| 39 | fputs ("NULLABLE\n", out); |
| 40 | for (i = ntokens; i < nsyms; i++) |
| 41 | fprintf (out, "\t%s: %s\n", tags[i], nullable[i] ? "yes" : "no"); |
| 42 | fputs ("\n\n", out); |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | set_nullable (void) |
| 47 | { |
| 48 | short *r; |
| 49 | short *s1; |
| 50 | short *s2; |
| 51 | shorts *p; |
| 52 | |
| 53 | short *squeue; |
| 54 | short *rcount; |
| 55 | shorts **rsets; |
| 56 | shorts *relts; |
| 57 | |
| 58 | if (trace_flag) |
| 59 | fprintf (stderr, "Entering set_nullable\n"); |
| 60 | |
| 61 | nullable = XCALLOC (char, nvars) - ntokens; |
| 62 | |
| 63 | squeue = XCALLOC (short, nvars); |
| 64 | s1 = s2 = squeue; |
| 65 | |
| 66 | rcount = XCALLOC (short, nrules + 1); |
| 67 | rsets = XCALLOC (shorts *, nvars) - ntokens; |
| 68 | /* This is said to be more elements than we actually use. |
| 69 | Supposedly nitems - nrules is enough. |
| 70 | But why take the risk? */ |
| 71 | relts = XCALLOC (shorts, nitems + nvars + 1); |
| 72 | p = relts; |
| 73 | |
| 74 | r = ritem; |
| 75 | while (*r) |
| 76 | { |
| 77 | if (*r < 0) |
| 78 | { |
| 79 | int symbol = rule_table[-(*r++)].lhs; |
| 80 | if (symbol >= 0 && !nullable[symbol]) |
| 81 | { |
| 82 | nullable[symbol] = 1; |
| 83 | *s2++ = symbol; |
| 84 | } |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | int any_tokens = 0; |
| 89 | int symbol; |
| 90 | short *r1 = r; |
| 91 | for (symbol = *r++; symbol > 0; symbol = *r++) |
| 92 | if (ISTOKEN (symbol)) |
| 93 | any_tokens = 1; |
| 94 | |
| 95 | if (!any_tokens) |
| 96 | { |
| 97 | int ruleno = -symbol; |
| 98 | r = r1; |
| 99 | for (symbol = *r++; symbol > 0; symbol = *r++) |
| 100 | { |
| 101 | rcount[ruleno]++; |
| 102 | p->next = rsets[symbol]; |
| 103 | p->value = ruleno; |
| 104 | rsets[symbol] = p; |
| 105 | p++; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | while (s1 < s2) |
| 112 | { |
| 113 | p = rsets[*s1++]; |
| 114 | while (p) |
| 115 | { |
| 116 | int ruleno = p->value; |
| 117 | p = p->next; |
| 118 | if (--rcount[ruleno] == 0) |
| 119 | { |
| 120 | int symbol = rule_table[ruleno].lhs; |
| 121 | if (symbol >= 0 && !nullable[symbol]) |
| 122 | { |
| 123 | nullable[symbol] = 1; |
| 124 | *s2++ = symbol; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | XFREE (squeue); |
| 131 | XFREE (rcount); |
| 132 | XFREE (rsets + ntokens); |
| 133 | XFREE (relts); |
| 134 | |
| 135 | if (trace_flag) |
| 136 | nullable_print (stderr); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | void |
| 141 | free_nullable (void) |
| 142 | { |
| 143 | XFREE (nullable + ntokens); |
| 144 | } |