]>
git.saurik.com Git - bison.git/blob - src/nullable.c
ef65ef46af481826b19ab90c4614b1c11551b562
   1 /* Calculate which nonterminals can expand into the null string for Bison. 
   3    Copyright (C) 1984, 1989, 2000, 2001, 2002, 2003, 2004, 2005 Free 
   4    Software Foundation, Inc. 
   6    This file is part of Bison, the GNU Compiler Compiler. 
   8    Bison is free software; you can redistribute it and/or modify 
   9    it under the terms of the GNU General Public License as published by 
  10    the Free Software Foundation; either version 2, or (at your option) 
  13    Bison is distributed in the hope that it will be useful, 
  14    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  16    GNU General Public License for more details. 
  18    You should have received a copy of the GNU General Public License 
  19    along with Bison; see the file COPYING.  If not, write to 
  20    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
  21    Boston, MA 02110-1301, USA.  */ 
  24 /* Set up NULLABLE, a vector saying which nonterminals can expand into 
  25    the null string.  NULLABLE[I - NTOKENS] is nonzero if symbol I can 
  37 /* Linked list of rules.  */ 
  38 typedef struct rule_list
 
  40   struct rule_list 
*next
; 
  44 bool *nullable 
= NULL
; 
  47 nullable_print (FILE *out
) 
  50   fputs ("NULLABLE\n", out
); 
  51   for (i 
= ntokens
; i 
< nsyms
; i
++) 
  52     fprintf (out
, "\t%s: %s\n", symbols
[i
]->tag
, 
  53              nullable
[i 
- ntokens
] ? "yes" : "no"); 
  58 nullable_compute (void) 
  65   symbol_number 
*squeue 
= xnmalloc (nvars
, sizeof *squeue
); 
  66   size_t *rcount 
= xcalloc (nrules
, sizeof *rcount
); 
  67   /* RITEM contains all the rules, including useless productions. 
  68      Hence we must allocate room for useless nonterminals too.  */ 
  69   rule_list 
**rsets 
= xcalloc (nvars
, sizeof *rsets
); 
  70   /* This is said to be more elements than we actually use. 
  71      Supposedly NRITEMS - NRULES is enough.  But why take the risk?  */ 
  72   rule_list 
*relts 
= xnmalloc (nritems 
+ nvars 
+ 1, sizeof *relts
); 
  74   nullable 
= xcalloc (nvars
, sizeof *nullable
); 
  79   for (ruleno 
= 0; ruleno 
< nrules
; ++ruleno
) 
  80     if (rules
[ruleno
].useful
) 
  82         rule 
*rules_ruleno 
= &rules
[ruleno
]; 
  83         if (rules_ruleno
->rhs
[0] >= 0) 
  85             /* This rule has a non empty RHS. */ 
  86             item_number 
*rp 
= NULL
; 
  87             bool any_tokens 
= false; 
  88             for (rp 
= rules_ruleno
->rhs
; *rp 
>= 0; ++rp
) 
  92             /* This rule has only nonterminals: schedule it for the second 
  95               for (rp 
= rules_ruleno
->rhs
; *rp 
>= 0; ++rp
) 
  98                   p
->next 
= rsets
[*rp 
- ntokens
]; 
  99                   p
->value 
= rules_ruleno
; 
 100                   rsets
[*rp 
- ntokens
] = p
; 
 106             /* This rule has an empty RHS. */ 
 107             if (item_number_as_rule_number (rules_ruleno
->rhs
[0]) != ruleno
) 
 109             if (rules_ruleno
->useful
 
 110                 && ! nullable
[rules_ruleno
->lhs
->number 
- ntokens
]) 
 112                 nullable
[rules_ruleno
->lhs
->number 
- ntokens
] = true; 
 113                 *s2
++ = rules_ruleno
->lhs
->number
; 
 119     for (p 
= rsets
[*s1
++ - ntokens
]; p
; p 
= p
->next
) 
 122         if (--rcount
[r
->number
] == 0) 
 123           if (r
->useful 
&& ! nullable
[r
->lhs
->number 
- ntokens
]) 
 125               nullable
[r
->lhs
->number 
- ntokens
] = true; 
 126               *s2
++ = r
->lhs
->number
; 
 135   if (trace_flag 
& trace_sets
) 
 136     nullable_print (stderr
);