]>
git.saurik.com Git - bison.git/blob - src/LR0.c
3288d9dcd910e4c1e166e755df09425d96595f36
1 /* Generate the nondeterministic finite state machine for bison,
2 Copyright 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. */
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 `----------------------------------------------------------------*/
154 fprintf (stderr
, "Entering new_itemsets\n");
157 for (i
= 0; i
< nsyms
; i
++)
158 kernel_end
[i
] = NULL
;
164 while (isp
< itemsetend
)
170 ksp
= kernel_end
[symbol
];
174 shift_symbol
[shiftcount
++] = symbol
;
175 ksp
= kernel_base
[symbol
];
179 kernel_end
[symbol
] = ksp
;
183 nshifts
= shiftcount
;
188 /*-----------------------------------------------------------------.
189 | Subroutine of get_state. Create a new state for those items, if |
191 `-----------------------------------------------------------------*/
194 new_state (int symbol
)
203 fprintf (stderr
, "Entering new_state, symbol = %d, state = %d\n",
207 if (nstates
>= MAXSHORT
)
208 fatal (_("too many states (max %d)"), MAXSHORT
);
210 isp1
= kernel_base
[symbol
];
211 iend
= kernel_end
[symbol
];
215 (core
*) xcalloc ((unsigned) (sizeof (core
) + (n
- 1) * sizeof (short)), 1);
216 p
->accessing_symbol
= symbol
;
224 last_state
->next
= p
;
233 /*--------------------------------------------------------------.
234 | Find the state number for the state we would get to (from the |
235 | current state) by shifting symbol. Create a new state if no |
236 | equivalent one exists already. Used by append_states. |
237 `--------------------------------------------------------------*/
240 get_state (int symbol
)
252 fprintf (stderr
, "Entering get_state, symbol = %d\n", symbol
);
255 isp1
= kernel_base
[symbol
];
256 iend
= kernel_end
[symbol
];
259 /* add up the target state's active item numbers to get a hash key */
264 key
= key
% STATE_TABLE_SIZE
;
266 sp
= state_table
[key
];
276 isp1
= kernel_base
[symbol
];
279 while (found
&& isp1
< iend
)
281 if (*isp1
++ != *isp2
++)
292 else /* bucket exhausted and no match */
294 sp
= sp
->link
= new_state (symbol
);
300 else /* bucket is empty */
302 state_table
[key
] = sp
= new_state (symbol
);
308 /*------------------------------------------------------------------.
309 | Use the information computed by new_itemsets to find the state |
310 | numbers reached by each shift transition from the current state. |
312 | shiftset is set up as a vector of state numbers of those states. |
313 `------------------------------------------------------------------*/
323 fprintf (stderr
, "Entering append_states\n");
326 /* first sort shift_symbol into increasing order */
328 for (i
= 1; i
< nshifts
; i
++)
330 symbol
= shift_symbol
[i
];
332 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
334 shift_symbol
[j
] = shift_symbol
[j
- 1];
337 shift_symbol
[j
] = symbol
;
340 for (i
= 0; i
< nshifts
; i
++)
342 symbol
= shift_symbol
[i
];
343 shiftset
[i
] = get_state (symbol
);
353 p
= (core
*) xcalloc ((unsigned) (sizeof (core
) - sizeof (short)), 1);
354 first_state
= last_state
= this_state
= p
;
367 p
= (shifts
*) xcalloc ((unsigned) (sizeof (shifts
) +
368 (nshifts
- 1) * sizeof (short)), 1);
370 p
->number
= this_state
->number
;
371 p
->nshifts
= nshifts
;
375 send
= shiftset
+ nshifts
;
382 last_shift
->next
= p
;
393 /*------------------------------------------------------------------.
394 | Subroutine of augment_automaton. Create the next-to-final state, |
395 | to which a shift has already been made in the initial state. |
396 `------------------------------------------------------------------*/
399 insert_start_shift (void)
404 statep
= (core
*) xcalloc ((unsigned) (sizeof (core
) - sizeof (short)), 1);
405 statep
->number
= nstates
;
406 statep
->accessing_symbol
= start_symbol
;
408 last_state
->next
= statep
;
411 /* Make a shift from this state to (what will be) the final state. */
412 sp
= XCALLOC (shifts
, 1);
413 sp
->number
= nstates
++;
415 sp
->shifts
[0] = nstates
;
417 last_shift
->next
= sp
;
422 /*------------------------------------------------------------------.
423 | Make sure that the initial state has a shift that accepts the |
424 | grammar's start symbol and goes to the next-to-final state, which |
425 | has a shift going to the final state, which has a shift to the |
426 | termination state. Create such states and shifts if they don't |
427 | happen to exist already. |
428 `------------------------------------------------------------------*/
431 augment_automaton (void)
447 statep
= first_state
->next
;
449 /* The states reached by shifts from first_state are numbered 1...K.
450 Look for one reached by start_symbol. */
451 while (statep
->accessing_symbol
< start_symbol
452 && statep
->number
< k
)
453 statep
= statep
->next
;
455 if (statep
->accessing_symbol
== start_symbol
)
457 /* We already have a next-to-final state.
458 Make sure it has a shift to what will be the final state. */
461 while (sp
&& sp
->number
< k
)
467 if (sp
&& sp
->number
== k
)
469 sp2
= (shifts
*) xcalloc ((unsigned) (sizeof (shifts
)
474 sp2
->nshifts
= sp
->nshifts
+ 1;
475 sp2
->shifts
[0] = nstates
;
476 for (i
= sp
->nshifts
; i
> 0; i
--)
477 sp2
->shifts
[i
] = sp
->shifts
[i
- 1];
479 /* Patch sp2 into the chain of shifts in place of sp,
481 sp2
->next
= sp
->next
;
483 if (sp
== last_shift
)
489 sp2
= XCALLOC (shifts
, 1);
492 sp2
->shifts
[0] = nstates
;
494 /* Patch sp2 into the chain of shifts between sp1 and sp. */
503 /* There is no next-to-final state as yet. */
504 /* Add one more shift in first_shift,
505 going to the next-to-final state (yet to be made). */
508 sp2
= (shifts
*) xcalloc (sizeof (shifts
)
509 + sp
->nshifts
* sizeof (short), 1);
510 sp2
->nshifts
= sp
->nshifts
+ 1;
512 /* Stick this shift into the vector at the proper place. */
513 statep
= first_state
->next
;
514 for (k
= 0, i
= 0; i
< sp
->nshifts
; k
++, i
++)
516 if (statep
->accessing_symbol
> start_symbol
&& i
== k
)
517 sp2
->shifts
[k
++] = nstates
;
518 sp2
->shifts
[k
] = sp
->shifts
[i
];
519 statep
= statep
->next
;
522 sp2
->shifts
[k
++] = nstates
;
524 /* Patch sp2 into the chain of shifts
525 in place of sp, at the beginning. */
526 sp2
->next
= sp
->next
;
528 if (last_shift
== sp
)
533 /* Create the next-to-final state, with shift to
534 what will be the final state. */
535 insert_start_shift ();
540 /* The initial state didn't even have any shifts.
541 Give it one shift, to the next-to-final state. */
542 sp
= XCALLOC (shifts
, 1);
544 sp
->shifts
[0] = nstates
;
546 /* Patch sp into the chain of shifts at the beginning. */
547 sp
->next
= first_shift
;
550 /* Create the next-to-final state, with shift to
551 what will be the final state. */
552 insert_start_shift ();
557 /* There are no shifts for any state.
558 Make one shift, from the initial state to the next-to-final state. */
560 sp
= XCALLOC (shifts
, 1);
562 sp
->shifts
[0] = nstates
;
564 /* Initialize the chain of shifts with sp. */
568 /* Create the next-to-final state, with shift to
569 what will be the final state. */
570 insert_start_shift ();
573 /* Make the final state--the one that follows a shift from the
575 The symbol for that shift is 0 (end-of-file). */
576 statep
= (core
*) xcalloc ((unsigned) (sizeof (core
) - sizeof (short)), 1);
577 statep
->number
= nstates
;
578 last_state
->next
= statep
;
581 /* Make the shift from the final state to the termination state. */
582 sp
= XCALLOC (shifts
, 1);
583 sp
->number
= nstates
++;
585 sp
->shifts
[0] = nstates
;
586 last_shift
->next
= sp
;
589 /* Note that the variable `final_state' refers to what we sometimes call
590 the termination state. */
591 final_state
= nstates
;
593 /* Make the termination state. */
594 statep
= (core
*) xcalloc ((unsigned) (sizeof (core
) - sizeof (short)), 1);
595 statep
->number
= nstates
++;
596 last_state
->next
= statep
;
601 /*----------------------------------------------------------------.
602 | Find which rules can be used for reduction transitions from the |
603 | current state and make a reductions structure for the state to |
604 | record their rule numbers. |
605 `----------------------------------------------------------------*/
608 save_reductions (void)
619 /* Find and count the active items that represent ends of rules. */
622 for (isp
= itemset
; isp
< itemsetend
; isp
++)
626 redset
[count
++] = -item
;
629 /* Make a reductions structure and copy the data into it. */
633 p
= (reductions
*) xcalloc ((unsigned) (sizeof (reductions
) +
634 (count
- 1) * sizeof (short)), 1);
636 p
->number
= this_state
->number
;
643 for (/* nothing */; rp1
< rend
; ++rp1
, ++rp2
)
648 last_reduction
->next
= p
;
660 /*-------------------------------------------------------------------.
661 | Compute the nondeterministic finite state machine (see state.h for |
662 | details) from the grammar. |
663 `-------------------------------------------------------------------*/
666 generate_states (void)
669 new_closure (nitems
);
674 /* Set up ruleset and itemset for the transitions out of this
675 state. ruleset gets a 1 bit for each rule that could reduce
676 now. itemset gets a vector of all the items that could be
678 closure (this_state
->items
, this_state
->nitems
);
679 /* record the reductions allowed out of this state */
681 /* find the itemsets of the states that shifts can reach */
683 /* find or create the core structures for those states */
686 /* create the shifts structures for the shifts to those states,
687 now that the state numbers transitioning to are known */
691 /* states are queued when they are created; process them all */
692 this_state
= this_state
->next
;
695 /* discard various storage */
699 /* set up initial and final states as parser wants them */
700 augment_automaton ();