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