home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / language / bison / Old_Bison / C / Closure < prev    next >
Encoding:
Text File  |  1990-04-01  |  7.0 KB  |  369 lines

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