]> git.saurik.com Git - bison.git/blame - src/state.h
* src/complain.h, src/complain.c (warn, complain): Remove, unused.
[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
8b752b00
AD
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.
a70083a3
AD
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
8b752b00 71 recorded in an errs structure, which holds the token numbers.
a70083a3 72
8b752b00 73 There is at least one goto transition present in state zero. It
a70083a3
AD
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).
8b752b00
AD
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. */
a70083a3
AD
81
82#ifndef STATE_H_
83# define STATE_H_
84
c0263492 85# include "bitsetv.h"
aa2aab3c 86
d57650a5
AD
87
88/*-------------------.
89| Numbering states. |
90`-------------------*/
91
92typedef 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
ccaf65bc
AD
98/*--------------.
99| Transitions. |
100`--------------*/
aa2aab3c 101
ccaf65bc 102typedef struct transtion_s
a70083a3 103{
ccaf65bc
AD
104 short num;
105 state_number_t states[1];
106} transitions_t;
d954473d
AD
107
108
8b752b00
AD
109/* What is the symbol labelling the transition to
110 TRANSITIONS->states[Num]? Can be a token (amongst which the error
111 token), or non terminals in case of gotos. */
b608206e 112
8b752b00
AD
113#define TRANSITION_SYMBOL(Transitions, Num) \
114 (states[Transitions->states[Num]]->accessing_symbol)
b608206e 115
8b752b00 116/* Is the TRANSITIONS->states[Num] a shift? (as opposed to gotos). */
aa2aab3c 117
8b752b00
AD
118#define TRANSITION_IS_SHIFT(Transitions, Num) \
119 (ISTOKEN (TRANSITION_SYMBOL (Transitions, Num)))
aa2aab3c 120
8b752b00 121/* Is the TRANSITIONS->states[Num] a goto?. */
aa2aab3c 122
8b752b00
AD
123#define TRANSITION_IS_GOTO(Transitions, Num) \
124 (!TRANSITION_IS_SHIFT (Transitions, Num))
aa2aab3c 125
8b752b00 126/* Is the TRANSITIONS->states[Num] labelled by the error token? */
aa2aab3c 127
8b752b00
AD
128#define TRANSITION_IS_ERROR(Transitions, Num) \
129 (TRANSITION_SYMBOL (Transitions, Num) == errtoken->number)
aa2aab3c 130
9839bbe5
AD
131/* When resolving a SR conflicts, if the reduction wins, the shift is
132 disabled. */
133
8b752b00
AD
134#define TRANSITION_DISABLE(Transitions, Num) \
135 (Transitions->states[Num] = 0)
9839bbe5 136
8b752b00
AD
137#define TRANSITION_IS_DISABLED(Transitions, Num) \
138 (Transitions->states[Num] == 0)
9839bbe5 139
ccaf65bc 140/* Return the state such these TRANSITIONS contain a shift/goto to it on
24c7d800
AD
141 SYMBOL. Aborts if none found. */
142struct state_s;
ccaf65bc
AD
143struct state_s *transitions_to PARAMS ((transitions_t *state,
144 symbol_number_t s));
145
aa2aab3c
AD
146
147/*-------.
148| Errs. |
149`-------*/
a70083a3 150
8a731ca8 151typedef struct errs_s
a70083a3 152{
d2576365
AD
153 short num;
154 symbol_number_t symbols[1];
8a731ca8 155} errs_t;
a70083a3 156
8b752b00 157errs_t *errs_new PARAMS ((int num, symbol_number_t *tokens));
f59c437a 158
a70083a3 159
aa2aab3c
AD
160/*-------------.
161| Reductions. |
162`-------------*/
a70083a3 163
8a731ca8 164typedef struct reductions_s
a70083a3 165{
d2576365
AD
166 short num;
167 rule_number_t rules[1];
8a731ca8 168} reductions_t;
d0fb370f 169
f693ad14
AD
170
171
172/*----------.
173| State_t. |
174`----------*/
175
176typedef struct state_s
177{
d57650a5 178 state_number_t number;
a49aecd5 179 symbol_number_t accessing_symbol;
8b752b00 180 transitions_t *transitions;
8a731ca8
AD
181 reductions_t *reductions;
182 errs_t *errs;
f693ad14
AD
183
184 /* Nonzero if no lookahead is needed to decide what to do in state S. */
185 char consistent;
186
53d4308d
AD
187 /* Used in LALR, not LR(0).
188
189 When a state is not consistent (there is an S/R or R/R conflict),
190 lookaheads are needed to enable the reductions. NLOOKAHEADS is
191 the number of lookahead guarded reductions of the
192 LOOKAHEADS_RULE. For each rule LOOKAHEADS_RULE[R], LOOKAHEADS[R]
193 is the bitset of the lookaheads enabling this reduction. */
3877f72b 194 int nlookaheads;
c0263492
AD
195 bitsetv lookaheads;
196 rule_t **lookaheads_rule;
f693ad14 197
b408954b
AD
198 /* If some conflicts were solved thanks to precedence/associativity,
199 a human readable description of the resolution. */
200 const char *solved_conflicts;
201
202 /* Its items. Must be last, since ITEMS can be arbitrarily large.
203 */
0c2d3f4c 204 unsigned short nitems;
62a3e4f0 205 item_number_t items[1];
f693ad14
AD
206} state_t;
207
df0e7316
AD
208extern state_number_t nstates;
209extern state_t *final_state;
210
211/* Create a new state with ACCESSING_SYMBOL for those items. */
df0e7316
AD
212state_t *state_new PARAMS ((symbol_number_t accessing_symbol,
213 size_t core_size, item_number_t *core));
f693ad14 214
8b752b00 215/* Set the transitions of STATE. */
ccaf65bc 216void state_transitions_set PARAMS ((state_t *state,
8b752b00 217 int num, state_number_t *transitions));
32e1e0a4 218
8a731ca8
AD
219/* Set the reductions of STATE. */
220void state_reductions_set PARAMS ((state_t *state,
8b752b00
AD
221 int num, rule_number_t *reductions));
222
223/* Set the errs of STATE. */
224void state_errs_set PARAMS ((state_t *state,
225 int num, symbol_number_t *errs));
8a731ca8 226
10e5b8bd
AD
227/* Print on OUT all the lookaheads such that this STATE wants to
228 reduce this RULE. */
10e5b8bd
AD
229void state_rule_lookaheads_print PARAMS ((state_t *state, rule_t *rule,
230 FILE *out));
231
c7ca99d4
AD
232/* Create/destroy the states hash table. */
233void state_hash_new PARAMS ((void));
234void state_hash_free PARAMS ((void));
235
236/* Find the state associated to the CORE, and return it. If it does
237 not exist yet, return NULL. */
238state_t *state_hash_lookup PARAMS ((size_t core_size, item_number_t *core));
239
240/* Insert STATE in the state hash table. */
241void state_hash_insert PARAMS ((state_t *state));
242
243/* All the states, indexed by the state number. */
244extern state_t **states;
245
246/* Free all the states. */
247void states_free PARAMS ((void));
a70083a3 248#endif /* !STATE_H_ */