Source code: java_cup/parse_reduce_table.java
1
2 package java_cup;
3
4 import java.util.Enumeration;
5
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.
10 *
11 * @see java_cup.parse_reduce_row
12 * @version last updated: 11/25/95
13 * @author Scott Hudson
14 */
15 public class parse_reduce_table {
16
17 /*-----------------------------------------------------------*/
18 /*--- Constructor(s) ----------------------------------------*/
19 /*-----------------------------------------------------------*/
20
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.
24 */
25 public parse_reduce_table()
26 {
27 /* determine how many states we are working with */
28 _num_states = lalr_state.number();
29
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();
34 }
35
36
37 /*-----------------------------------------------------------*/
38 /*--- (Access to) Instance Variables ------------------------*/
39 /*-----------------------------------------------------------*/
40
41 /** How many rows/states in the machine/table. */
42 protected int _num_states;
43
44 /** How many rows/states in the machine/table. */
45 public int num_states() {return _num_states;}
46
47 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
48
49 /** Actual array of rows, one per state */
50 public parse_reduce_row[] under_state;
51
52 /*-----------------------------------------------------------*/
53 /*--- General Methods ---------------------------------------*/
54 /*-----------------------------------------------------------*/
55
56 /** Convert to a string. */
57 public String toString()
58 {
59 String result;
60 lalr_state goto_st;
61 int cnt;
62
63 result = "-------- REDUCE_TABLE --------\n";
64 for (int row = 0; row < num_states(); row++)
65 {
66 result += "From state #" + row + "\n";
67 cnt = 0;
68 for (int col = 0; col < under_state[row].size(); col++)
69 {
70 /* pull out the table entry */
71 goto_st = under_state[row].under_non_term[col];
72
73 /* if it has action in it, print it */
74 if (goto_st != null)
75 {
76 result += " [non term " + col + "->";
77 result += "state " + goto_st.index() + "]";
78
79 /* end the line after the 3rd one */
80 cnt++;
81 if (cnt == 3)
82 {
83 result += "\n";
84 cnt = 0;
85 }
86 }
87 }
88 /* finish the line if we haven't just done that */
89 if (cnt != 0) result += "\n";
90 }
91 result += "-----------------------------";
92
93 return result;
94 }
95
96 /*-----------------------------------------------------------*/
97
98 }
99