nullable.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Part of the bison parser generator,
  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 up nullable, a vector saying which nonterminals can expand into the null string.
  18. nullable[i - ntokens] is nonzero if symbol i can do so. */
  19. #include <stdio.h>
  20. #include "types.h"
  21. #include "gram.h"
  22. #include "new.h"
  23. char *nullable;
  24. set_nullable()
  25. {
  26. register short *r;
  27. register short *s1;
  28. register short *s2;
  29. register int ruleno;
  30. register int symbol;
  31. register shorts *p;
  32. short *squeue;
  33. short *rcount;
  34. shorts **rsets;
  35. shorts *relts;
  36. char any_tokens;
  37. short *r1;
  38. #ifdef TRACE
  39. fprintf(stderr, "Entering set_nullable");
  40. #endif
  41. nullable = NEW2(nvars, char) - ntokens;
  42. squeue = NEW2(nvars, short);
  43. s1 = s2 = squeue;
  44. rcount = NEW2(nrules + 1, short);
  45. rsets = NEW2(nvars, shorts *) - ntokens;
  46. relts = NEW2(nitems + nvars + 1, shorts);
  47. p = relts;
  48. r = ritem;
  49. while (*r)
  50. {
  51. if (*r < 0)
  52. {
  53. symbol = rlhs[-(*r++)];
  54. if (!nullable[symbol])
  55. {
  56. nullable[symbol] = 1;
  57. *s2++ = symbol;
  58. }
  59. }
  60. else
  61. {
  62. r1 = r;
  63. any_tokens = 0;
  64. for (symbol = *r++; symbol > 0; symbol = *r++)
  65. {
  66. if (ISTOKEN(symbol))
  67. any_tokens = 1;
  68. }
  69. if (!any_tokens)
  70. {
  71. ruleno = -symbol;
  72. r = r1;
  73. for (symbol = *r++; symbol > 0; symbol = *r++)
  74. {
  75. rcount[ruleno]++;
  76. p->next = rsets[symbol];
  77. p->value = ruleno;
  78. rsets[symbol] = p;
  79. p++;
  80. }
  81. }
  82. }
  83. }
  84. while (s1 < s2)
  85. {
  86. p = rsets[*s1++];
  87. while (p)
  88. {
  89. ruleno = p->value;
  90. p = p->next;
  91. if (--rcount[ruleno] == 0)
  92. {
  93. symbol = rlhs[ruleno];
  94. if (!nullable[symbol])
  95. {
  96. nullable[symbol] = 1;
  97. *s2++ = symbol;
  98. }
  99. }
  100. }
  101. }
  102. FREE(squeue);
  103. FREE(rcount);
  104. FREE(rsets + ntokens);
  105. FREE(relts);
  106. }
  107. free_nullable()
  108. {
  109. FREE(nullable + ntokens);
  110. }