4 /** This class represents one row (corresponding to one machine state) of the
7 public class parse_action_row {
9 /*-----------------------------------------------------------*/
10 /*--- Constructor(s) ----------------------------------------*/
11 /*-----------------------------------------------------------*/
13 /** Simple constructor. Note: this should not be used until the number of
14 * terminals in the grammar has been established.
16 public parse_action_row()
18 /* make sure the size is set */
19 if (_size <= 0 ) _size = terminal.number();
21 /* allocate the array */
22 under_term = new parse_action[size()];
24 /* set each element to an error action */
25 for (int i=0; i<_size; i++)
26 under_term[i] = new parse_action();
29 /*-----------------------------------------------------------*/
30 /*--- (Access to) Static (Class) Variables ------------------*/
31 /*-----------------------------------------------------------*/
33 /** Number of columns (terminals) in every row. */
34 protected static int _size = 0;
36 /** Number of columns (terminals) in every row. */
37 public static int size() {return _size;}
39 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
41 /** Table of reduction counts (reused by compute_default()). */
42 protected static int reduction_count[] = null;
44 /*-----------------------------------------------------------*/
45 /*--- (Access to) Instance Variables ------------------------*/
46 /*-----------------------------------------------------------*/
48 /** Actual action entries for the row. */
49 public parse_action under_term[];
51 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
53 /** Default (reduce) action for this row. -1 will represent default
56 public int default_reduce;
58 /*-----------------------------------------------------------*/
59 /*--- General Methods ---------------------------------------*/
60 /*-----------------------------------------------------------*/
62 /** Compute the default (reduce) action for this row and store it in
63 * default_reduce. In the case of non-zero default we will have the
64 * effect of replacing all errors by that reduction. This may cause
65 * us to do erroneous reduces, but will never cause us to shift past
66 * the point of the error and never cause an incorrect parse. -1 will
67 * be used to encode the fact that no reduction can be used as a
68 * default (in which case error will be used).
70 public void compute_default()
72 int i, prod, max_prod, max_red;
74 /* if we haven't allocated the count table, do so now */
75 if (reduction_count == null)
76 reduction_count = new int[production.number()];
78 /* clear the reduction count table and maximums */
79 for (i = 0; i < production.number(); i++)
80 reduction_count[i] = 0;
84 /* walk down the row and look at the reduces */
85 for (i = 0; i < size(); i++)
86 if (under_term[i].kind() == parse_action.REDUCE)
88 /* count the reduce in the proper production slot and keep the
90 prod = ((reduce_action)under_term[i]).reduce_with().index();
91 reduction_count[prod]++;
92 if (reduction_count[prod] > max_red)
94 max_red = reduction_count[prod];
99 /* record the max as the default (or -1 for not found) */
100 default_reduce = max_prod;
103 /*-----------------------------------------------------------*/