DERIVES.C 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 1, 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, terminated with a zero. */
  18. #include <stdio.h>
  19. #include "system.h"
  20. #include "new.h"
  21. #include "types.h"
  22. #include "gram.h"
  23. short **derives;
  24. void
  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. void
  64. free_derives()
  65. {
  66. FREE(derives[ntokens]);
  67. FREE(derives + ntokens);
  68. }
  69. #ifdef DEBUG
  70. print_derives()
  71. {
  72. register int i;
  73. register short *sp;
  74. extern char **tags;
  75. printf("\n\n\nDERIVES\n\n");
  76. for (i = ntokens; i < nsyms; i++)
  77. {
  78. printf("%s derives", tags[i]);
  79. for (sp = derives[i]; *sp > 0; sp++)
  80. {
  81. printf(" %d", *sp);
  82. }
  83. putchar('\n');
  84. }
  85. putchar('\n');
  86. }
  87. #endif