parse method
Implementation
int parse(List<int> sym, int index) {
// assert(sym.length == prs.getMaxLa());
//
// First, we save the current length of the action tuple, in
// case an error is encountered and we need to restore the
// original configuration.
//
// Next, we declara and initialize the variable pos which will
// be used to indicate the highest useful position in stateStack
// as we are simulating the actions induced by the next k input
// terminals in sym.
//
// The location stack will be used here as a temporary stack
// to simulate these actions. We initialize its first useful
// offset here.
//
var save_action_length = action!.size(),
pos = stateStackTop,
location_top = stateStackTop - 1;
//
// When a reduce action is encountered, we compute all REDUCE
// and associated goto actions induced by the current token.
// Eventually, a SHIFT, SHIFT-REDUCE, ACCEPT or ERROR action is
// computed...
//
for (currentAction = tAction(stateStack[stateStackTop], sym, index);
currentAction <= NUM_RULES;
currentAction = tAction(currentAction, sym, index)) {
action!.add(currentAction);
do {
location_top -= (prs.rhs(currentAction) - 1);
var state = (location_top > pos
? locationStack[location_top]
: stateStack[location_top]);
currentAction = prs.ntAction(state, prs.lhs(currentAction));
} while (currentAction <= NUM_RULES);
//
// ... Update the maximum useful position of the
// stateSTACK, push goto state into stack, and
// continue by compute next action on current symbol
// and reentering the loop...
//
pos = pos < location_top ? pos : location_top;
try {
locationStack[location_top + 1] = currentAction;
} on RangeError {
reallocateStacks();
locationStack[location_top + 1] = currentAction;
}
}
//
// At this point, we have a shift, shift-reduce, accept or error
// action. stateSTACK contains the configuration of the state stack
// prior to executing any action on the currenttoken. locationStack
// contains the configuration of the state stack after executing all
// reduce actions induced by the current token. The variable pos
// indicates the highest position in the stateSTACK that is still
// useful after the reductions are executed.
//
if (currentAction > ERROR_ACTION || // SHIFT-REDUCE action ?
currentAction < ACCEPT_ACTION) // SHIFT action ?
{
action!.add(currentAction);
//
// If no error was detected, update the state stack with
// the info that was temporarily computed in the locationStack.
//
stateStackTop = location_top + 1;
for (var i = pos + 1; i <= stateStackTop; i++) {
stateStack[i] = locationStack[i];
}
//
// If we have a shift-reduce, process it as well as
// the goto-reduce actions that follow it.
//
if (currentAction > ERROR_ACTION) {
currentAction -= ERROR_ACTION;
do {
stateStackTop -= (prs.rhs(currentAction) - 1);
currentAction =
prs.ntAction(stateStack[stateStackTop], prs.lhs(currentAction));
} while (currentAction <= NUM_RULES);
}
//
// Process the transition - either a shift action of
// if we started out with a shift-reduce, the GOTO
// action that follows it.
//
try {
stateStack[++stateStackTop] = currentAction;
} on RangeError {
reallocateStacks();
stateStack[stateStackTop] = currentAction;
}
} else if (currentAction == ERROR_ACTION) {
action!.reset(save_action_length);
} // restore original action state.
return currentAction;
}