nullable.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Part of the bison parser generator,
  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 up nullable, a vector saying which nonterminals can expand into the null string.
  16. nullable[i - ntokens] is nonzero if symbol i can do so. */
  17. #include <stdio.h>
  18. #include "system.h"
  19. #include "types.h"
  20. #include "gram.h"
  21. #include "new.h"
  22. char *nullable;
  23. void
  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. /* This is said to be more elements than we actually use.
  47. Supposedly nitems - nrules is enough.
  48. But why take the risk? */
  49. relts = NEW2(nitems + nvars + 1, shorts);
  50. p = relts;
  51. r = ritem;
  52. while (*r)
  53. {
  54. if (*r < 0)
  55. {
  56. symbol = rlhs[-(*r++)];
  57. if (symbol >= 0 && !nullable[symbol])
  58. {
  59. nullable[symbol] = 1;
  60. *s2++ = symbol;
  61. }
  62. }
  63. else
  64. {
  65. r1 = r;
  66. any_tokens = 0;
  67. for (symbol = *r++; symbol > 0; symbol = *r++)
  68. {
  69. if (ISTOKEN(symbol))
  70. any_tokens = 1;
  71. }
  72. if (!any_tokens)
  73. {
  74. ruleno = -symbol;
  75. r = r1;
  76. for (symbol = *r++; symbol > 0; symbol = *r++)
  77. {
  78. rcount[ruleno]++;
  79. p->next = rsets[symbol];
  80. p->value = ruleno;
  81. rsets[symbol] = p;
  82. p++;
  83. }
  84. }
  85. }
  86. }
  87. while (s1 < s2)
  88. {
  89. p = rsets[*s1++];
  90. while (p)
  91. {
  92. ruleno = p->value;
  93. p = p->next;
  94. if (--rcount[ruleno] == 0)
  95. {
  96. symbol = rlhs[ruleno];
  97. if (symbol >= 0 && !nullable[symbol])
  98. {
  99. nullable[symbol] = 1;
  100. *s2++ = symbol;
  101. }
  102. }
  103. }
  104. }
  105. FREE(squeue);
  106. FREE(rcount);
  107. FREE(rsets + ntokens);
  108. FREE(relts);
  109. }
  110. void
  111. free_nullable()
  112. {
  113. FREE(nullable + ntokens);
  114. }