]>
git.saurik.com Git - bison.git/blob - src/LR0.c
1 /* Generate the nondeterministic finite state machine for bison,
2 Copyright (C) 1984, 1986, 1989 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* See comments in state.h for the data structures that represent it.
22 The entry point is generate_states. */
32 extern char *nullable
;
33 extern short *itemset
;
34 extern short *itemsetend
;
41 reductions
*first_reduction
;
48 void initialize_states();
50 void save_reductions();
51 void augment_automaton();
52 void insert_start_shift();
53 extern void initialize_closure();
54 extern void closure();
55 extern void finalize_closure();
56 extern void toomany();
58 static core
*this_state
;
59 static core
*last_state
;
60 static shifts
*last_shift
;
61 static reductions
*last_reduction
;
64 static short *shift_symbol
;
67 static short *shiftset
;
69 static short **kernel_base
;
70 static short **kernel_end
;
71 static short *kernel_items
;
73 /* hash table for states, to recognize equivalent ones. */
75 #define STATE_TABLE_SIZE 1009
76 static core
**state_table
;
83 register short *itemp
;
87 register short *symbol_count
;
90 symbol_count
= NEW2(nsyms
, short);
99 symbol_count
[symbol
]++;
104 /* see comments before new_itemsets. All the vectors of items
105 live inside kernel_items. The number of active items after
106 some symbol cannot be more than the number of times that symbol
107 appears as an item, which is symbol_count[symbol].
108 We allocate that much space for each symbol. */
110 kernel_base
= NEW2(nsyms
, short *);
111 kernel_items
= NEW2(count
, short);
114 for (i
= 0; i
< nsyms
; i
++)
116 kernel_base
[i
] = kernel_items
+ count
;
117 count
+= symbol_count
[i
];
120 shift_symbol
= symbol_count
;
121 kernel_end
= NEW2(nsyms
, short *);
130 shiftset
= NEW2(nsyms
, short);
131 redset
= NEW2(nrules
+ 1, short);
132 state_table
= NEW2(STATE_TABLE_SIZE
, core
*);
150 /* compute the nondeterministic finite state machine (see state.h for details)
156 initialize_closure(nitems
);
161 /* Set up ruleset and itemset for the transitions out of this state.
162 ruleset gets a 1 bit for each rule that could reduce now.
163 itemset gets a vector of all the items that could be accepted next. */
164 closure(this_state
->items
, this_state
->nitems
);
165 /* record the reductions allowed out of this state */
167 /* find the itemsets of the states that shifts can reach */
169 /* find or create the core structures for those states */
172 /* create the shifts structures for the shifts to those states,
173 now that the state numbers transitioning to are known */
177 /* states are queued when they are created; process them all */
178 this_state
= this_state
->next
;
181 /* discard various storage */
185 /* set up initial and final states as parser wants them */
191 /* Find which symbols can be shifted in the current state,
192 and for each one record which items would be active after that shift.
193 Uses the contents of itemset.
194 shift_symbol is set to a vector of the symbols that can be shifted.
195 For each symbol in the grammar, kernel_base[symbol] points to
196 a vector of item numbers activated if that symbol is shifted,
197 and kernel_end[symbol] points after the end of that vector. */
202 register int shiftcount
;
208 fprintf(stderr
, "Entering new_itemsets\n");
211 for (i
= 0; i
< nsyms
; i
++)
212 kernel_end
[i
] = NULL
;
218 while (isp
< itemsetend
)
224 ksp
= kernel_end
[symbol
];
228 shift_symbol
[shiftcount
++] = symbol
;
229 ksp
= kernel_base
[symbol
];
233 kernel_end
[symbol
] = ksp
;
237 nshifts
= shiftcount
;
242 /* Use the information computed by new_itemsets to find the state numbers
243 reached by each shift transition from the current state.
245 shiftset is set up as a vector of state numbers of those states. */
254 fprintf(stderr
, "Entering append_states\n");
257 /* first sort shift_symbol into increasing order */
259 for (i
= 1; i
< nshifts
; i
++)
261 symbol
= shift_symbol
[i
];
263 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
265 shift_symbol
[j
] = shift_symbol
[j
- 1];
268 shift_symbol
[j
] = symbol
;
271 for (i
= 0; i
< nshifts
; i
++)
273 symbol
= shift_symbol
[i
];
274 shiftset
[i
] = get_state(symbol
);
280 /* find the state number for the state we would get to
281 (from the current state) by shifting symbol.
282 Create a new state if no equivalent one exists already.
283 Used by append_states */
290 register short *isp1
;
291 register short *isp2
;
292 register short *iend
;
299 fprintf(stderr
, "Entering get_state, symbol = %d\n", symbol
);
302 isp1
= kernel_base
[symbol
];
303 iend
= kernel_end
[symbol
];
306 /* add up the target state's active item numbers to get a hash key */
311 key
= key
% STATE_TABLE_SIZE
;
313 sp
= state_table
[key
];
323 isp1
= kernel_base
[symbol
];
326 while (found
&& isp1
< iend
)
328 if (*isp1
++ != *isp2
++)
339 else /* bucket exhausted and no match */
341 sp
= sp
->link
= new_state(symbol
);
347 else /* bucket is empty */
349 state_table
[key
] = sp
= new_state(symbol
);
357 /* subroutine of get_state. create a new state for those items, if necessary. */
365 register short *isp1
;
366 register short *isp2
;
367 register short *iend
;
370 fprintf(stderr
, "Entering new_state, symbol = %d\n", symbol
);
373 if (nstates
>= MAXSHORT
)
376 isp1
= kernel_base
[symbol
];
377 iend
= kernel_end
[symbol
];
380 p
= (core
*) xmalloc((unsigned) (sizeof(core
) + (n
- 1) * sizeof(short)));
381 p
->accessing_symbol
= symbol
;
389 last_state
->next
= p
;
402 /* register unsigned *rp1; JF unused */
403 /* register unsigned *rp2; JF unused */
404 /* register unsigned *rend; JF unused */
406 p
= (core
*) xmalloc((unsigned) (sizeof(core
) - sizeof(short)));
407 first_state
= last_state
= this_state
= p
;
418 register short *send
;
420 p
= (shifts
*) xmalloc((unsigned) (sizeof(shifts
) +
421 (nshifts
- 1) * sizeof(short)));
423 p
->number
= this_state
->number
;
424 p
->nshifts
= nshifts
;
428 send
= shiftset
+ nshifts
;
435 last_shift
->next
= p
;
447 /* find which rules can be used for reduction transitions from the current state
448 and make a reductions structure for the state to record their rule numbers. */
457 register reductions
*p
;
461 /* find and count the active items that represent ends of rules */
464 for (isp
= itemset
; isp
< itemsetend
; isp
++)
469 redset
[count
++] = -item
;
473 /* make a reductions structure and copy the data into it. */
477 p
= (reductions
*) xmalloc((unsigned) (sizeof(reductions
) +
478 (count
- 1) * sizeof(short)));
480 p
->number
= this_state
->number
;
492 last_reduction
->next
= p
;
505 /* Make sure that the initial state has a shift that accepts the
506 grammar's start symbol and goes to the next-to-final state,
507 which has a shift going to the final state, which has a shift
508 to the termination state.
509 Create such states and shifts if they don't happen to exist already. */
515 /* register int found; JF unused */
516 register core
*statep
;
518 register shifts
*sp2
;
519 register shifts
*sp1
;
528 statep
= first_state
->next
;
530 /* The states reached by shifts from first_state are numbered 1...K.
531 Look for one reached by start_symbol. */
532 while (statep
->accessing_symbol
< start_symbol
533 && statep
->number
< k
)
534 statep
= statep
->next
;
536 if (statep
->accessing_symbol
== start_symbol
)
538 /* We already have a next-to-final state.
539 Make sure it has a shift to what will be the final state. */
542 while (sp
&& sp
->number
< k
)
548 if (sp
&& sp
->number
== k
)
550 sp2
= (shifts
*) xmalloc((unsigned) (sizeof(shifts
)
551 + sp
->nshifts
* sizeof(short)));
553 sp2
->nshifts
= sp
->nshifts
+ 1;
554 sp2
->shifts
[0] = nstates
;
555 for (i
= sp
->nshifts
; i
> 0; i
--)
556 sp2
->shifts
[i
] = sp
->shifts
[i
- 1];
558 /* Patch sp2 into the chain of shifts in place of sp,
560 sp2
->next
= sp
->next
;
562 if (sp
== last_shift
)
571 sp2
->shifts
[0] = nstates
;
573 /* Patch sp2 into the chain of shifts between sp1 and sp. */
582 /* There is no next-to-final state as yet. */
583 /* Add one more shift in first_shift,
584 going to the next-to-final state (yet to be made). */
587 sp2
= (shifts
*) xmalloc(sizeof(shifts
)
588 + sp
->nshifts
* sizeof(short));
589 sp2
->nshifts
= sp
->nshifts
+ 1;
591 /* Stick this shift into the vector at the proper place. */
592 statep
= first_state
->next
;
593 for (k
= 0, i
= 0; i
< sp
->nshifts
; k
++, i
++)
595 if (statep
->accessing_symbol
> start_symbol
&& i
== k
)
596 sp2
->shifts
[k
++] = nstates
;
597 sp2
->shifts
[k
] = sp
->shifts
[i
];
598 statep
= statep
->next
;
601 sp2
->shifts
[k
++] = nstates
;
603 /* Patch sp2 into the chain of shifts
604 in place of sp, at the beginning. */
605 sp2
->next
= sp
->next
;
607 if (last_shift
== sp
)
612 /* Create the next-to-final state, with shift to
613 what will be the final state. */
614 insert_start_shift();
619 /* The initial state didn't even have any shifts.
620 Give it one shift, to the next-to-final state. */
623 sp
->shifts
[0] = nstates
;
625 /* Patch sp into the chain of shifts at the beginning. */
626 sp
->next
= first_shift
;
629 /* Create the next-to-final state, with shift to
630 what will be the final state. */
631 insert_start_shift();
636 /* There are no shifts for any state.
637 Make one shift, from the initial state to the next-to-final state. */
641 sp
->shifts
[0] = nstates
;
643 /* Initialize the chain of shifts with sp. */
647 /* Create the next-to-final state, with shift to
648 what will be the final state. */
649 insert_start_shift();
652 /* Make the final state--the one that follows a shift from the
654 The symbol for that shift is 0 (end-of-file). */
655 statep
= (core
*) xmalloc((unsigned) (sizeof(core
) - sizeof(short)));
656 statep
->number
= nstates
;
657 last_state
->next
= statep
;
660 /* Make the shift from the final state to the termination state. */
662 sp
->number
= nstates
++;
664 sp
->shifts
[0] = nstates
;
665 last_shift
->next
= sp
;
668 /* Note that the variable `final_state' refers to what we sometimes call
669 the termination state. */
670 final_state
= nstates
;
672 /* Make the termination state. */
673 statep
= (core
*) xmalloc((unsigned) (sizeof(core
) - sizeof(short)));
674 statep
->number
= nstates
++;
675 last_state
->next
= statep
;
680 /* subroutine of augment_automaton.
681 Create the next-to-final state, to which a shift has already been made in
682 the initial state. */
686 register core
*statep
;
689 statep
= (core
*) xmalloc((unsigned) (sizeof(core
) - sizeof(short)));
690 statep
->number
= nstates
;
691 statep
->accessing_symbol
= start_symbol
;
693 last_state
->next
= statep
;
696 /* Make a shift from this state to (what will be) the final state. */
698 sp
->number
= nstates
++;
700 sp
->shifts
[0] = nstates
;
702 last_shift
->next
= sp
;