]> git.saurik.com Git - bison.git/blame - src/state.h
(symbol_number): Renamed from symbol_number_t.
[bison.git] / src / state.h
CommitLineData
d0fb370f 1/* Type definitions for nondeterministic finite state machine for bison,
784573d1
PE
2
3 Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software
4 Foundation, Inc.
d0fb370f 5
a70083a3 6 This file is part of Bison, the GNU Compiler Compiler.
d0fb370f 7
a70083a3
AD
8 Bison is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
d0fb370f 12
a70083a3
AD
13 Bison is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
d0fb370f 17
a70083a3
AD
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
d0fb370f
RS
22
23
24/* These type definitions are used to represent a nondeterministic
a70083a3
AD
25 finite state machine that parses the specified grammar. This
26 information is generated by the function generate_states in the
27 file LR0.
28
29 Each state of the machine is described by a set of items --
30 particular positions in particular rules -- that are the possible
31 places where parsing could continue when the machine is in this
32 state. These symbols at these items are the allowable inputs that
33 can follow now.
34
9801d40c 35 A core represents one state. States are numbered in the NUMBER
a70083a3 36 field. When generate_states is finished, the starting state is
9801d40c
AD
37 state 0 and NSTATES is the number of states. (FIXME: This sentence
38 is no longer true: A transition to a state whose state number is
39 NSTATES indicates termination.) All the cores are chained together
40 and FIRST_STATE points to the first one (state 0).
a70083a3
AD
41
42 For each state there is a particular symbol which must have been
43 the last thing accepted to reach that state. It is the
9801d40c 44 ACCESSING_SYMBOL of the core.
a70083a3 45
5123689b 46 Each core contains a vector of NITEMS items which are the indices
9801d40c 47 in the RITEMS vector of the items that are selected in this state.
a70083a3 48
8b752b00
AD
49 The two types of actions are shifts/gotos (push the lookahead token
50 and read another/goto to the state designated by a nterm) and
51 reductions (combine the last n things on the stack via a rule,
52 replace them with the symbol that the rule derives, and leave the
53 lookahead token alone). When the states are generated, these
54 actions are represented in two other lists.
55
784573d1 56 Each transition structure describes the possible transitions out
8b752b00
AD
57 of one state, the state whose number is in the number field. Each
58 contains a vector of numbers of the states that transitions can go
59 to. The accessing_symbol fields of those states' cores say what
60 kind of input leads to them.
61
62 A transition to state zero should be ignored: conflict resolution
63 deletes transitions by having them point to zero.
a70083a3
AD
64
65 Each reductions structure describes the possible reductions at the
66 state whose number is in the number field. The data is a list of
67 nreds rules, represented by their rule numbers. first_reduction
68 points to the list of these structures.
69
70 Conflict resolution can decide that certain tokens in certain
71 states should explicitly be errors (for implementing %nonassoc).
72 For each state, the tokens that are errors for this reason are
8b752b00 73 recorded in an errs structure, which holds the token numbers.
a70083a3 74
8b752b00 75 There is at least one goto transition present in state zero. It
a70083a3
AD
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).
8b752b00
AD
79 The final state has one shift, which goes to the termination state.
80 The reason for the extra state at the end is to placate the
81 parser's strategy of making all decisions one token ahead of its
82 actions. */
a70083a3
AD
83
84#ifndef STATE_H_
85# define STATE_H_
86
784573d1
PE
87# include <bitset.h>
88
89# include "gram.h"
90# include "symtab.h"
aa2aab3c 91
d57650a5
AD
92
93/*-------------------.
94| Numbering states. |
95`-------------------*/
96
784573d1
PE
97typedef short state_number;
98# define STATE_NUMBER_MAXIMUM SHRT_MAX
d57650a5 99
784573d1 100/* Be ready to map a state_number to an int. */
d57650a5
AD
101# define state_number_as_int(Tok) ((int) (Tok))
102
640748ee 103
784573d1 104typedef struct state state;
640748ee 105
ccaf65bc
AD
106/*--------------.
107| Transitions. |
108`--------------*/
aa2aab3c 109
784573d1 110typedef struct
a70083a3 111{
ccaf65bc 112 short num;
784573d1
PE
113 state *states[1];
114} transitions;
d954473d
AD
115
116
8b752b00
AD
117/* What is the symbol labelling the transition to
118 TRANSITIONS->states[Num]? Can be a token (amongst which the error
119 token), or non terminals in case of gotos. */
b608206e 120
8b752b00 121#define TRANSITION_SYMBOL(Transitions, Num) \
640748ee 122 (Transitions->states[Num]->accessing_symbol)
b608206e 123
8b752b00 124/* Is the TRANSITIONS->states[Num] a shift? (as opposed to gotos). */
aa2aab3c 125
8b752b00
AD
126#define TRANSITION_IS_SHIFT(Transitions, Num) \
127 (ISTOKEN (TRANSITION_SYMBOL (Transitions, Num)))
aa2aab3c 128
8b752b00 129/* Is the TRANSITIONS->states[Num] a goto?. */
aa2aab3c 130
8b752b00
AD
131#define TRANSITION_IS_GOTO(Transitions, Num) \
132 (!TRANSITION_IS_SHIFT (Transitions, Num))
aa2aab3c 133
8b752b00 134/* Is the TRANSITIONS->states[Num] labelled by the error token? */
aa2aab3c 135
8b752b00
AD
136#define TRANSITION_IS_ERROR(Transitions, Num) \
137 (TRANSITION_SYMBOL (Transitions, Num) == errtoken->number)
aa2aab3c 138
9839bbe5
AD
139/* When resolving a SR conflicts, if the reduction wins, the shift is
140 disabled. */
141
8b752b00 142#define TRANSITION_DISABLE(Transitions, Num) \
640748ee 143 (Transitions->states[Num] = NULL)
9839bbe5 144
8b752b00 145#define TRANSITION_IS_DISABLED(Transitions, Num) \
640748ee
AD
146 (Transitions->states[Num] == NULL)
147
148
149/* Iterate over each transition over a token (shifts). */
150#define FOR_EACH_SHIFT(Transitions, Iter) \
151 for (Iter = 0; \
152 Iter < Transitions->num \
153 && (TRANSITION_IS_DISABLED (Transitions, Iter) \
154 || TRANSITION_IS_SHIFT (Transitions, Iter)); \
155 ++Iter) \
156 if (!TRANSITION_IS_DISABLED (Transitions, Iter))
157
9839bbe5 158
784573d1
PE
159/* Return the state such SHIFTS contain a shift/goto to it on S.
160 Abort if none found. */
161struct state *transitions_to (transitions *shifts, symbol_number s);
ccaf65bc 162
aa2aab3c
AD
163
164/*-------.
165| Errs. |
166`-------*/
a70083a3 167
784573d1 168typedef struct
a70083a3 169{
d2576365 170 short num;
784573d1
PE
171 symbol *symbols[1];
172} errs;
a70083a3 173
784573d1 174errs *errs_new (int num, symbol **tokens);
f59c437a 175
a70083a3 176
aa2aab3c
AD
177/*-------------.
178| Reductions. |
179`-------------*/
a70083a3 180
784573d1 181typedef struct
a70083a3 182{
d2576365 183 short num;
cd08e51e 184 bitset *lookaheads;
784573d1
PE
185 rule *rules[1];
186} reductions;
d0fb370f 187
f693ad14
AD
188
189
640748ee 190/*---------.
784573d1 191| states. |
640748ee 192`---------*/
f693ad14 193
784573d1 194struct state
f693ad14 195{
784573d1
PE
196 state_number number;
197 symbol_number accessing_symbol;
198 transitions *transitions;
199 reductions *reductions;
200 errs *errs;
f693ad14
AD
201
202 /* Nonzero if no lookahead is needed to decide what to do in state S. */
203 char consistent;
204
b408954b
AD
205 /* If some conflicts were solved thanks to precedence/associativity,
206 a human readable description of the resolution. */
207 const char *solved_conflicts;
208
209 /* Its items. Must be last, since ITEMS can be arbitrarily large.
210 */
0c2d3f4c 211 unsigned short nitems;
784573d1 212 item_number items[1];
640748ee 213};
f693ad14 214
784573d1
PE
215extern state_number nstates;
216extern state *final_state;
df0e7316
AD
217
218/* Create a new state with ACCESSING_SYMBOL for those items. */
784573d1
PE
219state *state_new (symbol_number accessing_symbol,
220 size_t core_size, item_number *core);
f693ad14 221
8b752b00 222/* Set the transitions of STATE. */
784573d1 223void state_transitions_set (state *s, int num, state **trans);
32e1e0a4 224
8a731ca8 225/* Set the reductions of STATE. */
784573d1 226void state_reductions_set (state *s, int num, rule **reds);
8b752b00 227
784573d1 228int state_reduction_find (state *s, rule *r);
cd08e51e 229
8b752b00 230/* Set the errs of STATE. */
784573d1 231void state_errs_set (state *s, int num, symbol **errors);
8a731ca8 232
10e5b8bd 233/* Print on OUT all the lookaheads such that this STATE wants to
784573d1
PE
234 reduce R. */
235void state_rule_lookaheads_print (state *s, rule *r, FILE *out);
10e5b8bd 236
c7ca99d4 237/* Create/destroy the states hash table. */
d33cb3ae
PE
238void state_hash_new (void);
239void state_hash_free (void);
c7ca99d4
AD
240
241/* Find the state associated to the CORE, and return it. If it does
242 not exist yet, return NULL. */
784573d1 243state *state_hash_lookup (size_t core_size, item_number *core);
c7ca99d4
AD
244
245/* Insert STATE in the state hash table. */
784573d1 246void state_hash_insert (state *s);
c7ca99d4
AD
247
248/* All the states, indexed by the state number. */
784573d1 249extern state **states;
c7ca99d4
AD
250
251/* Free all the states. */
d33cb3ae 252void states_free (void);
a70083a3 253#endif /* !STATE_H_ */