Ptidej-v5.2-master/PADL Creator AOL/src/padl/creator/aolfile/javacup/parse_reduce_table.java
package padl.creator.aolfile.javacup;

/** This class represents the complete "reduce-goto" table of the parser.
 *  It has one row for each state in the parse machines, and a column for
 *  each terminal symbol.  Each entry contains a state number to shift to
 *  as the last step of a reduce. 
 *
 * @see     java_cup.parse_reduce_row
 * @version last updated: 11/25/95
 * @author  Scott Hudson
 */
public class parse_reduce_table {

	/*-----------------------------------------------------------*/
	/*--- Constructor(s) ----------------------------------------*/
	/*-----------------------------------------------------------*/

	/** How many rows/states in the machine/table. */
	protected int _num_states;

	/*-----------------------------------------------------------*/
	/*--- (Access to) Instance Variables ------------------------*/
	/*-----------------------------------------------------------*/

	/** Actual array of rows, one per state */
	public parse_reduce_row[] under_state;

	/** Simple constructor.  Note: all terminals, non-terminals, and productions 
	 *  must already have been entered, and the viable prefix recognizer should
	 *  have been constructed before this is called.
	 */
	public parse_reduce_table() {
		/* determine how many states we are working with */
		this._num_states = lalr_state.number();

		/* allocate the array and fill it in with empty rows */
		this.under_state = new parse_reduce_row[this._num_states];
		for (int i = 0; i < this._num_states; i++) {
			this.under_state[i] = new parse_reduce_row();
		}
	}

	/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

	/** How many rows/states in the machine/table. */
	public int num_states() {
		return this._num_states;
	}

	/*-----------------------------------------------------------*/
	/*--- General Methods ---------------------------------------*/
	/*-----------------------------------------------------------*/

	/** Convert to a string. */
	public String toString() {
		String result;
		lalr_state goto_st;
		int cnt;

		result = "-------- REDUCE_TABLE --------\n";
		for (int row = 0; row < this.num_states(); row++) {
			result += "From state #" + row + "\n";
			cnt = 0;
			for (int col = 0; col < parse_reduce_row.size(); col++) {
				/* pull out the table entry */
				goto_st = this.under_state[row].under_non_term[col];

				/* if it has action in it, print it */
				if (goto_st != null) {
					result += " [non term " + col + "->";
					result += "state " + goto_st.index() + "]";

					/* end the line after the 3rd one */
					cnt++;
					if (cnt == 3) {
						result += "\n";
						cnt = 0;
					}
				}
			}
			/* finish the line if we haven't just done that */
			if (cnt != 0) {
				result += "\n";
			}
		}
		result += "-----------------------------";

		return result;
	}

	/*-----------------------------------------------------------*/

}
