Search in sources :

Example 1 with DecisionInfo

use of org.antlr.v4.runtime.atn.DecisionInfo in project antlr4 by antlr.

the class TestAmbigParseTrees method testAmbiguousTrees.

public void testAmbiguousTrees(LexerGrammar lg, Grammar g, String startRule, String input, int decision, String expectedAmbigAlts, String overallTree, String[] expectedParseTrees) {
    InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
    LexerInterpreter lexEngine = lg.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    final GrammarParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
    parser.setProfile(true);
    parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
    // PARSE
    int ruleIndex = g.rules.get(startRule).index;
    ParserRuleContext parseTree = parser.parse(ruleIndex);
    assertEquals(overallTree, Trees.toStringTree(parseTree, nodeTextProvider));
    System.out.println();
    DecisionInfo[] decisionInfo = parser.getParseInfo().getDecisionInfo();
    List<AmbiguityInfo> ambiguities = decisionInfo[decision].ambiguities;
    assertEquals(1, ambiguities.size());
    AmbiguityInfo ambiguityInfo = ambiguities.get(0);
    List<ParserRuleContext> ambiguousParseTrees = GrammarParserInterpreter.getAllPossibleParseTrees(g, parser, tokens, decision, ambiguityInfo.ambigAlts, ambiguityInfo.startIndex, ambiguityInfo.stopIndex, ruleIndex);
    assertEquals(expectedAmbigAlts, ambiguityInfo.ambigAlts.toString());
    assertEquals(ambiguityInfo.ambigAlts.cardinality(), ambiguousParseTrees.size());
    for (int i = 0; i < ambiguousParseTrees.size(); i++) {
        ParserRuleContext t = ambiguousParseTrees.get(i);
        assertEquals(expectedParseTrees[i], Trees.toStringTree(t, nodeTextProvider));
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) DecisionInfo(org.antlr.v4.runtime.atn.DecisionInfo) AmbiguityInfo(org.antlr.v4.runtime.atn.AmbiguityInfo) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 2 with DecisionInfo

use of org.antlr.v4.runtime.atn.DecisionInfo in project antlr4 by antlr.

the class TestLookaheadTrees method testLookaheadTrees.

public void testLookaheadTrees(LexerGrammar lg, Grammar g, String input, String startRuleName, int decision, String[] expectedTrees) {
    int startRuleIndex = g.getRule(startRuleName).index;
    InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
    LexerInterpreter lexEngine = lg.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    GrammarParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
    parser.setProfile(true);
    ParseTree t = parser.parse(startRuleIndex);
    DecisionInfo decisionInfo = parser.getParseInfo().getDecisionInfo()[decision];
    LookaheadEventInfo lookaheadEventInfo = decisionInfo.SLL_MaxLookEvent;
    List<ParserRuleContext> lookaheadParseTrees = GrammarParserInterpreter.getLookaheadParseTrees(g, parser, tokens, startRuleIndex, lookaheadEventInfo.decision, lookaheadEventInfo.startIndex, lookaheadEventInfo.stopIndex);
    assertEquals(expectedTrees.length, lookaheadParseTrees.size());
    for (int i = 0; i < lookaheadParseTrees.size(); i++) {
        ParserRuleContext lt = lookaheadParseTrees.get(i);
        assertEquals(expectedTrees[i], Trees.toStringTree(lt, nodeTextProvider));
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) DecisionInfo(org.antlr.v4.runtime.atn.DecisionInfo) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) LookaheadEventInfo(org.antlr.v4.runtime.atn.LookaheadEventInfo)

Example 3 with DecisionInfo

use of org.antlr.v4.runtime.atn.DecisionInfo in project antlr4 by antlr.

the class TestParserProfiler method testRepeatedLL2.

@Test
public void testRepeatedLL2() throws Exception {
    Grammar g = new Grammar("parser grammar T;\n" + "s : ID ';'{}\n" + "  | ID '.'\n" + "  ;\n", lg);
    DecisionInfo[] info = interpAndGetDecisionInfo(lg, g, "s", "xyz;", "abc;");
    assertEquals(1, info.length);
    String expecting = "{decision=0, contextSensitivities=0, errors=0, ambiguities=0, SLL_lookahead=4, " + "SLL_ATNTransitions=2, SLL_DFATransitions=2, LL_Fallback=0, LL_lookahead=0, LL_ATNTransitions=0}";
    assertEquals(expecting, info[0].toString());
}
Also used : DecisionInfo(org.antlr.v4.runtime.atn.DecisionInfo) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 4 with DecisionInfo

use of org.antlr.v4.runtime.atn.DecisionInfo in project antlr4 by antlr.

the class TestParserProfiler method interpAndGetDecisionInfo.

public DecisionInfo[] interpAndGetDecisionInfo(LexerGrammar lg, Grammar g, String startRule, String... input) {
    LexerInterpreter lexEngine = lg.createLexerInterpreter(null);
    ParserInterpreter parser = g.createParserInterpreter(null);
    parser.setProfile(true);
    for (String s : input) {
        lexEngine.reset();
        parser.reset();
        lexEngine.setInputStream(new ANTLRInputStream(s));
        CommonTokenStream tokens = new CommonTokenStream(lexEngine);
        parser.setInputStream(tokens);
        Rule r = g.rules.get(startRule);
        if (r == null) {
            return parser.getParseInfo().getDecisionInfo();
        }
        ParserRuleContext t = parser.parse(r.index);
    //			try {
    //				Utils.waitForClose(t.inspect(parser).get());
    //			}
    //			catch (Exception e) {
    //				e.printStackTrace();
    //			}
    //
    //			System.out.println(t.toStringTree(parser));
    }
    return parser.getParseInfo().getDecisionInfo();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) ParserInterpreter(org.antlr.v4.runtime.ParserInterpreter) Rule(org.antlr.v4.tool.Rule) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 5 with DecisionInfo

use of org.antlr.v4.runtime.atn.DecisionInfo in project antlr4 by antlr.

the class TestParserProfiler method testContextSensitivity.

@Test
public void testContextSensitivity() throws Exception {
    Grammar g = new Grammar("parser grammar T;\n" + "a : '.' e ID \n" + "  | ';' e INT ID ;\n" + "e : INT | ;\n", lg);
    DecisionInfo[] info = interpAndGetDecisionInfo(lg, g, "a", "; 1 x");
    assertEquals(2, info.length);
    String expecting = "{decision=1, contextSensitivities=1, errors=0, ambiguities=0, SLL_lookahead=3, SLL_ATNTransitions=2, SLL_DFATransitions=0, LL_Fallback=1, LL_lookahead=3, LL_ATNTransitions=2}";
    assertEquals(expecting, info[1].toString());
}
Also used : DecisionInfo(org.antlr.v4.runtime.atn.DecisionInfo) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Aggregations

DecisionInfo (org.antlr.v4.runtime.atn.DecisionInfo)11 Grammar (org.antlr.v4.tool.Grammar)9 LexerGrammar (org.antlr.v4.tool.LexerGrammar)9 Test (org.junit.Test)9 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)3 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)3 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)3 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)3 GrammarParserInterpreter (org.antlr.v4.tool.GrammarParserInterpreter)2 Ignore (org.junit.Ignore)2 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)1 AmbiguityInfo (org.antlr.v4.runtime.atn.AmbiguityInfo)1 LookaheadEventInfo (org.antlr.v4.runtime.atn.LookaheadEventInfo)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 Rule (org.antlr.v4.tool.Rule)1