]>
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 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. */
38 static state_t
*first_state
= NULL
;
40 static state_t
*this_state
= NULL
;
41 static state_t
*last_state
= NULL
;
44 static short *shift_symbol
= NULL
;
46 static short *redset
= NULL
;
47 static short *shiftset
= NULL
;
49 static short **kernel_base
= NULL
;
50 static int *kernel_size
= NULL
;
51 static short *kernel_items
= NULL
;
53 /* hash table for states, to recognize equivalent ones. */
55 #define STATE_HASH_SIZE 1009
56 static state_t
**state_hash
= NULL
;
60 allocate_itemsets (void)
64 /* Count the number of occurrences of all the symbols in RITEMS.
65 Note that useless productions (hence useless nonterminals) are
66 browsed too, hence we need to allocate room for _all_ the
69 short *symbol_count
= XCALLOC (short, nsyms
+ nuseless_nonterminals
);
71 for (i
= 0; i
< nritems
; ++i
)
75 symbol_count
[ritem
[i
]]++;
78 /* See comments before new_itemsets. All the vectors of items
79 live inside KERNEL_ITEMS. The number of active items after
80 some symbol cannot be more than the number of times that symbol
81 appears as an item, which is symbol_count[symbol].
82 We allocate that much space for each symbol. */
84 kernel_base
= XCALLOC (short *, nsyms
);
86 kernel_items
= XCALLOC (short, count
);
89 for (i
= 0; i
< nsyms
; i
++)
91 kernel_base
[i
] = kernel_items
+ count
;
92 count
+= symbol_count
[i
];
96 kernel_size
= XCALLOC (int, nsyms
);
101 allocate_storage (void)
103 allocate_itemsets ();
105 shiftset
= XCALLOC (short, nsyms
);
106 redset
= XCALLOC (short, nrules
+ 1);
107 state_hash
= XCALLOC (state_t
*, STATE_HASH_SIZE
);
119 XFREE (kernel_items
);
126 /*----------------------------------------------------------------.
127 | Find which symbols can be shifted in the current state, and for |
128 | each one record which items would be active after that shift. |
129 | Uses the contents of itemset. |
131 | shift_symbol is set to a vector of the symbols that can be |
132 | shifted. For each symbol in the grammar, kernel_base[symbol] |
133 | points to a vector of item numbers activated if that symbol is |
134 | shifted, and kernel_size[symbol] is their numbers. |
135 `----------------------------------------------------------------*/
143 fprintf (stderr
, "Entering new_itemsets, state = %d\n",
146 for (i
= 0; i
< nsyms
; i
++)
149 shift_symbol
= XCALLOC (short, nsyms
);
152 for (i
= 0; i
< nitemset
; ++i
)
154 int symbol
= ritem
[itemset
[i
]];
157 if (!kernel_size
[symbol
])
159 shift_symbol
[nshifts
] = symbol
;
163 kernel_base
[symbol
][kernel_size
[symbol
]] = itemset
[i
] + 1;
164 kernel_size
[symbol
]++;
171 /*-----------------------------------------------------------------.
172 | Subroutine of get_state. Create a new state for those items, if |
174 `-----------------------------------------------------------------*/
177 new_state (int symbol
)
182 fprintf (stderr
, "Entering new_state, state = %d, symbol = %d (%s)\n",
183 this_state
->number
, symbol
, tags
[symbol
]);
185 if (nstates
>= MAXSHORT
)
186 fatal (_("too many states (max %d)"), MAXSHORT
);
188 p
= STATE_ALLOC (kernel_size
[symbol
]);
189 p
->accessing_symbol
= symbol
;
191 p
->nitems
= kernel_size
[symbol
];
193 shortcpy (p
->items
, kernel_base
[symbol
], kernel_size
[symbol
]);
195 last_state
->next
= p
;
199 /* If this is the eoftoken, then this is the final state. */
201 final_state
= p
->number
;
207 /*--------------------------------------------------------------.
208 | Find the state number for the state we would get to (from the |
209 | current state) by shifting symbol. Create a new state if no |
210 | equivalent one exists already. Used by append_states. |
211 `--------------------------------------------------------------*/
214 get_state (int symbol
)
221 fprintf (stderr
, "Entering get_state, state = %d, symbol = %d (%s)\n",
222 this_state
->number
, symbol
, tags
[symbol
]);
224 /* Add up the target state's active item numbers to get a hash key.
227 for (i
= 0; i
< kernel_size
[symbol
]; ++i
)
228 key
+= kernel_base
[symbol
][i
];
229 key
= key
% STATE_HASH_SIZE
;
230 sp
= state_hash
[key
];
237 if (sp
->nitems
== kernel_size
[symbol
])
240 for (i
= 0; i
< kernel_size
[symbol
]; ++i
)
241 if (kernel_base
[symbol
][i
] != sp
->items
[i
])
251 else /* bucket exhausted and no match */
253 sp
= sp
->link
= new_state (symbol
);
259 else /* bucket is empty */
261 state_hash
[key
] = sp
= new_state (symbol
);
265 fprintf (stderr
, "Exiting get_state => %d\n", sp
->number
);
270 /*------------------------------------------------------------------.
271 | Use the information computed by new_itemsets to find the state |
272 | numbers reached by each shift transition from the current state. |
274 | shiftset is set up as a vector of state numbers of those states. |
275 `------------------------------------------------------------------*/
285 fprintf (stderr
, "Entering append_states, state = %d\n",
288 /* first sort shift_symbol into increasing order */
290 for (i
= 1; i
< nshifts
; i
++)
292 symbol
= shift_symbol
[i
];
294 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
296 shift_symbol
[j
] = shift_symbol
[j
- 1];
299 shift_symbol
[j
] = symbol
;
302 for (i
= 0; i
< nshifts
; i
++)
303 shiftset
[i
] = get_state (shift_symbol
[i
]);
310 first_state
= last_state
= this_state
= STATE_ALLOC (0);
315 /*------------------------------------------------------------.
316 | Save the NSHIFTS of SHIFTSET into the current linked list. |
317 `------------------------------------------------------------*/
322 shifts
*p
= shifts_new (nshifts
);
323 shortcpy (p
->shifts
, shiftset
, nshifts
);
324 this_state
->shifts
= p
;
328 /*----------------------------------------------------------------.
329 | Find which rules can be used for reduction transitions from the |
330 | current state and make a reductions structure for the state to |
331 | record their rule numbers. |
332 `----------------------------------------------------------------*/
335 save_reductions (void)
340 /* If this is the final state, we want it to have no reductions at
341 all, although it has one for `START_SYMBOL EOF .'. */
342 if (this_state
->number
== final_state
)
345 /* Find and count the active items that represent ends of rules. */
346 for (i
= 0; i
< nitemset
; ++i
)
348 int item
= ritem
[itemset
[i
]];
350 redset
[count
++] = -item
;
353 /* Make a reductions structure and copy the data into it. */
354 this_state
->reductions
= reductions_new (count
);
355 shortcpy (this_state
->reductions
->rules
, redset
, count
);
359 /*--------------------.
360 | Build STATE_TABLE. |
361 `--------------------*/
364 set_state_table (void)
367 state_table
= XCALLOC (state_t
*, nstates
);
369 for (sp
= first_state
; sp
; sp
= sp
->next
)
371 /* Pessimization, but simplification of the code: make sure all
372 the states have a shifts, errs, and reductions, even if
375 sp
->shifts
= shifts_new (0);
377 sp
->errs
= errs_new (0);
379 sp
->reductions
= reductions_new (0);
381 state_table
[sp
->number
] = sp
;
385 /*-------------------------------------------------------------------.
386 | Compute the nondeterministic finite state machine (see state.h for |
387 | details) from the grammar. |
388 `-------------------------------------------------------------------*/
391 generate_states (void)
394 new_closure (nitems
);
400 fprintf (stderr
, "Processing state %d (reached by %s)\n",
401 this_state
->number
, tags
[this_state
->accessing_symbol
]);
402 /* Set up ruleset and itemset for the transitions out of this
403 state. ruleset gets a 1 bit for each rule that could reduce
404 now. itemset gets a vector of all the items that could be
406 closure (this_state
->items
, this_state
->nitems
);
407 /* record the reductions allowed out of this state */
409 /* find the itemsets of the states that shifts can reach */
411 /* find or create the core structures for those states */
414 /* create the shifts structures for the shifts to those states,
415 now that the state numbers transitioning to are known */
418 /* states are queued when they are created; process them all */
419 this_state
= this_state
->next
;
422 /* discard various storage */
426 /* Set up STATE_TABLE. */