]>
git.saurik.com Git - bison.git/blob - src/LR0.c
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. */
40 static state_t
*first_state
= NULL
;
42 static state_t
*this_state
= NULL
;
43 static state_t
*last_state
= NULL
;
46 static symbol_number_t
*shift_symbol
= NULL
;
48 static short *redset
= NULL
;
49 static state_number_t
*shiftset
= NULL
;
51 static item_number_t
**kernel_base
= NULL
;
52 static int *kernel_size
= NULL
;
53 static item_number_t
*kernel_items
= NULL
;
55 /* hash table for states, to recognize equivalent ones. */
57 #define STATE_HASH_SIZE 1009
58 static state_t
**state_hash
= NULL
;
62 allocate_itemsets (void)
67 /* Count the number of occurrences of all the symbols in RITEMS.
68 Note that useless productions (hence useless nonterminals) are
69 browsed too, hence we need to allocate room for _all_ the
72 short *symbol_count
= XCALLOC (short, nsyms
+ nuseless_nonterminals
);
74 for (r
= 1; r
< nrules
+ 1; ++r
)
75 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; ++rhsp
)
78 symbol_count
[*rhsp
]++;
81 /* See comments before new_itemsets. All the vectors of items
82 live inside KERNEL_ITEMS. The number of active items after
83 some symbol cannot be more than the number of times that symbol
84 appears as an item, which is symbol_count[symbol].
85 We allocate that much space for each symbol. */
87 kernel_base
= XCALLOC (item_number_t
*, nsyms
);
89 kernel_items
= XCALLOC (item_number_t
, count
);
92 for (i
= 0; i
< nsyms
; i
++)
94 kernel_base
[i
] = kernel_items
+ count
;
95 count
+= symbol_count
[i
];
99 kernel_size
= XCALLOC (int, nsyms
);
104 allocate_storage (void)
106 allocate_itemsets ();
108 shiftset
= XCALLOC (state_number_t
, nsyms
);
109 redset
= XCALLOC (short, nrules
+ 1);
110 state_hash
= XCALLOC (state_t
*, STATE_HASH_SIZE
);
111 shift_symbol
= XCALLOC (symbol_number_t
, nsyms
);
123 XFREE (kernel_items
);
130 /*----------------------------------------------------------------.
131 | Find which symbols can be shifted in the current state, and for |
132 | each one record which items would be active after that shift. |
133 | Uses the contents of itemset. |
135 | shift_symbol is set to a vector of the symbols that can be |
136 | shifted. For each symbol in the grammar, kernel_base[symbol] |
137 | points to a vector of item numbers activated if that symbol is |
138 | shifted, and kernel_size[symbol] is their numbers. |
139 `----------------------------------------------------------------*/
147 fprintf (stderr
, "Entering new_itemsets, state = %d\n",
150 for (i
= 0; i
< nsyms
; i
++)
155 for (i
= 0; i
< nritemset
; ++i
)
156 if (ritem
[itemset
[i
]] >= 0)
158 symbol_number_t symbol
159 = item_number_as_symbol_number (ritem
[itemset
[i
]]);
160 if (!kernel_size
[symbol
])
162 shift_symbol
[nshifts
] = symbol
;
166 kernel_base
[symbol
][kernel_size
[symbol
]] = itemset
[i
] + 1;
167 kernel_size
[symbol
]++;
173 /*-----------------------------------------------------------------.
174 | Subroutine of get_state. Create a new state for those items, if |
176 `-----------------------------------------------------------------*/
179 new_state (symbol_number_t symbol
, size_t core_size
, item_number_t
*core
)
184 fprintf (stderr
, "Entering new_state, state = %d, symbol = %d (%s)\n",
185 nstates
, symbol
, symbol_tag_get (symbols
[symbol
]));
187 res
= state_new (symbol
, core_size
, core
);
189 /* If this is the eoftoken, and this is not the initial state, then
190 this is the final state. */
191 if (symbol
== 0 && first_state
)
197 last_state
->next
= res
;
204 /*--------------------------------------------------------------.
205 | Find the state number for the state we would get to (from the |
206 | current state) by shifting symbol. Create a new state if no |
207 | equivalent one exists already. Used by append_states. |
208 `--------------------------------------------------------------*/
210 static state_number_t
211 get_state (symbol_number_t symbol
, size_t core_size
, item_number_t
*core
)
218 fprintf (stderr
, "Entering get_state, state = %d, symbol = %d (%s)\n",
219 this_state
->number
, symbol
,
220 symbol_tag_get (symbols
[symbol
]));
222 /* Add up the target state's active item numbers to get a hash key.
225 for (i
= 0; i
< core_size
; ++i
)
227 key
= key
% STATE_HASH_SIZE
;
228 sp
= state_hash
[key
];
235 if (sp
->nitems
== core_size
)
238 for (i
= 0; i
< core_size
; ++i
)
239 if (core
[i
] != sp
->items
[i
])
249 else /* bucket exhausted and no match */
251 sp
= sp
->link
= new_state (symbol
, core_size
, core
);
257 else /* bucket is empty */
259 state_hash
[key
] = sp
= new_state (symbol
, core_size
, core
);
263 fprintf (stderr
, "Exiting get_state => %d\n", sp
->number
);
268 /*------------------------------------------------------------------.
269 | Use the information computed by new_itemsets to find the state |
270 | numbers reached by each shift transition from the current state. |
272 | shiftset is set up as a vector of state numbers of those states. |
273 `------------------------------------------------------------------*/
280 symbol_number_t symbol
;
283 fprintf (stderr
, "Entering append_states, state = %d\n",
286 /* first sort shift_symbol into increasing order */
288 for (i
= 1; i
< nshifts
; i
++)
290 symbol
= shift_symbol
[i
];
292 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
294 shift_symbol
[j
] = shift_symbol
[j
- 1];
297 shift_symbol
[j
] = symbol
;
300 for (i
= 0; i
< nshifts
; i
++)
302 symbol
= shift_symbol
[i
];
303 shiftset
[i
] = get_state (symbol
,
304 kernel_size
[symbol
], kernel_base
[symbol
]);
312 /* The 0 at the lhs is the index of the item of this initial rule. */
313 kernel_base
[0][0] = 0;
315 this_state
= new_state (0, kernel_size
[0], kernel_base
[0]);
319 /*------------------------------------------------------------.
320 | Save the NSHIFTS of SHIFTSET into the current linked list. |
321 `------------------------------------------------------------*/
326 shifts
*p
= shifts_new (nshifts
);
327 memcpy (p
->shifts
, shiftset
, nshifts
* sizeof (shiftset
[0]));
328 this_state
->shifts
= p
;
332 /*----------------------------------------------------------------.
333 | Find which rules can be used for reduction transitions from the |
334 | current state and make a reductions structure for the state to |
335 | record their rule numbers. |
336 `----------------------------------------------------------------*/
339 save_reductions (void)
344 /* If this is the final state, we want it to have no reductions at
345 all, although it has one for `START_SYMBOL EOF .'. */
346 if (final_state
&& this_state
->number
== final_state
->number
)
349 /* Find and count the active items that represent ends of rules. */
350 for (i
= 0; i
< nritemset
; ++i
)
352 int item
= ritem
[itemset
[i
]];
354 redset
[count
++] = -item
;
357 /* Make a reductions structure and copy the data into it. */
358 this_state
->reductions
= reductions_new (count
);
359 memcpy (this_state
->reductions
->rules
, redset
, count
* sizeof (redset
[0]));
371 states
= XCALLOC (state_t
*, nstates
);
373 for (sp
= first_state
; sp
; sp
= sp
->next
)
375 /* Pessimization, but simplification of the code: make sure all
376 the states have a shifts, errs, and reductions, even if
379 sp
->shifts
= shifts_new (0);
381 sp
->errs
= errs_new (0);
383 sp
->reductions
= reductions_new (0);
385 states
[sp
->number
] = sp
;
389 /*-------------------------------------------------------------------.
390 | Compute the nondeterministic finite state machine (see state.h for |
391 | details) from the grammar. |
392 `-------------------------------------------------------------------*/
395 generate_states (void)
398 new_closure (nritems
);
404 fprintf (stderr
, "Processing state %d (reached by %s)\n",
406 symbol_tag_get (symbols
[this_state
->accessing_symbol
]));
407 /* Set up ruleset and itemset for the transitions out of this
408 state. ruleset gets a 1 bit for each rule that could reduce
409 now. itemset gets a vector of all the items that could be
411 closure (this_state
->items
, this_state
->nitems
);
412 /* record the reductions allowed out of this state */
414 /* find the itemsets of the states that shifts can reach */
416 /* find or create the core structures for those states */
419 /* create the shifts structures for the shifts to those states,
420 now that the state numbers transitioning to are known */
423 /* states are queued when they are created; process them all */
424 this_state
= this_state
->next
;
427 /* discard various storage */