home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / bison / lr0.c < prev    next >
C/C++ Source or Header  |  1990-06-25  |  14KB  |  669 lines

  1. /* Generate the nondeterministic finite state machine for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  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 1, or (at your option)
  9. any later version.
  10.  
  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.
  15.  
  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.  */
  19.  
  20.  
  21. /* See comments in state.h for the data structures that represent it.
  22.    The entry point is generate_states.  */
  23.  
  24. #include <stdio.h>
  25. #include "system.h"
  26. #include "machine.h"
  27. #include "new.h"
  28. #include "gram.h"
  29. #include "state.h"
  30.  
  31.  
  32. extern char *nullable;
  33. extern short *itemset;
  34. extern short *itemsetend;
  35.  
  36.  
  37. int nstates;
  38. int final_state;
  39. core *first_state;
  40. shifts *first_shift;
  41. reductions *first_reduction;
  42.  
  43. int get_state();
  44. core *new_state();
  45.  
  46. void new_itemsets();
  47. void append_states();
  48. void initialize_states();
  49. void save_shifts();
  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();
  57.  
  58. static core *this_state;
  59. static core *last_state;
  60. static shifts *last_shift;
  61. static reductions *last_reduction;
  62.  
  63. static int nshifts;
  64. static short *shift_symbol;
  65.  
  66. static short *redset;
  67. static short *shiftset;
  68.  
  69. static short **kernel_base;
  70. static short **kernel_end;
  71. static short *kernel_items;
  72.  
  73. /* hash table for states, to recognize equivalent ones.  */
  74.  
  75. #define    STATE_TABLE_SIZE    1009
  76. static core **state_table;
  77.  
  78.  
  79.  
  80. void
  81. allocate_itemsets()
  82. {
  83.   register short *itemp;
  84.   register int symbol;
  85.   register int i;
  86.   register int count;
  87.   register int max;
  88.   register short *symbol_count;
  89.  
  90.   count = 0;
  91.   symbol_count = NEW2(nsyms, short);
  92.  
  93.   itemp = ritem;
  94.   symbol = *itemp++;
  95.   while (symbol)
  96.     {
  97.       if (symbol > 0)
  98.     {
  99.       count++;
  100.       symbol_count[symbol]++;
  101.     }
  102.       symbol = *itemp++;
  103.     }
  104.  
  105.   /* see comments before new-itemset.  All the vectors of items
  106.      live inside kernel_items.  The number of active items after
  107.      some symbol cannot be more than the number of times that symbol
  108.      appears as an item, which is symbol_count[symbol].
  109.      We allocate that much space for each symbol.  */
  110.  
  111.   kernel_base = NEW2(nsyms, short *);
  112.   kernel_items = NEW2(count, short);
  113.  
  114.   count = 0;
  115.   max = 0;
  116.   for (i = 0; i < nsyms; i++)
  117.     {
  118.       kernel_base[i] = kernel_items + count;
  119.       count += symbol_count[i];
  120.       if (max < symbol_count[i])
  121.     max = symbol_count[i];
  122.     }
  123.  
  124.   shift_symbol = symbol_count;
  125.   kernel_end = NEW2(nsyms, short *);
  126. }
  127.  
  128.  
  129. void
  130. allocate_storage()
  131. {
  132.   allocate_itemsets();
  133.  
  134.   shiftset = NEW2(nsyms, short);
  135.   redset = NEW2(nrules + 1, short);
  136.   state_table = NEW2(STATE_TABLE_SIZE, core *);
  137. }
  138.  
  139.  
  140. void
  141. free_storage()
  142. {
  143.   FREE(shift_symbol);
  144.   FREE(redset);
  145.   FREE(shiftset);
  146.   FREE(kernel_base);
  147.   FREE(kernel_end);
  148.   FREE(kernel_items);
  149.   FREE(state_table);
  150. }
  151.  
  152.  
  153.  
  154. /* compute the nondeterministic finite state machine (see state.h for details)
  155. from the grammar.  */
  156. void
  157. generate_states()
  158. {
  159.   allocate_storage();
  160.   initialize_closure(nitems);
  161.   initialize_states();
  162.  
  163.   while (this_state)
  164.     {
  165.       /* Set up ruleset and itemset for the transitions out of this state.
  166.          ruleset gets a 1 bit for each rule that could reduce now.
  167.      itemset gets a vector of all the items that could be accepted next.  */
  168.       closure(this_state->items, this_state->nitems);
  169.       /* record the reductions allowed out of this state */
  170.       save_reductions();
  171.       /* find the itemsets of the states that shifts can reach */
  172.       new_itemsets();
  173.       /* find or create the core structures for those states */
  174.       append_states();
  175.  
  176.       /* create the shifts structures for the shifts to those states,
  177.          now that the state numbers transitioning to are known */
  178.       if (nshifts > 0)
  179.         save_shifts();
  180.  
  181.       /* states are queued when they are created; process them all */
  182.       this_state = this_state->next;
  183.     }
  184.  
  185.   /* discard various storage */
  186.   finalize_closure();
  187.   free_storage();
  188.  
  189.   /* set up initial and final states as parser wants them */
  190.   augment_automaton();
  191. }
  192.  
  193.  
  194.  
  195. /* Find which symbols can be shifted in the current state,
  196.    and for each one record which items would be active after that shift.
  197.    Uses the contents of itemset.
  198.    shift_symbol is set to a vector of the symbols that can be shifted.
  199.    For each symbol in the grammer, kernel_base[symbol] points to
  200.    a vector of item numbers activated if that symbol is shifted,
  201.    and kernel_end[symbol] points after the end of that vector.  */
  202. void
  203. new_itemsets()
  204. {
  205.   register int i;
  206.   register int shiftcount;
  207.   register short *isp;
  208.   register short *ksp;
  209.   register int symbol;
  210.  
  211. #ifdef    TRACE
  212.   fprintf(stderr, "Entering new_itemsets\n");
  213. #endif
  214.  
  215.   for (i = 0; i < nsyms; i++)
  216.     kernel_end[i] = NULL;
  217.  
  218.   shiftcount = 0;
  219.  
  220.   isp = itemset;
  221.  
  222.   while (isp < itemsetend)
  223.     {
  224.       i = *isp++;
  225.       symbol = ritem[i];
  226.       if (symbol > 0)
  227.     {
  228.           ksp = kernel_end[symbol];
  229.  
  230.           if (!ksp)
  231.         {
  232.           shift_symbol[shiftcount++] = symbol;
  233.           ksp = kernel_base[symbol];
  234.         }
  235.  
  236.           *ksp++ = i + 1;
  237.           kernel_end[symbol] = ksp;
  238.     }
  239.     }
  240.  
  241.   nshifts = shiftcount;
  242. }
  243.  
  244.  
  245.  
  246. /* Use the information computed by new_itemset to find the state numbers
  247.    reached by each shift transition from the current state.
  248.  
  249.    shiftset is set up as a vector of state numbers of those states.  */
  250. void
  251. append_states()
  252. {
  253.   register int i;
  254.   register int j;
  255.   register int symbol;
  256.  
  257. #ifdef    TRACE
  258.   fprintf(stderr, "Entering append_states\n");
  259. #endif
  260.  
  261.   /* first sort shift_symbol into increasing order */
  262.  
  263.   for (i = 1; i < nshifts; i++)
  264.     {
  265.       symbol = shift_symbol[i];
  266.       j = i;
  267.       while (j > 0 && shift_symbol[j - 1] > symbol)
  268.     {
  269.       shift_symbol[j] = shift_symbol[j - 1];
  270.       j--;
  271.     }
  272.       shift_symbol[j] = symbol;
  273.     }
  274.  
  275.   for (i = 0; i < nshifts; i++)
  276.     {
  277.       symbol = shift_symbol[i];
  278.       shiftset[i] = get_state(symbol);
  279.     }
  280. }
  281.  
  282.  
  283.  
  284. /* find the state number for the state we would get to
  285. (from the current state) by shifting symbol.
  286. Create a new state if no equivalent one exists already.
  287. Used by append_states  */
  288.  
  289. int
  290. get_state(symbol)
  291. int symbol;
  292. {
  293.   register int key;
  294.   register short *isp1;
  295.   register short *isp2;
  296.   register short *iend;
  297.   register core *sp;
  298.   register int found;
  299.  
  300.   int n;
  301.  
  302. #ifdef    TRACE
  303.   fprintf(stderr, "Entering get_state, symbol = %d\n", symbol);
  304. #endif
  305.  
  306.   isp1 = kernel_base[symbol];
  307.   iend = kernel_end[symbol];
  308.   n = iend - isp1;
  309.  
  310.   /* add up the target state's active item numbers to get a hash key */
  311.   key = 0;
  312.   while (isp1 < iend)
  313.     key += *isp1++;
  314.  
  315.   key = key % STATE_TABLE_SIZE;
  316.  
  317.   sp = state_table[key];
  318.  
  319.   if (sp)
  320.     {
  321.       found = 0;
  322.       while (!found)
  323.     {
  324.       if (sp->nitems == n)
  325.         {
  326.           found = 1;
  327.           isp1 = kernel_base[symbol];
  328.           isp2 = sp->items;
  329.  
  330.           while (found && isp1 < iend)
  331.         {
  332.           if (*isp1++ != *isp2++)
  333.             found = 0;
  334.         }
  335.         }
  336.  
  337.       if (!found)
  338.         {
  339.           if (sp->link)
  340.         {
  341.           sp = sp->link;
  342.         }
  343.           else   /* bucket exhausted and no match */
  344.         {
  345.           sp = sp->link = new_state(symbol);
  346.           found = 1;
  347.         }
  348.         }
  349.     }
  350.     }
  351.   else      /* bucket is empty */
  352.     {
  353.       state_table[key] = sp = new_state(symbol);
  354.     }
  355.  
  356.   return (sp->number);
  357. }
  358.  
  359.  
  360.  
  361. /* subroutine of get_state.  create a new state for those items, if necessary.  */
  362.  
  363. core *
  364. new_state(symbol)
  365. int symbol;
  366. {
  367.   register int n;
  368.   register core *p;
  369.   register short *isp1;
  370.   register short *isp2;
  371.   register short *iend;
  372.  
  373. #ifdef    TRACE
  374.   fprintf(stderr, "Entering new_state, symbol = %d\n", symbol);
  375. #endif
  376.  
  377.   if (nstates >= MAXSHORT)
  378.     toomany("states");
  379.  
  380.   isp1 = kernel_base[symbol];
  381.   iend = kernel_end[symbol];
  382.   n = iend - isp1;
  383.  
  384.   p = (core *) mallocate((unsigned) (sizeof(core) + (n - 1) * sizeof(short)));
  385.   p->accessing_symbol = symbol;
  386.   p->number = nstates;
  387.   p->nitems = n;
  388.  
  389.   isp2 = p->items;
  390.   while (isp1 < iend)
  391.     *isp2++ = *isp1++;
  392.  
  393.   last_state->next = p;
  394.   last_state = p;
  395.  
  396.   nstates++;
  397.  
  398.   return (p);
  399. }
  400.  
  401.  
  402. void
  403. initialize_states()
  404. {
  405.   register core *p;
  406. /*  register unsigned *rp1; JF unused */
  407. /*  register unsigned *rp2; JF unused */
  408. /*  register unsigned *rend; JF unused */
  409.  
  410.   p = (core *) mallocate((unsigned) (sizeof(core) - sizeof(short)));
  411.   first_state = last_state = this_state = p;
  412.   nstates = 1;
  413. }
  414.  
  415.  
  416. void
  417. save_shifts()
  418. {
  419.   register shifts *p;
  420.   register short *sp1;
  421.   register short *sp2;
  422.   register short *send;
  423.  
  424.   p = (shifts *) mallocate((unsigned) (sizeof(shifts) +
  425.                        (nshifts - 1) * sizeof(short)));
  426.  
  427.   p->number = this_state->number;
  428.   p->nshifts = nshifts;
  429.  
  430.   sp1 = shiftset;
  431.   sp2 = p->shifts;
  432.   send = shiftset + nshifts;
  433.  
  434.   while (sp1 < send)
  435.     *sp2++ = *sp1++;
  436.  
  437.   if (last_shift)
  438.     {
  439.       last_shift->next = p;
  440.       last_shift = p;
  441.     }
  442.   else
  443.     {
  444.       first_shift = p;
  445.       last_shift = p;
  446.     }
  447. }
  448.  
  449.  
  450.  
  451. /* find which rules can be used for reduction transitions from the current state
  452.    and make a reductions structure for the state to record their rule numbers.  */
  453. void
  454. save_reductions()
  455. {
  456.   register short *isp;
  457.   register short *rp1;
  458.   register short *rp2;
  459.   register int item;
  460.   register int count;
  461.   register reductions *p;
  462.  
  463.   short *rend;
  464.  
  465.   /* find and count the active items that represent ends of rules */
  466.  
  467.   count = 0;
  468.   for (isp = itemset; isp < itemsetend; isp++)
  469.     {
  470.       item = ritem[*isp];
  471.       if (item < 0)
  472.     {
  473.       redset[count++] = -item;
  474.     }
  475.     }
  476.  
  477.   /* make a reductions structure and copy the data into it.  */
  478.  
  479.   if (count)
  480.     {
  481.       p = (reductions *) mallocate((unsigned) (sizeof(reductions) +
  482.                            (count - 1) * sizeof(short)));
  483.  
  484.       p->number = this_state->number;
  485.       p->nreds = count;
  486.  
  487.       rp1 = redset;
  488.       rp2 = p->rules;
  489.       rend = rp1 + count;
  490.  
  491.       while (rp1 < rend)
  492.     *rp2++ = *rp1++;
  493.  
  494.       if (last_reduction)
  495.     {
  496.       last_reduction->next = p;
  497.       last_reduction = p;
  498.     }
  499.       else
  500.     {
  501.       first_reduction = p;
  502.       last_reduction = p;
  503.     }
  504.     }
  505. }
  506.  
  507.  
  508.  
  509. /* Make sure that the initial state has a shift that accepts the
  510. grammar's start symbol and goes to the next-to-final state,
  511. which has a shift going to the final state, which has a shift
  512. to the termination state.
  513. Create such states and shifts if they don't happen to exist already.  */
  514. void
  515. augment_automaton()
  516. {
  517.   register int i;
  518.   register int k;
  519. /*  register int found; JF unused */
  520.   register core *statep;
  521.   register shifts *sp;
  522.   register shifts *sp2;
  523.   register shifts *sp1;
  524.  
  525.   sp = first_shift;
  526.  
  527.   if (sp)
  528.     {
  529.       if (sp->number == 0)
  530.     {
  531.       k = sp->nshifts;
  532.       statep = first_state->next;
  533.  
  534.       while (statep->accessing_symbol < start_symbol
  535.           && statep->number < k)
  536.         statep = statep->next;
  537.  
  538.       if (statep->accessing_symbol == start_symbol)
  539.         {
  540.           k = statep->number;
  541.  
  542.           while (sp->number < k)
  543.         {
  544.           sp1 = sp;
  545.           sp = sp->next;
  546.         }
  547.  
  548.           if (sp->number == k)
  549.         {
  550.           sp2 = (shifts *) mallocate((unsigned) (sizeof(shifts)
  551.                              + sp->nshifts * sizeof(short)));
  552.           sp2->next = sp->next;
  553.           sp2->number = k;
  554.           sp2->nshifts = sp->nshifts + 1;
  555.           sp2->shifts[0] = nstates;
  556.           for (i = sp->nshifts; i > 0; i--)
  557.             sp2->shifts[i] = sp->shifts[i - 1];
  558.  
  559.           sp1->next = sp2;
  560.           FREE(sp);
  561.         }
  562.           else
  563.         {
  564.           sp2 = NEW(shifts);
  565.           sp2->next = sp;
  566.           sp2->number = k;
  567.           sp2->nshifts = 1;
  568.           sp2->shifts[0] = nstates;
  569.  
  570.           sp1->next = sp2;
  571.           if (!sp)
  572.             last_shift = sp2;
  573.         }
  574.         }
  575.       else
  576.         {
  577.           k = statep->number;
  578.           sp = first_shift;
  579.  
  580.           sp2 = (shifts *) mallocate((unsigned) (sizeof(shifts)
  581.                              + sp->nshifts * sizeof(short)));
  582.           sp2->next = sp->next;
  583.           sp2->nshifts = sp->nshifts + 1;
  584.  
  585.           for (i = 0; i < k; i++)
  586.         sp2->shifts[i] = sp->shifts[i];
  587.  
  588.           sp2->shifts[k] = nstates;
  589.  
  590.           for (i = k; i < sp->nshifts; i++)
  591.         sp2->shifts[i + 1] = sp->shifts[i];
  592.  
  593.           first_shift = sp2;
  594.           if (last_shift == sp)
  595.         last_shift = sp2;
  596.  
  597.           FREE(sp);
  598.  
  599.           insert_start_shift();
  600.         }
  601.     }
  602.       else
  603.     {
  604.       sp = NEW(shifts);
  605.       sp->next = first_shift;
  606.       sp->nshifts = 1;
  607.       sp->shifts[0] = nstates;
  608.  
  609.       first_shift = sp;
  610.  
  611.       insert_start_shift();
  612.     }
  613.     }
  614.   else
  615.     {
  616.       sp = NEW(shifts);
  617.       sp->nshifts = 1;
  618.       sp->shifts[0] = nstates;
  619.  
  620.       first_shift = sp;
  621.       last_shift = sp;
  622.  
  623.       insert_start_shift();
  624.     }
  625.  
  626.   statep = (core *) mallocate((unsigned) (sizeof(core) - sizeof(short)));
  627.   statep->number = nstates;
  628.   last_state->next = statep;
  629.   last_state = statep;
  630.  
  631.   sp = NEW(shifts);
  632.   sp->number = nstates++;
  633.   sp->nshifts = 1;
  634.   sp->shifts[0] = nstates;
  635.   last_shift->next = sp;
  636.   last_shift = sp;
  637.  
  638.   final_state = nstates;
  639.  
  640.   statep = (core *) mallocate((unsigned) (sizeof(core) - sizeof(short)));
  641.   statep->number = nstates++;
  642.   last_state->next = statep;
  643.   last_state = statep;
  644. }
  645.  
  646.  
  647. /* subroutine of augment_automaton */
  648. void
  649. insert_start_shift()
  650. {
  651.   register core *statep;
  652.   register shifts *sp;
  653.  
  654.   statep = (core *) mallocate((unsigned) (sizeof(core) - sizeof(short)));
  655.   statep->number = nstates;
  656.   statep->accessing_symbol = start_symbol;
  657.  
  658.   last_state->next = statep;
  659.   last_state = statep;
  660.  
  661.   sp = NEW(shifts);
  662.   sp->number = nstates++;
  663.   sp->nshifts = 1;
  664.   sp->shifts[0] = nstates;
  665.  
  666.   last_shift->next = sp;
  667.   last_shift = sp;
  668. }
  669.