]>
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. */
37 core
*first_state
= NULL
;
38 shifts
*first_shift
= NULL
;
39 reductions
*first_reduction
= NULL
;
41 static core
*this_state
= NULL
;
42 static core
*last_state
= NULL
;
43 static shifts
*last_shift
= NULL
;
44 static reductions
*last_reduction
= NULL
;
47 static short *shift_symbol
= NULL
;
49 static short *redset
= NULL
;
50 static short *shiftset
= NULL
;
52 static short **kernel_base
= NULL
;
53 static int *kernel_size
= NULL
;
54 static short *kernel_items
= NULL
;
56 /* hash table for states, to recognize equivalent ones. */
58 #define STATE_TABLE_SIZE 1009
59 static core
**state_table
= NULL
;
63 allocate_itemsets (void)
67 /* Count the number of occurrences of all the symbols in RITEMS.
68 Note that useless productions (hence useless nonterminals) are
69 browsed too, hence we need to allocate room for _all_ the
72 short *symbol_count
= XCALLOC (short, nsyms
+ nuseless_nonterminals
);
74 for (i
= 0; ritem
[i
]; ++i
)
78 symbol_count
[ritem
[i
]]++;
81 /* See comments before new_itemsets. All the vectors of items
82 live inside KERNEL_ITEMS. The number of active items after
83 some symbol cannot be more than the number of times that symbol
84 appears as an item, which is symbol_count[symbol].
85 We allocate that much space for each symbol. */
87 kernel_base
= XCALLOC (short *, nsyms
);
89 kernel_items
= XCALLOC (short, count
);
92 for (i
= 0; i
< nsyms
; i
++)
94 kernel_base
[i
] = kernel_items
+ count
;
95 count
+= symbol_count
[i
];
99 kernel_size
= XCALLOC (int, nsyms
);
104 allocate_storage (void)
106 allocate_itemsets ();
108 shiftset
= XCALLOC (short, nsyms
);
109 redset
= XCALLOC (short, nrules
+ 1);
110 state_table
= XCALLOC (core
*, STATE_TABLE_SIZE
);
122 XFREE (kernel_items
);
129 /*----------------------------------------------------------------.
130 | Find which symbols can be shifted in the current state, and for |
131 | each one record which items would be active after that shift. |
132 | Uses the contents of itemset. |
134 | shift_symbol is set to a vector of the symbols that can be |
135 | shifted. For each symbol in the grammar, kernel_base[symbol] |
136 | points to a vector of item numbers activated if that symbol is |
137 | shifted, and kernel_size[symbol] is their numbers. |
138 `----------------------------------------------------------------*/
147 fprintf (stderr
, "Entering new_itemsets, state = %d\n",
150 for (i
= 0; i
< nsyms
; i
++)
153 shift_symbol
= XCALLOC (short, nsyms
);
156 for (i
= 0; i
< itemsetsize
; ++i
)
158 int symbol
= ritem
[itemset
[i
]];
161 if (!kernel_size
[symbol
])
163 shift_symbol
[shiftcount
] = symbol
;
167 kernel_base
[symbol
][kernel_size
[symbol
]] = itemset
[i
] + 1;
168 kernel_size
[symbol
]++;
172 nshifts
= shiftcount
;
177 /*-----------------------------------------------------------------.
178 | Subroutine of get_state. Create a new state for those items, if |
180 `-----------------------------------------------------------------*/
183 new_state (int symbol
)
188 fprintf (stderr
, "Entering new_state, state = %d, symbol = %d (%s)\n",
189 this_state
->number
, symbol
, tags
[symbol
]);
191 if (nstates
>= MAXSHORT
)
192 fatal (_("too many states (max %d)"), MAXSHORT
);
194 p
= CORE_ALLOC (kernel_size
[symbol
]);
195 p
->accessing_symbol
= symbol
;
197 p
->nitems
= kernel_size
[symbol
];
199 shortcpy (p
->items
, kernel_base
[symbol
], kernel_size
[symbol
]);
201 last_state
->next
= p
;
209 /*--------------------------------------------------------------.
210 | Find the state number for the state we would get to (from the |
211 | current state) by shifting symbol. Create a new state if no |
212 | equivalent one exists already. Used by append_states. |
213 `--------------------------------------------------------------*/
216 get_state (int symbol
)
223 fprintf (stderr
, "Entering get_state, state = %d, symbol = %d (%s)\n",
224 this_state
->number
, symbol
, tags
[symbol
]);
226 /* Add up the target state's active item numbers to get a hash key.
229 for (i
= 0; i
< kernel_size
[symbol
]; ++i
)
230 key
+= kernel_base
[symbol
][i
];
231 key
= key
% STATE_TABLE_SIZE
;
232 sp
= state_table
[key
];
239 if (sp
->nitems
== kernel_size
[symbol
])
242 for (i
= 0; i
< kernel_size
[symbol
]; ++i
)
243 if (kernel_base
[symbol
][i
] != sp
->items
[i
])
253 else /* bucket exhausted and no match */
255 sp
= sp
->link
= new_state (symbol
);
261 else /* bucket is empty */
263 state_table
[key
] = sp
= new_state (symbol
);
267 fprintf (stderr
, "Exiting get_state => %d\n", sp
->number
);
272 /*------------------------------------------------------------------.
273 | Use the information computed by new_itemsets to find the state |
274 | numbers reached by each shift transition from the current state. |
276 | shiftset is set up as a vector of state numbers of those states. |
277 `------------------------------------------------------------------*/
287 fprintf (stderr
, "Entering append_states, state = %d\n",
290 /* first sort shift_symbol into increasing order */
292 for (i
= 1; i
< nshifts
; i
++)
294 symbol
= shift_symbol
[i
];
296 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
298 shift_symbol
[j
] = shift_symbol
[j
- 1];
301 shift_symbol
[j
] = symbol
;
304 for (i
= 0; i
< nshifts
; i
++)
305 shiftset
[i
] = get_state (shift_symbol
[i
]);
312 first_state
= last_state
= this_state
= CORE_ALLOC (0);
317 /*------------------------------------------------------------.
318 | Save the NSHIFTS of SHIFTSET into the current linked list. |
319 `------------------------------------------------------------*/
324 shifts
*p
= shifts_new (nshifts
);
326 p
->number
= this_state
->number
;
328 shortcpy (p
->shifts
, shiftset
, nshifts
);
331 last_shift
->next
= p
;
338 /*------------------------------------------------------------------.
339 | Subroutine of augment_automaton. Create the next-to-final state, |
340 | to which a shift has already been made in the initial state. |
341 `------------------------------------------------------------------*/
344 insert_start_shift (void)
349 statep
= CORE_ALLOC (0);
350 statep
->number
= nstates
;
351 statep
->accessing_symbol
= start_symbol
;
353 last_state
->next
= statep
;
356 /* Make a shift from this state to (what will be) the final state. */
358 sp
->number
= nstates
++;
359 sp
->shifts
[0] = nstates
;
361 last_shift
->next
= sp
;
366 /*------------------------------------------------------------------.
367 | Make sure that the initial state has a shift that accepts the |
368 | grammar's start symbol and goes to the next-to-final state, which |
369 | has a shift going to the final state, which has a shift to the |
370 | termination state. Create such states and shifts if they don't |
371 | happen to exist already. |
372 `------------------------------------------------------------------*/
375 augment_automaton (void)
385 /* There are no shifts for any state. Make one shift, from the
386 initial state to the next-to-final state. */
389 sp
->shifts
[0] = nstates
;
391 /* Initialize the chain of shifts with sp. */
395 /* Create the next-to-final state, with shift to
396 what will be the final state. */
397 insert_start_shift ();
399 else if (sp
->number
== 0)
401 statep
= first_state
->next
;
403 /* The states reached by shifts from FIRST_STATE are numbered
404 1..(SP->NSHIFTS). Look for one reached by START_SYMBOL. */
405 while (statep
->accessing_symbol
< start_symbol
406 && statep
->number
< sp
->nshifts
)
407 statep
= statep
->next
;
409 if (statep
->accessing_symbol
== start_symbol
)
411 /* We already have a next-to-final state.
412 Make sure it has a shift to what will be the final state. */
413 while (sp
&& sp
->number
< statep
->number
)
419 if (sp
&& sp
->number
== statep
->number
)
422 shifts
*sp2
= shifts_new (sp
->nshifts
+ 1);
423 sp2
->number
= statep
->number
;
424 sp2
->shifts
[0] = nstates
;
425 for (i
= sp
->nshifts
; i
> 0; i
--)
426 sp2
->shifts
[i
] = sp
->shifts
[i
- 1];
428 /* Patch sp2 into the chain of shifts in place of sp,
430 sp2
->next
= sp
->next
;
432 if (sp
== last_shift
)
438 shifts
*sp2
= shifts_new (1);
439 sp2
->number
= statep
->number
;
440 sp2
->shifts
[0] = nstates
;
442 /* Patch sp2 into the chain of shifts between sp1 and sp. */
454 /* There is no next-to-final state as yet. */
455 /* Add one more shift in first_shift,
456 going to the next-to-final state (yet to be made). */
459 sp2
= shifts_new (sp
->nshifts
+ 1);
461 /* Stick this shift into the vector at the proper place. */
462 statep
= first_state
->next
;
463 for (k
= 0, i
= 0; i
< sp
->nshifts
; k
++, i
++)
465 if (statep
->accessing_symbol
> start_symbol
&& i
== k
)
466 sp2
->shifts
[k
++] = nstates
;
467 sp2
->shifts
[k
] = sp
->shifts
[i
];
468 statep
= statep
->next
;
471 sp2
->shifts
[k
++] = nstates
;
473 /* Patch sp2 into the chain of shifts
474 in place of sp, at the beginning. */
475 sp2
->next
= sp
->next
;
477 if (last_shift
== sp
)
482 /* Create the next-to-final state, with shift to
483 what will be the final state. */
484 insert_start_shift ();
489 /* The initial state didn't even have any shifts.
490 Give it one shift, to the next-to-final state. */
492 sp
->shifts
[0] = nstates
;
494 /* Patch sp into the chain of shifts at the beginning. */
495 sp
->next
= first_shift
;
498 /* Create the next-to-final state, with shift to
499 what will be the final state. */
500 insert_start_shift ();
503 /* Make the final state--the one that follows a shift from the
505 The symbol for that shift is 0 (end-of-file). */
506 statep
= CORE_ALLOC (0);
507 statep
->number
= nstates
;
508 last_state
->next
= statep
;
511 /* Make the shift from the final state to the termination state. */
513 sp
->number
= nstates
++;
514 sp
->shifts
[0] = nstates
;
515 last_shift
->next
= sp
;
518 /* Note that the variable `final_state' refers to what we sometimes call
519 the termination state. */
520 final_state
= nstates
;
522 /* Make the termination state. */
523 statep
= CORE_ALLOC (0);
524 statep
->number
= nstates
++;
525 last_state
->next
= statep
;
530 /*----------------------------------------------------------------.
531 | Find which rules can be used for reduction transitions from the |
532 | current state and make a reductions structure for the state to |
533 | record their rule numbers. |
534 `----------------------------------------------------------------*/
537 save_reductions (void)
542 /* Find and count the active items that represent ends of rules. */
545 for (i
= 0; i
< itemsetsize
; ++i
)
547 int item
= ritem
[itemset
[i
]];
549 redset
[count
++] = -item
;
552 /* Make a reductions structure and copy the data into it. */
556 reductions
*p
= REDUCTIONS_ALLOC (count
);
558 p
->number
= this_state
->number
;
561 shortcpy (p
->rules
, redset
, count
);
564 last_reduction
->next
= p
;
572 /*-------------------------------------------------------------------.
573 | Compute the nondeterministic finite state machine (see state.h for |
574 | details) from the grammar. |
575 `-------------------------------------------------------------------*/
578 generate_states (void)
581 new_closure (nitems
);
587 fprintf (stderr
, "Processing state %d (reached by %s)\n",
588 this_state
->number
, tags
[this_state
->accessing_symbol
]);
589 /* Set up ruleset and itemset for the transitions out of this
590 state. ruleset gets a 1 bit for each rule that could reduce
591 now. itemset gets a vector of all the items that could be
593 closure (this_state
->items
, this_state
->nitems
);
594 /* record the reductions allowed out of this state */
596 /* find the itemsets of the states that shifts can reach */
598 /* find or create the core structures for those states */
601 /* create the shifts structures for the shifts to those states,
602 now that the state numbers transitioning to are known */
605 /* states are queued when they are created; process them all */
606 this_state
= this_state
->next
;
609 /* discard various storage */
613 /* set up initial and final states as parser wants them */
614 augment_automaton ();