1. package com.sun.java_cup.internal.runtime;
  2. import java.util.Stack;
  3. /** This class implements a temporary or "virtual" parse stack that
  4. * replaces the top portion of the actual parse stack (the part that
  5. * has been changed by some set of operations) while maintaining its
  6. * original contents. This data structure is used when the parse needs
  7. * to "parse ahead" to determine if a given error recovery attempt will
  8. * allow the parse to continue far enough to consider it successful. Once
  9. * success or failure of parse ahead is determined the system then
  10. * reverts to the original parse stack (which has not actually been
  11. * modified). Since parse ahead does not execute actions, only parse
  12. * state is maintained on the virtual stack, not full Symbol objects.
  13. *
  14. * @see com.sun.java_cup.internal.runtime.lr_parser
  15. * @version last updated: 7/3/96
  16. * @author Frank Flannery
  17. */
  18. public class virtual_parse_stack {
  19. /*-----------------------------------------------------------*/
  20. /*--- Constructor(s) ----------------------------------------*/
  21. /*-----------------------------------------------------------*/
  22. /** Constructor to build a virtual stack out of a real stack. */
  23. public virtual_parse_stack(Stack shadowing_stack) throws java.lang.Exception
  24. {
  25. /* sanity check */
  26. if (shadowing_stack == null)
  27. throw new Exception(
  28. "Internal parser error: attempt to create null virtual stack");
  29. /* set up our internals */
  30. real_stack = shadowing_stack;
  31. vstack = new Stack();
  32. real_next = 0;
  33. /* get one element onto the virtual portion of the stack */
  34. get_from_real();
  35. }
  36. /*-----------------------------------------------------------*/
  37. /*--- (Access to) Instance Variables ------------------------*/
  38. /*-----------------------------------------------------------*/
  39. /** The real stack that we shadow. This is accessed when we move off
  40. * the bottom of the virtual portion of the stack, but is always left
  41. * unmodified.
  42. */
  43. protected Stack real_stack;
  44. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  45. /** Top of stack indicator for where we leave off in the real stack.
  46. * This is measured from top of stack, so 0 would indicate that no
  47. * elements have been "moved" from the real to virtual stack.
  48. */
  49. protected int real_next;
  50. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  51. /** The virtual top portion of the stack. This stack contains Integer
  52. * objects with state numbers. This stack shadows the top portion
  53. * of the real stack within the area that has been modified (via operations
  54. * on the virtual stack). When this portion of the stack becomes empty we
  55. * transfer elements from the underlying stack onto this stack.
  56. */
  57. protected Stack vstack;
  58. /*-----------------------------------------------------------*/
  59. /*--- General Methods ---------------------------------------*/
  60. /*-----------------------------------------------------------*/
  61. /** Transfer an element from the real to the virtual stack. This assumes
  62. * that the virtual stack is currently empty.
  63. */
  64. protected void get_from_real()
  65. {
  66. Symbol stack_sym;
  67. /* don't transfer if the real stack is empty */
  68. if (real_next >= real_stack.size()) return;
  69. /* get a copy of the first Symbol we have not transfered */
  70. stack_sym = (Symbol)real_stack.elementAt(real_stack.size()-1-real_next);
  71. /* record the transfer */
  72. real_next++;
  73. /* put the state number from the Symbol onto the virtual stack */
  74. vstack.push(new Integer(stack_sym.parse_state));
  75. }
  76. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  77. /** Indicate whether the stack is empty. */
  78. public boolean empty()
  79. {
  80. /* if vstack is empty then we were unable to transfer onto it and
  81. the whole thing is empty. */
  82. return vstack.empty();
  83. }
  84. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  85. /** Return value on the top of the stack (without popping it). */
  86. public int top() throws java.lang.Exception
  87. {
  88. if (vstack.empty())
  89. throw new Exception(
  90. "Internal parser error: top() called on empty virtual stack");
  91. return ((Integer)vstack.peek()).intValue();
  92. }
  93. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  94. /** Pop the stack. */
  95. public void pop() throws java.lang.Exception
  96. {
  97. if (vstack.empty())
  98. throw new Exception(
  99. "Internal parser error: pop from empty virtual stack");
  100. /* pop it */
  101. vstack.pop();
  102. /* if we are now empty transfer an element (if there is one) */
  103. if (vstack.empty())
  104. get_from_real();
  105. }
  106. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  107. /** Push a state number onto the stack. */
  108. public void push(int state_num)
  109. {
  110. vstack.push(new Integer(state_num));
  111. }
  112. /*-----------------------------------------------------------*/
  113. }