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