]>
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 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., 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. */
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
);
69 /* If this is the endtoken, and this is not the initial state, then
70 this is the final state. */
71 if (sym
== 0 && first_state
)
80 last_state
->next
= node
;
87 static symbol_number
*shift_symbol
;
90 static state
**shiftset
;
92 static item_number
**kernel_base
;
93 static int *kernel_size
;
94 static item_number
*kernel_items
;
98 allocate_itemsets (void)
104 /* Count the number of occurrences of all the symbols in RITEMS.
105 Note that useless productions (hence useless nonterminals) are
106 browsed too, hence we need to allocate room for _all_ the
109 short int *symbol_count
= xcalloc (nsyms
+ nuseless_nonterminals
,
110 sizeof *symbol_count
);
112 for (r
= 0; r
< nrules
; ++r
)
113 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; ++rhsp
)
116 symbol_count
[*rhsp
]++;
119 /* See comments before new_itemsets. All the vectors of items
120 live inside KERNEL_ITEMS. The number of active items after
121 some symbol S cannot be more than the number of times that S
122 appears as an item, which is SYMBOL_COUNT[S].
123 We allocate that much space for each symbol. */
125 kernel_base
= xnmalloc (nsyms
, sizeof *kernel_base
);
126 kernel_items
= xnmalloc (count
, sizeof *kernel_items
);
129 for (i
= 0; i
< nsyms
; i
++)
131 kernel_base
[i
] = kernel_items
+ count
;
132 count
+= symbol_count
[i
];
136 kernel_size
= xnmalloc (nsyms
, sizeof *kernel_size
);
141 allocate_storage (void)
143 allocate_itemsets ();
145 shiftset
= xnmalloc (nsyms
, sizeof *shiftset
);
146 redset
= xnmalloc (nrules
, sizeof *redset
);
148 shift_symbol
= xnmalloc (nsyms
, sizeof *shift_symbol
);
167 /*---------------------------------------------------------------.
168 | Find which symbols can be shifted in S, and for each one |
169 | record which items would be active after that shift. Uses the |
170 | contents of itemset. |
172 | shift_symbol is set to a vector of the symbols that can be |
173 | shifted. For each symbol in the grammar, kernel_base[symbol] |
174 | points to a vector of item numbers activated if that symbol is |
175 | shifted, and kernel_size[symbol] is their numbers. |
176 `---------------------------------------------------------------*/
179 new_itemsets (state
*s
)
183 if (trace_flag
& trace_automaton
)
184 fprintf (stderr
, "Entering new_itemsets, state = %d\n", s
->number
);
186 for (i
= 0; i
< nsyms
; i
++)
191 for (i
= 0; i
< nritemset
; ++i
)
192 if (ritem
[itemset
[i
]] >= 0)
194 symbol_number sym
= item_number_as_symbol_number (ritem
[itemset
[i
]]);
195 if (!kernel_size
[sym
])
197 shift_symbol
[nshifts
] = sym
;
201 kernel_base
[sym
][kernel_size
[sym
]] = itemset
[i
] + 1;
208 /*--------------------------------------------------------------.
209 | Find the state we would get to (from the current state) by |
210 | shifting SYM. Create a new state if no equivalent one exists |
211 | already. Used by append_states. |
212 `--------------------------------------------------------------*/
215 get_state (symbol_number sym
, size_t core_size
, item_number
*core
)
219 if (trace_flag
& trace_automaton
)
220 fprintf (stderr
, "Entering get_state, symbol = %d (%s)\n",
221 sym
, symbols
[sym
]->tag
);
223 sp
= state_hash_lookup (core_size
, core
);
225 sp
= state_list_append (sym
, core_size
, core
);
227 if (trace_flag
& trace_automaton
)
228 fprintf (stderr
, "Exiting get_state => %d\n", sp
->number
);
233 /*---------------------------------------------------------------.
234 | Use the information computed by new_itemsets to find the state |
235 | numbers reached by each shift transition from S. |
237 | SHIFTSET is set up as a vector of those states. |
238 `---------------------------------------------------------------*/
241 append_states (state
*s
)
245 if (trace_flag
& trace_automaton
)
246 fprintf (stderr
, "Entering append_states, state = %d\n", s
->number
);
248 /* First sort shift_symbol into increasing order. */
250 for (i
= 1; i
< nshifts
; i
++)
252 symbol_number sym
= shift_symbol
[i
];
254 for (j
= i
; 0 < j
&& sym
< shift_symbol
[j
- 1]; j
--)
255 shift_symbol
[j
] = shift_symbol
[j
- 1];
256 shift_symbol
[j
] = sym
;
259 for (i
= 0; i
< nshifts
; i
++)
261 symbol_number sym
= shift_symbol
[i
];
262 shiftset
[i
] = get_state (sym
, kernel_size
[sym
], kernel_base
[sym
]);
267 /*----------------------------------------------------------------.
268 | Find which rules can be used for reduction transitions from the |
269 | current state and make a reductions structure for the state to |
270 | record their rule numbers. |
271 `----------------------------------------------------------------*/
274 save_reductions (state
*s
)
279 /* Find and count the active items that represent ends of rules. */
280 for (i
= 0; i
< nritemset
; ++i
)
282 int item
= ritem
[itemset
[i
]];
284 redset
[count
++] = &rules
[item_number_as_rule_number (item
)];
287 /* Make a reductions structure and copy the data into it. */
288 state_reductions_set (s
, count
, redset
);
299 states
= xcalloc (nstates
, sizeof *states
);
303 state_list
*this = first_state
;
305 /* Pessimization, but simplification of the code: make sure all
306 the states have valid transitions and reductions members,
307 even if reduced to 0. It is too soon for errs, which are
308 computed later, but set_conflicts. */
309 state
*s
= this->state
;
311 state_transitions_set (s
, 0, 0);
313 state_reductions_set (s
, 0, 0);
315 states
[s
->number
] = s
;
317 first_state
= this->next
;
325 /*-------------------------------------------------------------------.
326 | Compute the nondeterministic finite state machine (see state.h for |
327 | details) from the grammar. |
328 `-------------------------------------------------------------------*/
331 generate_states (void)
333 item_number initial_core
= 0;
334 state_list
*list
= NULL
;
336 new_closure (nritems
);
338 /* Create the initial state. The 0 at the lhs is the index of the
339 item of this initial rule. */
340 state_list_append (0, 1, &initial_core
);
346 state
*s
= list
->state
;
347 if (trace_flag
& trace_automaton
)
348 fprintf (stderr
, "Processing state %d (reached by %s)\n",
350 symbols
[s
->accessing_symbol
]->tag
);
351 /* Set up ruleset and itemset for the transitions out of this
352 state. ruleset gets a 1 bit for each rule that could reduce
353 now. itemset gets a vector of all the items that could be
355 closure (s
->items
, s
->nitems
);
356 /* Record the reductions allowed out of this state. */
358 /* Find the itemsets of the states that shifts can reach. */
360 /* Find or create the core structures for those states. */
363 /* Create the shifts structures for the shifts to those states,
364 now that the state numbers transitioning to are known. */
365 state_transitions_set (s
, nshifts
, shiftset
);
367 /* states are queued when they are created; process them all.
372 /* discard various storage */