]>
git.saurik.com Git - bison.git/blob - src/LR0.c
d1d703ffef7fe82dcd722add9bb70349cb6f26fe
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. */
35 core
*first_state
= NULL
;
36 shifts
*first_shift
= NULL
;
37 reductions
*first_reduction
= NULL
;
39 static core
*this_state
= NULL
;
40 static core
*last_state
= NULL
;
41 static shifts
*last_shift
= NULL
;
42 static reductions
*last_reduction
= NULL
;
45 static short *shift_symbol
= NULL
;
47 static short *redset
= NULL
;
48 static short *shiftset
= NULL
;
50 static short **kernel_base
= NULL
;
51 static short **kernel_end
= NULL
;
52 static short *kernel_items
= NULL
;
54 /* hash table for states, to recognize equivalent ones. */
56 #define STATE_TABLE_SIZE 1009
57 static core
**state_table
= NULL
;
61 allocate_itemsets (void)
67 short *symbol_count
= NULL
;
70 symbol_count
= XCALLOC (short, nsyms
);
79 symbol_count
[symbol
]++;
84 /* See comments before new_itemsets. All the vectors of items
85 live inside KERNEL_ITEMS. The number of active items after
86 some symbol cannot be more than the number of times that symbol
87 appears as an item, which is symbol_count[symbol].
88 We allocate that much space for each symbol. */
90 kernel_base
= XCALLOC (short *, nsyms
);
92 kernel_items
= XCALLOC (short, count
);
95 for (i
= 0; i
< nsyms
; i
++)
97 kernel_base
[i
] = kernel_items
+ count
;
98 count
+= symbol_count
[i
];
101 shift_symbol
= symbol_count
;
102 kernel_end
= XCALLOC (short *, nsyms
);
107 allocate_storage (void)
109 allocate_itemsets ();
111 shiftset
= XCALLOC (short, nsyms
);
112 redset
= XCALLOC (short, nrules
+ 1);
113 state_table
= XCALLOC (core
*, STATE_TABLE_SIZE
);
120 XFREE (shift_symbol
);
125 XFREE (kernel_items
);
132 /*----------------------------------------------------------------.
133 | Find which symbols can be shifted in the current state, and for |
134 | each one record which items would be active after that shift. |
135 | Uses the contents of itemset. |
137 | shift_symbol is set to a vector of the symbols that can be |
138 | shifted. For each symbol in the grammar, kernel_base[symbol] |
139 | points to a vector of item numbers activated if that symbol is |
140 | shifted, and kernel_end[symbol] points after the end of that |
142 `----------------------------------------------------------------*/
151 fprintf (stderr
, "Entering new_itemsets, state = %d\n",
155 for (i
= 0; i
< nsyms
; i
++)
156 kernel_end
[i
] = NULL
;
160 for (i
= 0; i
< itemsetend
- itemset
; ++i
)
162 int symbol
= ritem
[itemset
[i
]];
165 short *ksp
= kernel_end
[symbol
];
169 shift_symbol
[shiftcount
] = symbol
;
170 ksp
= kernel_base
[symbol
];
174 *ksp
++ = itemset
[i
] + 1;
175 kernel_end
[symbol
] = ksp
;
179 nshifts
= shiftcount
;
184 /*-----------------------------------------------------------------.
185 | Subroutine of get_state. Create a new state for those items, if |
187 `-----------------------------------------------------------------*/
190 new_state (int symbol
)
196 fprintf (stderr
, "Entering new_state, state = %d, symbol = %d\n",
200 if (nstates
>= MAXSHORT
)
201 fatal (_("too many states (max %d)"), MAXSHORT
);
203 n
= kernel_end
[symbol
] - kernel_base
[symbol
];
206 p
->accessing_symbol
= symbol
;
210 shortcpy (p
->items
, kernel_base
[symbol
], n
);
212 last_state
->next
= p
;
220 /*--------------------------------------------------------------.
221 | Find the state number for the state we would get to (from the |
222 | current state) by shifting symbol. Create a new state if no |
223 | equivalent one exists already. Used by append_states. |
224 `--------------------------------------------------------------*/
227 get_state (int symbol
)
234 int n
= kernel_end
[symbol
] - kernel_base
[symbol
];
237 fprintf (stderr
, "Entering get_state, state = %d, symbol = %d\n",
241 /* Add up the target state's active item numbers to get a hash key.
244 for (i
= 0; i
< n
; ++i
)
245 key
+= kernel_base
[symbol
][i
];
246 key
= key
% STATE_TABLE_SIZE
;
247 sp
= state_table
[key
];
258 for (i
= 0; i
< n
; ++i
)
259 if (kernel_base
[symbol
][i
] != sp
->items
[i
])
269 else /* bucket exhausted and no match */
271 sp
= sp
->link
= new_state (symbol
);
277 else /* bucket is empty */
279 state_table
[key
] = sp
= new_state (symbol
);
285 /*------------------------------------------------------------------.
286 | Use the information computed by new_itemsets to find the state |
287 | numbers reached by each shift transition from the current state. |
289 | shiftset is set up as a vector of state numbers of those states. |
290 `------------------------------------------------------------------*/
300 fprintf (stderr
, "Entering append_states\n");
303 /* first sort shift_symbol into increasing order */
305 for (i
= 1; i
< nshifts
; i
++)
307 symbol
= shift_symbol
[i
];
309 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
311 shift_symbol
[j
] = shift_symbol
[j
- 1];
314 shift_symbol
[j
] = symbol
;
317 for (i
= 0; i
< nshifts
; i
++)
318 shiftset
[i
] = get_state (shift_symbol
[i
]);
325 first_state
= last_state
= this_state
= CORE_ALLOC (0);
333 shifts
*p
= SHIFTS_ALLOC (nshifts
);
335 p
->number
= this_state
->number
;
336 p
->nshifts
= nshifts
;
338 shortcpy (p
->shifts
, shiftset
, nshifts
);
341 last_shift
->next
= p
;
348 /*------------------------------------------------------------------.
349 | Subroutine of augment_automaton. Create the next-to-final state, |
350 | to which a shift has already been made in the initial state. |
351 `------------------------------------------------------------------*/
354 insert_start_shift (void)
359 statep
= CORE_ALLOC (0);
360 statep
->number
= nstates
;
361 statep
->accessing_symbol
= start_symbol
;
363 last_state
->next
= statep
;
366 /* Make a shift from this state to (what will be) the final state. */
367 sp
= SHIFTS_ALLOC (1);
368 sp
->number
= nstates
++;
370 sp
->shifts
[0] = nstates
;
372 last_shift
->next
= sp
;
377 /*------------------------------------------------------------------.
378 | Make sure that the initial state has a shift that accepts the |
379 | grammar's start symbol and goes to the next-to-final state, which |
380 | has a shift going to the final state, which has a shift to the |
381 | termination state. Create such states and shifts if they don't |
382 | happen to exist already. |
383 `------------------------------------------------------------------*/
386 augment_automaton (void)
402 statep
= first_state
->next
;
404 /* The states reached by shifts from first_state are numbered 1...K.
405 Look for one reached by start_symbol. */
406 while (statep
->accessing_symbol
< start_symbol
407 && statep
->number
< k
)
408 statep
= statep
->next
;
410 if (statep
->accessing_symbol
== start_symbol
)
412 /* We already have a next-to-final state.
413 Make sure it has a shift to what will be the final state. */
416 while (sp
&& sp
->number
< k
)
422 if (sp
&& sp
->number
== k
)
424 sp2
= SHIFTS_ALLOC (sp
->nshifts
+ 1);
426 sp2
->nshifts
= sp
->nshifts
+ 1;
427 sp2
->shifts
[0] = nstates
;
428 for (i
= sp
->nshifts
; i
> 0; i
--)
429 sp2
->shifts
[i
] = sp
->shifts
[i
- 1];
431 /* Patch sp2 into the chain of shifts in place of sp,
433 sp2
->next
= sp
->next
;
435 if (sp
== last_shift
)
441 sp2
= SHIFTS_ALLOC (1);
444 sp2
->shifts
[0] = nstates
;
446 /* Patch sp2 into the chain of shifts between sp1 and sp. */
455 /* There is no next-to-final state as yet. */
456 /* Add one more shift in first_shift,
457 going to the next-to-final state (yet to be made). */
460 sp2
= SHIFTS_ALLOC (sp
->nshifts
+ 1);
461 sp2
->nshifts
= sp
->nshifts
+ 1;
463 /* Stick this shift into the vector at the proper place. */
464 statep
= first_state
->next
;
465 for (k
= 0, i
= 0; i
< sp
->nshifts
; k
++, i
++)
467 if (statep
->accessing_symbol
> start_symbol
&& i
== k
)
468 sp2
->shifts
[k
++] = nstates
;
469 sp2
->shifts
[k
] = sp
->shifts
[i
];
470 statep
= statep
->next
;
473 sp2
->shifts
[k
++] = nstates
;
475 /* Patch sp2 into the chain of shifts
476 in place of sp, at the beginning. */
477 sp2
->next
= sp
->next
;
479 if (last_shift
== sp
)
484 /* Create the next-to-final state, with shift to
485 what will be the final state. */
486 insert_start_shift ();
491 /* The initial state didn't even have any shifts.
492 Give it one shift, to the next-to-final state. */
493 sp
= SHIFTS_ALLOC (1);
495 sp
->shifts
[0] = nstates
;
497 /* Patch sp into the chain of shifts at the beginning. */
498 sp
->next
= first_shift
;
501 /* Create the next-to-final state, with shift to
502 what will be the final state. */
503 insert_start_shift ();
508 /* There are no shifts for any state.
509 Make one shift, from the initial state to the next-to-final state. */
511 sp
= SHIFTS_ALLOC (1);
513 sp
->shifts
[0] = nstates
;
515 /* Initialize the chain of shifts with sp. */
519 /* Create the next-to-final state, with shift to
520 what will be the final state. */
521 insert_start_shift ();
524 /* Make the final state--the one that follows a shift from the
526 The symbol for that shift is 0 (end-of-file). */
527 statep
= CORE_ALLOC (0);
528 statep
->number
= nstates
;
529 last_state
->next
= statep
;
532 /* Make the shift from the final state to the termination state. */
533 sp
= SHIFTS_ALLOC (1);
534 sp
->number
= nstates
++;
536 sp
->shifts
[0] = nstates
;
537 last_shift
->next
= sp
;
540 /* Note that the variable `final_state' refers to what we sometimes call
541 the termination state. */
542 final_state
= nstates
;
544 /* Make the termination state. */
545 statep
= CORE_ALLOC (0);
546 statep
->number
= nstates
++;
547 last_state
->next
= statep
;
552 /*----------------------------------------------------------------.
553 | Find which rules can be used for reduction transitions from the |
554 | current state and make a reductions structure for the state to |
555 | record their rule numbers. |
556 `----------------------------------------------------------------*/
559 save_reductions (void)
568 /* Find and count the active items that represent ends of rules. */
571 for (isp
= itemset
; isp
< itemsetend
; isp
++)
575 redset
[count
++] = -item
;
578 /* Make a reductions structure and copy the data into it. */
582 p
= REDUCTIONS_ALLOC (count
);
584 p
->number
= this_state
->number
;
587 shortcpy (p
->rules
, redset
, count
);
590 last_reduction
->next
= p
;
598 /*-------------------------------------------------------------------.
599 | Compute the nondeterministic finite state machine (see state.h for |
600 | details) from the grammar. |
601 `-------------------------------------------------------------------*/
604 generate_states (void)
607 new_closure (nitems
);
612 /* Set up ruleset and itemset for the transitions out of this
613 state. ruleset gets a 1 bit for each rule that could reduce
614 now. itemset gets a vector of all the items that could be
616 closure (this_state
->items
, this_state
->nitems
);
617 /* record the reductions allowed out of this state */
619 /* find the itemsets of the states that shifts can reach */
621 /* find or create the core structures for those states */
624 /* create the shifts structures for the shifts to those states,
625 now that the state numbers transitioning to are known */
629 /* states are queued when they are created; process them all */
630 this_state
= this_state
->next
;
633 /* discard various storage */
637 /* set up initial and final states as parser wants them */
638 augment_automaton ();