Search in sources :

Example 1 with NFAState

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

the class TokensDFA method getDOTString.

@Override
public String getDOTString() throws Exception {
    ANTLRGrammarEngine eg = window.getGrammarEngine().getANTLRGrammarEngine();
    eg.analyze();
    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");
    // Left-to-right
    dg.setRankdir("LR");
    return dg.getDOT(dfa.startState);
}
Also used : ANTLRGrammarEngine(org.antlr.works.grammar.antlr.ANTLRGrammarEngine) DOTGenerator(org.antlr.tool.DOTGenerator) NFAState(org.antlr.analysis.NFAState) Grammar(org.antlr.tool.Grammar) Rule(org.antlr.tool.Rule) DFA(org.antlr.analysis.DFA)

Example 2 with NFAState

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

the class ANTLRGrammarEngineImpl method computeRuleError.

private void computeRuleError(GrammarError error, GrammarNonDeterminismMessage message) {
    List nonDetAlts = message.probe.getNonDeterministicAltsForState(message.problemState);
    Set disabledAlts = message.probe.getDisabledAlternatives(message.problemState);
    int firstAlt = 0;
    for (Object nonDetAlt : nonDetAlts) {
        Integer displayAltI = (Integer) nonDetAlt;
        NFAState nfaStart = message.probe.dfa.getNFADecisionStartState();
        int tracePathAlt = nfaStart.translateDisplayAltToWalkAlt(displayAltI);
        if (firstAlt == 0)
            firstAlt = tracePathAlt;
        List path = message.probe.getNFAPathStatesForAlt(firstAlt, tracePathAlt, error.getLabels());
        error.addPath(path, disabledAlts.contains(displayAltI));
        error.addStates(path);
        // Find all rules enclosing each state (because a path can extend over multiple rules)
        for (Object aPath : path) {
            NFAState state = (NFAState) aPath;
            error.addRule(state.enclosingRule.name);
        }
    }
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) List(java.util.List) NFAState(org.antlr.analysis.NFAState)

Example 3 with NFAState

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

the class DecisionDFAEngine method getDecisionDFAItems.

public List<DecisionDFAItem> getDecisionDFAItems() {
    List<DecisionDFAItem> items = new ArrayList<DecisionDFAItem>();
    for (int lineIndex : decisionDFA.keySet()) {
        for (int columnIndex : decisionDFA.get(lineIndex)) {
            DFA dfa = getDFAAtPosition(lineIndex, columnIndex);
            if (dfa == null) {
                System.err.println("DFA is null for line " + lineIndex + " and column " + columnIndex);
                continue;
            }
            Grammar g = discoveredLexerGrammar;
            if (g != null) {
                Rule r = g.getRule(Grammar.ARTIFICIAL_TOKENS_RULENAME);
                NFAState s = (NFAState) r.startState.transition(0).target;
                if (s == null) {
                    System.err.println("NFAState s is null for rule " + r.name);
                    continue;
                }
                // Ignore tokens DFA
                if (dfa.getDecisionNumber() == s.getDecisionNumber())
                    continue;
            }
            Color c = new Color(0, 128, 64);
            String title = "DFA decision " + dfa.getDecisionNumber();
            String info = "";
            if (usesSemPreds.contains(dfa.getDecisionNumber())) {
                info += "uses semantic predicate";
                c = new Color(255, 220, 0);
            } else if (usesSynPreds.contains(dfa.getDecisionNumber())) {
                info += "uses syntactic predicate";
                c = new Color(255, 220, 0);
            }
            if (dfa.isCyclic()) {
                if (info.length() > 0)
                    info += ", ";
                info += "cyclic";
            }
            if (info.length() > 0)
                info += ", ";
            if (dfa.getNumberOfStates() != 0) {
                info += dfa.getNumberOfStates() + " states";
            } else {
                info += "<=" + dfa.getMaxStateNumber() + " states";
            }
            Point p = window.textEditor.getLineTextPositionsAtLineIndex(lineIndex - 1);
            if (p != null) {
                DecisionDFAItem item = new DecisionDFAItem(window);
                item.setAttributes(null, p.x + columnIndex - 1, p.x + columnIndex, lineIndex - 1, c, title + " (" + info + ")");
                item.shape = ATEOverlayManager.SHAPE_RECT;
                items.add(item);
            }
        }
    }
    return items;
}
Also used : NFAState(org.antlr.analysis.NFAState) Grammar(org.antlr.tool.Grammar) Rule(org.antlr.tool.Rule) DFA(org.antlr.analysis.DFA)

Example 4 with NFAState

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

the class FAFactory method targetStateOfTransition.

public NFAState targetStateOfTransition(Transition transition) {
    NFAState target;
    if (transition instanceof RuleClosureTransition) {
        RuleClosureTransition rct = (RuleClosureTransition) transition;
        target = rct.followState;
    } else {
        target = (NFAState) transition.target;
    }
    return target;
}
Also used : RuleClosureTransition(org.antlr.analysis.RuleClosureTransition) NFAState(org.antlr.analysis.NFAState)

Example 5 with NFAState

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

the class FAFactory method isAlternativeTransitionEndingAtSameState.

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;
}
Also used : 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