]>
git.saurik.com Git - bison.git/blob - src/nullable.c
   1 /* Calculate which nonterminals can expand into the null string for Bison. 
   3    Copyright (C) 1984, 1989, 2000, 2001, 2002, 2003 Free Software 
   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., 59 Temple Place - Suite 330, 
  21    Boston, MA 02111-1307, 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 
  36 /* Linked list of rules.  */ 
  37 typedef struct rule_list
 
  39   struct rule_list 
*next
; 
  43 bool *nullable 
= NULL
; 
  46 nullable_print (FILE *out
) 
  49   fputs ("NULLABLE\n", out
); 
  50   for (i 
= ntokens
; i 
< nsyms
; i
++) 
  51     fprintf (out
, "\t%s: %s\n", symbols
[i
]->tag
, 
  52              nullable
[i 
- ntokens
] ? "yes" : "no"); 
  57 nullable_compute (void) 
  64   symbol_number 
*squeue 
= CALLOC (squeue
, nvars
); 
  65   short *rcount 
= CALLOC (rcount
, nrules
); 
  66   /* RITEM contains all the rules, including useless productions. 
  67      Hence we must allocate room for useless nonterminals too.  */ 
  68   rule_list 
**rsets 
= CALLOC (rsets
, nvars
); 
  69   /* This is said to be more elements than we actually use. 
  70      Supposedly NRITEMS - NRULES is enough.  But why take the risk?  */ 
  71   rule_list 
*relts 
= CALLOC (relts
, nritems 
+ nvars 
+ 1); 
  73   CALLOC (nullable
, nvars
); 
  78   for (ruleno 
= 0; ruleno 
< nrules
; ++ruleno
) 
  79     if (rules
[ruleno
].useful
) 
  81         rule 
*rules_ruleno 
= &rules
[ruleno
]; 
  82         if (rules_ruleno
->rhs
[0] >= 0) 
  84             /* This rule has a non empty RHS. */ 
  85             item_number 
*rp 
= NULL
; 
  86             bool any_tokens 
= false; 
  87             for (rp 
= rules_ruleno
->rhs
; *rp 
>= 0; ++rp
) 
  91             /* This rule has only nonterminals: schedule it for the second 
  94               for (rp 
= rules_ruleno
->rhs
; *rp 
>= 0; ++rp
) 
  97                   p
->next 
= rsets
[*rp 
- ntokens
]; 
  98                   p
->value 
= rules_ruleno
; 
  99                   rsets
[*rp 
- ntokens
] = p
; 
 105             /* This rule has an empty RHS. */ 
 106             if (item_number_as_rule_number (rules_ruleno
->rhs
[0]) != ruleno
) 
 108             if (rules_ruleno
->useful
 
 109                 && ! nullable
[rules_ruleno
->lhs
->number 
- ntokens
]) 
 111                 nullable
[rules_ruleno
->lhs
->number 
- ntokens
] = true; 
 112                 *s2
++ = rules_ruleno
->lhs
->number
; 
 118     for (p 
= rsets
[*s1
++ - ntokens
]; p
; p 
= p
->next
) 
 121         if (--rcount
[r
->number
] == 0) 
 122           if (r
->useful 
&& ! nullable
[r
->lhs
->number 
- ntokens
]) 
 124               nullable
[r
->lhs
->number 
- ntokens
] = true; 
 125               *s2
++ = r
->lhs
->number
; 
 134   if (trace_flag 
& trace_sets
) 
 135     nullable_print (stderr
);