]> git.saurik.com Git - bison.git/blame - src/state.h
Regen.
[bison.git] / src / state.h
CommitLineData
d0fb370f 1/* Type definitions for nondeterministic finite state machine for bison,
d954473d 2 Copyright 1984, 1989, 2000, 2001 Free Software Foundation, Inc.
d0fb370f 3
a70083a3 4 This file is part of Bison, the GNU Compiler Compiler.
d0fb370f 5
a70083a3
AD
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.
d0fb370f 10
a70083a3
AD
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.
d0fb370f 15
a70083a3
AD
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. */
d0fb370f
RS
20
21
22/* These type definitions are used to represent a nondeterministic
a70083a3
AD
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
9801d40c 33 A core represents one state. States are numbered in the NUMBER
a70083a3 34 field. When generate_states is finished, the starting state is
9801d40c
AD
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).
a70083a3
AD
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
9801d40c 42 ACCESSING_SYMBOL of the core.
a70083a3 43
5123689b 44 Each core contains a vector of NITEMS items which are the indices
9801d40c 45 in the RITEMS vector of the items that are selected in this state.
a70083a3 46
a70083a3
AD
47 The two types of transitions are shifts (push the lookahead token
48 and read another) and reductions (combine the last n things on the
49 stack via a rule, replace them with the symbol that the rule
50 derives, and leave the lookahead token alone). When the states are
51 generated, these transitions are represented in two other lists.
52
53 Each shifts structure describes the possible shift transitions out
54 of one state, the state whose number is in the number field. The
55 shifts structures are linked through next and first_shift points to
56 them. Each contains a vector of numbers of the states that shift
57 transitions can go to. The accessing_symbol fields of those
58 states' cores say what kind of input leads to them.
59
60 A shift to state zero should be ignored. Conflict resolution
61 deletes shifts by changing them 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 has the state number in its
72 number field. The rest of the errs structure is full of token
73 numbers.
74
75 There is at least one shift transition present in state zero. It
76 leads to a next-to-final state whose accessing_symbol is the
77 grammar's start symbol. The next-to-final state has one shift to
78 the final state, whose accessing_symbol is zero (end of input).
79 The final state has one shift, which goes to the termination state
80 (whose number is nstates-1). The reason for the extra state at the
81 end is to placate the parser's strategy of making all decisions one
82 token ahead of its actions. */
83
84#ifndef STATE_H_
85# define STATE_H_
86
c0263492 87# include "bitsetv.h"
aa2aab3c 88
d57650a5
AD
89
90/*-------------------.
91| Numbering states. |
92`-------------------*/
93
94typedef short state_number_t;
95# define STATE_NUMBER_MAX ((state_number_t) SHRT_MAX)
96
97/* Be ready to map a state_number_t to an int. */
98# define state_number_as_int(Tok) ((int) (Tok))
99
ccaf65bc
AD
100/*--------------.
101| Transitions. |
102`--------------*/
aa2aab3c 103
ccaf65bc 104typedef struct transtion_s
a70083a3 105{
ccaf65bc
AD
106 short num;
107 state_number_t states[1];
108} transitions_t;
d954473d
AD
109
110
ccaf65bc 111/* What is the symbol which is shifted by TRANSITIONS->states[Shift]? Can
b608206e
AD
112 be a token (amongst which the error token), or non terminals in
113 case of gotos. */
114
ccaf65bc
AD
115#define TRANSITION_SYMBOL(Transitions, Shift) \
116 (states[Transitions->states[Shift]]->accessing_symbol)
b608206e 117
ccaf65bc 118/* Is the TRANSITIONS->states[Shift] a real shift? (as opposed to gotos.) */
aa2aab3c 119
ccaf65bc
AD
120#define TRANSITION_IS_SHIFT(Transitions, Shift) \
121 (ISTOKEN (TRANSITION_SYMBOL (Transitions, Shift)))
aa2aab3c 122
ccaf65bc 123/* Is the TRANSITIONS->states[Shift] a goto?. */
aa2aab3c 124
ccaf65bc
AD
125#define TRANSITION_IS_GOTO(Transitions, Shift) \
126 (!TRANSITION_IS_SHIFT (Transitions, Shift))
aa2aab3c 127
ccaf65bc 128/* Is the TRANSITIONS->states[Shift] then handling of the error token?. */
aa2aab3c 129
ccaf65bc
AD
130#define TRANSITION_IS_ERROR(Transitions, Shift) \
131 (TRANSITION_SYMBOL (Transitions, Shift) == errtoken->number)
aa2aab3c 132
9839bbe5
AD
133/* When resolving a SR conflicts, if the reduction wins, the shift is
134 disabled. */
135
ccaf65bc
AD
136#define TRANSITION_DISABLE(Transitions, Shift) \
137 (Transitions->states[Shift] = 0)
9839bbe5 138
ccaf65bc
AD
139#define TRANSITION_IS_DISABLED(Transitions, Shift) \
140 (Transitions->states[Shift] == 0)
9839bbe5 141
ccaf65bc 142/* Return the state such these TRANSITIONS contain a shift/goto to it on
24c7d800
AD
143 SYMBOL. Aborts if none found. */
144struct state_s;
ccaf65bc
AD
145struct state_s *transitions_to PARAMS ((transitions_t *state,
146 symbol_number_t s));
147
aa2aab3c
AD
148
149/*-------.
150| Errs. |
151`-------*/
a70083a3 152
8a731ca8 153typedef struct errs_s
a70083a3 154{
d2576365
AD
155 short num;
156 symbol_number_t symbols[1];
8a731ca8 157} errs_t;
a70083a3 158
8a731ca8
AD
159errs_t *errs_new PARAMS ((int n));
160errs_t *errs_dup PARAMS ((errs_t *src));
f59c437a 161
a70083a3 162
aa2aab3c
AD
163/*-------------.
164| Reductions. |
165`-------------*/
a70083a3 166
8a731ca8 167typedef struct reductions_s
a70083a3 168{
d2576365
AD
169 short num;
170 rule_number_t rules[1];
8a731ca8 171} reductions_t;
d0fb370f 172
f693ad14
AD
173
174
175/*----------.
176| State_t. |
177`----------*/
178
179typedef struct state_s
180{
d57650a5 181 state_number_t number;
a49aecd5 182 symbol_number_t accessing_symbol;
ccaf65bc 183 transitions_t *shifts;
8a731ca8
AD
184 reductions_t *reductions;
185 errs_t *errs;
f693ad14
AD
186
187 /* Nonzero if no lookahead is needed to decide what to do in state S. */
188 char consistent;
189
53d4308d
AD
190 /* Used in LALR, not LR(0).
191
192 When a state is not consistent (there is an S/R or R/R conflict),
193 lookaheads are needed to enable the reductions. NLOOKAHEADS is
194 the number of lookahead guarded reductions of the
195 LOOKAHEADS_RULE. For each rule LOOKAHEADS_RULE[R], LOOKAHEADS[R]
196 is the bitset of the lookaheads enabling this reduction. */
3877f72b 197 int nlookaheads;
c0263492
AD
198 bitsetv lookaheads;
199 rule_t **lookaheads_rule;
f693ad14 200
b408954b
AD
201 /* If some conflicts were solved thanks to precedence/associativity,
202 a human readable description of the resolution. */
203 const char *solved_conflicts;
204
205 /* Its items. Must be last, since ITEMS can be arbitrarily large.
206 */
0c2d3f4c 207 unsigned short nitems;
62a3e4f0 208 item_number_t items[1];
f693ad14
AD
209} state_t;
210
df0e7316
AD
211extern state_number_t nstates;
212extern state_t *final_state;
213
214/* Create a new state with ACCESSING_SYMBOL for those items. */
df0e7316
AD
215state_t *state_new PARAMS ((symbol_number_t accessing_symbol,
216 size_t core_size, item_number_t *core));
f693ad14 217
32e1e0a4 218/* Set the shifts of STATE. */
ccaf65bc 219void state_transitions_set PARAMS ((state_t *state,
32e1e0a4
AD
220 int nshifts, state_number_t *shifts));
221
8a731ca8
AD
222/* Set the reductions of STATE. */
223void state_reductions_set PARAMS ((state_t *state,
224 int nreductions, short *reductions));
225
10e5b8bd
AD
226/* Print on OUT all the lookaheads such that this STATE wants to
227 reduce this RULE. */
10e5b8bd
AD
228void state_rule_lookaheads_print PARAMS ((state_t *state, rule_t *rule,
229 FILE *out));
230
c7ca99d4
AD
231/* Create/destroy the states hash table. */
232void state_hash_new PARAMS ((void));
233void state_hash_free PARAMS ((void));
234
235/* Find the state associated to the CORE, and return it. If it does
236 not exist yet, return NULL. */
237state_t *state_hash_lookup PARAMS ((size_t core_size, item_number_t *core));
238
239/* Insert STATE in the state hash table. */
240void state_hash_insert PARAMS ((state_t *state));
241
242/* All the states, indexed by the state number. */
243extern state_t **states;
244
245/* Free all the states. */
246void states_free PARAMS ((void));
a70083a3 247#endif /* !STATE_H_ */