]>
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 short **kernel_end
= 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_end
= XCALLOC (short *, 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_end[symbol] points after the end of that |
137 `----------------------------------------------------------------*/
146 fprintf (stderr
, "Entering new_itemsets, state = %d\n",
149 for (i
= 0; i
< nsyms
; i
++)
150 kernel_end
[i
] = NULL
;
154 for (i
= 0; i
< itemsetend
- itemset
; ++i
)
156 int symbol
= ritem
[itemset
[i
]];
159 short *ksp
= kernel_end
[symbol
];
163 shift_symbol
[shiftcount
] = symbol
;
164 ksp
= kernel_base
[symbol
];
168 *ksp
++ = itemset
[i
] + 1;
169 kernel_end
[symbol
] = ksp
;
173 nshifts
= shiftcount
;
178 /*-----------------------------------------------------------------.
179 | Subroutine of get_state. Create a new state for those items, if |
181 `-----------------------------------------------------------------*/
184 new_state (int symbol
)
190 fprintf (stderr
, "Entering new_state, state = %d, symbol = %d (%s)\n",
191 this_state
->number
, symbol
, tags
[symbol
]);
193 if (nstates
>= MAXSHORT
)
194 fatal (_("too many states (max %d)"), MAXSHORT
);
196 n
= kernel_end
[symbol
] - kernel_base
[symbol
];
199 p
->accessing_symbol
= symbol
;
203 shortcpy (p
->items
, kernel_base
[symbol
], n
);
205 last_state
->next
= p
;
213 /*--------------------------------------------------------------.
214 | Find the state number for the state we would get to (from the |
215 | current state) by shifting symbol. Create a new state if no |
216 | equivalent one exists already. Used by append_states. |
217 `--------------------------------------------------------------*/
220 get_state (int symbol
)
227 int n
= kernel_end
[symbol
] - kernel_base
[symbol
];
230 fprintf (stderr
, "Entering get_state, state = %d, symbol = %d (%s)\n",
231 this_state
->number
, symbol
, tags
[symbol
]);
233 /* Add up the target state's active item numbers to get a hash key.
236 for (i
= 0; i
< n
; ++i
)
237 key
+= kernel_base
[symbol
][i
];
238 key
= key
% STATE_TABLE_SIZE
;
239 sp
= state_table
[key
];
250 for (i
= 0; i
< n
; ++i
)
251 if (kernel_base
[symbol
][i
] != sp
->items
[i
])
261 else /* bucket exhausted and no match */
263 sp
= sp
->link
= new_state (symbol
);
269 else /* bucket is empty */
271 state_table
[key
] = sp
= new_state (symbol
);
275 fprintf (stderr
, "Exiting get_state => %d\n", sp
->number
);
280 /*------------------------------------------------------------------.
281 | Use the information computed by new_itemsets to find the state |
282 | numbers reached by each shift transition from the current state. |
284 | shiftset is set up as a vector of state numbers of those states. |
285 `------------------------------------------------------------------*/
295 fprintf (stderr
, "Entering append_states, state = %d\n",
298 /* first sort shift_symbol into increasing order */
300 for (i
= 1; i
< nshifts
; i
++)
302 symbol
= shift_symbol
[i
];
304 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
306 shift_symbol
[j
] = shift_symbol
[j
- 1];
309 shift_symbol
[j
] = symbol
;
312 for (i
= 0; i
< nshifts
; i
++)
313 shiftset
[i
] = get_state (shift_symbol
[i
]);
320 first_state
= last_state
= this_state
= CORE_ALLOC (0);
328 shifts
*p
= SHIFTS_ALLOC (nshifts
);
330 p
->number
= this_state
->number
;
331 p
->nshifts
= nshifts
;
333 shortcpy (p
->shifts
, shiftset
, nshifts
);
336 last_shift
->next
= p
;
343 /*------------------------------------------------------------------.
344 | Subroutine of augment_automaton. Create the next-to-final state, |
345 | to which a shift has already been made in the initial state. |
346 `------------------------------------------------------------------*/
349 insert_start_shift (void)
354 statep
= CORE_ALLOC (0);
355 statep
->number
= nstates
;
356 statep
->accessing_symbol
= start_symbol
;
358 last_state
->next
= statep
;
361 /* Make a shift from this state to (what will be) the final state. */
362 sp
= SHIFTS_ALLOC (1);
363 sp
->number
= nstates
++;
365 sp
->shifts
[0] = nstates
;
367 last_shift
->next
= sp
;
372 /*------------------------------------------------------------------.
373 | Make sure that the initial state has a shift that accepts the |
374 | grammar's start symbol and goes to the next-to-final state, which |
375 | has a shift going to the final state, which has a shift to the |
376 | termination state. Create such states and shifts if they don't |
377 | happen to exist already. |
378 `------------------------------------------------------------------*/
381 augment_automaton (void)
397 statep
= first_state
->next
;
399 /* The states reached by shifts from first_state are numbered 1...K.
400 Look for one reached by start_symbol. */
401 while (statep
->accessing_symbol
< start_symbol
402 && statep
->number
< k
)
403 statep
= statep
->next
;
405 if (statep
->accessing_symbol
== start_symbol
)
407 /* We already have a next-to-final state.
408 Make sure it has a shift to what will be the final state. */
411 while (sp
&& sp
->number
< k
)
417 if (sp
&& sp
->number
== k
)
419 sp2
= SHIFTS_ALLOC (sp
->nshifts
+ 1);
421 sp2
->nshifts
= sp
->nshifts
+ 1;
422 sp2
->shifts
[0] = nstates
;
423 for (i
= sp
->nshifts
; i
> 0; i
--)
424 sp2
->shifts
[i
] = sp
->shifts
[i
- 1];
426 /* Patch sp2 into the chain of shifts in place of sp,
428 sp2
->next
= sp
->next
;
430 if (sp
== last_shift
)
436 sp2
= SHIFTS_ALLOC (1);
439 sp2
->shifts
[0] = nstates
;
441 /* Patch sp2 into the chain of shifts between sp1 and sp. */
450 /* There is no next-to-final state as yet. */
451 /* Add one more shift in first_shift,
452 going to the next-to-final state (yet to be made). */
455 sp2
= SHIFTS_ALLOC (sp
->nshifts
+ 1);
456 sp2
->nshifts
= sp
->nshifts
+ 1;
458 /* Stick this shift into the vector at the proper place. */
459 statep
= first_state
->next
;
460 for (k
= 0, i
= 0; i
< sp
->nshifts
; k
++, i
++)
462 if (statep
->accessing_symbol
> start_symbol
&& i
== k
)
463 sp2
->shifts
[k
++] = nstates
;
464 sp2
->shifts
[k
] = sp
->shifts
[i
];
465 statep
= statep
->next
;
468 sp2
->shifts
[k
++] = nstates
;
470 /* Patch sp2 into the chain of shifts
471 in place of sp, at the beginning. */
472 sp2
->next
= sp
->next
;
474 if (last_shift
== sp
)
479 /* Create the next-to-final state, with shift to
480 what will be the final state. */
481 insert_start_shift ();
486 /* The initial state didn't even have any shifts.
487 Give it one shift, to the next-to-final state. */
488 sp
= SHIFTS_ALLOC (1);
490 sp
->shifts
[0] = nstates
;
492 /* Patch sp into the chain of shifts at the beginning. */
493 sp
->next
= first_shift
;
496 /* Create the next-to-final state, with shift to
497 what will be the final state. */
498 insert_start_shift ();
503 /* There are no shifts for any state.
504 Make one shift, from the initial state to the next-to-final state. */
506 sp
= SHIFTS_ALLOC (1);
508 sp
->shifts
[0] = nstates
;
510 /* Initialize the chain of shifts with sp. */
514 /* Create the next-to-final state, with shift to
515 what will be the final state. */
516 insert_start_shift ();
519 /* Make the final state--the one that follows a shift from the
521 The symbol for that shift is 0 (end-of-file). */
522 statep
= CORE_ALLOC (0);
523 statep
->number
= nstates
;
524 last_state
->next
= statep
;
527 /* Make the shift from the final state to the termination state. */
528 sp
= SHIFTS_ALLOC (1);
529 sp
->number
= nstates
++;
531 sp
->shifts
[0] = nstates
;
532 last_shift
->next
= sp
;
535 /* Note that the variable `final_state' refers to what we sometimes call
536 the termination state. */
537 final_state
= nstates
;
539 /* Make the termination state. */
540 statep
= CORE_ALLOC (0);
541 statep
->number
= nstates
++;
542 last_state
->next
= statep
;
547 /*----------------------------------------------------------------.
548 | Find which rules can be used for reduction transitions from the |
549 | current state and make a reductions structure for the state to |
550 | record their rule numbers. |
551 `----------------------------------------------------------------*/
554 save_reductions (void)
563 /* Find and count the active items that represent ends of rules. */
566 for (isp
= itemset
; isp
< itemsetend
; isp
++)
570 redset
[count
++] = -item
;
573 /* Make a reductions structure and copy the data into it. */
577 p
= REDUCTIONS_ALLOC (count
);
579 p
->number
= this_state
->number
;
582 shortcpy (p
->rules
, redset
, count
);
585 last_reduction
->next
= p
;
593 /*-------------------------------------------------------------------.
594 | Compute the nondeterministic finite state machine (see state.h for |
595 | details) from the grammar. |
596 `-------------------------------------------------------------------*/
599 generate_states (void)
602 new_closure (nitems
);
607 /* Set up ruleset and itemset for the transitions out of this
608 state. ruleset gets a 1 bit for each rule that could reduce
609 now. itemset gets a vector of all the items that could be
611 closure (this_state
->items
, this_state
->nitems
);
612 /* record the reductions allowed out of this state */
614 /* find the itemsets of the states that shifts can reach */
616 /* find or create the core structures for those states */
619 /* create the shifts structures for the shifts to those states,
620 now that the state numbers transitioning to are known */
624 /* states are queued when they are created; process them all */
625 this_state
= this_state
->next
;
628 /* discard various storage */
632 /* set up initial and final states as parser wants them */
633 augment_automaton ();