]>
git.saurik.com Git - bison.git/blob - src/nullable.c
1 /* Part of the bison parser generator,
2 Copyright (C) 1984, 1989, 2000, 2001, 2002 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 /* Linked list of rules. */
35 typedef struct rule_list
37 struct rule_list
*next
;
41 bool *nullable
= NULL
;
44 nullable_print (FILE *out
)
47 fputs ("NULLABLE\n", out
);
48 for (i
= ntokens
; i
< nsyms
; i
++)
49 fprintf (out
, "\t%s: %s\n", symbols
[i
]->tag
, nullable
[i
] ? "yes" : "no");
54 nullable_compute (void)
61 symbol_number
*squeue
= XCALLOC (symbol_number
, nvars
);
62 short *rcount
= XCALLOC (short, nrules
);
63 /* RITEM contains all the rules, including useless productions.
64 Hence we must allocate room for useless nonterminals too. */
65 rule_list
**rsets
= XCALLOC (rule_list
*, nvars
) - ntokens
;
66 /* This is said to be more elements than we actually use.
67 Supposedly NRITEMS - NRULES is enough. But why take the risk? */
68 rule_list
*relts
= XCALLOC (rule_list
, nritems
+ nvars
+ 1);
70 nullable
= XCALLOC (bool, nvars
) - ntokens
;
75 for (ruleno
= 0; ruleno
< nrules
; ++ruleno
)
76 if (rules
[ruleno
].useful
)
78 rule
*rules_ruleno
= &rules
[ruleno
];
79 if (rules_ruleno
->rhs
[0] >= 0)
81 /* This rule has a non empty RHS. */
82 item_number
*r
= NULL
;
84 for (r
= rules_ruleno
->rhs
; *r
>= 0; ++r
)
88 /* This rule has only nonterminals: schedule it for the second
91 for (r
= rules_ruleno
->rhs
; *r
>= 0; ++r
)
95 p
->value
= rules_ruleno
;
102 /* This rule has an empty RHS. */
103 if (item_number_as_rule_number (rules_ruleno
->rhs
[0]) != ruleno
)
105 if (rules_ruleno
->useful
&& !nullable
[rules_ruleno
->lhs
->number
])
107 nullable
[rules_ruleno
->lhs
->number
] = 1;
108 *s2
++ = rules_ruleno
->lhs
->number
;
114 for (p
= rsets
[*s1
++]; p
; p
= p
->next
)
117 if (--rcount
[r
->number
] == 0)
118 if (r
->useful
&& !nullable
[r
->lhs
->number
])
120 nullable
[r
->lhs
->number
] = 1;
121 *s2
++ = r
->lhs
->number
;
127 XFREE (rsets
+ ntokens
);
130 if (trace_flag
& trace_sets
)
131 nullable_print (stderr
);
138 XFREE (nullable
+ ntokens
);