Search in sources :

Example 76 with DFA

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

the class TimeLexerSpeed method lex_new_java_utf8.

public void lex_new_java_utf8(int n, boolean clearLexerDFACache) throws Exception {
    ClassLoader loader = TimeLexerSpeed.class.getClassLoader();
    InputStream is = loader.getResourceAsStream(Parser_java_file);
    try {
        long size = getResourceSize(loader, Parser_java_file);
        CharStream input = CharStreams.fromStream(is, Charset.forName("UTF-8"), Parser_java_file, size);
        JavaLexer lexer = new JavaLexer(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%s\n", currentMethodName, (int) avg, n, input.size(), clearLexerDFACache ? " DFA cleared" : "");
    } finally {
        is.close();
    }
}
Also used : JavaLexer(org.antlr.v4.test.runtime.java.api.JavaLexer) InputStream(java.io.InputStream) CharStream(org.antlr.v4.runtime.CharStream) IOException(java.io.IOException)

Example 77 with DFA

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

the class ATN method clearDFA.

public final void clearDFA() {
    decisionToDFA = new DFA[decisionToState.size()];
    for (int i = 0; i < decisionToDFA.length; i++) {
        decisionToDFA[i] = new DFA(decisionToState.get(i), i);
    }
    modeToDFA = new DFA[modeToStartState.size()];
    for (int i = 0; i < modeToDFA.length; i++) {
        modeToDFA[i] = new DFA(modeToStartState.get(i));
    }
    contextCache.clear();
    LL1Table.clear();
}
Also used : DFA(org.antlr.v4.runtime.dfa.DFA)

Example 78 with DFA

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

the class ATN method defineMode.

public void defineMode(@NotNull String name, @NotNull TokensStartState s) {
    modeNameToStartState.put(name, s);
    modeToStartState.add(s);
    modeToDFA = Arrays.copyOf(modeToDFA, modeToStartState.size());
    modeToDFA[modeToDFA.length - 1] = new DFA(s);
    defineDecisionState(s);
}
Also used : DFA(org.antlr.v4.runtime.dfa.DFA)

Example 79 with DFA

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

the class TestATNParserPrediction method checkPredictedAlt.

/**
 * first check that the ATN predicts right alt.
 *  Then check adaptive prediction.
 */
public void checkPredictedAlt(LexerGrammar lg, Grammar g, int decision, String inputString, int expectedAlt) {
    Tool.internalOption_ShowATNConfigsInDFA = true;
    ATN lexatn = createATN(lg, true);
    LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn);
    IntegerList types = getTokenTypesViaATN(inputString, lexInterp);
    System.out.println(types);
    semanticProcess(lg);
    g.importVocab(lg);
    semanticProcess(g);
    ParserATNFactory f = new ParserATNFactory(g);
    ATN atn = f.createATN();
    DOTGenerator dot = new DOTGenerator(g);
    Rule r = g.getRule("a");
    if (r != null)
        System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    r = g.getRule("b");
    if (r != null)
        System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    r = g.getRule("e");
    if (r != null)
        System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    r = g.getRule("ifstat");
    if (r != null)
        System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    r = g.getRule("block");
    if (r != null)
        System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    // Check ATN prediction
    // ParserATNSimulator<Token> interp = new ParserATNSimulator<Token>(atn);
    TokenStream input = new IntTokenStream(types);
    ParserInterpreterForTesting interp = new ParserInterpreterForTesting(g, input);
    DecisionState startState = atn.decisionToState.get(decision);
    DFA dfa = new DFA(startState, decision);
    int alt = interp.adaptivePredict(input, decision, ParserRuleContext.emptyContext());
    System.out.println(dot.getDOT(dfa, false));
    assertEquals(expectedAlt, alt);
    // Check adaptive prediction
    input.seek(0);
    alt = interp.adaptivePredict(input, decision, null);
    assertEquals(expectedAlt, alt);
    // run 2x; first time creates DFA in atn
    input.seek(0);
    alt = interp.adaptivePredict(input, decision, null);
    assertEquals(expectedAlt, alt);
}
Also used : ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) DOTGenerator(org.antlr.v4.tool.DOTGenerator) TokenStream(org.antlr.v4.runtime.TokenStream) LexerATNSimulator(org.antlr.v4.runtime.atn.LexerATNSimulator) IntegerList(org.antlr.v4.runtime.misc.IntegerList) ATN(org.antlr.v4.runtime.atn.ATN) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) DecisionState(org.antlr.v4.runtime.atn.DecisionState) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 80 with DFA

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

the class TestATNParserPrediction method testRecursiveLeftPrefix.

@Test
public void testRecursiveLeftPrefix() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n" + "LP : '(' ;\n" + "RP : ')' ;\n" + "INT : '0'..'9'+ ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "tokens {A,B,C,LP,RP,INT}\n" + "a : e B | e C ;\n" + "e : LP e RP\n" + "  | INT\n" + "  ;");
    int decision = 0;
    checkPredictedAlt(lg, g, decision, "34b", 1);
    checkPredictedAlt(lg, g, decision, "34c", 2);
    checkPredictedAlt(lg, g, decision, "((34))b", 1);
    checkPredictedAlt(lg, g, decision, "((34))c", 2);
    // After matching these inputs for decision, what is DFA after each prediction?
    String[] inputs = { "34b", "34c", "((34))b", "((34))c" };
    String[] dfa = { "s0-INT->s1\n" + "s1-'b'->:s2=>1\n", "s0-INT->s1\n" + "s1-'b'->:s2=>1\n" + "s1-'c'->:s3=>2\n", "s0-'('->s4\n" + "s0-INT->s1\n" + "s1-'b'->:s2=>1\n" + "s1-'c'->:s3=>2\n" + "s4-'('->s5\n" + "s5-INT->s6\n" + "s6-')'->s7\n" + "s7-')'->s1\n", "s0-'('->s4\n" + "s0-INT->s1\n" + "s1-'b'->:s2=>1\n" + "s1-'c'->:s3=>2\n" + "s4-'('->s5\n" + "s5-INT->s6\n" + "s6-')'->s7\n" + "s7-')'->s1\n" };
    checkDFAConstruction(lg, g, decision, inputs, dfa);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

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