home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / online / source / c / compilers / Bison.sit.hqx / Bison / Source / closure.c < prev    next >
Text File  |  1992-08-21  |  7KB  |  362 lines

  1. /***********************************************************
  2.  *
  3.  * Macintosh/MPW version of GNU Bison 1.18
  4.  * Please read the README_MPW file for more information
  5.  *
  6.  ***********************************************************/
  7.  
  8. /* Subroutines for bison
  9.    Copyright (C) 1984, 1989 Free Software Foundation, Inc.
  10.  
  11. This file is part of Bison, the GNU Compiler Compiler.
  12.  
  13. Bison is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2, or (at your option)
  16. any later version.
  17.  
  18. Bison is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with Bison; see the file COPYING.  If not, write to
  25. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  26.  
  27.  
  28. /* subroutines of file LR0.c.
  29.  
  30. Entry points:
  31.  
  32.   closure (items, n)
  33.  
  34. Given a vector of item numbers items, of length n,
  35. set up ruleset and itemset to indicate what rules could be run
  36. and which items could be accepted when those items are the active ones.
  37.  
  38. ruleset contains a bit for each rule.  closure sets the bits
  39. for all rules which could potentially describe the next input to be read.
  40.  
  41. itemset is a vector of item numbers; itemsetend points to just beyond the end
  42.  of the part of it that is significant.
  43. closure places there the indices of all items which represent units of
  44. input that could arrive next.
  45.  
  46.   initialize_closure (n)
  47.  
  48. Allocates the itemset and ruleset vectors,
  49. and precomputes useful data so that closure can be called.
  50. n is the number of elements to allocate for itemset.
  51.  
  52.   finalize_closure ()
  53.  
  54. Frees itemset, ruleset and internal data.
  55.  
  56. */
  57.  
  58. #include <stdio.h>
  59. #include "system.h"
  60. #include "machine.h"
  61. #include "new.h"
  62. #include "gram.h"
  63.  
  64. #ifdef macintosh
  65. #pragma segment bison
  66. #endif
  67.  
  68. extern short **derives;
  69. extern char **tags;
  70.  
  71. void set_fderives();
  72. void set_firsts();
  73.  
  74. extern void RTC();
  75.  
  76. short *itemset;
  77. short *itemsetend;
  78. static unsigned *ruleset;
  79.  
  80. /* internal data.  See comments before set_fderives and set_firsts.  */
  81. static unsigned *fderives;
  82. static unsigned *firsts;
  83.  
  84. /* number of words required to hold a bit for each rule */
  85. static int rulesetsize;
  86.  
  87. /* number of words required to hold a bit for each variable */
  88. static int varsetsize;
  89.  
  90.  
  91. void
  92. initialize_closure(n)
  93. int n;
  94. {
  95.   itemset = NEW2(n, short);
  96.  
  97.   rulesetsize = WORDSIZE(nrules + 1);
  98.   ruleset = NEW2(rulesetsize, unsigned);
  99.  
  100.   set_fderives();
  101. }
  102.  
  103.  
  104.  
  105. /* set fderives to an nvars by nrules matrix of bits
  106.    indicating which rules can help derive the beginning of the data
  107.    for each nonterminal.  For example, if symbol 5 can be derived as
  108.    the sequence of symbols 8 3 20, and one of the rules for deriving
  109.    symbol 8 is rule 4, then the [5 - ntokens, 4] bit in fderives is set.  */
  110. void
  111. set_fderives()
  112. {
  113.   register unsigned *rrow;
  114.   register unsigned *vrow;
  115.   register int j;
  116.   register unsigned cword;
  117.   register short *rp;
  118.   register int b;
  119.  
  120.   int ruleno;
  121.   int i;
  122.  
  123.   fderives = NEW2(nvars * rulesetsize, unsigned) - ntokens * rulesetsize;
  124.  
  125.   set_firsts();
  126.  
  127.   rrow = fderives + ntokens * rulesetsize;
  128.  
  129.   for (i = ntokens; i < nsyms; i++)
  130.     {
  131.       vrow = firsts + ((i - ntokens) * varsetsize);
  132.       cword = *vrow++;
  133.       b = 0;
  134.       for (j = ntokens; j < nsyms; j++)
  135.     {
  136.       if (cword & (1 << b))
  137.         {
  138.           rp = derives[j];
  139.           while ((ruleno = *rp++) > 0)
  140.         {
  141.           SETBIT(rrow, ruleno);
  142.         }
  143.         }
  144.  
  145.       b++;
  146.       if (b >= BITS_PER_WORD && j + 1 < nsyms)
  147.         {
  148.           cword = *vrow++;
  149.           b = 0;
  150.         }
  151.     }
  152.  
  153.       rrow += rulesetsize;
  154.     }
  155.  
  156. #ifdef    DEBUG
  157.   print_fderives();
  158. #endif
  159.  
  160.   FREE(firsts);
  161. }
  162.  
  163.  
  164.  
  165. /* set firsts to be an nvars by nvars bit matrix indicating which items
  166.    can represent the beginning of the input corresponding to which other items.
  167.    For example, if some rule expands symbol 5 into the sequence of symbols 8 3 20,
  168.    the symbol 8 can be the beginning of the data for symbol 5,
  169.    so the bit [8 - ntokens, 5 - ntokens] in firsts is set. */
  170. void
  171. set_firsts()
  172. {
  173.   register unsigned *row;
  174. /*   register int done; JF unused */
  175.   register int symbol;
  176.   register short *sp;
  177.   register int rowsize;
  178.  
  179.   int i;
  180.  
  181.   varsetsize = rowsize = WORDSIZE(nvars);
  182.  
  183.   firsts = NEW2(nvars * rowsize, unsigned);
  184.  
  185.   row = firsts;
  186.   for (i = ntokens; i < nsyms; i++)
  187.     {
  188.       sp = derives[i];
  189.       while (*sp >= 0)
  190.     {
  191.       symbol = ritem[rrhs[*sp++]];
  192.       if (ISVAR(symbol))
  193.         {
  194.           symbol -= ntokens;
  195.           SETBIT(row, symbol);
  196.         }
  197.     }
  198.  
  199.       row += rowsize;
  200.     }
  201.  
  202.   RTC(firsts, nvars);
  203.  
  204. #ifdef    DEBUG
  205.   print_firsts();
  206. #endif
  207. }
  208.  
  209.  
  210. void
  211. closure(core, n)
  212. short *core;
  213. int n;
  214. {
  215.   register int ruleno;
  216.   register unsigned word;
  217.   register short *csp;
  218.   register unsigned *dsp;
  219.   register unsigned *rsp;
  220.  
  221.   short *csend;
  222.   unsigned *rsend;
  223.   int symbol;
  224.   int itemno;
  225.  
  226.   rsp = ruleset;
  227.   rsend = ruleset + rulesetsize;
  228.   csend = core + n;
  229.  
  230.   if (n == 0)
  231.     {
  232.       dsp = fderives + start_symbol * rulesetsize;
  233.       while (rsp < rsend)
  234.     *rsp++ = *dsp++;
  235.     }
  236.   else
  237.     {
  238.       while (rsp < rsend)
  239.     *rsp++ = 0;
  240.  
  241.       csp = core;
  242.       while (csp < csend)
  243.     {
  244.       symbol = ritem[*csp++];
  245.       if (ISVAR(symbol))
  246.         {
  247.           dsp = fderives + symbol * rulesetsize;
  248.           rsp = ruleset;
  249.           while (rsp < rsend)
  250.         *rsp++ |= *dsp++;
  251.         }
  252.     }
  253.     }
  254.  
  255.   ruleno = 0;
  256.   itemsetend = itemset;
  257.   csp = core;
  258.   rsp = ruleset;
  259.   while (rsp < rsend)
  260.     {
  261.       word = *rsp++;
  262.       if (word == 0)
  263.     {
  264.       ruleno += BITS_PER_WORD;
  265.     }
  266.       else
  267.     {
  268.       register int b;
  269.  
  270.       for (b = 0; b < BITS_PER_WORD; b++)
  271.         {
  272.           if (word & (1 << b))
  273.         {
  274.           itemno = rrhs[ruleno];
  275.           while (csp < csend && *csp < itemno)
  276.             *itemsetend++ = *csp++;
  277.           *itemsetend++ = itemno;
  278.         }
  279.  
  280.           ruleno++;
  281.         }
  282.     }
  283.     }
  284.  
  285.   while (csp < csend)
  286.     *itemsetend++ = *csp++;
  287.  
  288. #ifdef    DEBUG
  289.   print_closure(n);
  290. #endif
  291. }
  292.  
  293.  
  294. void
  295. finalize_closure()
  296. {
  297.   FREE(itemset);
  298.   FREE(ruleset);
  299.   FREE(fderives + ntokens * rulesetsize);
  300. }
  301.  
  302.  
  303.  
  304. #ifdef    DEBUG
  305.  
  306. print_closure(n)
  307. int n;
  308. {
  309.   register short *isp;
  310.  
  311.   printf("\n\nn = %d\n\n", n);
  312.   for (isp = itemset; isp < itemsetend; isp++)
  313.     printf("   %d\n", *isp);
  314. }
  315.  
  316.  
  317.  
  318. print_firsts()
  319. {
  320.   register int i;
  321.   register int j;
  322.   register unsigned *rowp;
  323.  
  324.   printf("\n\n\nFIRSTS\n\n");
  325.  
  326.   for (i = ntokens; i < nsyms; i++)
  327.     {
  328.       printf("\n\n%s firsts\n\n", tags[i]);
  329.  
  330.       rowp = firsts + ((i - ntokens) * varsetsize);
  331.  
  332.       for (j = 0; j < nvars; j++)
  333.     if (BITISSET (rowp, j))
  334.       printf("   %s\n", tags[j + ntokens]);
  335.     }
  336. }
  337.  
  338.  
  339.  
  340. print_fderives()
  341. {
  342.   register int i;
  343.   register int j;
  344.   register unsigned *rp;
  345.  
  346.   printf("\n\n\nFDERIVES\n");
  347.  
  348.   for (i = ntokens; i < nsyms; i++)
  349.     {
  350.       printf("\n\n%s derives\n\n", tags[i]);
  351.       rp = fderives + i * rulesetsize;
  352.  
  353.       for (j = 0; j <= nrules; j++)
  354.     if (BITISSET (rp, j))
  355.       printf("   %d\n", j);
  356.     }
  357.  
  358.   fflush(stdout);
  359. }
  360.  
  361. #endif
  362.