]>
git.saurik.com Git - bison.git/blob - src/LR0.c
76f8145274ee6a08832a457834142224101fccc2
1 /* Generate the nondeterministic finite state machine for bison,
3 Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
8 Bison is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 Bison is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* See comments in state.h for the data structures that represent it.
25 The entry point is generate_states. */
42 typedef struct state_list_s
44 struct state_list_s
*next
;
48 static state_list_t
*first_state
= NULL
;
49 static state_list_t
*last_state
= NULL
;
52 /*------------------------------------------------------------------.
53 | A state was just discovered from another state. Queue it for |
54 | later examination, in order to find its transitions. Return it. |
55 `------------------------------------------------------------------*/
58 state_list_append (symbol_number_t symbol
,
59 size_t core_size
, item_number_t
*core
)
61 state_list_t
*node
= XMALLOC (state_list_t
, 1);
62 state_t
*state
= state_new (symbol
, core_size
, core
);
64 if (trace_flag
& trace_automaton
)
65 fprintf (stderr
, _("state_list_append (state = %d, symbol = %d (%s))\n"),
66 nstates
, symbol
, symbols
[symbol
]->tag
);
68 /* If this is the endtoken, and this is not the initial state, then
69 this is the final state. */
70 if (symbol
== 0 && first_state
)
79 last_state
->next
= node
;
86 static symbol_number_t
*shift_symbol
= NULL
;
88 static rule_t
**redset
= NULL
;
89 static state_t
**shiftset
= NULL
;
91 static item_number_t
**kernel_base
= NULL
;
92 static int *kernel_size
= NULL
;
93 static item_number_t
*kernel_items
= NULL
;
97 allocate_itemsets (void)
103 /* Count the number of occurrences of all the symbols in RITEMS.
104 Note that useless productions (hence useless nonterminals) are
105 browsed too, hence we need to allocate room for _all_ the
108 short *symbol_count
= XCALLOC (short, nsyms
+ nuseless_nonterminals
);
110 for (r
= 0; r
< nrules
; ++r
)
111 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; ++rhsp
)
114 symbol_count
[*rhsp
]++;
117 /* See comments before new_itemsets. All the vectors of items
118 live inside KERNEL_ITEMS. The number of active items after
119 some symbol cannot be more than the number of times that symbol
120 appears as an item, which is SYMBOL_COUNT[SYMBOL].
121 We allocate that much space for each symbol. */
123 kernel_base
= XCALLOC (item_number_t
*, nsyms
);
125 kernel_items
= XCALLOC (item_number_t
, count
);
128 for (i
= 0; i
< nsyms
; i
++)
130 kernel_base
[i
] = kernel_items
+ count
;
131 count
+= symbol_count
[i
];
135 kernel_size
= XCALLOC (int, nsyms
);
140 allocate_storage (void)
142 allocate_itemsets ();
144 shiftset
= XCALLOC (state_t
*, nsyms
);
145 redset
= XCALLOC (rule_t
*, nrules
);
147 shift_symbol
= XCALLOC (symbol_number_t
, nsyms
);
159 XFREE (kernel_items
);
166 /*---------------------------------------------------------------.
167 | Find which symbols can be shifted in STATE, and for each one |
168 | record which items would be active after that shift. Uses the |
169 | contents of itemset. |
171 | shift_symbol is set to a vector of the symbols that can be |
172 | shifted. For each symbol in the grammar, kernel_base[symbol] |
173 | points to a vector of item numbers activated if that symbol is |
174 | shifted, and kernel_size[symbol] is their numbers. |
175 `---------------------------------------------------------------*/
178 new_itemsets (state_t
*state
)
182 if (trace_flag
& trace_automaton
)
183 fprintf (stderr
, _("Entering new_itemsets, state = %d\n"),
186 for (i
= 0; i
< nsyms
; i
++)
191 for (i
= 0; i
< nritemset
; ++i
)
192 if (ritem
[itemset
[i
]] >= 0)
194 symbol_number_t symbol
195 = item_number_as_symbol_number (ritem
[itemset
[i
]]);
196 if (!kernel_size
[symbol
])
198 shift_symbol
[nshifts
] = symbol
;
202 kernel_base
[symbol
][kernel_size
[symbol
]] = itemset
[i
] + 1;
203 kernel_size
[symbol
]++;
209 /*-----------------------------------------------------------------.
210 | Find the state we would get to (from the current state) by |
211 | shifting SYMBOL. Create a new state if no equivalent one exists |
212 | already. Used by append_states. |
213 `-----------------------------------------------------------------*/
216 get_state (symbol_number_t symbol
, size_t core_size
, item_number_t
*core
)
220 if (trace_flag
& trace_automaton
)
221 fprintf (stderr
, _("Entering get_state, symbol = %d (%s)\n"),
222 symbol
, symbols
[symbol
]->tag
);
224 sp
= state_hash_lookup (core_size
, core
);
226 sp
= state_list_append (symbol
, core_size
, core
);
228 if (trace_flag
& trace_automaton
)
229 fprintf (stderr
, _("Exiting get_state => %d\n"), sp
->number
);
234 /*---------------------------------------------------------------.
235 | Use the information computed by new_itemsets to find the state |
236 | numbers reached by each shift transition from STATE. |
238 | SHIFTSET is set up as a vector of those states. |
239 `---------------------------------------------------------------*/
242 append_states (state_t
*state
)
246 symbol_number_t symbol
;
248 if (trace_flag
& trace_automaton
)
249 fprintf (stderr
, _("Entering append_states, state = %d\n"),
252 /* first sort shift_symbol into increasing order */
254 for (i
= 1; i
< nshifts
; i
++)
256 symbol
= shift_symbol
[i
];
258 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
260 shift_symbol
[j
] = shift_symbol
[j
- 1];
263 shift_symbol
[j
] = symbol
;
266 for (i
= 0; i
< nshifts
; i
++)
268 symbol
= shift_symbol
[i
];
269 shiftset
[i
] = get_state (symbol
,
270 kernel_size
[symbol
], kernel_base
[symbol
]);
275 /*----------------------------------------------------------------.
276 | Find which rules can be used for reduction transitions from the |
277 | current state and make a reductions structure for the state to |
278 | record their rule numbers. |
279 `----------------------------------------------------------------*/
282 save_reductions (state_t
*state
)
287 /* Find and count the active items that represent ends of rules. */
288 for (i
= 0; i
< nritemset
; ++i
)
290 int item
= ritem
[itemset
[i
]];
292 redset
[count
++] = &rules
[item_number_as_rule_number (item
)];
295 /* Make a reductions structure and copy the data into it. */
296 state_reductions_set (state
, count
, redset
);
307 states
= XCALLOC (state_t
*, nstates
);
311 state_list_t
*this = first_state
;
313 /* Pessimization, but simplification of the code: make sure all
314 the states have valid transitions and reductions members,
315 even if reduced to 0. It is too soon for errs, which are
316 computed later, but set_conflicts. */
317 state_t
*state
= this->state
;
318 if (!state
->transitions
)
319 state_transitions_set (state
, 0, 0);
320 if (!state
->reductions
)
321 state_reductions_set (state
, 0, 0);
323 states
[state
->number
] = state
;
325 first_state
= this->next
;
333 /*-------------------------------------------------------------------.
334 | Compute the nondeterministic finite state machine (see state.h for |
335 | details) from the grammar. |
336 `-------------------------------------------------------------------*/
339 generate_states (void)
341 state_list_t
*list
= NULL
;
343 new_closure (nritems
);
345 /* Create the initial state. The 0 at the lhs is the index of the
346 item of this initial rule. */
347 kernel_base
[0][0] = 0;
349 state_list_append (0, kernel_size
[0], kernel_base
[0]);
355 state_t
*state
= list
->state
;
356 if (trace_flag
& trace_automaton
)
357 fprintf (stderr
, _("Processing state %d (reached by %s)\n"),
359 symbols
[state
->accessing_symbol
]->tag
);
360 /* Set up ruleset and itemset for the transitions out of this
361 state. ruleset gets a 1 bit for each rule that could reduce
362 now. itemset gets a vector of all the items that could be
364 closure (state
->items
, state
->nitems
);
365 /* Record the reductions allowed out of this state. */
366 save_reductions (state
);
367 /* Find the itemsets of the states that shifts can reach. */
368 new_itemsets (state
);
369 /* Find or create the core structures for those states. */
370 append_states (state
);
372 /* Create the shifts structures for the shifts to those states,
373 now that the state numbers transitioning to are known. */
374 state_transitions_set (state
, nshifts
, shiftset
);
376 /* States are queued when they are created; process them all.
381 /* discard various storage */