]>
git.saurik.com Git - bison.git/blob - src/LR0.c
ef7524b2483193035b7de2975d580e60a819053b
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 state_number_t nstates
= 0;
41 /* FINAL_STATE is properly set by new_state when it recognizes its
42 accessing symbol: EOF. */
43 state_t
*final_state
= NULL
;
44 static state_t
*first_state
= NULL
;
46 static state_t
*this_state
= NULL
;
47 static state_t
*last_state
= NULL
;
50 static symbol_number_t
*shift_symbol
= NULL
;
52 static short *redset
= NULL
;
53 static state_number_t
*shiftset
= NULL
;
55 static item_number_t
**kernel_base
= NULL
;
56 static int *kernel_size
= NULL
;
57 static item_number_t
*kernel_items
= NULL
;
59 /* hash table for states, to recognize equivalent ones. */
61 #define STATE_HASH_SIZE 1009
62 static state_t
**state_hash
= NULL
;
66 allocate_itemsets (void)
71 /* Count the number of occurrences of all the symbols in RITEMS.
72 Note that useless productions (hence useless nonterminals) are
73 browsed too, hence we need to allocate room for _all_ the
76 short *symbol_count
= XCALLOC (short, nsyms
+ nuseless_nonterminals
);
78 for (r
= 1; r
< nrules
+ 1; ++r
)
79 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; ++rhsp
)
82 symbol_count
[*rhsp
]++;
85 /* See comments before new_itemsets. All the vectors of items
86 live inside KERNEL_ITEMS. The number of active items after
87 some symbol cannot be more than the number of times that symbol
88 appears as an item, which is symbol_count[symbol].
89 We allocate that much space for each symbol. */
91 kernel_base
= XCALLOC (item_number_t
*, nsyms
);
93 kernel_items
= XCALLOC (item_number_t
, count
);
96 for (i
= 0; i
< nsyms
; i
++)
98 kernel_base
[i
] = kernel_items
+ count
;
99 count
+= symbol_count
[i
];
103 kernel_size
= XCALLOC (int, nsyms
);
108 allocate_storage (void)
110 allocate_itemsets ();
112 shiftset
= XCALLOC (state_number_t
, nsyms
);
113 redset
= XCALLOC (short, nrules
+ 1);
114 state_hash
= XCALLOC (state_t
*, STATE_HASH_SIZE
);
115 shift_symbol
= XCALLOC (symbol_number_t
, nsyms
);
127 XFREE (kernel_items
);
134 /*----------------------------------------------------------------.
135 | Find which symbols can be shifted in the current state, and for |
136 | each one record which items would be active after that shift. |
137 | Uses the contents of itemset. |
139 | shift_symbol is set to a vector of the symbols that can be |
140 | shifted. For each symbol in the grammar, kernel_base[symbol] |
141 | points to a vector of item numbers activated if that symbol is |
142 | shifted, and kernel_size[symbol] is their numbers. |
143 `----------------------------------------------------------------*/
151 fprintf (stderr
, "Entering new_itemsets, state = %d\n",
154 for (i
= 0; i
< nsyms
; i
++)
159 for (i
= 0; i
< nritemset
; ++i
)
160 if (ritem
[itemset
[i
]] >= 0)
162 symbol_number_t symbol
163 = item_number_as_symbol_number (ritem
[itemset
[i
]]);
164 if (!kernel_size
[symbol
])
166 shift_symbol
[nshifts
] = symbol
;
170 kernel_base
[symbol
][kernel_size
[symbol
]] = itemset
[i
] + 1;
171 kernel_size
[symbol
]++;
177 /*-----------------------------------------------------------------.
178 | Subroutine of get_state. Create a new state for those items, if |
180 `-----------------------------------------------------------------*/
183 new_state (symbol_number_t symbol
, size_t core_size
, item_number_t
*core
)
188 fprintf (stderr
, "Entering new_state, state = %d, symbol = %d (%s)\n",
189 nstates
, symbol
, symbol_tag_get (symbols
[symbol
]));
191 if (nstates
>= STATE_NUMBER_MAX
)
192 fatal (_("too many states (max %d)"), STATE_NUMBER_MAX
);
194 p
= STATE_ALLOC (core_size
);
195 p
->accessing_symbol
= symbol
;
197 p
->solved_conflicts
= NULL
;
199 p
->nitems
= core_size
;
200 memcpy (p
->items
, core
, core_size
* sizeof (core
[0]));
202 /* If this is the eoftoken, and this is not the initial state, then
203 this is the final state. */
204 if (symbol
== 0 && first_state
)
210 last_state
->next
= p
;
219 /*--------------------------------------------------------------.
220 | Find the state number for the state we would get to (from the |
221 | current state) by shifting symbol. Create a new state if no |
222 | equivalent one exists already. Used by append_states. |
223 `--------------------------------------------------------------*/
225 static state_number_t
226 get_state (symbol_number_t symbol
, size_t core_size
, item_number_t
*core
)
233 fprintf (stderr
, "Entering get_state, state = %d, symbol = %d (%s)\n",
234 this_state
->number
, symbol
,
235 symbol_tag_get (symbols
[symbol
]));
237 /* Add up the target state's active item numbers to get a hash key.
240 for (i
= 0; i
< core_size
; ++i
)
242 key
= key
% STATE_HASH_SIZE
;
243 sp
= state_hash
[key
];
250 if (sp
->nitems
== core_size
)
253 for (i
= 0; i
< core_size
; ++i
)
254 if (core
[i
] != sp
->items
[i
])
264 else /* bucket exhausted and no match */
266 sp
= sp
->link
= new_state (symbol
, core_size
, core
);
272 else /* bucket is empty */
274 state_hash
[key
] = sp
= new_state (symbol
, core_size
, core
);
278 fprintf (stderr
, "Exiting get_state => %d\n", sp
->number
);
283 /*------------------------------------------------------------------.
284 | Use the information computed by new_itemsets to find the state |
285 | numbers reached by each shift transition from the current state. |
287 | shiftset is set up as a vector of state numbers of those states. |
288 `------------------------------------------------------------------*/
295 symbol_number_t symbol
;
298 fprintf (stderr
, "Entering append_states, state = %d\n",
301 /* first sort shift_symbol into increasing order */
303 for (i
= 1; i
< nshifts
; i
++)
305 symbol
= shift_symbol
[i
];
307 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
309 shift_symbol
[j
] = shift_symbol
[j
- 1];
312 shift_symbol
[j
] = symbol
;
315 for (i
= 0; i
< nshifts
; i
++)
317 symbol
= shift_symbol
[i
];
318 shiftset
[i
] = get_state (symbol
,
319 kernel_size
[symbol
], kernel_base
[symbol
]);
327 /* The 0 at the lhs is the index of the item of this initial rule. */
328 kernel_base
[0][0] = 0;
330 this_state
= new_state (0, kernel_size
[0], kernel_base
[0]);
334 /*------------------------------------------------------------.
335 | Save the NSHIFTS of SHIFTSET into the current linked list. |
336 `------------------------------------------------------------*/
341 shifts
*p
= shifts_new (nshifts
);
342 memcpy (p
->shifts
, shiftset
, nshifts
* sizeof (shiftset
[0]));
343 this_state
->shifts
= p
;
347 /*----------------------------------------------------------------.
348 | Find which rules can be used for reduction transitions from the |
349 | current state and make a reductions structure for the state to |
350 | record their rule numbers. |
351 `----------------------------------------------------------------*/
354 save_reductions (void)
359 /* If this is the final state, we want it to have no reductions at
360 all, although it has one for `START_SYMBOL EOF .'. */
361 if (final_state
&& this_state
->number
== final_state
->number
)
364 /* Find and count the active items that represent ends of rules. */
365 for (i
= 0; i
< nritemset
; ++i
)
367 int item
= ritem
[itemset
[i
]];
369 redset
[count
++] = -item
;
372 /* Make a reductions structure and copy the data into it. */
373 this_state
->reductions
= reductions_new (count
);
374 memcpy (this_state
->reductions
->rules
, redset
, count
* sizeof (redset
[0]));
386 states
= XCALLOC (state_t
*, nstates
);
388 for (sp
= first_state
; sp
; sp
= sp
->next
)
390 /* Pessimization, but simplification of the code: make sure all
391 the states have a shifts, errs, and reductions, even if
394 sp
->shifts
= shifts_new (0);
396 sp
->errs
= errs_new (0);
398 sp
->reductions
= reductions_new (0);
400 states
[sp
->number
] = sp
;
404 /*-------------------------------------------------------------------.
405 | Compute the nondeterministic finite state machine (see state.h for |
406 | details) from the grammar. |
407 `-------------------------------------------------------------------*/
410 generate_states (void)
413 new_closure (nritems
);
419 fprintf (stderr
, "Processing state %d (reached by %s)\n",
421 symbol_tag_get (symbols
[this_state
->accessing_symbol
]));
422 /* Set up ruleset and itemset for the transitions out of this
423 state. ruleset gets a 1 bit for each rule that could reduce
424 now. itemset gets a vector of all the items that could be
426 closure (this_state
->items
, this_state
->nitems
);
427 /* record the reductions allowed out of this state */
429 /* find the itemsets of the states that shifts can reach */
431 /* find or create the core structures for those states */
434 /* create the shifts structures for the shifts to those states,
435 now that the state numbers transitioning to are known */
438 /* states are queued when they are created; process them all */
439 this_state
= this_state
->next
;
442 /* discard various storage */