Search in sources :

Example 31 with ParserATNFactory

use of org.antlr.v4.automata.ParserATNFactory 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 32 with ParserATNFactory

use of org.antlr.v4.automata.ParserATNFactory in project antlr4 by tunnelvisionlabs.

the class TestCodeGeneration method getEvalInfoForString.

public List<String> getEvalInfoForString(String grammarString, String pattern) throws RecognitionException {
    ErrorQueue equeue = new ErrorQueue();
    Grammar g = new Grammar(grammarString);
    List<String> evals = new ArrayList<String>();
    if (g.ast != null && !g.ast.hasErrors) {
        SemanticPipeline sem = new SemanticPipeline(g);
        sem.process();
        ATNFactory factory = new ParserATNFactory(g);
        if (g.isLexer())
            factory = new LexerATNFactory((LexerGrammar) g);
        g.atn = factory.createATN();
        CodeGenerator gen = new CodeGenerator(g);
        ST outputFileST = gen.generateParser();
        // STViz viz = outputFileST.inspect();
        // try {
        // viz.waitForClose();
        // }
        // catch (Exception e) {
        // e.printStackTrace();
        // }
        boolean debug = false;
        DebugInterpreter interp = new DebugInterpreter(outputFileST.groupThatCreatedThisInstance, outputFileST.impl.nativeGroup.errMgr, debug);
        InstanceScope scope = new InstanceScope(null, outputFileST);
        StringWriter sw = new StringWriter();
        AutoIndentWriter out = new AutoIndentWriter(sw);
        interp.exec(out, scope);
        for (String e : interp.evals) {
            if (e.contains(pattern)) {
                evals.add(e);
            }
        }
    }
    if (equeue.size() > 0) {
        System.err.println(equeue.toString());
    }
    return evals;
}
Also used : SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) ST(org.stringtemplate.v4.ST) ArrayList(java.util.ArrayList) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CodeGenerator(org.antlr.v4.codegen.CodeGenerator) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) AutoIndentWriter(org.stringtemplate.v4.AutoIndentWriter) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) StringWriter(java.io.StringWriter) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) InstanceScope(org.stringtemplate.v4.InstanceScope)

Example 33 with ParserATNFactory

use of org.antlr.v4.automata.ParserATNFactory in project antlr4 by antlr.

the class TestATNConstruction method checkTokensRule.

void checkTokensRule(LexerGrammar g, String modeName, String expecting) {
    // }
    if (modeName == null)
        modeName = "DEFAULT_MODE";
    if (g.modes.get(modeName) == null) {
        System.err.println("no such mode " + modeName);
        return;
    }
    ParserATNFactory f = new LexerATNFactory(g);
    ATN nfa = f.createATN();
    ATNState startState = nfa.modeNameToStartState.get(modeName);
    ATNPrinter serializer = new ATNPrinter(g, startState);
    String result = serializer.asString();
    // System.out.print(result);
    assertEquals(expecting, result);
}
Also used : ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNPrinter(org.antlr.v4.automata.ATNPrinter) ATN(org.antlr.v4.runtime.atn.ATN) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) ATNState(org.antlr.v4.runtime.atn.ATNState)

Aggregations

ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)33 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)22 ATN (org.antlr.v4.runtime.atn.ATN)19 LexerGrammar (org.antlr.v4.tool.LexerGrammar)17 ATNFactory (org.antlr.v4.automata.ATNFactory)12 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)12 STGroupString (org.stringtemplate.v4.STGroupString)12 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)10 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)9 ATNState (org.antlr.v4.runtime.atn.ATNState)9 DOTGenerator (org.antlr.v4.tool.DOTGenerator)9 Grammar (org.antlr.v4.tool.Grammar)9 Rule (org.antlr.v4.tool.Rule)9 ST (org.stringtemplate.v4.ST)9 ATNDeserializer (org.antlr.v4.runtime.atn.ATNDeserializer)8 ATNPrinter (org.antlr.v4.automata.ATNPrinter)7 STGroup (org.stringtemplate.v4.STGroup)7 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)6 AnalysisPipeline (org.antlr.v4.analysis.AnalysisPipeline)5 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)4