Esempio n. 1
0
  public ParserState(int startState, TokenStream tokStream) {
    reallocateStateStack();
    stateStackTop = 0;
    stateStack[0] = startState;

    //
    // The tuple tokens will eventually contain the sequence
    // of tokens that resulted in a successful parse. We leave
    // it up to the "Stream" implementer to define the predecessor
    // of the first token as he sees fit.
    //
    tokStream.reset(); // Position at first token.
    tokens = new IntTuple(tokStream.getStreamLength());
    tokens.add(tokStream.getPrevious(tokStream.peek()));
  }
Esempio n. 2
0
  private boolean repairable(int error_token) {
    //
    // Allocate configuration stack.
    //
    ConfigurationStack configuration_stack = new ConfigurationStack(prs);

    //
    // 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 start_token = tokStream.peek();
    int final_token = tokStream.getStreamLength(); // unreachable
    int curtok = 0;
    int current_kind = ERROR_SYMBOL;
    int act = tAction(state.stateStack[state.stateStackTop], current_kind);

    for (; ; ) {
      if (act <= NUM_RULES) {
        state.stateStackTop--;
        act = process_repair_reductions(act);
      } else if (act > ERROR_ACTION) {
        curtok = tokStream.getToken();
        if (curtok > final_token) return true;
        current_kind = tokStream.getKind(curtok);
        act = process_repair_reductions(act - ERROR_ACTION);
      } else if (act < ACCEPT_ACTION) {
        curtok = tokStream.getToken();
        if (curtok > final_token) return true;
        current_kind = tokStream.getKind(curtok);
      } else if (act == ERROR_ACTION) {
        ConfigurationElement configuration = configuration_stack.pop();
        if (configuration == null) act = ERROR_ACTION;
        else {
          state.stateStackTop = configuration.stack_top;
          configuration.retrieveStack(state.stateStack);
          act = configuration.act;
          curtok = configuration.curtok;
          if (curtok == 0) {
            current_kind = ERROR_SYMBOL;
            tokStream.reset(start_token);
          } else {
            current_kind = tokStream.getKind(curtok);
            tokStream.reset(tokStream.getNext(curtok));
          }
          continue;
        }
        break;
      } else if (act > ACCEPT_ACTION) {
        if (configuration_stack.findConfiguration(state.stateStack, state.stateStackTop, curtok))
          act = ERROR_ACTION;
        else {
          configuration_stack.push(state.stateStack, state.stateStackTop, act + 1, curtok, 0);
          act = prs.baseAction(act);
        }
        continue;
      } else break; // assert(act == ACCEPT_ACTION);
      try {
        //
        // We consider a configuration to be acceptable for recovery
        // if we are able to consume enough symbols in the remaining
        // tokens to reach another potential recovery point past the
        // original error token.
        //
        if ((curtok > error_token) && (final_token == tokStream.getStreamLength())) {
          //
          // If the ERROR_SYMBOL is a valid Action Adjunct in the
          // state
          // "act" then we set the terminating token as the successor
          // of
          // the current token. I.e., we have to be able to parse at
          // least
          // two tokens past the re-synch point before we claim
          // victory.
          //
          if (recoverableState(act)) final_token = skipTokens ? curtok : tokStream.getNext(curtok);
        }

        state.stateStack[++state.stateStackTop] = act;
      } catch (IndexOutOfBoundsException e) {
        state.reallocateStateStack();
        state.stateStack[state.stateStackTop] = act;
      }

      act = tAction(act, current_kind);
    }

    //
    // If we can reach the end of the input successfully, we claim victory.
    //
    return (act == ACCEPT_ACTION);
  }
Esempio n. 3
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);
  }