]> git.saurik.com Git - bison.git/blame - src/state.h
Minor code cleanup in parser table construction.
[bison.git] / src / state.h
CommitLineData
2073d769 1/* Type definitions for nondeterministic finite state machine for Bison.
784573d1 2
779e7ceb
PE
3 Copyright (C) 1984, 1989, 2000, 2001, 2002, 2003, 2004 Free
4 Software 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
0fb669f9
PE
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, 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
b09f4f48 47 in the RITEM vector of the items that are selected in this state.
a70083a3 48
742e4900 49 The two types of actions are shifts/gotos (push the lookahead token
8b752b00
AD
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
742e4900 53 lookahead token alone). When the states are generated, these
8b752b00
AD
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
b09f4f48
JD
66 state whose number is in the number field. rules is an array of
67 num rules. lookahead_tokens is an array of bitsets, one per rule.
a70083a3
AD
68
69 Conflict resolution can decide that certain tokens in certain
70 states should explicitly be errors (for implementing %nonassoc).
71 For each state, the tokens that are errors for this reason are
8b752b00 72 recorded in an errs structure, which holds the token numbers.
a70083a3 73
8b752b00 74 There is at least one goto transition present in state zero. It
a70083a3
AD
75 leads to a next-to-final state whose accessing_symbol is the
76 grammar's start symbol. The next-to-final state has one shift to
77 the final state, whose accessing_symbol is zero (end of input).
8b752b00
AD
78 The final state has one shift, which goes to the termination state.
79 The reason for the extra state at the end is to placate the
80 parser's strategy of making all decisions one token ahead of its
81 actions. */
a70083a3
AD
82
83#ifndef STATE_H_
84# define STATE_H_
85
784573d1
PE
86# include <bitset.h>
87
88# include "gram.h"
89# include "symtab.h"
aa2aab3c 90
d57650a5
AD
91
92/*-------------------.
93| Numbering states. |
94`-------------------*/
95
f6fbd3da
PE
96typedef int state_number;
97# define STATE_NUMBER_MAXIMUM INT_MAX
d57650a5 98
784573d1 99/* Be ready to map a state_number to an int. */
2073d769
PE
100static inline int
101state_number_as_int (state_number s)
102{
103 return s;
104}
d57650a5 105
640748ee 106
784573d1 107typedef struct state state;
640748ee 108
ccaf65bc
AD
109/*--------------.
110| Transitions. |
111`--------------*/
aa2aab3c 112
784573d1 113typedef struct
a70083a3 114{
f6fbd3da 115 int num;
784573d1
PE
116 state *states[1];
117} transitions;
d954473d
AD
118
119
8b752b00
AD
120/* What is the symbol labelling the transition to
121 TRANSITIONS->states[Num]? Can be a token (amongst which the error
122 token), or non terminals in case of gotos. */
b608206e 123
8b752b00 124#define TRANSITION_SYMBOL(Transitions, Num) \
640748ee 125 (Transitions->states[Num]->accessing_symbol)
b608206e 126
8b752b00 127/* Is the TRANSITIONS->states[Num] a shift? (as opposed to gotos). */
aa2aab3c 128
8b752b00
AD
129#define TRANSITION_IS_SHIFT(Transitions, Num) \
130 (ISTOKEN (TRANSITION_SYMBOL (Transitions, Num)))
aa2aab3c 131
8b752b00 132/* Is the TRANSITIONS->states[Num] a goto?. */
aa2aab3c 133
8b752b00
AD
134#define TRANSITION_IS_GOTO(Transitions, Num) \
135 (!TRANSITION_IS_SHIFT (Transitions, Num))
aa2aab3c 136
8b752b00 137/* Is the TRANSITIONS->states[Num] labelled by the error token? */
aa2aab3c 138
8b752b00
AD
139#define TRANSITION_IS_ERROR(Transitions, Num) \
140 (TRANSITION_SYMBOL (Transitions, Num) == errtoken->number)
aa2aab3c 141
9839bbe5
AD
142/* When resolving a SR conflicts, if the reduction wins, the shift is
143 disabled. */
144
8b752b00 145#define TRANSITION_DISABLE(Transitions, Num) \
640748ee 146 (Transitions->states[Num] = NULL)
9839bbe5 147
8b752b00 148#define TRANSITION_IS_DISABLED(Transitions, Num) \
640748ee
AD
149 (Transitions->states[Num] == NULL)
150
151
152/* Iterate over each transition over a token (shifts). */
153#define FOR_EACH_SHIFT(Transitions, Iter) \
154 for (Iter = 0; \
155 Iter < Transitions->num \
156 && (TRANSITION_IS_DISABLED (Transitions, Iter) \
157 || TRANSITION_IS_SHIFT (Transitions, Iter)); \
158 ++Iter) \
159 if (!TRANSITION_IS_DISABLED (Transitions, Iter))
160
9839bbe5 161
a737b216 162/* Return the state such SHIFTS contain a shift/goto to it on SYM.
784573d1 163 Abort if none found. */
a737b216 164struct state *transitions_to (transitions *shifts, symbol_number sym);
ccaf65bc 165
aa2aab3c
AD
166
167/*-------.
168| Errs. |
169`-------*/
a70083a3 170
784573d1 171typedef struct
a70083a3 172{
f6fbd3da 173 int num;
784573d1
PE
174 symbol *symbols[1];
175} errs;
a70083a3 176
784573d1 177errs *errs_new (int num, symbol **tokens);
f59c437a 178
a70083a3 179
aa2aab3c
AD
180/*-------------.
181| Reductions. |
182`-------------*/
a70083a3 183
784573d1 184typedef struct
a70083a3 185{
f6fbd3da 186 int num;
742e4900 187 bitset *lookahead_tokens;
784573d1
PE
188 rule *rules[1];
189} reductions;
d0fb370f 190
f693ad14
AD
191
192
640748ee 193/*---------.
784573d1 194| states. |
640748ee 195`---------*/
f693ad14 196
784573d1 197struct state
f693ad14 198{
784573d1
PE
199 state_number number;
200 symbol_number accessing_symbol;
201 transitions *transitions;
202 reductions *reductions;
203 errs *errs;
f693ad14 204
742e4900 205 /* Nonzero if no lookahead is needed to decide what to do in state S. */
f693ad14
AD
206 char consistent;
207
b408954b
AD
208 /* If some conflicts were solved thanks to precedence/associativity,
209 a human readable description of the resolution. */
210 const char *solved_conflicts;
211
212 /* Its items. Must be last, since ITEMS can be arbitrarily large.
213 */
f6fbd3da 214 size_t nitems;
784573d1 215 item_number items[1];
640748ee 216};
f693ad14 217
784573d1
PE
218extern state_number nstates;
219extern state *final_state;
df0e7316
AD
220
221/* Create a new state with ACCESSING_SYMBOL for those items. */
784573d1
PE
222state *state_new (symbol_number accessing_symbol,
223 size_t core_size, item_number *core);
f693ad14 224
8b752b00 225/* Set the transitions of STATE. */
784573d1 226void state_transitions_set (state *s, int num, state **trans);
32e1e0a4 227
8a731ca8 228/* Set the reductions of STATE. */
784573d1 229void state_reductions_set (state *s, int num, rule **reds);
8b752b00 230
784573d1 231int state_reduction_find (state *s, rule *r);
cd08e51e 232
8b752b00 233/* Set the errs of STATE. */
784573d1 234void state_errs_set (state *s, int num, symbol **errors);
8a731ca8 235
742e4900 236/* Print on OUT all the lookahead tokens such that this STATE wants to
784573d1 237 reduce R. */
742e4900 238void state_rule_lookahead_tokens_print (state *s, rule *r, FILE *out);
10e5b8bd 239
c7ca99d4 240/* Create/destroy the states hash table. */
d33cb3ae
PE
241void state_hash_new (void);
242void state_hash_free (void);
c7ca99d4
AD
243
244/* Find the state associated to the CORE, and return it. If it does
245 not exist yet, return NULL. */
784573d1 246state *state_hash_lookup (size_t core_size, item_number *core);
c7ca99d4
AD
247
248/* Insert STATE in the state hash table. */
784573d1 249void state_hash_insert (state *s);
c7ca99d4
AD
250
251/* All the states, indexed by the state number. */
784573d1 252extern state **states;
c7ca99d4
AD
253
254/* Free all the states. */
d33cb3ae 255void states_free (void);
a70083a3 256#endif /* !STATE_H_ */