NULLABLE.C 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "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. 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. void
  108. free_nullable()
  109. {
  110. FREE(nullable + ntokens);
  111. }