Package org.antlr.analysis

Examples of org.antlr.analysis.NFAState


        return ruleRefState;
    }

    public NFAState targetStateOfTransition(Transition transition) {
        NFAState target;
        if(transition instanceof RuleClosureTransition) {
            RuleClosureTransition rct = (RuleClosureTransition)transition;
            target = rct.followState;
        } else {
            target = (NFAState)transition.target;
View Full Code Here


        }
        return state.getNumberOfTransitions()>1;
    }

    public boolean isAlternativeTransitionEndingAtSameState(NFAState state) {
        NFAState endState = endStateOfAlternative((NFAState)state.transition(0).target);

        for(int t=1; t<state.getNumberOfTransitions(); t++) {
            Transition transition = state.transition(t);
            NFAState newEndState = endStateOfAlternative((NFAState)transition.target);
            if(!endState.equals(newEndState))
                return false;
        }
        return true;
    }
View Full Code Here

    }

    public NFAState endStateOfAlternative(NFAState alt) {
        int endOfBlockStateNumber = alt.endOfBlockStateNumber;

        NFAState state = alt;
        while(state.stateNumber != endOfBlockStateNumber) {
            state = (NFAState)state.transition(0).target;
        }
        return state;
    }
View Full Code Here

        Grammar g = eg.getLexerGrammar();
        if(g == null) {
            throw new Exception("Cannot show tokens DFA because there is no lexer grammar");
        }
        Rule r = g.getRule(Grammar.ARTIFICIAL_TOKENS_RULENAME);
        NFAState s = (NFAState)r.startState.transition(0).target;
        DFA dfa = g.getLookaheadDFA(s.getDecisionNumber());

        DOTGenerator dg = new DOTGenerator(g);
        dg.setArrowheadType("none");
        dg.setRankdir("LR");    // Left-to-right
        return dg.getDOT( dfa.startState );
