1. package com.sun.java_cup.internal;
  2. import java.util.Enumeration;
  3. /** This class represents the complete "reduce-goto" table of the parser.
  4. * It has one row for each state in the parse machines, and a column for
  5. * each terminal symbol. Each entry contains a state number to shift to
  6. * as the last step of a reduce.
  7. *
  8. * @see com.sun.java_cup.internal.parse_reduce_row
  9. * @version last updated: 11/25/95
  10. * @author Scott Hudson
  11. */
  12. public class parse_reduce_table {
  13. /*-----------------------------------------------------------*/
  14. /*--- Constructor(s) ----------------------------------------*/
  15. /*-----------------------------------------------------------*/
  16. /** Simple constructor. Note: all terminals, non-terminals, and productions
  17. * must already have been entered, and the viable prefix recognizer should
  18. * have been constructed before this is called.
  19. */
  20. public parse_reduce_table()
  21. {
  22. /* determine how many states we are working with */
  23. _num_states = lalr_state.number();
  24. /* allocate the array and fill it in with empty rows */
  25. under_state = new parse_reduce_row[_num_states];
  26. for (int i=0; i<_num_states; i++)
  27. under_state[i] = new parse_reduce_row();
  28. }
  29. /*-----------------------------------------------------------*/
  30. /*--- (Access to) Instance Variables ------------------------*/
  31. /*-----------------------------------------------------------*/
  32. /** How many rows/states in the machine/table. */
  33. protected int _num_states;
  34. /** How many rows/states in the machine/table. */
  35. public int num_states() {return _num_states;}
  36. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  37. /** Actual array of rows, one per state */
  38. public parse_reduce_row[] under_state;
  39. /*-----------------------------------------------------------*/
  40. /*--- General Methods ---------------------------------------*/
  41. /*-----------------------------------------------------------*/
  42. /** Convert to a string. */
  43. public String toString()
  44. {
  45. String result;
  46. lalr_state goto_st;
  47. int cnt;
  48. result = "-------- REDUCE_TABLE --------\n";
  49. for (int row = 0; row < num_states(); row++)
  50. {
  51. result += "From state #" + row + "\n";
  52. cnt = 0;
  53. for (int col = 0; col < under_state[row].size(); col++)
  54. {
  55. /* pull out the table entry */
  56. goto_st = under_state[row].under_non_term[col];
  57. /* if it has action in it, print it */
  58. if (goto_st != null)
  59. {
  60. result += " [non term " + col + "->";
  61. result += "state " + goto_st.index() + "]";
  62. /* end the line after the 3rd one */
  63. cnt++;
  64. if (cnt == 3)
  65. {
  66. result += "\n";
  67. cnt = 0;
  68. }
  69. }
  70. }
  71. /* finish the line if we haven't just done that */
  72. if (cnt != 0) result += "\n";
  73. }
  74. result += "-----------------------------";
  75. return result;
  76. }
  77. /*-----------------------------------------------------------*/
  78. }