derives.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Match rules with nonterminals for bison,
  2. Copyright (C) 1984 Bob Corbett and Free Software Foundation, Inc.
  3. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the BISON General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute BISON,
  9. but only under the conditions described in the BISON General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with BISON so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, you are welcome to use, share and improve this program.
  15. You are forbidden to forbid anyone else to use, share and improve
  16. what you give them. Help stamp out software-hoarding! */
  17. /* set_derives finds, for each variable (nonterminal), which rules can derive it.
  18. It sets up the value of derives so that
  19. derives[i - ntokens] points to a vector of rule numbers, terminated with a zero. */
  20. #include <stdio.h>
  21. #include "new.h"
  22. #include "types.h"
  23. #include "gram.h"
  24. short **derives;
  25. set_derives()
  26. {
  27. register int i;
  28. register int lhs;
  29. register shorts *p;
  30. register short *q;
  31. register shorts **dset;
  32. register shorts *delts;
  33. dset = NEW2(nvars, shorts *) - ntokens;
  34. delts = NEW2(nrules + 1, shorts);
  35. p = delts;
  36. for (i = nrules; i > 0; i--)
  37. {
  38. lhs = rlhs[i];
  39. p->next = dset[lhs];
  40. p->value = i;
  41. dset[lhs] = p;
  42. p++;
  43. }
  44. derives = NEW2(nvars, short *) - ntokens;
  45. q = NEW2(nvars + nrules, short);
  46. for (i = ntokens; i < nsyms; i++)
  47. {
  48. derives[i] = q;
  49. p = dset[i];
  50. while (p)
  51. {
  52. *q++ = p->value;
  53. p = p->next;
  54. }
  55. *q++ = -1;
  56. }
  57. #ifdef DEBUG
  58. print_derives();
  59. #endif
  60. FREE(dset + ntokens);
  61. FREE(delts);
  62. }
  63. free_derives()
  64. {
  65. FREE(derives[ntokens]);
  66. FREE(derives + ntokens);
  67. }
  68. #ifdef DEBUG
  69. print_derives()
  70. {
  71. register int i;
  72. register short *sp;
  73. extern char **tags;
  74. printf("\n\n\nDERIVES\n\n");
  75. for (i = ntokens; i < nsyms; i++)
  76. {
  77. printf("%s derives", tags[i]);
  78. for (sp = derives[i]; *sp > 0; sp++)
  79. {
  80. printf(" %d", *sp);
  81. }
  82. putchar('\n');
  83. }
  84. putchar('\n');
  85. }
  86. #endif