View Full Code Here

                    // More than one transition candidates found. Look if any of these
                    // transitions's target state correspond to the next path state to avoid
                    // missing a transition: this can happen if a transition is a subset of
                    // the others (the next state of the path can return no transition at all)
                    if(getPathIndex() +1 < path.size()) {
                        NFAState nextPathState = (NFAState) path.get(getPathIndex() +1);
                        for (FATransition t : candidateTransitions) {
                            if (t.target.stateNumber == nextPathState.stateNumber) {
                                pathIndex = getPathIndex() + 1;    // always points to the next element after the transition
                                return t;
                            }
View Full Code Here

         * The problem here is to use the information stored in the transition of the
         * graphical representation to figure out exactly which graphical node corresponds
         * to the path.
         */

        NFAState state;
        GNode node;
        NFAState nextState = null;
        GNode nextNode = null;
        for(pathIndex = 0; getPathIndex() < path.size(); pathIndex = getPathIndex() + 1) {
            if(getPathIndex() == 0) {
                nextState = (NFAState)path.get(getPathIndex());
                nextNode = findNodeForStateNumber(nextState.stateNumber);
View Full Code Here

    }

    public boolean containsAtLeastOneState(List states) {
        for (GNode node : nodes) {
            for (Object state1 : states) {
                NFAState state = (NFAState) state1;
                if (node.containsStateNumber(state.stateNumber))
                    return true;
            }
        }
        return false;
View Full Code Here

  /** an experimental method to generate random phrases for a given
   *  grammar given a start rule.  Return a list of token types.
   */
  protected static void randomPhrase(Grammar g, List<Integer> tokenTypes, String startRule) {
    NFAState state = g.getRuleStartState(startRule);
    NFAState stopState = g.getRuleStopState(startRule);

    Stack<NFAState> ruleInvocationStack = new Stack<NFAState>();
    while ( true ) {
      if ( state==stopState && ruleInvocationStack.isEmpty() ) {
        break;
      }
      if ( debug ) System.out.println("state "+state);
      if ( state.getNumberOfTransitions()==0 ) {
        if ( debug ) System.out.println("dangling state: "+state);
        return;
      }
      // end of rule node
      if ( state.isAcceptState() ) {
        NFAState invokingState = ruleInvocationStack.pop();
        if ( debug ) System.out.println("pop invoking state "+invokingState);
        //System.out.println("leave "+state.enclosingRule.name);
        RuleClosureTransition invokingTransition =
          (RuleClosureTransition)invokingState.transition[0];
        // move to node after state that invoked this rule
        state = invokingTransition.followState;
        continue;
      }
      if ( state.getNumberOfTransitions()==1 ) {
        // no branching, just take this path
        Transition t0 = state.transition[0];
        if ( t0 instanceof RuleClosureTransition ) {
          ruleInvocationStack.push(state);
          if ( debug ) System.out.println("push state "+state);
          //System.out.println("call "+((RuleClosureTransition)t0).rule.name);
          //System.out.println("stack depth="+ruleInvocationStack.size());
        }
        else if ( t0.label.isSet() || t0.label.isAtom() ) {
          tokenTypes.add( getTokenType(t0.label) );
        }
        state = (NFAState)t0.target;
        continue;
      }

      int decisionNumber = state.getDecisionNumber();
      if ( decisionNumber==0 ) {
        System.out.println("weird: no decision number but a choice node");
        continue;
      }
      // decision point, pick ith alternative randomly
      int n = g.getNumberOfAltsForDecisionNFA(state);
      int randomAlt = random.nextInt(n) + 1;
      if ( debug ) System.out.println("randomAlt="+randomAlt);
      NFAState altStartState =
        g.getNFAStateForAltOfDecision(state, randomAlt);
      Transition t = altStartState.transition[0];
      state = (NFAState)t.target;
    }
  }
View Full Code Here

      Set<Integer> disabledAlts = probe.getDisabledAlternatives(problemState);
      for (Integer altI : disabledAlts) {
        String tokenName =
          probe.getTokenNameForTokensRuleAlt(altI);
        // reset the line/col to the token definition (pick last one)
        NFAState ruleStart =
          probe.dfa.nfa.grammar.getRuleStartState(tokenName);
        line = ruleStart.associatedASTNode.getLine();
        column = ruleStart.associatedASTNode.getCharPositionInLine();
        st.add("disabled", tokenName);
      }
    }
    else {
      st.add("disabled", probe.getDisabledAlternatives(problemState));
    }

    List<Integer> nondetAlts = probe.getNonDeterministicAltsForState(problemState);
    NFAState nfaStart = probe.dfa.getNFADecisionStartState();
    // all state paths have to begin with same NFA state
    int firstAlt = 0;
    if ( nondetAlts!=null ) {
      for (Integer displayAltI : nondetAlts) {
        if ( DecisionProbe.verbose ) {
          int tracePathAlt =
            nfaStart.translateDisplayAltToWalkAlt(displayAltI);
          if ( firstAlt == 0 ) {
            firstAlt = tracePathAlt;
          }
          List<? extends NFAState> path =
            probe.getNFAPathStatesForAlt(firstAlt,
View Full Code Here

    factory = new NFAFactory(nfa);

    Collection<Rule> rules = getRules();
    for (Rule r : rules) {
      String ruleName = r.name;
      NFAState ruleBeginState = factory.newState();
      ruleBeginState.setDescription("rule "+ruleName+" start");
      ruleBeginState.enclosingRule = r;
      r.startState = ruleBeginState;
      NFAState ruleEndState = factory.newState();
      ruleEndState.setDescription("rule "+ruleName+" end");
      ruleEndState.setAcceptState(true);
      ruleEndState.enclosingRule = r;
      r.stopState = ruleEndState;
    }
  }
View Full Code Here

TOP

Related Classes of org.antlr.analysis.NFAState

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.