state.h 4.8 KB

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