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