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