derives.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Match rules with nonterminals for bison,
  2. Copyright (C) 1984, 1989 Free Software Foundation, Inc.
  3. This file is part of Bison, the GNU Compiler Compiler.
  4. Bison is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. Bison is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Bison; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /* set_derives finds, for each variable (nonterminal), which rules can derive it.
  16. It sets up the value of derives so that
  17. derives[i - ntokens] points to a vector of rule numbers,
  18. terminated with -1. */
  19. #include <stdio.h>
  20. #include "system.h"
  21. #include "new.h"
  22. #include "types.h"
  23. #include "gram.h"
  24. short **derives;
  25. void
  26. set_derives()
  27. {
  28. register int i;
  29. register int lhs;
  30. register shorts *p;
  31. register short *q;
  32. register shorts **dset;
  33. register shorts *delts;
  34. dset = NEW2(nvars, shorts *) - ntokens;
  35. delts = NEW2(nrules + 1, shorts);
  36. p = delts;
  37. for (i = nrules; i > 0; i--)
  38. {
  39. lhs = rlhs[i];
  40. if (lhs >= 0)
  41. {
  42. p->next = dset[lhs];
  43. p->value = i;
  44. dset[lhs] = p;
  45. p++;
  46. }
  47. }
  48. derives = NEW2(nvars, short *) - ntokens;
  49. q = NEW2(nvars + nrules, short);
  50. for (i = ntokens; i < nsyms; i++)
  51. {
  52. derives[i] = q;
  53. p = dset[i];
  54. while (p)
  55. {
  56. *q++ = p->value;
  57. p = p->next;
  58. }
  59. *q++ = -1;
  60. }
  61. #ifdef DEBUG
  62. print_derives();
  63. #endif
  64. FREE(dset + ntokens);
  65. FREE(delts);
  66. }
  67. void
  68. free_derives()
  69. {
  70. FREE(derives[ntokens]);
  71. FREE(derives + ntokens);
  72. }
  73. #ifdef DEBUG
  74. print_derives()
  75. {
  76. register int i;
  77. register short *sp;
  78. extern char **tags;
  79. printf("\n\n\nDERIVES\n\n");
  80. for (i = ntokens; i < nsyms; i++)
  81. {
  82. printf("%s derives", tags[i]);
  83. for (sp = derives[i]; *sp > 0; sp++)
  84. {
  85. printf(" %d", *sp);
  86. }
  87. putchar('\n');
  88. }
  89. putchar('\n');
  90. }
  91. #endif