nullable.c 2.5 KB

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