Search in sources :

Example 91 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by antlr.

the class DOTGenerator method getDOT.

public String getDOT(DFA dfa, boolean isLexer) {
    if (dfa.s0 == null)
        return null;
    ST dot = stlib.getInstanceOf("dfa");
    dot.add("name", "DFA" + dfa.decision);
    dot.add("startState", dfa.s0.stateNumber);
    // dot.add("useBox", Tool.internalOption_ShowATNConfigsInDFA);
    dot.add("rankdir", rankdir);
    // define stop states first; seems to be a bug in DOT where doublecircle
    for (DFAState d : dfa.states.keySet()) {
        if (!d.isAcceptState)
            continue;
        ST st = stlib.getInstanceOf("stopstate");
        st.add("name", "s" + d.stateNumber);
        st.add("label", getStateLabel(d));
        dot.add("states", st);
    }
    for (DFAState d : dfa.states.keySet()) {
        if (d.isAcceptState)
            continue;
        if (d.stateNumber == Integer.MAX_VALUE)
            continue;
        ST st = stlib.getInstanceOf("state");
        st.add("name", "s" + d.stateNumber);
        st.add("label", getStateLabel(d));
        dot.add("states", st);
    }
    for (DFAState d : dfa.states.keySet()) {
        if (d.edges != null) {
            for (int i = 0; i < d.edges.length; i++) {
                DFAState target = d.edges[i];
                if (target == null)
                    continue;
                if (target.stateNumber == Integer.MAX_VALUE)
                    continue;
                // we shift up for EOF as -1 for parser
                int ttype = i - 1;
                String label = String.valueOf(ttype);
                if (isLexer)
                    label = "'" + getEdgeLabel(new StringBuilder().appendCodePoint(i).toString()) + "'";
                else if (grammar != null)
                    label = grammar.getTokenDisplayName(ttype);
                ST st = stlib.getInstanceOf("edge");
                st.add("label", label);
                st.add("src", "s" + d.stateNumber);
                st.add("target", "s" + target.stateNumber);
                st.add("arrowhead", arrowhead);
                dot.add("edges", st);
            }
        }
    }
    String output = dot.render();
    return Utils.sortLinesInString(output);
}
Also used : ST(org.stringtemplate.v4.ST) DFAState(org.antlr.v4.runtime.dfa.DFAState)

Example 92 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by antlr.

the class TimeLexerSpeed method lex_legacy_grapheme_utf8.

public void lex_legacy_grapheme_utf8(String fileName, int n, boolean clearLexerDFACache) throws Exception {
    try (InputStream is = TimeLexerSpeed.class.getClassLoader().getResourceAsStream(PerfDir + "/" + fileName);
        InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isr)) {
        CharStream input = new ANTLRInputStream(br);
        graphemesLexer lexer = new graphemesLexer(input);
        double avg = tokenize(lexer, n, clearLexerDFACache);
        String currentMethodName = new Exception().getStackTrace()[0].getMethodName();
        if (output)
            System.out.printf("%27s average time %5dus over %4d runs of %5d symbols from %s%s\n", currentMethodName, (int) avg, n, input.size(), fileName, clearLexerDFACache ? " DFA cleared" : "");
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) CharStream(org.antlr.v4.runtime.CharStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) IOException(java.io.IOException)

Example 93 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by antlr.

the class TimeLexerSpeed method tokenize.

public double tokenize(Lexer lexer, int n, boolean clearLexerDFACache) {
    // always wipe the DFA before we begin tests so previous tests
    // don't affect this run!
    lexer.getInterpreter().clearDFA();
    long[] times = new long[n];
    for (int i = 0; i < n; i++) {
        lexer.reset();
        if (clearLexerDFACache) {
            lexer.getInterpreter().clearDFA();
        }
        long start = System.nanoTime();
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        // lex whole file.
        tokens.fill();
        // int size = lexer.getInputStream().size();
        long stop = System.nanoTime();
        times[i] = (stop - start) / 1000;
    // if ( output ) System.out.printf("Tokenized %d char in %dus\n", size, times[i]);
    }
    Arrays.sort(times);
    // drop highest 20% of times
    times = Arrays.copyOfRange(times, 0, times.length - (int) (n * .2));
    return avg(times);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream)

Example 94 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by antlr.

the class Parser method getDFAStrings.

/**
 * For debugging and other purposes.
 */
public List<String> getDFAStrings() {
    synchronized (_interp.decisionToDFA) {
        List<String> s = new ArrayList<String>();
        for (int d = 0; d < _interp.decisionToDFA.length; d++) {
            DFA dfa = _interp.decisionToDFA[d];
            s.add(dfa.toString(getVocabulary()));
        }
        return s;
    }
}
Also used : ArrayList(java.util.ArrayList) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 95 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by antlr.

the class Parser method dumpDFA.

/**
 * For debugging and other purposes.
 */
public void dumpDFA() {
    synchronized (_interp.decisionToDFA) {
        boolean seenOne = false;
        for (int d = 0; d < _interp.decisionToDFA.length; d++) {
            DFA dfa = _interp.decisionToDFA[d];
            if (!dfa.states.isEmpty()) {
                if (seenOne)
                    System.out.println();
                System.out.println("Decision " + dfa.decision + ":");
                System.out.print(dfa.toString(getVocabulary()));
                seenOne = true;
            }
        }
    }
}
Also used : DFA(org.antlr.v4.runtime.dfa.DFA)

Aggregations

DFA (org.antlr.v4.runtime.dfa.DFA)37 DFAState (org.antlr.v4.runtime.dfa.DFAState)28 Test (org.junit.Test)19 Grammar (org.antlr.v4.tool.Grammar)18 LexerGrammar (org.antlr.v4.tool.LexerGrammar)18 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)16 ArrayList (java.util.ArrayList)14 STGroupString (org.stringtemplate.v4.STGroupString)14 CharStream (org.antlr.v4.runtime.CharStream)13 IOException (java.io.IOException)10 IntegerList (org.antlr.v4.runtime.misc.IntegerList)10 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)9 InputStream (java.io.InputStream)8 BitSet (java.util.BitSet)8 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)7 ATN (org.antlr.v4.runtime.atn.ATN)7 Interval (org.antlr.v4.runtime.misc.Interval)7 NotNull (org.antlr.v4.runtime.misc.NotNull)7 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)6 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)5