]> git.saurik.com Git - bison.git/blame - src/state.h
(yylineno, yyoutname): Remove static vars, replacing
[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
cd08e51e 85# include "bitset.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
640748ee
AD
98
99typedef struct state_s state_t;
100
ccaf65bc
AD
101/*--------------.
102| Transitions. |
103`--------------*/
aa2aab3c 104
ccaf65bc 105typedef struct transtion_s
a70083a3 106{
ccaf65bc 107 short num;
640748ee 108 state_t *states[1];
ccaf65bc 109} transitions_t;
d954473d
AD
110
111
8b752b00
AD
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. */
b608206e 115
8b752b00 116#define TRANSITION_SYMBOL(Transitions, Num) \
640748ee 117 (Transitions->states[Num]->accessing_symbol)
b608206e 118
8b752b00 119/* Is the TRANSITIONS->states[Num] a shift? (as opposed to gotos). */
aa2aab3c 120
8b752b00
AD
121#define TRANSITION_IS_SHIFT(Transitions, Num) \
122 (ISTOKEN (TRANSITION_SYMBOL (Transitions, Num)))
aa2aab3c 123
8b752b00 124/* Is the TRANSITIONS->states[Num] a goto?. */
aa2aab3c 125
8b752b00
AD
126#define TRANSITION_IS_GOTO(Transitions, Num) \
127 (!TRANSITION_IS_SHIFT (Transitions, Num))
aa2aab3c 128
8b752b00 129/* Is the TRANSITIONS->states[Num] labelled by the error token? */
aa2aab3c 130
8b752b00
AD
131#define TRANSITION_IS_ERROR(Transitions, Num) \
132 (TRANSITION_SYMBOL (Transitions, Num) == errtoken->number)
aa2aab3c 133
9839bbe5
AD
134/* When resolving a SR conflicts, if the reduction wins, the shift is
135 disabled. */
136
8b752b00 137#define TRANSITION_DISABLE(Transitions, Num) \
640748ee 138 (Transitions->states[Num] = NULL)
9839bbe5 139
8b752b00 140#define TRANSITION_IS_DISABLED(Transitions, Num) \
640748ee
AD
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
9839bbe5 153
ccaf65bc 154/* Return the state such these TRANSITIONS contain a shift/goto to it on
24c7d800
AD
155 SYMBOL. Aborts if none found. */
156struct state_s;
d33cb3ae 157struct state_s *transitions_to (transitions_t *state, symbol_number_t s);
ccaf65bc 158
aa2aab3c
AD
159
160/*-------.
161| Errs. |
162`-------*/
a70083a3 163
8a731ca8 164typedef struct errs_s
a70083a3 165{
d2576365 166 short num;
640748ee 167 symbol_t *symbols[1];
8a731ca8 168} errs_t;
a70083a3 169
d33cb3ae 170errs_t *errs_new (int num, symbol_t **tokens);
f59c437a 171
a70083a3 172
aa2aab3c
AD
173/*-------------.
174| Reductions. |
175`-------------*/
a70083a3 176
8a731ca8 177typedef struct reductions_s
a70083a3 178{
d2576365 179 short num;
cd08e51e 180 bitset *lookaheads;
640748ee 181 rule_t *rules[1];
8a731ca8 182} reductions_t;
d0fb370f 183
f693ad14
AD
184
185
640748ee
AD
186/*---------.
187| States. |
188`---------*/
f693ad14 189
640748ee 190struct state_s
f693ad14 191{
d57650a5 192 state_number_t number;
a49aecd5 193 symbol_number_t accessing_symbol;
8b752b00 194 transitions_t *transitions;
8a731ca8
AD
195 reductions_t *reductions;
196 errs_t *errs;
f693ad14
AD
197
198 /* Nonzero if no lookahead is needed to decide what to do in state S. */
199 char consistent;
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];
640748ee 209};
f693ad14 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. */
d33cb3ae
PE
215state_t *state_new (symbol_number_t accessing_symbol,
216 size_t core_size, item_number_t *core);
f693ad14 217
8b752b00 218/* Set the transitions of STATE. */
d33cb3ae 219void state_transitions_set (state_t *state, int num, state_t **transitions);
32e1e0a4 220
8a731ca8 221/* Set the reductions of STATE. */
d33cb3ae 222void state_reductions_set (state_t *state, int num, rule_t **reductions);
8b752b00 223
d33cb3ae 224int state_reduction_find (state_t *state, rule_t *rule);
cd08e51e 225
8b752b00 226/* Set the errs of STATE. */
d33cb3ae 227void state_errs_set (state_t *state, int num, symbol_t **errs);
8a731ca8 228
10e5b8bd
AD
229/* Print on OUT all the lookaheads such that this STATE wants to
230 reduce this RULE. */
d33cb3ae 231void state_rule_lookaheads_print (state_t *state, rule_t *rule, FILE *out);
10e5b8bd 232
c7ca99d4 233/* Create/destroy the states hash table. */
d33cb3ae
PE
234void state_hash_new (void);
235void state_hash_free (void);
c7ca99d4
AD
236
237/* Find the state associated to the CORE, and return it. If it does
238 not exist yet, return NULL. */
d33cb3ae 239state_t *state_hash_lookup (size_t core_size, item_number_t *core);
c7ca99d4
AD
240
241/* Insert STATE in the state hash table. */
d33cb3ae 242void state_hash_insert (state_t *state);
c7ca99d4
AD
243
244/* All the states, indexed by the state number. */
245extern state_t **states;
246
247/* Free all the states. */
d33cb3ae 248void states_free (void);
a70083a3 249#endif /* !STATE_H_ */