]>
git.saurik.com Git - bison.git/blob - src/LR0.c
15b29f3010d851ddf2228de254c23bccbefd621e
1 /* Generate the nondeterministic finite state machine for bison,
2 Copyright 1984, 1986, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
22 /* See comments in state.h for the data structures that represent it.
23 The entry point is generate_states. */
39 /* Initialize the final state to -1, otherwise, it might be set to 0
40 by default, and since we don't compute the reductions of the final
41 state, we end up not computing the reductions of the initial state,
42 which is of course needed.
44 FINAL_STATE is properly set by new_state when it recognizes the
45 accessing symbol: EOF. */
47 static state_t
*first_state
= NULL
;
49 static state_t
*this_state
= NULL
;
50 static state_t
*last_state
= NULL
;
53 static short *shift_symbol
= NULL
;
55 static short *redset
= NULL
;
56 static short *shiftset
= NULL
;
58 static short **kernel_base
= NULL
;
59 static int *kernel_size
= NULL
;
60 static short *kernel_items
= NULL
;
62 /* hash table for states, to recognize equivalent ones. */
64 #define STATE_HASH_SIZE 1009
65 static state_t
**state_hash
= NULL
;
69 allocate_itemsets (void)
74 /* Count the number of occurrences of all the symbols in RITEMS.
75 Note that useless productions (hence useless nonterminals) are
76 browsed too, hence we need to allocate room for _all_ the
79 short *symbol_count
= XCALLOC (short, nsyms
+ nuseless_nonterminals
);
81 for (r
= 1; r
< nrules
+ 1; ++r
)
82 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; ++rhsp
)
85 symbol_count
[*rhsp
]++;
88 /* See comments before new_itemsets. All the vectors of items
89 live inside KERNEL_ITEMS. The number of active items after
90 some symbol cannot be more than the number of times that symbol
91 appears as an item, which is symbol_count[symbol].
92 We allocate that much space for each symbol. */
94 kernel_base
= XCALLOC (short *, nsyms
);
96 kernel_items
= XCALLOC (short, count
);
99 for (i
= 0; i
< nsyms
; i
++)
101 kernel_base
[i
] = kernel_items
+ count
;
102 count
+= symbol_count
[i
];
106 kernel_size
= XCALLOC (int, nsyms
);
111 allocate_storage (void)
113 allocate_itemsets ();
115 shiftset
= XCALLOC (short, nsyms
);
116 redset
= XCALLOC (short, nrules
+ 1);
117 state_hash
= XCALLOC (state_t
*, STATE_HASH_SIZE
);
118 shift_symbol
= XCALLOC (short, nsyms
);
130 XFREE (kernel_items
);
137 /*----------------------------------------------------------------.
138 | Find which symbols can be shifted in the current state, and for |
139 | each one record which items would be active after that shift. |
140 | Uses the contents of itemset. |
142 | shift_symbol is set to a vector of the symbols that can be |
143 | shifted. For each symbol in the grammar, kernel_base[symbol] |
144 | points to a vector of item numbers activated if that symbol is |
145 | shifted, and kernel_size[symbol] is their numbers. |
146 `----------------------------------------------------------------*/
154 fprintf (stderr
, "Entering new_itemsets, state = %d\n",
157 for (i
= 0; i
< nsyms
; i
++)
162 for (i
= 0; i
< nitemset
; ++i
)
164 int symbol
= ritem
[itemset
[i
]];
167 if (!kernel_size
[symbol
])
169 shift_symbol
[nshifts
] = symbol
;
173 kernel_base
[symbol
][kernel_size
[symbol
]] = itemset
[i
] + 1;
174 kernel_size
[symbol
]++;
181 /*-----------------------------------------------------------------.
182 | Subroutine of get_state. Create a new state for those items, if |
184 `-----------------------------------------------------------------*/
187 new_state (int symbol
)
192 fprintf (stderr
, "Entering new_state, state = %d, symbol = %d (%s)\n",
193 this_state
->number
, symbol
, symbols
[symbol
]->tag
);
195 if (nstates
>= MAXSHORT
)
196 fatal (_("too many states (max %d)"), MAXSHORT
);
198 p
= STATE_ALLOC (kernel_size
[symbol
]);
199 p
->accessing_symbol
= symbol
;
201 p
->nitems
= kernel_size
[symbol
];
203 shortcpy (p
->items
, kernel_base
[symbol
], kernel_size
[symbol
]);
205 last_state
->next
= p
;
209 /* If this is the eoftoken, then this is the final state. */
211 final_state
= p
->number
;
217 /*--------------------------------------------------------------.
218 | Find the state number for the state we would get to (from the |
219 | current state) by shifting symbol. Create a new state if no |
220 | equivalent one exists already. Used by append_states. |
221 `--------------------------------------------------------------*/
224 get_state (int symbol
)
231 fprintf (stderr
, "Entering get_state, state = %d, symbol = %d (%s)\n",
232 this_state
->number
, symbol
, symbols
[symbol
]->tag
);
234 /* Add up the target state's active item numbers to get a hash key.
237 for (i
= 0; i
< kernel_size
[symbol
]; ++i
)
238 key
+= kernel_base
[symbol
][i
];
239 key
= key
% STATE_HASH_SIZE
;
240 sp
= state_hash
[key
];
247 if (sp
->nitems
== kernel_size
[symbol
])
250 for (i
= 0; i
< kernel_size
[symbol
]; ++i
)
251 if (kernel_base
[symbol
][i
] != sp
->items
[i
])
261 else /* bucket exhausted and no match */
263 sp
= sp
->link
= new_state (symbol
);
269 else /* bucket is empty */
271 state_hash
[key
] = sp
= new_state (symbol
);
275 fprintf (stderr
, "Exiting get_state => %d\n", sp
->number
);
280 /*------------------------------------------------------------------.
281 | Use the information computed by new_itemsets to find the state |
282 | numbers reached by each shift transition from the current state. |
284 | shiftset is set up as a vector of state numbers of those states. |
285 `------------------------------------------------------------------*/
295 fprintf (stderr
, "Entering append_states, state = %d\n",
298 /* first sort shift_symbol into increasing order */
300 for (i
= 1; i
< nshifts
; i
++)
302 symbol
= shift_symbol
[i
];
304 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
306 shift_symbol
[j
] = shift_symbol
[j
- 1];
309 shift_symbol
[j
] = symbol
;
312 for (i
= 0; i
< nshifts
; i
++)
313 shiftset
[i
] = get_state (shift_symbol
[i
]);
320 first_state
= last_state
= this_state
= STATE_ALLOC (0);
325 /*------------------------------------------------------------.
326 | Save the NSHIFTS of SHIFTSET into the current linked list. |
327 `------------------------------------------------------------*/
332 shifts
*p
= shifts_new (nshifts
);
333 shortcpy (p
->shifts
, shiftset
, nshifts
);
334 this_state
->shifts
= p
;
338 /*----------------------------------------------------------------.
339 | Find which rules can be used for reduction transitions from the |
340 | current state and make a reductions structure for the state to |
341 | record their rule numbers. |
342 `----------------------------------------------------------------*/
345 save_reductions (void)
350 /* If this is the final state, we want it to have no reductions at
351 all, although it has one for `START_SYMBOL EOF .'. */
352 if (this_state
->number
== final_state
)
355 /* Find and count the active items that represent ends of rules. */
356 for (i
= 0; i
< nitemset
; ++i
)
358 int item
= ritem
[itemset
[i
]];
360 redset
[count
++] = -item
;
363 /* Make a reductions structure and copy the data into it. */
364 this_state
->reductions
= reductions_new (count
);
365 shortcpy (this_state
->reductions
->rules
, redset
, count
);
377 states
= XCALLOC (state_t
*, nstates
);
379 for (sp
= first_state
; sp
; sp
= sp
->next
)
381 /* Pessimization, but simplification of the code: make sure all
382 the states have a shifts, errs, and reductions, even if
385 sp
->shifts
= shifts_new (0);
387 sp
->errs
= errs_new (0);
389 sp
->reductions
= reductions_new (0);
391 states
[sp
->number
] = sp
;
395 /*-------------------------------------------------------------------.
396 | Compute the nondeterministic finite state machine (see state.h for |
397 | details) from the grammar. |
398 `-------------------------------------------------------------------*/
401 generate_states (void)
404 new_closure (nritems
);
410 fprintf (stderr
, "Processing state %d (reached by %s)\n",
412 symbols
[this_state
->accessing_symbol
]->tag
);
413 /* Set up ruleset and itemset for the transitions out of this
414 state. ruleset gets a 1 bit for each rule that could reduce
415 now. itemset gets a vector of all the items that could be
417 closure (this_state
->items
, this_state
->nitems
);
418 /* record the reductions allowed out of this state */
420 /* find the itemsets of the states that shifts can reach */
422 /* find or create the core structures for those states */
425 /* create the shifts structures for the shifts to those states,
426 now that the state numbers transitioning to are known */
429 /* states are queued when they are created; process them all */
430 this_state
= this_state
->next
;
433 /* discard various storage */