예제 #1
0
  private void backtrackParseUpToError(int initial_token, int error_token) {
    //
    // Allocate configuration stack.
    //
    state.configurationStack = new ConfigurationStack(prs);
    state.trialActionStack = new LinkedList<Integer>();
    state.trialActionStack.add(Integer.valueOf(state.trialActionCount));

    //
    // Keep parsing until we successfully reach the end of file or
    // an error is encountered. The list of actions executed will
    // be stored in the "action" tuple.
    //

    // tokStream.reset(initial_token);
    int start_token = tokStream.peek();
    state.curtok = (initial_token > 0 ? initial_token : tokStream.getToken());
    int current_kind = tokStream.getKind(state.curtok);
    state.act = tAction(state.stateStack[state.stateStackTop], current_kind);

    state.tokens.add(state.curtok);
    state.locationStack[state.stateStackTop] = state.tokens.size();
    state.actionStack[state.stateStackTop] = state.actionCount;
    state.undoStack[state.stateStackTop] = state.trialActionCount;

    for (; ; ) {
      //
      // if the parser needs to stop processing,
      // it may do so here.
      //
      if (monitor != null && monitor.isCancelled()) return;

      state.parserLocationStack[state.stateStackTop] = state.curtok;

      if (state.act <= NUM_RULES) {
        state.actionCount++;
        state.stateStackTop--;
        state.act = process_backtrack_reductions(state.act);
      } else if (state.act > ERROR_ACTION) {
        state.actionCount++;
        state.lastToken = state.curtok;
        state.curtok = tokStream.getToken();
        current_kind = tokStream.getKind(state.curtok);
        state.tokens.add(state.curtok);
        state.act = process_backtrack_reductions(state.act - ERROR_ACTION);
      } else if (state.act < ACCEPT_ACTION) {
        state.actionCount++;
        state.lastToken = state.curtok;
        state.curtok = tokStream.getToken();
        current_kind = tokStream.getKind(state.curtok);
        state.tokens.add(state.curtok);
      } else if (state.act == ERROR_ACTION) {
        if (state.curtok != error_token) {
          ConfigurationElement configuration = state.configurationStack.pop();
          if (configuration == null) state.act = ERROR_ACTION;
          else {
            boolean shouldPop = prs.baseAction(configuration.conflict_index) == 0;
            undoActions(shouldPop);
            state.actionCount = configuration.action_length;
            state.act = configuration.act;
            int next_token_index = configuration.curtok;
            state.tokens.reset(next_token_index);
            state.curtok = state.tokens.get(next_token_index - 1);
            current_kind = tokStream.getKind(state.curtok);
            tokStream.reset(
                state.curtok == initial_token ? start_token : tokStream.getNext(state.curtok));
            state.stateStackTop = configuration.stack_top;
            configuration.retrieveStack(state.stateStack);
            state.locationStack[state.stateStackTop] = state.tokens.size();
            state.actionStack[state.stateStackTop] = state.actionCount;
            state.undoStack[state.stateStackTop] = state.trialActionCount;
            continue;
          }
        }
        break;
      } else if (state.act > ACCEPT_ACTION) {
        if (state.configurationStack.findConfiguration(
            state.stateStack, state.stateStackTop, state.tokens.size())) state.act = ERROR_ACTION;
        else {
          state.configurationStack.push(
              state.stateStack,
              state.stateStackTop,
              state.act + 1,
              state.tokens.size(),
              state.actionCount);
          state.trialActionStack.add(Integer.valueOf(state.trialActionCount));
          state.act = prs.baseAction(state.act);
        }
        continue;
      } else break; // assert(act == ACCEPT_ACTION);

      state.stateStack[++state.stateStackTop] = state.act; // no need
      // to check
      // if out of
      // bounds
      state.locationStack[state.stateStackTop] = state.tokens.size();
      state.actionStack[state.stateStackTop] = state.actionCount;
      state.undoStack[state.stateStackTop] = state.trialActionCount;
      state.act = tAction(state.act, current_kind);
    }

    // assert(curtok == error_token);

    return;
  }
