]>
git.saurik.com Git - bison.git/blob - src/LR0.c
9643ee8dc92e676c28eb65094e442ff5029ff757
1 /* Generate the nondeterministic finite state machine for bison,
2 Copyright (C) 1984, 1986, 1989, 2000 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. */
33 extern short *itemset
;
34 extern short *itemsetend
;
40 reductions
*first_reduction
;
42 static core
*this_state
;
43 static core
*last_state
;
44 static shifts
*last_shift
;
45 static reductions
*last_reduction
;
48 static short *shift_symbol
;
51 static short *shiftset
;
53 static short **kernel_base
;
54 static short **kernel_end
;
55 static short *kernel_items
;
57 /* hash table for states, to recognize equivalent ones. */
59 #define STATE_TABLE_SIZE 1009
60 static core
**state_table
;
64 allocate_itemsets (void)
73 symbol_count
= NEW2 (nsyms
, short);
82 symbol_count
[symbol
]++;
87 /* See comments before new_itemsets. All the vectors of items
88 live inside KERNEL_ITEMS. The number of active items after
89 some symbol cannot be more than the number of times that symbol
90 appears as an item, which is symbol_count[symbol].
91 We allocate that much space for each symbol. */
93 kernel_base
= NEW2 (nsyms
, short *);
94 kernel_items
= NEW2 (count
, short);
97 for (i
= 0; i
< nsyms
; i
++)
99 kernel_base
[i
] = kernel_items
+ count
;
100 count
+= symbol_count
[i
];
103 shift_symbol
= symbol_count
;
104 kernel_end
= NEW2 (nsyms
, short *);
109 allocate_storage (void)
111 allocate_itemsets ();
113 shiftset
= NEW2 (nsyms
, short);
114 redset
= NEW2 (nrules
+ 1, short);
115 state_table
= NEW2 (STATE_TABLE_SIZE
, core
*);
134 /*----------------------------------------------------------------.
135 | Find which symbols can be shifted in the current state, and for |
136 | each one record which items would be active after that shift. |
137 | Uses the contents of itemset. |
139 | shift_symbol is set to a vector of the symbols that can be |
140 | shifted. For each symbol in the grammar, kernel_base[symbol] |
141 | points to a vector of item numbers activated if that symbol is |
142 | shifted, and kernel_end[symbol] points after the end of that |
144 `----------------------------------------------------------------*/
156 fprintf (stderr
, "Entering new_itemsets\n");
159 for (i
= 0; i
< nsyms
; i
++)
160 kernel_end
[i
] = NULL
;
166 while (isp
< itemsetend
)
172 ksp
= kernel_end
[symbol
];
176 shift_symbol
[shiftcount
++] = symbol
;
177 ksp
= kernel_base
[symbol
];
181 kernel_end
[symbol
] = ksp
;
185 nshifts
= shiftcount
;
190 /*-----------------------------------------------------------------.
191 | Subroutine of get_state. Create a new state for those items, if |
193 `-----------------------------------------------------------------*/
196 new_state (int symbol
)
205 fprintf (stderr
, "Entering new_state, symbol = %d\n", symbol
);
208 if (nstates
>= MAXSHORT
)
209 fatal (_("too many states (max %d)"), MAXSHORT
);
211 isp1
= kernel_base
[symbol
];
212 iend
= kernel_end
[symbol
];
216 (core
*) xmalloc ((unsigned) (sizeof (core
) + (n
- 1) * sizeof (short)));
217 p
->accessing_symbol
= symbol
;
225 last_state
->next
= p
;
234 /*--------------------------------------------------------------.
235 | Find the state number for the state we would get to (from the |
236 | current state) by shifting symbol. Create a new state if no |
237 | equivalent one exists already. Used by append_states. |
238 `--------------------------------------------------------------*/
241 get_state (int symbol
)
253 fprintf (stderr
, "Entering get_state, symbol = %d\n", symbol
);
256 isp1
= kernel_base
[symbol
];
257 iend
= kernel_end
[symbol
];
260 /* add up the target state's active item numbers to get a hash key */
265 key
= key
% STATE_TABLE_SIZE
;
267 sp
= state_table
[key
];
277 isp1
= kernel_base
[symbol
];
280 while (found
&& isp1
< iend
)
282 if (*isp1
++ != *isp2
++)
293 else /* bucket exhausted and no match */
295 sp
= sp
->link
= new_state (symbol
);
301 else /* bucket is empty */
303 state_table
[key
] = sp
= new_state (symbol
);
309 /*------------------------------------------------------------------.
310 | Use the information computed by new_itemsets to find the state |
311 | numbers reached by each shift transition from the current state. |
313 | shiftset is set up as a vector of state numbers of those states. |
314 `------------------------------------------------------------------*/
324 fprintf (stderr
, "Entering append_states\n");
327 /* first sort shift_symbol into increasing order */
329 for (i
= 1; i
< nshifts
; i
++)
331 symbol
= shift_symbol
[i
];
333 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
335 shift_symbol
[j
] = shift_symbol
[j
- 1];
338 shift_symbol
[j
] = symbol
;
341 for (i
= 0; i
< nshifts
; i
++)
343 symbol
= shift_symbol
[i
];
344 shiftset
[i
] = get_state (symbol
);
354 p
= (core
*) xmalloc ((unsigned) (sizeof (core
) - sizeof (short)));
355 first_state
= last_state
= this_state
= p
;
368 p
= (shifts
*) xmalloc ((unsigned) (sizeof (shifts
) +
369 (nshifts
- 1) * sizeof (short)));
371 p
->number
= this_state
->number
;
372 p
->nshifts
= nshifts
;
376 send
= shiftset
+ nshifts
;
383 last_shift
->next
= p
;
394 /*------------------------------------------------------------------.
395 | Subroutine of augment_automaton. Create the next-to-final state, |
396 | to which a shift has already been made in the initial state. |
397 `------------------------------------------------------------------*/
400 insert_start_shift (void)
405 statep
= (core
*) xmalloc ((unsigned) (sizeof (core
) - sizeof (short)));
406 statep
->number
= nstates
;
407 statep
->accessing_symbol
= start_symbol
;
409 last_state
->next
= statep
;
412 /* Make a shift from this state to (what will be) the final state. */
414 sp
->number
= nstates
++;
416 sp
->shifts
[0] = nstates
;
418 last_shift
->next
= sp
;
423 /*------------------------------------------------------------------.
424 | Make sure that the initial state has a shift that accepts the |
425 | grammar's start symbol and goes to the next-to-final state, which |
426 | has a shift going to the final state, which has a shift to the |
427 | termination state. Create such states and shifts if they don't |
428 | happen to exist already. |
429 `------------------------------------------------------------------*/
432 augment_automaton (void)
448 statep
= first_state
->next
;
450 /* The states reached by shifts from first_state are numbered 1...K.
451 Look for one reached by start_symbol. */
452 while (statep
->accessing_symbol
< start_symbol
453 && statep
->number
< k
)
454 statep
= statep
->next
;
456 if (statep
->accessing_symbol
== start_symbol
)
458 /* We already have a next-to-final state.
459 Make sure it has a shift to what will be the final state. */
462 while (sp
&& sp
->number
< k
)
468 if (sp
&& sp
->number
== k
)
470 sp2
= (shifts
*) xmalloc ((unsigned) (sizeof (shifts
)
475 sp2
->nshifts
= sp
->nshifts
+ 1;
476 sp2
->shifts
[0] = nstates
;
477 for (i
= sp
->nshifts
; i
> 0; i
--)
478 sp2
->shifts
[i
] = sp
->shifts
[i
- 1];
480 /* Patch sp2 into the chain of shifts in place of sp,
482 sp2
->next
= sp
->next
;
484 if (sp
== last_shift
)
493 sp2
->shifts
[0] = nstates
;
495 /* Patch sp2 into the chain of shifts between sp1 and sp. */
504 /* There is no next-to-final state as yet. */
505 /* Add one more shift in first_shift,
506 going to the next-to-final state (yet to be made). */
509 sp2
= (shifts
*) xmalloc (sizeof (shifts
)
510 + sp
->nshifts
* sizeof (short));
511 sp2
->nshifts
= sp
->nshifts
+ 1;
513 /* Stick this shift into the vector at the proper place. */
514 statep
= first_state
->next
;
515 for (k
= 0, i
= 0; i
< sp
->nshifts
; k
++, i
++)
517 if (statep
->accessing_symbol
> start_symbol
&& i
== k
)
518 sp2
->shifts
[k
++] = nstates
;
519 sp2
->shifts
[k
] = sp
->shifts
[i
];
520 statep
= statep
->next
;
523 sp2
->shifts
[k
++] = nstates
;
525 /* Patch sp2 into the chain of shifts
526 in place of sp, at the beginning. */
527 sp2
->next
= sp
->next
;
529 if (last_shift
== sp
)
534 /* Create the next-to-final state, with shift to
535 what will be the final state. */
536 insert_start_shift ();
541 /* The initial state didn't even have any shifts.
542 Give it one shift, to the next-to-final state. */
545 sp
->shifts
[0] = nstates
;
547 /* Patch sp into the chain of shifts at the beginning. */
548 sp
->next
= first_shift
;
551 /* Create the next-to-final state, with shift to
552 what will be the final state. */
553 insert_start_shift ();
558 /* There are no shifts for any state.
559 Make one shift, from the initial state to the next-to-final state. */
563 sp
->shifts
[0] = nstates
;
565 /* Initialize the chain of shifts with sp. */
569 /* Create the next-to-final state, with shift to
570 what will be the final state. */
571 insert_start_shift ();
574 /* Make the final state--the one that follows a shift from the
576 The symbol for that shift is 0 (end-of-file). */
577 statep
= (core
*) xmalloc ((unsigned) (sizeof (core
) - sizeof (short)));
578 statep
->number
= nstates
;
579 last_state
->next
= statep
;
582 /* Make the shift from the final state to the termination state. */
584 sp
->number
= nstates
++;
586 sp
->shifts
[0] = nstates
;
587 last_shift
->next
= sp
;
590 /* Note that the variable `final_state' refers to what we sometimes call
591 the termination state. */
592 final_state
= nstates
;
594 /* Make the termination state. */
595 statep
= (core
*) xmalloc ((unsigned) (sizeof (core
) - sizeof (short)));
596 statep
->number
= nstates
++;
597 last_state
->next
= statep
;
602 /*----------------------------------------------------------------.
603 | Find which rules can be used for reduction transitions from the |
604 | current state and make a reductions structure for the state to |
605 | record their rule numbers. |
606 `----------------------------------------------------------------*/
609 save_reductions (void)
620 /* Find and count the active items that represent ends of rules. */
623 for (isp
= itemset
; isp
< itemsetend
; isp
++)
627 redset
[count
++] = -item
;
630 /* Make a reductions structure and copy the data into it. */
634 p
= (reductions
*) xmalloc ((unsigned) (sizeof (reductions
) +
635 (count
- 1) * sizeof (short)));
637 p
->number
= this_state
->number
;
644 for (/* nothing */; rp1
< rend
; ++rp1
, ++rp2
)
649 last_reduction
->next
= p
;
661 /*-------------------------------------------------------------------.
662 | Compute the nondeterministic finite state machine (see state.h for |
663 | details) from the grammar. |
664 `-------------------------------------------------------------------*/
667 generate_states (void)
670 new_closure (nitems
);
675 /* Set up ruleset and itemset for the transitions out of this
676 state. ruleset gets a 1 bit for each rule that could reduce
677 now. itemset gets a vector of all the items that could be
679 closure (this_state
->items
, this_state
->nitems
);
680 /* record the reductions allowed out of this state */
682 /* find the itemsets of the states that shifts can reach */
684 /* find or create the core structures for those states */
687 /* create the shifts structures for the shifts to those states,
688 now that the state numbers transitioning to are known */
692 /* states are queued when they are created; process them all */
693 this_state
= this_state
->next
;
696 /* discard various storage */
700 /* set up initial and final states as parser wants them */
701 augment_automaton ();