Home | History | Annotate | Download | only in java_cup
      1 
      2 package java_cup;
      3 
      4 
      5 /** This class represents the complete "reduce-goto" table of the parser.
      6  *  It has one row for each state in the parse machines, and a column for
      7  *  each terminal symbol.  Each entry contains a state number to shift to
      8  *  as the last step of a reduce.
      9  *
     10  * @see     java_cup.parse_reduce_row
     11  * @version last updated: 11/25/95
     12  * @author  Scott Hudson
     13  */
     14 public class parse_reduce_table {
     15 
     16   /*-----------------------------------------------------------*/
     17   /*--- Constructor(s) ----------------------------------------*/
     18   /*-----------------------------------------------------------*/
     19 
     20   /** Simple constructor.  Note: all terminals, non-terminals, and productions
     21    *  must already have been entered, and the viable prefix recognizer should
     22    *  have been constructed before this is called.
     23    */
     24   public parse_reduce_table()
     25     {
     26       /* determine how many states we are working with */
     27       _num_states = lalr_state.number();
     28 
     29       /* allocate the array and fill it in with empty rows */
     30       under_state = new parse_reduce_row[_num_states];
     31       for (int i=0; i<_num_states; i++)
     32     under_state[i] = new parse_reduce_row();
     33     }
     34 
     35 
     36   /*-----------------------------------------------------------*/
     37   /*--- (Access to) Instance Variables ------------------------*/
     38   /*-----------------------------------------------------------*/
     39 
     40   /** How many rows/states in the machine/table. */
     41   protected int _num_states;
     42 
     43   /** How many rows/states in the machine/table. */
     44   public int num_states() {return _num_states;}
     45 
     46   /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
     47 
     48   /** Actual array of rows, one per state */
     49   public  parse_reduce_row[] under_state;
     50 
     51   /*-----------------------------------------------------------*/
     52   /*--- General Methods ---------------------------------------*/
     53   /*-----------------------------------------------------------*/
     54 
     55   /** Convert to a string. */
     56   public String toString()
     57     {
     58       String result;
     59       lalr_state goto_st;
     60       int cnt;
     61 
     62       result = "-------- REDUCE_TABLE --------\n";
     63       for (int row = 0; row < num_states(); row++)
     64     {
     65       result += "From state #" + row + "\n";
     66       cnt = 0;
     67       for (int col = 0; col < under_state[row].size(); col++)
     68         {
     69           /* pull out the table entry */
     70           goto_st = under_state[row].under_non_term[col];
     71 
     72           /* if it has action in it, print it */
     73           if (goto_st != null)
     74         {
     75           result += col + ":";
     76           result += goto_st.index();
     77 
     78           /* end the line after the 3rd one */
     79           cnt++;
     80           if (cnt == 3)
     81             {
     82               result += "\n";
     83               cnt = 0;
     84             }
     85         }
     86         }
     87           /* finish the line if we haven't just done that */
     88       if (cnt != 0) result += "\n";
     89     }
     90       result += "-----------------------------";
     91 
     92       return result;
     93     }
     94 
     95   /*-----------------------------------------------------------*/
     96 
     97 };
     98 
     99