Package org.antlr.analysis

Examples of org.antlr.analysis.NFAState


  /** 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 ruleInvocationStack = new Stack();
    while ( true ) {
      if ( state==stopState && ruleInvocationStack.size()==0 ) {
        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 = (NFAState)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


      for (Iterator it = disabledAlts.iterator(); it.hasNext();) {
        Integer altI = (Integer) it.next();
        String tokenName =
          probe.getTokenNameForTokensRuleAlt(altI.intValue());
        // 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 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 (Iterator iter = nondetAlts.iterator(); iter.hasNext();) {
        Integer displayAltI = (Integer) iter.next();
        if ( DecisionProbe.verbose ) {
          int tracePathAlt =
            nfaStart.translateDisplayAltToWalkAlt(displayAltI.intValue());
          if ( firstAlt == 0 ) {
            firstAlt = tracePathAlt;
          }
          List path =
            probe.getNFAPathStatesForAlt(firstAlt,
View Full Code Here

                             listOfRecursiveCycles);
        // we're back from visiting that rule
        visitedDuringRecursionCheck.remove(refRuleDef);
        // must keep going in this rule then
        if ( callReachedAcceptState ) {
          NFAState followingState =
            ((RuleClosureTransition) t0).followState;
          stateReachesAcceptState |=
            traceStatesLookingForLeftRecursion(followingState,
                               visitedStates,
                               listOfRecursiveCycles);
View Full Code Here

    grammar.leftRecursiveRules = new HashSet();
    List listOfRecursiveCycles = new ArrayList(); // List<Set<String(rule-name)>>
    for (int i = 0; i < grammar.ruleIndexToRuleList.size(); i++) {
      String ruleName = (String)grammar.ruleIndexToRuleList.elementAt(i);
      if ( ruleName!=null ) {
        NFAState s = grammar.getRuleStartState(ruleName);
        grammar.visitedDuringRecursionCheck = new HashSet();
        grammar.visitedDuringRecursionCheck.add(ruleName);
        Set visitedStates = new HashSet();
        traceStatesLookingForLeftRecursion(s, visitedStates, listOfRecursiveCycles);
      }
View Full Code Here

                             listOfRecursiveCycles);
        // we're back from visiting that rule
        grammar.visitedDuringRecursionCheck.remove(targetRuleName);
        // must keep going in this rule then
        if ( callReachedAcceptState ) {
          NFAState followingState =
            ((RuleClosureTransition)t0).getFollowState();
          stateReachesAcceptState |=
            traceStatesLookingForLeftRecursion(followingState,
                               visitedStates,
                               listOfRecursiveCycles);
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 tokenTypes, String startRule) {
    NFAState state = g.getRuleStartState(startRule);
    NFAState stopState = g.getRuleStopState(startRule);

    Stack ruleInvocationStack = new Stack();
    while ( true ) {
      if ( state==stopState && ruleInvocationStack.size()==0 ) {
        break;
      }
      //System.out.println("state "+state);
      if ( state.getNumberOfTransitions()==0 ) {
        //System.out.println("dangling state: "+state);
        return;
      }
      // end of rule node
      if ( state.isAcceptState() ) {
        NFAState invokingState = (NFAState)ruleInvocationStack.pop();
        // System.out.println("pop invoking state "+invokingState);
        RuleClosureTransition invokingTransition =
          (RuleClosureTransition)invokingState.transition(0);
        // move to node after state that invoked this rule
        state = invokingTransition.getFollowState();
        continue;
      }
      if ( state.getNumberOfTransitions()==1 ) {
        // no branching, just take this path
        Transition t0 = state.transition(0);
        if ( t0 instanceof RuleClosureTransition ) {
          ruleInvocationStack.push(state);
          // System.out.println("push state "+state);
          int ruleIndex = ((RuleClosureTransition)t0).getRuleIndex();
          //System.out.println("invoke "+g.getRuleName(ruleIndex));
        }
        else if ( !t0.label.isEpsilon() ) {
          tokenTypes.add( getTokenType(t0.label) );
          //System.out.println(t0.label.toString(g));
        }
        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;
      //System.out.println("randomAlt="+randomAlt);
      NFAState altStartState =
        g.getNFAStateForAltOfDecision(state, randomAlt);
      Transition t = altStartState.transition(0);
      /*
      start of a decision could never be a labeled transition
      if ( !t.label.isEpsilon() ) {
        tokenTypes.add( getTokenType(t.label) );
      }
View Full Code Here

      for (Iterator it = disabledAlts.iterator(); it.hasNext();) {
        Integer altI = (Integer) it.next();
        String tokenName =
          probe.getTokenNameForTokensRuleAlt(altI.intValue());
        // reset the line/col to the token definition (pick last one)
        NFAState ruleStart =
          probe.dfa.nfa.grammar.getRuleStartState(tokenName);
        line = ruleStart.getAssociatedASTNode().getLine();
        column = ruleStart.getAssociatedASTNode().getColumn();
        st.setAttribute("disabled", tokenName);
      }
    }
    else {
      st.setAttribute("disabled", probe.getDisabledAlternatives(problemState));
    }

    List 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 (Iterator iter = nondetAlts.iterator(); iter.hasNext();) {
        Integer displayAltI = (Integer) iter.next();
        if ( DecisionProbe.verbose ) {
          int tracePathAlt =
            nfaStart.translateDisplayAltToWalkAlt(probe.dfa,
                                displayAltI.intValue());
          if ( firstAlt == 0 ) {
            firstAlt = tracePathAlt;
          }
          List path =
View Full Code Here

      for (int i = 0; i < alts.size(); i++) {
        Integer altI = (Integer) alts.get(i);
        String tokenName =
          probe.getTokenNameForTokensRuleAlt(altI.intValue());
        // reset the line/col to the token definition
        NFAState ruleStart =
          probe.dfa.nfa.grammar.getRuleStartState(tokenName);
        line = ruleStart.getAssociatedASTNode().getLine();
        column = ruleStart.getAssociatedASTNode().getColumn();
        st.setAttribute("tokens", tokenName);
      }
    }
    else {
      // regular alt numbers, show the alts
View Full Code Here

                             listOfRecursiveCycles);
        // we're back from visiting that rule
        visitedDuringRecursionCheck.remove(refRuleDef);
        // must keep going in this rule then
        if ( callReachedAcceptState ) {
          NFAState followingState =
            ((RuleClosureTransition) t0).followState;
          stateReachesAcceptState |=
            traceStatesLookingForLeftRecursion(followingState,
                               visitedStates,
                               listOfRecursiveCycles);
View Full Code Here

                             listOfRecursiveCycles);
        // we're back from visiting that rule
        visitedDuringRecursionCheck.remove(refRuleDef);
        // must keep going in this rule then
        if ( callReachedAcceptState ) {
          NFAState followingState =
            ((RuleClosureTransition) t0).followState;
          stateReachesAcceptState |=
            traceStatesLookingForLeftRecursion(followingState,
                               visitedStates,
                               listOfRecursiveCycles);
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.