]> git.saurik.com Git - bison.git/blob - src/state.h
Instead of attaching lookaheads and duplicating the rules being
[bison.git] / src / state.h
1 /* Type definitions for nondeterministic finite state machine for bison,
2 Copyright 1984, 1989, 2000, 2001 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 /* These type definitions are used to represent a nondeterministic
23 finite state machine that parses the specified grammar. This
24 information is generated by the function generate_states in the
25 file LR0.
26
27 Each state of the machine is described by a set of items --
28 particular positions in particular rules -- that are the possible
29 places where parsing could continue when the machine is in this
30 state. These symbols at these items are the allowable inputs that
31 can follow now.
32
33 A core represents one state. States are numbered in the NUMBER
34 field. When generate_states is finished, the starting state is
35 state 0 and NSTATES is the number of states. (FIXME: This sentence
36 is no longer true: A transition to a state whose state number is
37 NSTATES indicates termination.) All the cores are chained together
38 and FIRST_STATE points to the first one (state 0).
39
40 For each state there is a particular symbol which must have been
41 the last thing accepted to reach that state. It is the
42 ACCESSING_SYMBOL of the core.
43
44 Each core contains a vector of NITEMS items which are the indices
45 in the RITEMS vector of the items that are selected in this state.
46
47 The two types of actions are shifts/gotos (push the lookahead token
48 and read another/goto to the state designated by a nterm) and
49 reductions (combine the last n things on the stack via a rule,
50 replace them with the symbol that the rule derives, and leave the
51 lookahead token alone). When the states are generated, these
52 actions are represented in two other lists.
53
54 Each transition_t structure describes the possible transitions out
55 of one state, the state whose number is in the number field. Each
56 contains a vector of numbers of the states that transitions can go
57 to. The accessing_symbol fields of those states' cores say what
58 kind of input leads to them.
59
60 A transition to state zero should be ignored: conflict resolution
61 deletes transitions by having them point to zero.
62
63 Each reductions structure describes the possible reductions at the
64 state whose number is in the number field. The data is a list of
65 nreds rules, represented by their rule numbers. first_reduction
66 points to the list of these structures.
67
68 Conflict resolution can decide that certain tokens in certain
69 states should explicitly be errors (for implementing %nonassoc).
70 For each state, the tokens that are errors for this reason are
71 recorded in an errs structure, which holds the token numbers.
72
73 There is at least one goto transition present in state zero. It
74 leads to a next-to-final state whose accessing_symbol is the
75 grammar's start symbol. The next-to-final state has one shift to
76 the final state, whose accessing_symbol is zero (end of input).
77 The final state has one shift, which goes to the termination state.
78 The reason for the extra state at the end is to placate the
79 parser's strategy of making all decisions one token ahead of its
80 actions. */
81
82 #ifndef STATE_H_
83 # define STATE_H_
84
85 # include "bitset.h"
86
87
88 /*-------------------.
89 | Numbering states. |
90 `-------------------*/
91
92 typedef short state_number_t;
93 # define STATE_NUMBER_MAX ((state_number_t) SHRT_MAX)
94
95 /* Be ready to map a state_number_t to an int. */
96 # define state_number_as_int(Tok) ((int) (Tok))
97
98
99 typedef struct state_s state_t;
100
101 /*--------------.
102 | Transitions. |
103 `--------------*/
104
105 typedef struct transtion_s
106 {
107 short num;
108 state_t *states[1];
109 } transitions_t;
110
111
112 /* What is the symbol labelling the transition to
113 TRANSITIONS->states[Num]? Can be a token (amongst which the error
114 token), or non terminals in case of gotos. */
115
116 #define TRANSITION_SYMBOL(Transitions, Num) \
117 (Transitions->states[Num]->accessing_symbol)
118
119 /* Is the TRANSITIONS->states[Num] a shift? (as opposed to gotos). */
120
121 #define TRANSITION_IS_SHIFT(Transitions, Num) \
122 (ISTOKEN (TRANSITION_SYMBOL (Transitions, Num)))
123
124 /* Is the TRANSITIONS->states[Num] a goto?. */
125
126 #define TRANSITION_IS_GOTO(Transitions, Num) \
127 (!TRANSITION_IS_SHIFT (Transitions, Num))
128
129 /* Is the TRANSITIONS->states[Num] labelled by the error token? */
130
131 #define TRANSITION_IS_ERROR(Transitions, Num) \
132 (TRANSITION_SYMBOL (Transitions, Num) == errtoken->number)
133
134 /* When resolving a SR conflicts, if the reduction wins, the shift is
135 disabled. */
136
137 #define TRANSITION_DISABLE(Transitions, Num) \
138 (Transitions->states[Num] = NULL)
139
140 #define TRANSITION_IS_DISABLED(Transitions, Num) \
141 (Transitions->states[Num] == NULL)
142
143
144 /* Iterate over each transition over a token (shifts). */
145 #define FOR_EACH_SHIFT(Transitions, Iter) \
146 for (Iter = 0; \
147 Iter < Transitions->num \
148 && (TRANSITION_IS_DISABLED (Transitions, Iter) \
149 || TRANSITION_IS_SHIFT (Transitions, Iter)); \
150 ++Iter) \
151 if (!TRANSITION_IS_DISABLED (Transitions, Iter))
152
153
154 /* Return the state such these TRANSITIONS contain a shift/goto to it on
155 SYMBOL. Aborts if none found. */
156 struct state_s;
157 struct state_s *transitions_to PARAMS ((transitions_t *state,
158 symbol_number_t s));
159
160
161 /*-------.
162 | Errs. |
163 `-------*/
164
165 typedef struct errs_s
166 {
167 short num;
168 symbol_t *symbols[1];
169 } errs_t;
170
171 errs_t *errs_new PARAMS ((int num, symbol_t **tokens));
172
173
174 /*-------------.
175 | Reductions. |
176 `-------------*/
177
178 typedef struct reductions_s
179 {
180 short num;
181 bitset *lookaheads;
182 rule_t *rules[1];
183 } reductions_t;
184
185
186
187 /*---------.
188 | States. |
189 `---------*/
190
191 struct state_s
192 {
193 state_number_t number;
194 symbol_number_t accessing_symbol;
195 transitions_t *transitions;
196 reductions_t *reductions;
197 errs_t *errs;
198
199 /* Nonzero if no lookahead is needed to decide what to do in state S. */
200 char consistent;
201
202 /* If some conflicts were solved thanks to precedence/associativity,
203 a human readable description of the resolution. */
204 const char *solved_conflicts;
205
206 /* Its items. Must be last, since ITEMS can be arbitrarily large.
207 */
208 unsigned short nitems;
209 item_number_t items[1];
210 };
211
212 extern state_number_t nstates;
213 extern state_t *final_state;
214
215 /* Create a new state with ACCESSING_SYMBOL for those items. */
216 state_t *state_new PARAMS ((symbol_number_t accessing_symbol,
217 size_t core_size, item_number_t *core));
218
219 /* Set the transitions of STATE. */
220 void state_transitions_set PARAMS ((state_t *state,
221 int num, state_t **transitions));
222
223 /* Set the reductions of STATE. */
224 void state_reductions_set PARAMS ((state_t *state,
225 int num, rule_t **reductions));
226
227 int state_reduction_find PARAMS ((state_t *state, rule_t *rule));
228
229 /* Set the errs of STATE. */
230 void state_errs_set PARAMS ((state_t *state,
231 int num, symbol_t **errs));
232
233 /* Print on OUT all the lookaheads such that this STATE wants to
234 reduce this RULE. */
235 void state_rule_lookaheads_print PARAMS ((state_t *state, rule_t *rule,
236 FILE *out));
237
238 /* Create/destroy the states hash table. */
239 void state_hash_new PARAMS ((void));
240 void state_hash_free PARAMS ((void));
241
242 /* Find the state associated to the CORE, and return it. If it does
243 not exist yet, return NULL. */
244 state_t *state_hash_lookup PARAMS ((size_t core_size, item_number_t *core));
245
246 /* Insert STATE in the state hash table. */
247 void state_hash_insert PARAMS ((state_t *state));
248
249 /* All the states, indexed by the state number. */
250 extern state_t **states;
251
252 /* Free all the states. */
253 void states_free PARAMS ((void));
254 #endif /* !STATE_H_ */