]>
git.saurik.com Git - bison.git/blob - src/nullable.c
1 /* Part of the bison parser generator,
2 Copyright 1984, 1989, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
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
34 char *nullable
= NULL
;
37 nullable_print (FILE *out
)
40 fputs ("NULLABLE\n", out
);
41 for (i
= ntokens
; i
< nsyms
; i
++)
42 fprintf (out
, "\t%s: %s\n", symbols
[i
]->tag
, nullable
[i
] ? "yes" : "no");
54 symbol_number_t
*squeue
= XCALLOC (symbol_number_t
, nvars
);
55 short *rcount
= XCALLOC (short, nrules
+ 1);
56 /* RITEM contains all the rules, including useless productions.
57 Hence we must allocate room for useless nonterminals too. */
58 shorts
**rsets
= XCALLOC (shorts
*, nvars
) - ntokens
;
59 /* This is said to be more elements than we actually use.
60 Supposedly NRITEMS - NRULES is enough. But why take the risk? */
61 shorts
*relts
= XCALLOC (shorts
, nritems
+ nvars
+ 1);
64 fprintf (stderr
, "Entering set_nullable\n");
66 nullable
= XCALLOC (char, nvars
) - ntokens
;
71 for (ruleno
= 1; ruleno
< nrules
+ 1; ++ruleno
)
72 if (rules
[ruleno
].useful
)
74 rule_t
*rule
= &rules
[ruleno
];
75 if (rule
->rhs
[0] >= 0)
77 /* This rule has a non empty RHS. */
78 item_number_t
*r
= NULL
;
80 for (r
= rule
->rhs
; *r
>= 0; ++r
)
84 /* This rule has only nonterminals: schedule it for the second
87 for (r
= rule
->rhs
; *r
>= 0; ++r
)
98 /* This rule has an empty RHS. */
99 assert (rule_number_of_item_number (rule
->rhs
[0]) == ruleno
);
100 if (rule
->useful
&& !nullable
[rule
->lhs
->number
])
102 nullable
[rule
->lhs
->number
] = 1;
103 *s2
++ = rule
->lhs
->number
;
109 for (p
= rsets
[*s1
++]; p
; p
= p
->next
)
112 if (--rcount
[ruleno
] == 0)
113 if (rules
[ruleno
].useful
&& !nullable
[rules
[ruleno
].lhs
->number
])
115 nullable
[rules
[ruleno
].lhs
->number
] = 1;
116 *s2
++ = rules
[ruleno
].lhs
->number
;
122 XFREE (rsets
+ ntokens
);
126 nullable_print (stderr
);
133 XFREE (nullable
+ ntokens
);