]>
git.saurik.com Git - bison.git/blob - src/LR0.c
1 /* Generate the nondeterministic finite state machine for Bison.
3 Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002, 2004, 2005 Free
4 Software Foundation, Inc.
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., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 /* See comments in state.h for the data structures that represent it.
25 The entry point is generate_states. */
44 typedef struct state_list
46 struct state_list
*next
;
50 static state_list
*first_state
= NULL
;
51 static state_list
*last_state
= NULL
;
54 /*------------------------------------------------------------------.
55 | A state was just discovered from another state. Queue it for |
56 | later examination, in order to find its transitions. Return it. |
57 `------------------------------------------------------------------*/
60 state_list_append (symbol_number sym
, size_t core_size
, item_number
*core
)
62 state_list
*node
= xmalloc (sizeof *node
);
63 state
*s
= state_new (sym
, core_size
, core
);
65 if (trace_flag
& trace_automaton
)
66 fprintf (stderr
, "state_list_append (state = %d, symbol = %d (%s))\n",
67 nstates
, sym
, symbols
[sym
]->tag
);
75 last_state
->next
= node
;
82 static symbol_number
*shift_symbol
;
85 static state
**shiftset
;
87 static item_number
**kernel_base
;
88 static int *kernel_size
;
89 static item_number
*kernel_items
;
93 allocate_itemsets (void)
99 /* Count the number of occurrences of all the symbols in RITEMS.
100 Note that useless productions (hence useless nonterminals) are
101 browsed too, hence we need to allocate room for _all_ the
104 size_t *symbol_count
= xcalloc (nsyms
+ nuseless_nonterminals
,
105 sizeof *symbol_count
);
107 for (r
= 0; r
< nrules
; ++r
)
108 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; ++rhsp
)
111 symbol_count
[*rhsp
]++;
114 /* See comments before new_itemsets. All the vectors of items
115 live inside KERNEL_ITEMS. The number of active items after
116 some symbol S cannot be more than the number of times that S
117 appears as an item, which is SYMBOL_COUNT[S].
118 We allocate that much space for each symbol. */
120 kernel_base
= xnmalloc (nsyms
, sizeof *kernel_base
);
121 kernel_items
= xnmalloc (count
, sizeof *kernel_items
);
124 for (i
= 0; i
< nsyms
; i
++)
126 kernel_base
[i
] = kernel_items
+ count
;
127 count
+= symbol_count
[i
];
131 kernel_size
= xnmalloc (nsyms
, sizeof *kernel_size
);
136 allocate_storage (void)
138 allocate_itemsets ();
140 shiftset
= xnmalloc (nsyms
, sizeof *shiftset
);
141 redset
= xnmalloc (nrules
, sizeof *redset
);
143 shift_symbol
= xnmalloc (nsyms
, sizeof *shift_symbol
);
162 /*---------------------------------------------------------------.
163 | Find which symbols can be shifted in S, and for each one |
164 | record which items would be active after that shift. Uses the |
165 | contents of itemset. |
167 | shift_symbol is set to a vector of the symbols that can be |
168 | shifted. For each symbol in the grammar, kernel_base[symbol] |
169 | points to a vector of item numbers activated if that symbol is |
170 | shifted, and kernel_size[symbol] is their numbers. |
171 `---------------------------------------------------------------*/
174 new_itemsets (state
*s
)
178 if (trace_flag
& trace_automaton
)
179 fprintf (stderr
, "Entering new_itemsets, state = %d\n", s
->number
);
181 memset (kernel_size
, 0, nsyms
* sizeof *kernel_size
);
185 for (i
= 0; i
< nritemset
; ++i
)
186 if (ritem
[itemset
[i
]] >= 0)
188 symbol_number sym
= item_number_as_symbol_number (ritem
[itemset
[i
]]);
189 if (!kernel_size
[sym
])
191 shift_symbol
[nshifts
] = sym
;
195 kernel_base
[sym
][kernel_size
[sym
]] = itemset
[i
] + 1;
202 /*--------------------------------------------------------------.
203 | Find the state we would get to (from the current state) by |
204 | shifting SYM. Create a new state if no equivalent one exists |
205 | already. Used by append_states. |
206 `--------------------------------------------------------------*/
209 get_state (symbol_number sym
, size_t core_size
, item_number
*core
)
213 if (trace_flag
& trace_automaton
)
214 fprintf (stderr
, "Entering get_state, symbol = %d (%s)\n",
215 sym
, symbols
[sym
]->tag
);
217 s
= state_hash_lookup (core_size
, core
);
219 s
= state_list_append (sym
, core_size
, core
);
221 if (trace_flag
& trace_automaton
)
222 fprintf (stderr
, "Exiting get_state => %d\n", s
->number
);
227 /*---------------------------------------------------------------.
228 | Use the information computed by new_itemsets to find the state |
229 | numbers reached by each shift transition from S. |
231 | SHIFTSET is set up as a vector of those states. |
232 `---------------------------------------------------------------*/
235 append_states (state
*s
)
239 if (trace_flag
& trace_automaton
)
240 fprintf (stderr
, "Entering append_states, state = %d\n", s
->number
);
242 /* First sort shift_symbol into increasing order. */
244 for (i
= 1; i
< nshifts
; i
++)
246 symbol_number sym
= shift_symbol
[i
];
248 for (j
= i
; 0 < j
&& sym
< shift_symbol
[j
- 1]; j
--)
249 shift_symbol
[j
] = shift_symbol
[j
- 1];
250 shift_symbol
[j
] = sym
;
253 for (i
= 0; i
< nshifts
; i
++)
255 symbol_number sym
= shift_symbol
[i
];
256 shiftset
[i
] = get_state (sym
, kernel_size
[sym
], kernel_base
[sym
]);
261 /*----------------------------------------------------------------.
262 | Find which rules can be used for reduction transitions from the |
263 | current state and make a reductions structure for the state to |
264 | record their rule numbers. |
265 `----------------------------------------------------------------*/
268 save_reductions (state
*s
)
273 /* Find and count the active items that represent ends of rules. */
274 for (i
= 0; i
< nritemset
; ++i
)
276 item_number item
= ritem
[itemset
[i
]];
277 if (item_number_is_rule_number (item
))
279 rule_number r
= item_number_as_rule_number (item
);
280 redset
[count
++] = &rules
[r
];
283 /* This is "reduce 0", i.e., accept. */
284 assert (!final_state
);
290 /* Make a reductions structure and copy the data into it. */
291 state_reductions_set (s
, count
, redset
);
302 states
= xcalloc (nstates
, sizeof *states
);
306 state_list
*this = first_state
;
308 /* Pessimization, but simplification of the code: make sure all
309 the states have valid transitions and reductions members,
310 even if reduced to 0. It is too soon for errs, which are
311 computed later, but set_conflicts. */
312 state
*s
= this->state
;
314 state_transitions_set (s
, 0, 0);
316 state_reductions_set (s
, 0, 0);
318 states
[s
->number
] = s
;
320 first_state
= this->next
;
328 /*-------------------------------------------------------------------.
329 | Compute the nondeterministic finite state machine (see state.h for |
330 | details) from the grammar. |
331 `-------------------------------------------------------------------*/
334 generate_states (void)
336 item_number initial_core
= 0;
337 state_list
*list
= NULL
;
339 new_closure (nritems
);
341 /* Create the initial state. The 0 at the lhs is the index of the
342 item of this initial rule. */
343 state_list_append (0, 1, &initial_core
);
345 /* States are queued when they are created; process them all. */
346 for (list
= first_state
; list
; list
= list
->next
)
348 state
*s
= list
->state
;
349 if (trace_flag
& trace_automaton
)
350 fprintf (stderr
, "Processing state %d (reached by %s)\n",
352 symbols
[s
->accessing_symbol
]->tag
);
353 /* Set up ruleset and itemset for the transitions out of this
354 state. ruleset gets a 1 bit for each rule that could reduce
355 now. itemset gets a vector of all the items that could be
357 closure (s
->items
, s
->nitems
);
358 /* Record the reductions allowed out of this state. */
360 /* Find the itemsets of the states that shifts can reach. */
362 /* Find or create the core structures for those states. */
365 /* Create the shifts structures for the shifts to those states,
366 now that the state numbers transitioning to are known. */
367 state_transitions_set (s
, nshifts
, shiftset
);
370 /* discard various storage */