Search in sources :

Example 16 with DOTGenerator

use of org.antlr.v4.tool.DOTGenerator in project antlr4 by antlr.

the class Tool method generateATNs.

public void generateATNs(Grammar g) {
    DOTGenerator dotGenerator = new DOTGenerator(g);
    List<Grammar> grammars = new ArrayList<Grammar>();
    grammars.add(g);
    List<Grammar> imported = g.getAllImportedGrammars();
    if (imported != null)
        grammars.addAll(imported);
    for (Grammar ig : grammars) {
        for (Rule r : ig.rules.values()) {
            try {
                String dot = dotGenerator.getDOT(g.atn.ruleToStartState[r.index], g.isLexer());
                if (dot != null) {
                    writeDOTFile(g, r, dot);
                }
            } catch (IOException ioe) {
                errMgr.toolError(ErrorType.CANNOT_WRITE_FILE, ioe);
            }
        }
    }
}
Also used : DOTGenerator(org.antlr.v4.tool.DOTGenerator) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Rule(org.antlr.v4.tool.Rule) IOException(java.io.IOException)

Example 17 with DOTGenerator

use of org.antlr.v4.tool.DOTGenerator in project antlr4 by tunnelvisionlabs.

the class BaseTest method checkRuleATN.

void checkRuleATN(Grammar g, String ruleName, String expecting) {
    DOTGenerator dot = new DOTGenerator(g);
    System.out.println(dot.getDOT(g.atn.ruleToStartState[g.getRule(ruleName).index]));
    Rule r = g.getRule(ruleName);
    ATNState startState = g.getATN().ruleToStartState[r.index];
    ATNPrinter serializer = new ATNPrinter(g, startState);
    String result = serializer.asString();
    // System.out.print(result);
    assertEquals(expecting, result);
}
Also used : DOTGenerator(org.antlr.v4.tool.DOTGenerator) ATNPrinter(org.antlr.v4.automata.ATNPrinter) Rule(org.antlr.v4.tool.Rule) TestRule(org.junit.rules.TestRule) STGroupString(org.stringtemplate.v4.STGroupString) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 18 with DOTGenerator

use of org.antlr.v4.tool.DOTGenerator 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 19 with DOTGenerator

use of org.antlr.v4.tool.DOTGenerator in project antlr4 by tunnelvisionlabs.

the class TestExpectedTokens method testFollowIncludedInLeftRecursiveRule.

// Test for https://github.com/antlr/antlr4/issues/1480
// can't reproduce
@Test
public void testFollowIncludedInLeftRecursiveRule() throws Exception {
    String gtext = "grammar T;\n" + "s : expr EOF ;\n" + "expr : L expr R\n" + "     | expr PLUS expr\n" + "     | ID\n" + "     ;\n";
    Grammar g = new Grammar(gtext);
    String atnText = "RuleStart_expr_2->BlockStart_13\n" + "BlockStart_13->s7\n" + "BlockStart_13->s12\n" + "s7-action_1:-1->s8\n" + "s12-ID->BlockEnd_14\n" + "s8-L->s9\n" + "BlockEnd_14->StarLoopEntry_20\n" + "s9-expr->RuleStart_expr_2\n" + "StarLoopEntry_20->StarBlockStart_18\n" + "StarLoopEntry_20->s21\n" + "s10-R->s11\n" + "StarBlockStart_18->s15\n" + "s21->RuleStop_expr_3\n" + "s11->BlockEnd_14\n" + "s15-2 >= _p->s16\n" + "RuleStop_expr_3->s5\n" + "RuleStop_expr_3->s10\n" + "RuleStop_expr_3->BlockEnd_19\n" + "s16-PLUS->s17\n" + "s17-expr->RuleStart_expr_2\n" + "BlockEnd_19->StarLoopBack_22\n" + "StarLoopBack_22->StarLoopEntry_20\n";
    checkRuleATN(g, "expr", atnText);
    ATN atn = g.getATN();
    // DOTGenerator gen = new DOTGenerator(g);
    // String dot = gen.getDOT(atn.states.get(2), g.getRuleNames(), false);
    // System.out.println(dot);
    // Simulate call stack after input '(x' from rule s
    ParserRuleContext callStackFrom_s = new ParserRuleContext(null, 4);
    ParserRuleContext callStackFrom_expr = new ParserRuleContext(callStackFrom_s, 9);
    int afterID = 14;
    IntervalSet tokens = atn.getExpectedTokens(afterID, callStackFrom_expr);
    assertEquals("{R, PLUS}", tokens.toString(g.getVocabulary()));
    // Simulate call stack after input '(x' from within rule expr
    callStackFrom_expr = new ParserRuleContext(null, 9);
    tokens = atn.getExpectedTokens(afterID, callStackFrom_expr);
    assertEquals("{R, PLUS}", tokens.toString(g.getVocabulary()));
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) Grammar(org.antlr.v4.tool.Grammar) ATN(org.antlr.v4.runtime.atn.ATN) Test(org.junit.Test)

Aggregations

DOTGenerator (org.antlr.v4.tool.DOTGenerator)16 ATN (org.antlr.v4.runtime.atn.ATN)15 Rule (org.antlr.v4.tool.Rule)13 ATNState (org.antlr.v4.runtime.atn.ATNState)11 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)9 ATNPrinter (org.antlr.v4.automata.ATNPrinter)7 STGroupString (org.stringtemplate.v4.STGroupString)7 Grammar (org.antlr.v4.tool.Grammar)6 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)5 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)4 IntegerList (org.antlr.v4.runtime.misc.IntegerList)4 LexerGrammar (org.antlr.v4.tool.LexerGrammar)4 Test (org.junit.Test)4 TokenStream (org.antlr.v4.runtime.TokenStream)3 DFA (org.antlr.v4.runtime.dfa.DFA)3 TestRule (org.junit.rules.TestRule)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 CharStream (org.antlr.v4.runtime.CharStream)2