예제 #2
0
  //
  // Parse the input until either the parse completes successfully or
  // an error is encountered. This function returns an integer that
  // represents the last action that was executed by the parser. If
  // the parse was succesful, then the tuple "action" contains the
  // successful sequence of actions that was executed.
  //
  private int backtrackParse(int initial_token) {
    //
    // Allocate configuration stack.
    //
    state.configurationStack = new ConfigurationStack(prs);
    state.trialActionStack = new LinkedList<Integer>();
    state.trialActionStack.add(Integer.valueOf(state.trialActionCount));

    //
    // Keep parsing until we successfully reach the end of file or
    // an error is encountered. The list of actions executed will
    // be stored in the "action" tuple.
    //
    int error_token = 0;
    int maxStackTop = state.stateStackTop;
    int start_token = tokStream.peek();
    state.curtok = (initial_token > 0 ? initial_token : tokStream.getToken());
    int current_kind = tokStream.getKind(state.curtok);
    state.act = tAction(state.stateStack[state.stateStackTop], current_kind);

    //
    // The main driver loop
    //
    for (; ; ) {
      //
      // if the parser needs to stop processing,
      // it may do so here.
      //
      if (monitor != null && monitor.isCancelled()) return 0;

      state.parserLocationStack[state.stateStackTop] = state.curtok;

      if (state.act <= NUM_RULES) {
        state.actionCount++;
        state.stateStackTop--;
        state.act = process_backtrack_reductions(state.act);
      } else if (state.act > ERROR_ACTION) {
        state.actionCount++;
        state.lastToken = state.curtok;
        state.curtok = tokStream.getToken();
        current_kind = tokStream.getKind(state.curtok);
        state.act = process_backtrack_reductions(state.act - ERROR_ACTION);
      } else if (state.act < ACCEPT_ACTION) {
        state.actionCount++;
        state.lastToken = state.curtok;
        state.curtok = tokStream.getToken();
        current_kind = tokStream.getKind(state.curtok);
      }

      if (state.act == ERROR_ACTION) {
        error_token = (error_token > state.curtok ? error_token : state.curtok);

        ConfigurationElement configuration = state.configurationStack.pop();
        if (configuration == null) state.act = ERROR_ACTION;
        else {
          boolean shouldPop = prs.baseAction(configuration.conflict_index) == 0;
          undoActions(shouldPop);
          state.actionCount = configuration.action_length;
          state.act = configuration.act;
          state.curtok = configuration.curtok;
          current_kind = tokStream.getKind(state.curtok);
          tokStream.reset(
              state.curtok == initial_token ? start_token : tokStream.getNext(state.curtok));
          state.stateStackTop = configuration.stack_top;
          configuration.retrieveStack(state.stateStack);
          continue;
        }
        break;
      }
      if (state.act > ACCEPT_ACTION && state.act != ERROR_ACTION) {
        if (state.configurationStack.findConfiguration(
            state.stateStack, state.stateStackTop, state.curtok)) state.act = ERROR_ACTION;
        else {
          state.configurationStack.push(
              state.stateStack,
              state.stateStackTop,
              state.act + 1,
              state.curtok,
              state.actionCount);
          state.trialActionStack.add(Integer.valueOf(state.trialActionCount));
          state.act = prs.baseAction(state.act);
          maxStackTop = state.stateStackTop > maxStackTop ? state.stateStackTop : maxStackTop;
        }
        continue;
      } else if (state.act == ACCEPT_ACTION) {
        break;
      }
      try {
        state.stateStack[++state.stateStackTop] = state.act;
      } catch (IndexOutOfBoundsException e) {
        state.reallocateStateStack();
        state.stateStack[state.stateStackTop] = state.act;
      }

      state.act = tAction(state.act, current_kind);
    }

    // System.out.println("****Number of configurations: " +
    // configuration_stack.configurationSize());
    // System.out.println("****Number of elements in stack tree: " +
    // configuration_stack.numStateElements());
    // System.out.println("****Number of elements in stacks: " +
    // configuration_stack.stacksSize());
    // System.out.println("****Number of actions: " + action.size());
    // System.out.println("****Max Stack Size = " + maxStackTop);
    // System.out.flush();
    return (state.act == ERROR_ACTION ? error_token : 0);
  }