state.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Type definitions for nondeterministic finite state machine 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. /* These type definitions are used to represent a nondeterministic
  16. finite state machine that parses the specified grammar.
  17. This information is generated by the function generate_states
  18. in the file LR0.
  19. Each state of the machine is described by a set of items --
  20. particular positions in particular rules -- that are the possible
  21. places where parsing could continue when the machine is in this state.
  22. These symbols at these items are the allowable inputs that can follow now.
  23. A core represents one state. States are numbered in the number field.
  24. When generate_states is finished, the starting state is state 0
  25. and nstates is the number of states. (A transition to a state
  26. whose state number is nstates indicates termination.) All the cores
  27. are chained together and first_state points to the first one (state 0).
  28. For each state there is a particular symbol which must have been the
  29. last thing accepted to reach that state. It is the accessing_symbol
  30. of the core.
  31. Each core contains a vector of nitems items which are the indices
  32. in the ritems vector of the items that are selected in this state.
  33. The link field is used for chaining buckets that hash states by
  34. their itemsets. This is for recognizing equivalent states and
  35. combining them when the states are generated.
  36. The two types of transitions are shifts (push the lookahead token
  37. and read another) and reductions (combine the last n things on the
  38. stack via a rule, replace them with the symbol that the rule derives,
  39. and leave the lookahead token alone). When the states are generated,
  40. these transitions are represented in two other lists.
  41. Each shifts structure describes the possible shift transitions out
  42. of one state, the state whose number is in the number field.
  43. The shifts structures are linked through next and first_shift points to them.
  44. Each contains a vector of numbers of the states that shift transitions
  45. can go to. The accessing_symbol fields of those states' cores say what kind
  46. of input leads to them.
  47. A shift to state zero should be ignored. Conflict resolution
  48. deletes shifts by changing them to zero.
  49. Each reductions structure describes the possible reductions at the state
  50. whose number is in the number field. The data is a list of nreds rules,
  51. represented by their rule numbers. first_reduction points to the list
  52. of these structures.
  53. Conflict resolution can decide that certain tokens in certain
  54. states should explicitly be errors (for implementing %nonassoc).
  55. For each state, the tokens that are errors for this reason
  56. are recorded in an errs structure, which has the state number
  57. in its number field. The rest of the errs structure is full
  58. of token numbers.
  59. There is at least one shift transition present in state zero.
  60. It leads to a next-to-final state whose accessing_symbol is
  61. the grammar's start symbol. The next-to-final state has one shift
  62. to the final state, whose accessing_symbol is zero (end of input).
  63. The final state has one shift, which goes to the termination state
  64. (whose number is nstates, and for which there is no core structure).
  65. The reason for the extra state at the end is to placate the parser's
  66. strategy of making all decisions one token ahead of its actions. */
  67. typedef
  68. struct core
  69. {
  70. struct core *next;
  71. struct core *link;
  72. short number;
  73. short accessing_symbol;
  74. short nitems;
  75. short items[1];
  76. }
  77. core;
  78. typedef
  79. struct shifts
  80. {
  81. struct shifts *next;
  82. short number;
  83. short nshifts;
  84. short shifts[1];
  85. }
  86. shifts;
  87. typedef
  88. struct errs
  89. {
  90. short nerrs;
  91. short errs[1];
  92. }
  93. errs;
  94. typedef
  95. struct reductions
  96. {
  97. struct reductions *next;
  98. short number;
  99. short nreds;
  100. short rules[1];
  101. }
  102. reductions;
  103. extern int nstates;
  104. extern core *first_state;
  105. extern shifts *first_shift;
  106. extern reductions *first_reduction;