Home | History | Annotate | Download | only in java_cup
      1 
      2 package java_cup;
      3 
      4 /** This class represents one row (corresponding to one machine state) of the
      5  *  parse action table.
      6  */
      7 public class parse_action_row {
      8 
      9   /*-----------------------------------------------------------*/
     10   /*--- Constructor(s) ----------------------------------------*/
     11   /*-----------------------------------------------------------*/
     12 
     13   /** Simple constructor.  Note: this should not be used until the number of
     14    *  terminals in the grammar has been established.
     15    */
     16   public parse_action_row()
     17     {
     18       /* make sure the size is set */
     19       if (_size <= 0 )  _size = terminal.number();
     20 
     21       /* allocate the array */
     22       under_term = new parse_action[size()];
     23 
     24       /* set each element to an error action */
     25       for (int i=0; i<_size; i++)
     26     under_term[i] = new parse_action();
     27     }
     28 
     29   /*-----------------------------------------------------------*/
     30   /*--- (Access to) Static (Class) Variables ------------------*/
     31   /*-----------------------------------------------------------*/
     32 
     33   /** Number of columns (terminals) in every row. */
     34   protected static int _size = 0;
     35 
     36   /** Number of columns (terminals) in every row. */
     37   public static int size() {return _size;}
     38 
     39   /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
     40 
     41   /** Table of reduction counts (reused by compute_default()). */
     42   protected static int reduction_count[] = null;
     43 
     44   /*-----------------------------------------------------------*/
     45   /*--- (Access to) Instance Variables ------------------------*/
     46   /*-----------------------------------------------------------*/
     47 
     48   /** Actual action entries for the row. */
     49   public parse_action under_term[];
     50 
     51   /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
     52 
     53   /** Default (reduce) action for this row.  -1 will represent default
     54    *  of error.
     55    */
     56   public int default_reduce;
     57 
     58   /*-----------------------------------------------------------*/
     59   /*--- General Methods ---------------------------------------*/
     60   /*-----------------------------------------------------------*/
     61 
     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).
     69    */
     70   public void compute_default()
     71     {
     72       int i, prod, max_prod, max_red;
     73 
     74       /* if we haven't allocated the count table, do so now */
     75       if (reduction_count == null)
     76     reduction_count = new int[production.number()];
     77 
     78       /* clear the reduction count table and maximums */
     79       for (i = 0; i < production.number(); i++)
     80     reduction_count[i] = 0;
     81       max_prod = -1;
     82       max_red = 0;
     83 
     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)
     87       {
     88         /* count the reduce in the proper production slot and keep the
     89            max up to date */
     90         prod = ((reduce_action)under_term[i]).reduce_with().index();
     91         reduction_count[prod]++;
     92         if (reduction_count[prod] > max_red)
     93           {
     94         max_red = reduction_count[prod];
     95         max_prod = prod;
     96           }
     97       }
     98 
     99        /* record the max as the default (or -1 for not found) */
    100        default_reduce = max_prod;
    101     }
    102 
    103   /*-----------------------------------------------------------*/
    104 
    105 };
    106 
    107