Search in sources :

Example 11 with NFAState

use of org.antlr.analysis.NFAState in project antlrworks by antlr.

the class VisualDrawing method threadProcessRule.

private void threadProcessRule() throws Exception {
    if (threadRule == null)
        return;
    String error = null;
    ErrorListener.getThreadInstance().setPrintToConsole(false);
    if (syntaxDiagramTab.getEngineGrammar().hasGrammar()) {
        NFAState startState = null;
        try {
            startState = syntaxDiagramTab.getEngineGrammar().getRuleStartState(threadRule.name);
        } catch (Exception e) {
        // ignore
        }
        if (startState == null) {
            error = "Cannot display rule \"" + threadRule + "\" because start state not found";
        }
    } else {
        error = "Cannot display rule \"" + threadRule + "\" because grammar could not be generated";
    }
    if (error != null) {
        syntaxDiagramTab.setPlaceholder(error);
        //visual.getConsole().println(error, Console.LEVEL_ERROR);
        return;
    }
    // Try to get the optimized graph from cache first. If the grammar didn't change (i.e. user
    // only moving cursor in the text zone), the speed-up can be important.
    createGraphsForRule(threadRule);
    threadLastProcessedRule = threadRule;
    refresh();
}
Also used : NFAState(org.antlr.analysis.NFAState)

Example 12 with NFAState

use of org.antlr.analysis.NFAState in project antlrworks by antlr.

the class FAFactory method buildRecursiveState.

public FAState buildRecursiveState(NFAState state, Set<NFAState> currentPath) {
    if (processedStates.get(state) != null) {
        FAState js = processedStates.get(state);
        if (currentPath.contains(state)) {
            // Set this temporary flag to indicate to the parent method that
            // the transition to be created has to be flagged as "loop".
            js.loop = true;
        }
        return js;
    }
    FAState js = new FAState(state);
    processedStates.put(state, js);
    currentPath.add(state);
    if (state.isAcceptState()) {
        // Stop as soon as we reach an accepted state
        return js;
    }
    for (int t = 0; t < state.getNumberOfTransitions(); t++) {
        FAState parentState = js;
        Transition transition = state.transition(t);
        NFAState target = (NFAState) transition.target;
        if (targetStateIsInAnotherRule(transition)) {
            target = targetStateOfTransition(transition);
            parentState = createRuleReferenceState(parentState, transition, null);
        }
        if (transition.isEpsilon()) {
            buildRecursiveSkipState(parentState, target, new HashSet<NFAState>(currentPath), new ArrayList<Integer>());
        } else {
            FAState targetState = buildRecursiveState(target, new HashSet<NFAState>(currentPath));
            if (targetState.loop) {
                // Handle "loop" transition by creating a "normal" transition and assigning a flag
                // to this transition so when drawing it, we can draw the arrow at the right place
                // (in the reverse direction)
                targetState.addTransition(new FATransition(transition.label.toString(g), parentState), true);
                targetState.loop = false;
            } else
                parentState.addTransition(new FATransition(transition.label.toString(g), targetState));
        }
    }
    return js;
}
Also used : NFAState(org.antlr.analysis.NFAState) Transition(org.antlr.analysis.Transition) RuleClosureTransition(org.antlr.analysis.RuleClosureTransition) NFAState(org.antlr.analysis.NFAState)

Example 13 with NFAState

use of org.antlr.analysis.NFAState in project antlrworks by antlr.

the class FAAnalysis method toString.

public String toString() {
    StringBuilder s = new StringBuilder();
    Iterator iterator = stateIncomingTransitionCount.keySet().iterator();
    while (iterator.hasNext()) {
        NFAState key = (NFAState) iterator.next();
        Integer count = (Integer) stateIncomingTransitionCount.get(key);
        s.append(key.stateNumber + " = " + count.intValue() + "\n");
    }
    return s.toString();
}
Also used : NFAState(org.antlr.analysis.NFAState)

Example 14 with NFAState

use of org.antlr.analysis.NFAState in project antlrworks by antlr.

the class FAFactory method endStateOfAlternative.

public NFAState endStateOfAlternative(NFAState alt) {
    int endOfBlockStateNumber = alt.endOfBlockStateNumber;
    NFAState state = alt;
    while (state.stateNumber != endOfBlockStateNumber) {
        state = (NFAState) state.transition(0).target;
    }
    return state;
}
Also used : NFAState(org.antlr.analysis.NFAState)

Example 15 with NFAState

use of org.antlr.analysis.NFAState in project antlrworks by antlr.

the class FAFactory method buildRecursiveSkipState.

/** This method is used to skip redundant state following epsilon transition.
     *
     */
public void buildRecursiveSkipState(FAState parentState, NFAState state, Set<NFAState> currentPath, List<Integer> skippedStates) {
    if (canBeSkipped(state)) {
        // If the state can be skipped, apply recursively the same method for each transition(s)
        // providing the parent state.
        // Record each skipped state. They will be added later to the transition that replace them
        Integer skippedState = state.stateNumber;
        skippedStates.add(skippedState);
        skippedStatesMap.put(skippedState, parentState);
        for (int t = 0; t < state.getNumberOfTransitions(); t++) {
            Transition transition = state.transition(t);
            if (targetStateIsInAnotherRule(transition)) {
                NFAState target = targetStateOfTransition(transition);
                FAState ruleRefState = createRuleReferenceState(parentState, transition, skippedStates);
                buildRecursiveSkipState(ruleRefState, target, currentPath, new ArrayList<Integer>(skippedStates));
            } else
                buildRecursiveSkipState(parentState, (NFAState) transition.target, currentPath, new ArrayList<Integer>(skippedStates));
        }
    } else {
        // The state cannot be skipped. Build the remaining of the NFA...
        FAState targetState = buildRecursiveState(state, currentPath);
        // (this is the simplification ;-))
        if (targetState.loop) {
            // See comment above (in the previous method)
            targetState.addTransition(new FATransition(parentState, skippedStates), true);
            targetState.loop = false;
        } else
            parentState.addTransition(new FATransition(targetState, skippedStates));
    }
}
Also used : NFAState(org.antlr.analysis.NFAState) Transition(org.antlr.analysis.Transition) RuleClosureTransition(org.antlr.analysis.RuleClosureTransition) NFAState(org.antlr.analysis.NFAState)

Aggregations

NFAState (org.antlr.analysis.NFAState)17 ArrayList (java.util.ArrayList)5 RuleClosureTransition (org.antlr.analysis.RuleClosureTransition)4 Transition (org.antlr.analysis.Transition)3 FAState (org.antlr.works.visualization.fa.FAState)3 FATransition (org.antlr.works.visualization.fa.FATransition)3 List (java.util.List)2 DFA (org.antlr.analysis.DFA)2 Grammar (org.antlr.tool.Grammar)2 Rule (org.antlr.tool.Rule)2 FAFactory (org.antlr.works.visualization.fa.FAFactory)2 GGraph (org.antlr.works.visualization.graphics.graph.GGraph)2 Set (java.util.Set)1 DOTGenerator (org.antlr.tool.DOTGenerator)1 ANTLRGrammarEngine (org.antlr.works.grammar.antlr.ANTLRGrammarEngine)1 GGraphGroup (org.antlr.works.visualization.graphics.graph.GGraphGroup)1 GPath (org.antlr.works.visualization.graphics.path.GPath)1 GPathElement (org.antlr.works.visualization.graphics.path.GPathElement)1 GNode (org.antlr.works.visualization.graphics.shape.GNode)1