]> git.saurik.com Git - bison.git/blob - src/state.h
14017afedec657d3f4b67b1c6bfc713c226d39c4
[bison.git] / src / state.h
1 /* Type definitions for nondeterministic finite state machine for bison,
2 Copyright 1984, 1989, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
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.
10
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.
15
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. */
20
21
22 /* These type definitions are used to represent a nondeterministic
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
33 A core represents one state. States are numbered in the number
34 field. When generate_states is finished, the starting state is
35 state 0 and nstates is the number of states. (A transition to a
36 state whose state number is nstates indicates termination.) All
37 the cores are chained together and first_state points to the first
38 one (state 0).
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
42 accessing_symbol of the core.
43
44 Each core contains a vector of nitems items which are the indices
45 in the ritems vector of the items that are selected in this state.
46
47 The link field is used for chaining buckets that hash states by
48 their itemsets. This is for recognizing equivalent states and
49 combining them when the states are generated.
50
51 The two types of transitions are shifts (push the lookahead token
52 and read another) and reductions (combine the last n things on the
53 stack via a rule, replace them with the symbol that the rule
54 derives, and leave the lookahead token alone). When the states are
55 generated, these transitions are represented in two other lists.
56
57 Each shifts structure describes the possible shift transitions out
58 of one state, the state whose number is in the number field. The
59 shifts structures are linked through next and first_shift points to
60 them. Each contains a vector of numbers of the states that shift
61 transitions can go to. The accessing_symbol fields of those
62 states' cores say what kind of input leads to them.
63
64 A shift to state zero should be ignored. Conflict resolution
65 deletes shifts by changing them to zero.
66
67 Each reductions structure describes the possible reductions at the
68 state whose number is in the number field. The data is a list of
69 nreds rules, represented by their rule numbers. first_reduction
70 points to the list of these structures.
71
72 Conflict resolution can decide that certain tokens in certain
73 states should explicitly be errors (for implementing %nonassoc).
74 For each state, the tokens that are errors for this reason are
75 recorded in an errs structure, which has the state number in its
76 number field. The rest of the errs structure is full of token
77 numbers.
78
79 There is at least one shift transition present in state zero. It
80 leads to a next-to-final state whose accessing_symbol is the
81 grammar's start symbol. The next-to-final state has one shift to
82 the final state, whose accessing_symbol is zero (end of input).
83 The final state has one shift, which goes to the termination state
84 (whose number is nstates-1). The reason for the extra state at the
85 end is to placate the parser's strategy of making all decisions one
86 token ahead of its actions. */
87
88 #ifndef STATE_H_
89 # define STATE_H_
90
91
92 /*---------.
93 | Shifts. |
94 `---------*/
95
96 typedef struct shifts
97 {
98 short nshifts;
99 short shifts[1];
100 } shifts;
101
102 shifts *shifts_new PARAMS ((int n));
103
104
105 /* What is the symbol which is shifted by SHIFTS->shifts[Shift]? Can
106 be a token (amongst which the error token), or non terminals in
107 case of gotos. */
108
109 #define SHIFT_SYMBOL(Shifts, Shift) \
110 (state_table[Shifts->shifts[Shift]]->accessing_symbol)
111
112 /* Is the SHIFTS->shifts[Shift] a real shift? (as opposed to gotos.) */
113
114 #define SHIFT_IS_SHIFT(Shifts, Shift) \
115 (ISTOKEN (SHIFT_SYMBOL (Shifts, Shift)))
116
117 /* Is the SHIFTS->shifts[Shift] a goto?. */
118
119 #define SHIFT_IS_GOTO(Shifts, Shift) \
120 (!SHIFT_IS_SHIFT (Shifts, Shift))
121
122 /* Is the SHIFTS->shifts[Shift] then handling of the error token?. */
123
124 #define SHIFT_IS_ERROR(Shifts, Shift) \
125 (SHIFT_SYMBOL (Shifts, Shift) == error_token_number)
126
127 /* When resolving a SR conflicts, if the reduction wins, the shift is
128 disabled. */
129
130 #define SHIFT_DISABLE(Shifts, Shift) \
131 (Shifts->shifts[Shift] = 0)
132
133 #define SHIFT_IS_DISABLED(Shifts, Shift) \
134 (Shifts->shifts[Shift] == 0)
135
136
137 /*-------.
138 | Errs. |
139 `-------*/
140
141 typedef struct errs
142 {
143 short nerrs;
144 short errs[1];
145 } errs;
146
147 errs *errs_new PARAMS ((int n));
148 errs *errs_dup PARAMS ((errs *src));
149
150
151 /*-------------.
152 | Reductions. |
153 `-------------*/
154
155 typedef struct reductions
156 {
157 short nreds;
158 short rules[1];
159 } reductions;
160
161 #define REDUCTIONS_ALLOC(Nreductions) \
162 (reductions *) xcalloc ((unsigned) (sizeof (reductions) \
163 + (Nreductions - 1) * sizeof (short)), 1)
164
165
166
167 /*----------.
168 | State_t. |
169 `----------*/
170
171 typedef struct state_s
172 {
173 struct state_s *next;
174 struct state_s *link;
175
176 short number;
177 short accessing_symbol;
178 shifts *shifts;
179 reductions *reductions;
180 errs *errs;
181
182 /* Nonzero if no lookahead is needed to decide what to do in state S. */
183 char consistent;
184
185 /* Used in LALR, not LR(0). */
186 /* Pseudo pointer into LA. */
187 short lookaheadsp;
188 int nlookaheads;
189
190 /* Its items. */
191 short nitems;
192 short items[1];
193 } state_t;
194
195 #define STATE_ALLOC(Nitems) \
196 (state_t *) xcalloc ((unsigned) (sizeof (state_t) \
197 + (Nitems - 1) * sizeof (short)), 1)
198
199 #endif /* !STATE_H_ */