]>
git.saurik.com Git - bison.git/blob - src/LR0.c
d7134aab58da102102259c2515e5410e7264cb57
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 typedef struct state_list_s
42 struct state_list_s
*next
;
46 static state_list_t
*first_state
= NULL
;
47 static state_list_t
*last_state
= NULL
;
50 /*------------------------------------------------------------------.
51 | A state was just discovered from another state. Queue it for |
52 | later examination, in order to find its transitions. Return it. |
53 `------------------------------------------------------------------*/
56 state_list_append (symbol_number_t symbol
,
57 size_t core_size
, item_number_t
*core
)
59 state_list_t
*node
= XMALLOC (state_list_t
, 1);
60 state_t
*state
= state_new (symbol
, core_size
, core
);
62 if (trace_flag
& trace_automaton
)
63 fprintf (stderr
, "state_list_append (state = %d, symbol = %d (%s))\n",
64 nstates
, symbol
, symbols
[symbol
]->tag
);
66 /* If this is the endtoken, and this is not the initial state, then
67 this is the final state. */
68 if (symbol
== 0 && first_state
)
77 last_state
->next
= node
;
84 static symbol_number_t
*shift_symbol
= NULL
;
86 static rule_t
**redset
= NULL
;
87 static state_t
**shiftset
= NULL
;
89 static item_number_t
**kernel_base
= NULL
;
90 static int *kernel_size
= NULL
;
91 static item_number_t
*kernel_items
= NULL
;
95 allocate_itemsets (void)
101 /* Count the number of occurrences of all the symbols in RITEMS.
102 Note that useless productions (hence useless nonterminals) are
103 browsed too, hence we need to allocate room for _all_ the
106 short *symbol_count
= XCALLOC (short, nsyms
+ nuseless_nonterminals
);
108 for (r
= 0; r
< nrules
; ++r
)
109 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; ++rhsp
)
112 symbol_count
[*rhsp
]++;
115 /* See comments before new_itemsets. All the vectors of items
116 live inside KERNEL_ITEMS. The number of active items after
117 some symbol cannot be more than the number of times that symbol
118 appears as an item, which is SYMBOL_COUNT[SYMBOL].
119 We allocate that much space for each symbol. */
121 kernel_base
= XCALLOC (item_number_t
*, nsyms
);
123 kernel_items
= XCALLOC (item_number_t
, count
);
126 for (i
= 0; i
< nsyms
; i
++)
128 kernel_base
[i
] = kernel_items
+ count
;
129 count
+= symbol_count
[i
];
133 kernel_size
= XCALLOC (int, nsyms
);
138 allocate_storage (void)
140 allocate_itemsets ();
142 shiftset
= XCALLOC (state_t
*, nsyms
);
143 redset
= XCALLOC (rule_t
*, nrules
);
145 shift_symbol
= XCALLOC (symbol_number_t
, nsyms
);
157 XFREE (kernel_items
);
164 /*---------------------------------------------------------------.
165 | Find which symbols can be shifted in STATE, and for each one |
166 | record which items would be active after that shift. Uses the |
167 | contents of itemset. |
169 | shift_symbol is set to a vector of the symbols that can be |
170 | shifted. For each symbol in the grammar, kernel_base[symbol] |
171 | points to a vector of item numbers activated if that symbol is |
172 | shifted, and kernel_size[symbol] is their numbers. |
173 `---------------------------------------------------------------*/
176 new_itemsets (state_t
*state
)
180 if (trace_flag
& trace_automaton
)
181 fprintf (stderr
, "Entering new_itemsets, state = %d\n",
184 for (i
= 0; i
< nsyms
; i
++)
189 for (i
= 0; i
< nritemset
; ++i
)
190 if (ritem
[itemset
[i
]] >= 0)
192 symbol_number_t symbol
193 = item_number_as_symbol_number (ritem
[itemset
[i
]]);
194 if (!kernel_size
[symbol
])
196 shift_symbol
[nshifts
] = symbol
;
200 kernel_base
[symbol
][kernel_size
[symbol
]] = itemset
[i
] + 1;
201 kernel_size
[symbol
]++;
207 /*-----------------------------------------------------------------.
208 | Find the state we would get to (from the current state) by |
209 | shifting SYMBOL. Create a new state if no equivalent one exists |
210 | already. Used by append_states. |
211 `-----------------------------------------------------------------*/
214 get_state (symbol_number_t symbol
, size_t core_size
, item_number_t
*core
)
218 if (trace_flag
& trace_automaton
)
219 fprintf (stderr
, "Entering get_state, symbol = %d (%s)\n",
220 symbol
, symbols
[symbol
]->tag
);
222 sp
= state_hash_lookup (core_size
, core
);
224 sp
= state_list_append (symbol
, core_size
, core
);
226 if (trace_flag
& trace_automaton
)
227 fprintf (stderr
, "Exiting get_state => %d\n", sp
->number
);
232 /*---------------------------------------------------------------.
233 | Use the information computed by new_itemsets to find the state |
234 | numbers reached by each shift transition from STATE. |
236 | SHIFTSET is set up as a vector of those states. |
237 `---------------------------------------------------------------*/
240 append_states (state_t
*state
)
244 symbol_number_t symbol
;
246 if (trace_flag
& trace_automaton
)
247 fprintf (stderr
, "Entering append_states, state = %d\n",
250 /* first sort shift_symbol into increasing order */
252 for (i
= 1; i
< nshifts
; i
++)
254 symbol
= shift_symbol
[i
];
256 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
258 shift_symbol
[j
] = shift_symbol
[j
- 1];
261 shift_symbol
[j
] = symbol
;
264 for (i
= 0; i
< nshifts
; i
++)
266 symbol
= shift_symbol
[i
];
267 shiftset
[i
] = get_state (symbol
,
268 kernel_size
[symbol
], kernel_base
[symbol
]);
273 /*----------------------------------------------------------------.
274 | Find which rules can be used for reduction transitions from the |
275 | current state and make a reductions structure for the state to |
276 | record their rule numbers. |
277 `----------------------------------------------------------------*/
280 save_reductions (state_t
*state
)
285 /* Find and count the active items that represent ends of rules. */
286 for (i
= 0; i
< nritemset
; ++i
)
288 int item
= ritem
[itemset
[i
]];
290 redset
[count
++] = &rules
[item_number_as_rule_number (item
)];
293 /* Make a reductions structure and copy the data into it. */
294 state_reductions_set (state
, count
, redset
);
305 states
= XCALLOC (state_t
*, nstates
);
309 state_list_t
*this = first_state
;
311 /* Pessimization, but simplification of the code: make sure all
312 the states have valid transitions and reductions members,
313 even if reduced to 0. It is too soon for errs, which are
314 computed later, but set_conflicts. */
315 state_t
*state
= this->state
;
316 if (!state
->transitions
)
317 state_transitions_set (state
, 0, 0);
318 if (!state
->reductions
)
319 state_reductions_set (state
, 0, 0);
321 states
[state
->number
] = state
;
323 first_state
= this->next
;
331 /*-------------------------------------------------------------------.
332 | Compute the nondeterministic finite state machine (see state.h for |
333 | details) from the grammar. |
334 `-------------------------------------------------------------------*/
337 generate_states (void)
339 state_list_t
*list
= NULL
;
341 new_closure (nritems
);
343 /* Create the initial state. The 0 at the lhs is the index of the
344 item of this initial rule. */
345 kernel_base
[0][0] = 0;
347 state_list_append (0, kernel_size
[0], kernel_base
[0]);
353 state_t
*state
= list
->state
;
354 if (trace_flag
& trace_automaton
)
355 fprintf (stderr
, "Processing state %d (reached by %s)\n",
357 symbols
[state
->accessing_symbol
]->tag
);
358 /* Set up ruleset and itemset for the transitions out of this
359 state. ruleset gets a 1 bit for each rule that could reduce
360 now. itemset gets a vector of all the items that could be
362 closure (state
->items
, state
->nitems
);
363 /* Record the reductions allowed out of this state. */
364 save_reductions (state
);
365 /* Find the itemsets of the states that shifts can reach. */
366 new_itemsets (state
);
367 /* Find or create the core structures for those states. */
368 append_states (state
);
370 /* Create the shifts structures for the shifts to those states,
371 now that the state numbers transitioning to are known. */
372 state_transitions_set (state
, nshifts
, shiftset
);
374 /* States are queued when they are created; process them all.
379 /* discard various storage */