001: package java_cup;
002:
003: /** This class represents one row (corresponding to one machine state) of the
004: * parse action table.
005: */
006: public class parse_action_row {
007:
008: /*-----------------------------------------------------------*/
009: /*--- Constructor(s) ----------------------------------------*/
010: /*-----------------------------------------------------------*/
011:
012: /** Simple constructor. Note: this should not be used until the number of
013: * terminals in the grammar has been established.
014: */
015: public parse_action_row() {
016: /* make sure the size is set */
017: if (_size <= 0)
018: _size = terminal.number();
019:
020: /* allocate the array */
021: under_term = new parse_action[size()];
022:
023: /* set each element to an error action */
024: for (int i = 0; i < _size; i++)
025: under_term[i] = new parse_action();
026: }
027:
028: /*-----------------------------------------------------------*/
029: /*--- (Access to) Static (Class) Variables ------------------*/
030: /*-----------------------------------------------------------*/
031:
032: /** Number of columns (terminals) in every row. */
033: protected static int _size = 0;
034:
035: /** Number of columns (terminals) in every row. */
036: public static int size() {
037: return _size;
038: }
039:
040: //Hm Added clear to clear all static fields
041: public static void clear() {
042: _size = 0;
043: reduction_count = null;
044: }
045:
046: /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
047:
048: /** Table of reduction counts (reused by compute_default()). */
049: protected static int reduction_count[] = null;
050:
051: /*-----------------------------------------------------------*/
052: /*--- (Access to) Instance Variables ------------------------*/
053: /*-----------------------------------------------------------*/
054:
055: /** Actual action entries for the row. */
056: public parse_action under_term[];
057:
058: /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
059:
060: /** Default (reduce) action for this row. -1 will represent default
061: * of error.
062: */
063: public int default_reduce;
064:
065: /*-----------------------------------------------------------*/
066: /*--- General Methods ---------------------------------------*/
067: /*-----------------------------------------------------------*/
068:
069: /** Compute the default (reduce) action for this row and store it in
070: * default_reduce. In the case of non-zero default we will have the
071: * effect of replacing all errors by that reduction. This may cause
072: * us to do erroneous reduces, but will never cause us to shift past
073: * the point of the error and never cause an incorrect parse. -1 will
074: * be used to encode the fact that no reduction can be used as a
075: * default (in which case error will be used).
076: */
077: public void compute_default() {
078: int i, prod, max_prod, max_red;
079:
080: /* if we haven't allocated the count table, do so now */
081: if (reduction_count == null)
082: reduction_count = new int[production.number()];
083:
084: /* clear the reduction count table and maximums */
085: for (i = 0; i < production.number(); i++)
086: reduction_count[i] = 0;
087: max_prod = -1;
088: max_red = 0;
089:
090: /* walk down the row and look at the reduces */
091: for (i = 0; i < size(); i++)
092: if (under_term[i].kind() == parse_action.REDUCE) {
093: /* count the reduce in the proper production slot and keep the
094: max up to date */
095: prod = ((reduce_action) under_term[i]).reduce_with()
096: .index();
097: reduction_count[prod]++;
098: if (reduction_count[prod] > max_red) {
099: max_red = reduction_count[prod];
100: max_prod = prod;
101: }
102: }
103:
104: /* record the max as the default (or -1 for not found) */
105: default_reduce = max_prod;
106: }
107:
108: /*-----------------------------------------------------------*/
109:
110: }
|