4 import java.util.Enumeration;
6 /** This class represents the complete "action" table of the parser.
7 * It has one row for each state in the parse machine, and a column for
8 * each terminal symbol. Each entry in the table represents a shift,
11 * @see java_cup.parse_action
12 * @see java_cup.parse_action_row
13 * @version last updated: 11/25/95
14 * @author Scott Hudson
16 public class parse_action_table {
18 /*-----------------------------------------------------------*/
19 /*--- Constructor(s) ----------------------------------------*/
20 /*-----------------------------------------------------------*/
22 /** Simple constructor. All terminals, non-terminals, and productions must
23 * already have been entered, and the viable prefix recognizer should
24 * have been constructed before this is called.
26 public parse_action_table()
28 /* determine how many states we are working with */
29 _num_states = lalr_state.number();
31 /* allocate the array and fill it in with empty rows */
32 under_state = new parse_action_row[_num_states];
33 for (int i=0; i<_num_states; i++)
34 under_state[i] = new parse_action_row();
37 /*-----------------------------------------------------------*/
38 /*--- (Access to) Instance Variables ------------------------*/
39 /*-----------------------------------------------------------*/
41 /** How many rows/states are in the machine/table. */
42 protected int _num_states;
44 /** How many rows/states are in the machine/table. */
45 public int num_states() {return _num_states;}
47 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
49 /** Actual array of rows, one per state. */
50 public parse_action_row[] under_state;
52 /*-----------------------------------------------------------*/
53 /*--- General Methods ---------------------------------------*/
54 /*-----------------------------------------------------------*/
56 /** Check the table to ensure that all productions have been reduced.
57 * Issue a warning message (to System.err) for each production that
60 public void check_reductions()
66 /* tabulate reductions -- look at every table entry */
67 for (int row = 0; row < num_states(); row++)
69 for (int col = 0; col < under_state[row].size(); col++)
71 /* look at the action entry to see if its a reduce */
72 act = under_state[row].under_term[col];
73 if (act != null && act.kind() == parse_action.REDUCE)
75 /* tell production that we used it */
76 ((reduce_action)act).reduce_with().note_reduction_use();
81 /* now go across every production and make sure we hit it */
82 for (Enumeration p = production.all(); p.hasMoreElements(); )
84 prod = (production)p.nextElement();
86 /* if we didn't hit it give a warning */
87 if (prod.num_reductions() == 0)
92 /* give a warning if they haven't been turned off */
95 System.err.println("*** Production \"" +
96 prod.to_simple_string() + "\" never reduced");
97 lexer.warning_count++;
103 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*
105 /** Convert to a string. */
106 public String toString()
111 result = "-------- ACTION_TABLE --------\n";
112 for (int row = 0; row < num_states(); row++)
114 result += "From state #" + row + "\n";
116 for (int col = 0; col < under_state[row].size(); col++)
118 /* if the action is not an error print it */
119 if (under_state[row].under_term[col].kind() != parse_action.ERROR)
121 result += " [term " + col + ":" + under_state[row].under_term[col] + "]";
123 /* end the line after the 2nd one */
132 /* finish the line if we haven't just done that */
133 if (cnt != 0) result += "\n";
135 result += "------------------------------";
140 /*-----------------------------------------------------------*/