]>
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 size_t *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 short *symbol_count
= NULL
;
70 symbol_count
= XCALLOC (short, nsyms
);
72 for (i
= 0; ritem
[i
]; ++i
)
76 symbol_count
[ritem
[i
]]++;
79 /* See comments before new_itemsets. All the vectors of items
80 live inside KERNEL_ITEMS. The number of active items after
81 some symbol cannot be more than the number of times that symbol
82 appears as an item, which is symbol_count[symbol].
83 We allocate that much space for each symbol. */
85 kernel_base
= XCALLOC (short *, nsyms
);
87 kernel_items
= XCALLOC (short, count
);
90 for (i
= 0; i
< nsyms
; i
++)
92 kernel_base
[i
] = kernel_items
+ count
;
93 count
+= symbol_count
[i
];
96 shift_symbol
= symbol_count
;
97 kernel_size
= XCALLOC (size_t, nsyms
);
102 allocate_storage (void)
104 allocate_itemsets ();
106 shiftset
= XCALLOC (short, nsyms
);
107 redset
= XCALLOC (short, nrules
+ 1);
108 state_table
= XCALLOC (core
*, STATE_TABLE_SIZE
);
115 XFREE (shift_symbol
);
120 XFREE (kernel_items
);
127 /*----------------------------------------------------------------.
128 | Find which symbols can be shifted in the current state, and for |
129 | each one record which items would be active after that shift. |
130 | Uses the contents of itemset. |
132 | shift_symbol is set to a vector of the symbols that can be |
133 | shifted. For each symbol in the grammar, kernel_base[symbol] |
134 | points to a vector of item numbers activated if that symbol is |
135 | shifted, and kernel_size[symbol] is their numbers. |
136 `----------------------------------------------------------------*/
145 fprintf (stderr
, "Entering new_itemsets, state = %d\n",
148 for (i
= 0; i
< nsyms
; i
++)
153 for (i
= 0; i
< itemsetend
- itemset
; ++i
)
155 int symbol
= ritem
[itemset
[i
]];
158 if (!kernel_size
[symbol
])
160 shift_symbol
[shiftcount
] = symbol
;
164 kernel_base
[symbol
][kernel_size
[symbol
]] = itemset
[i
] + 1;
165 kernel_size
[symbol
]++;
169 nshifts
= shiftcount
;
174 /*-----------------------------------------------------------------.
175 | Subroutine of get_state. Create a new state for those items, if |
177 `-----------------------------------------------------------------*/
180 new_state (int symbol
)
185 fprintf (stderr
, "Entering new_state, state = %d, symbol = %d (%s)\n",
186 this_state
->number
, symbol
, tags
[symbol
]);
188 if (nstates
>= MAXSHORT
)
189 fatal (_("too many states (max %d)"), MAXSHORT
);
191 p
= CORE_ALLOC (kernel_size
[symbol
]);
192 p
->accessing_symbol
= symbol
;
194 p
->nitems
= kernel_size
[symbol
];
196 shortcpy (p
->items
, kernel_base
[symbol
], kernel_size
[symbol
]);
198 last_state
->next
= p
;
206 /*--------------------------------------------------------------.
207 | Find the state number for the state we would get to (from the |
208 | current state) by shifting symbol. Create a new state if no |
209 | equivalent one exists already. Used by append_states. |
210 `--------------------------------------------------------------*/
213 get_state (int symbol
)
221 fprintf (stderr
, "Entering get_state, state = %d, symbol = %d (%s)\n",
222 this_state
->number
, symbol
, tags
[symbol
]);
224 /* Add up the target state's active item numbers to get a hash key.
227 for (i
= 0; i
< kernel_size
[symbol
]; ++i
)
228 key
+= kernel_base
[symbol
][i
];
229 key
= key
% STATE_TABLE_SIZE
;
230 sp
= state_table
[key
];
237 if (sp
->nitems
== kernel_size
[symbol
])
241 for (i
= 0; i
< kernel_size
[symbol
]; ++i
)
242 if (kernel_base
[symbol
][i
] != sp
->items
[i
])
252 else /* bucket exhausted and no match */
254 sp
= sp
->link
= new_state (symbol
);
260 else /* bucket is empty */
262 state_table
[key
] = sp
= new_state (symbol
);
266 fprintf (stderr
, "Exiting get_state => %d\n", sp
->number
);
271 /*------------------------------------------------------------------.
272 | Use the information computed by new_itemsets to find the state |
273 | numbers reached by each shift transition from the current state. |
275 | shiftset is set up as a vector of state numbers of those states. |
276 `------------------------------------------------------------------*/
286 fprintf (stderr
, "Entering append_states, state = %d\n",
289 /* first sort shift_symbol into increasing order */
291 for (i
= 1; i
< nshifts
; i
++)
293 symbol
= shift_symbol
[i
];
295 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
297 shift_symbol
[j
] = shift_symbol
[j
- 1];
300 shift_symbol
[j
] = symbol
;
303 for (i
= 0; i
< nshifts
; i
++)
304 shiftset
[i
] = get_state (shift_symbol
[i
]);
311 first_state
= last_state
= this_state
= CORE_ALLOC (0);
319 shifts
*p
= SHIFTS_ALLOC (nshifts
);
321 p
->number
= this_state
->number
;
322 p
->nshifts
= nshifts
;
324 shortcpy (p
->shifts
, shiftset
, nshifts
);
327 last_shift
->next
= p
;
334 /*------------------------------------------------------------------.
335 | Subroutine of augment_automaton. Create the next-to-final state, |
336 | to which a shift has already been made in the initial state. |
337 `------------------------------------------------------------------*/
340 insert_start_shift (void)
345 statep
= CORE_ALLOC (0);
346 statep
->number
= nstates
;
347 statep
->accessing_symbol
= start_symbol
;
349 last_state
->next
= statep
;
352 /* Make a shift from this state to (what will be) the final state. */
353 sp
= SHIFTS_ALLOC (1);
354 sp
->number
= nstates
++;
356 sp
->shifts
[0] = nstates
;
358 last_shift
->next
= sp
;
363 /*------------------------------------------------------------------.
364 | Make sure that the initial state has a shift that accepts the |
365 | grammar's start symbol and goes to the next-to-final state, which |
366 | has a shift going to the final state, which has a shift to the |
367 | termination state. Create such states and shifts if they don't |
368 | happen to exist already. |
369 `------------------------------------------------------------------*/
372 augment_automaton (void)
388 statep
= first_state
->next
;
390 /* The states reached by shifts from first_state are numbered 1...K.
391 Look for one reached by start_symbol. */
392 while (statep
->accessing_symbol
< start_symbol
393 && statep
->number
< k
)
394 statep
= statep
->next
;
396 if (statep
->accessing_symbol
== start_symbol
)
398 /* We already have a next-to-final state.
399 Make sure it has a shift to what will be the final state. */
402 while (sp
&& sp
->number
< k
)
408 if (sp
&& sp
->number
== k
)
410 sp2
= SHIFTS_ALLOC (sp
->nshifts
+ 1);
412 sp2
->nshifts
= sp
->nshifts
+ 1;
413 sp2
->shifts
[0] = nstates
;
414 for (i
= sp
->nshifts
; i
> 0; i
--)
415 sp2
->shifts
[i
] = sp
->shifts
[i
- 1];
417 /* Patch sp2 into the chain of shifts in place of sp,
419 sp2
->next
= sp
->next
;
421 if (sp
== last_shift
)
427 sp2
= SHIFTS_ALLOC (1);
430 sp2
->shifts
[0] = nstates
;
432 /* Patch sp2 into the chain of shifts between sp1 and sp. */
441 /* There is no next-to-final state as yet. */
442 /* Add one more shift in first_shift,
443 going to the next-to-final state (yet to be made). */
446 sp2
= SHIFTS_ALLOC (sp
->nshifts
+ 1);
447 sp2
->nshifts
= sp
->nshifts
+ 1;
449 /* Stick this shift into the vector at the proper place. */
450 statep
= first_state
->next
;
451 for (k
= 0, i
= 0; i
< sp
->nshifts
; k
++, i
++)
453 if (statep
->accessing_symbol
> start_symbol
&& i
== k
)
454 sp2
->shifts
[k
++] = nstates
;
455 sp2
->shifts
[k
] = sp
->shifts
[i
];
456 statep
= statep
->next
;
459 sp2
->shifts
[k
++] = nstates
;
461 /* Patch sp2 into the chain of shifts
462 in place of sp, at the beginning. */
463 sp2
->next
= sp
->next
;
465 if (last_shift
== sp
)
470 /* Create the next-to-final state, with shift to
471 what will be the final state. */
472 insert_start_shift ();
477 /* The initial state didn't even have any shifts.
478 Give it one shift, to the next-to-final state. */
479 sp
= SHIFTS_ALLOC (1);
481 sp
->shifts
[0] = nstates
;
483 /* Patch sp into the chain of shifts at the beginning. */
484 sp
->next
= first_shift
;
487 /* Create the next-to-final state, with shift to
488 what will be the final state. */
489 insert_start_shift ();
494 /* There are no shifts for any state.
495 Make one shift, from the initial state to the next-to-final state. */
497 sp
= SHIFTS_ALLOC (1);
499 sp
->shifts
[0] = nstates
;
501 /* Initialize the chain of shifts with sp. */
505 /* Create the next-to-final state, with shift to
506 what will be the final state. */
507 insert_start_shift ();
510 /* Make the final state--the one that follows a shift from the
512 The symbol for that shift is 0 (end-of-file). */
513 statep
= CORE_ALLOC (0);
514 statep
->number
= nstates
;
515 last_state
->next
= statep
;
518 /* Make the shift from the final state to the termination state. */
519 sp
= SHIFTS_ALLOC (1);
520 sp
->number
= nstates
++;
522 sp
->shifts
[0] = nstates
;
523 last_shift
->next
= sp
;
526 /* Note that the variable `final_state' refers to what we sometimes call
527 the termination state. */
528 final_state
= nstates
;
530 /* Make the termination state. */
531 statep
= CORE_ALLOC (0);
532 statep
->number
= nstates
++;
533 last_state
->next
= statep
;
538 /*----------------------------------------------------------------.
539 | Find which rules can be used for reduction transitions from the |
540 | current state and make a reductions structure for the state to |
541 | record their rule numbers. |
542 `----------------------------------------------------------------*/
545 save_reductions (void)
554 /* Find and count the active items that represent ends of rules. */
557 for (isp
= itemset
; isp
< itemsetend
; isp
++)
561 redset
[count
++] = -item
;
564 /* Make a reductions structure and copy the data into it. */
568 p
= REDUCTIONS_ALLOC (count
);
570 p
->number
= this_state
->number
;
573 shortcpy (p
->rules
, redset
, count
);
576 last_reduction
->next
= p
;
584 /*-------------------------------------------------------------------.
585 | Compute the nondeterministic finite state machine (see state.h for |
586 | details) from the grammar. |
587 `-------------------------------------------------------------------*/
590 generate_states (void)
593 new_closure (nitems
);
598 /* Set up ruleset and itemset for the transitions out of this
599 state. ruleset gets a 1 bit for each rule that could reduce
600 now. itemset gets a vector of all the items that could be
602 closure (this_state
->items
, this_state
->nitems
);
603 /* record the reductions allowed out of this state */
605 /* find the itemsets of the states that shifts can reach */
607 /* find or create the core structures for those states */
610 /* create the shifts structures for the shifts to those states,
611 now that the state numbers transitioning to are known */
615 /* states are queued when they are created; process them all */
616 this_state
= this_state
->next
;
619 /* discard various storage */
623 /* set up initial and final states as parser wants them */
624 augment_automaton ();