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 / derives.c < prev    next >
Text File  |  1992-08-21  |  3KB  |  130 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. /* Match rules with nonterminals 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. /* set_derives finds, for each variable (nonterminal), which rules can derive it.
  29.    It sets up the value of derives so that
  30.    derives[i - ntokens] points to a vector of rule numbers,
  31.    terminated with -1.  */
  32.  
  33. #include <stdio.h>
  34. #include "system.h"
  35. #include "new.h"
  36. #include "types.h"
  37. #include "gram.h"
  38.  
  39. #ifdef macintosh
  40. #pragma segment bison
  41. #endif
  42.  
  43.  
  44. short **derives;
  45.  
  46. void
  47. set_derives()
  48. {
  49.   register int i;
  50.   register int lhs;
  51.   register shorts *p;
  52.   register short *q;
  53.   register shorts **dset;
  54.   register shorts *delts;
  55.  
  56.   dset = NEW2(nvars, shorts *) - ntokens;
  57.   delts = NEW2(nrules + 1, shorts);
  58.  
  59.   p = delts;
  60.   for (i = nrules; i > 0; i--)
  61.     {
  62.       lhs = rlhs[i];
  63.       if (lhs >= 0)
  64.     {
  65.       p->next = dset[lhs];
  66.       p->value = i;
  67.       dset[lhs] = p;
  68.       p++;
  69.     }
  70.     }
  71.  
  72.   derives = NEW2(nvars, short *) - ntokens;
  73.   q = NEW2(nvars + nrules, short);
  74.  
  75.   for (i = ntokens; i < nsyms; i++)
  76.     {
  77.       derives[i] = q;
  78.       p = dset[i];
  79.       while (p)
  80.     {
  81.       *q++ = p->value;
  82.       p = p->next;
  83.     }
  84.       *q++ = -1;
  85.     }
  86.  
  87. #ifdef    DEBUG
  88.   print_derives();
  89. #endif
  90.  
  91.   FREE(dset + ntokens);
  92.   FREE(delts);
  93. }
  94.  
  95. void
  96. free_derives()
  97. {
  98.   FREE(derives[ntokens]);
  99.   FREE(derives + ntokens);
  100. }
  101.  
  102.  
  103.  
  104. #ifdef    DEBUG
  105.  
  106. print_derives()
  107. {
  108.   register int i;
  109.   register short *sp;
  110.  
  111.   extern char **tags;
  112.  
  113.   printf("\n\n\nDERIVES\n\n");
  114.  
  115.   for (i = ntokens; i < nsyms; i++)
  116.     {
  117.       printf("%s derives", tags[i]);
  118.       for (sp = derives[i]; *sp > 0; sp++)
  119.     {
  120.       printf("  %d", *sp);
  121.     }
  122.       putchar('\n');
  123.     }
  124.  
  125.   putchar('\n');
  126. }
  127.  
  128. #endif
  129.  
  130.