Search in sources :

Example 61 with LexerGrammar

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

the class TestAmbigParseTrees method testAmbigAltsAtRoot.

@Test
public void testAmbigAltsAtRoot() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "s : A x C" + "  | A B C" + "  ;" + "x : B ; \n", lg);
    String startRule = "s";
    String input = "abc";
    String expectedAmbigAlts = "{1, 2}";
    int decision = 0;
    String expectedOverallTree = "(s:1 a (x:1 b) c)";
    String[] expectedParseTrees = { "(s:1 a (x:1 b) c)", "(s:2 a b c)" };
    testAmbiguousTrees(lg, g, startRule, input, decision, expectedAmbigAlts, expectedOverallTree, expectedParseTrees);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 62 with LexerGrammar

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

the class TestAmbigParseTrees method testAmbigAltDipsIntoOuterContextToRoot.

@Test
public void testAmbigAltDipsIntoOuterContextToRoot() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "SELF : 'self' ;\n" + "ID : [a-z]+ ;\n" + "DOT : '.' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "e : p (DOT ID)* ;\n" + "p : SELF" + "  | SELF DOT ID" + "  ;", lg);
    String startRule = "e";
    String input = "self.x";
    String expectedAmbigAlts = "{1, 2}";
    // decision in p
    int decision = 1;
    String expectedOverallTree = "(e:1 (p:1 self) . x)";
    String[] expectedParseTrees = { "(e:1 (p:1 self) . x)", "(p:2 self . x)" };
    testAmbiguousTrees(lg, g, startRule, input, decision, expectedAmbigAlts, expectedOverallTree, expectedParseTrees);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 63 with LexerGrammar

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

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(CharStreams.fromString(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.getAmbiguousAlternatives(), ambiguityInfo.startIndex, ambiguityInfo.stopIndex, ruleIndex);
    assertEquals(expectedAmbigAlts, ambiguityInfo.getAmbiguousAlternatives().toString());
    assertEquals(ambiguityInfo.getAmbiguousAlternatives().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)

Example 64 with LexerGrammar

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

the class TestAmbigParseTrees method testParseDecisionWithinAmbiguousStartRule.

@Test
public void testParseDecisionWithinAmbiguousStartRule() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "s : A x C" + "  | A B C" + "  ;" + "x : B ; \n", lg);
    testInterpAtSpecificAlt(lg, g, "s", 1, "abc", "(s:1 a (x:1 b) c)");
    testInterpAtSpecificAlt(lg, g, "s", 2, "abc", "(s:2 a b c)");
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 65 with LexerGrammar

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

the class TestAmbigParseTrees method testInterpAtSpecificAlt.

void testInterpAtSpecificAlt(LexerGrammar lg, Grammar g, String startRule, int startAlt, String input, String expectedParseTree) {
    LexerInterpreter lexEngine = lg.createLexerInterpreter(CharStreams.fromString(input));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    ParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
    RuleStartState ruleStartState = g.atn.ruleToStartState[g.getRule(startRule).index];
    Transition tr = ruleStartState.transition(0);
    ATNState t2 = tr.target;
    if (!(t2 instanceof BasicBlockStartState)) {
        throw new IllegalArgumentException("rule has no decision: " + startRule);
    }
    parser.addDecisionOverride(((DecisionState) t2).decision, 0, startAlt);
    ParseTree t = parser.parse(g.rules.get(startRule).index);
    InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
    assertEquals(expectedParseTree, Trees.toStringTree(t, nodeTextProvider));
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) ParserInterpreter(org.antlr.v4.runtime.ParserInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) Transition(org.antlr.v4.runtime.atn.Transition) BasicBlockStartState(org.antlr.v4.runtime.atn.BasicBlockStartState) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ATNState(org.antlr.v4.runtime.atn.ATNState)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)444 Test (org.junit.Test)410 Grammar (org.antlr.v4.tool.Grammar)140 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)120 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)100 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)86 ATN (org.antlr.v4.runtime.atn.ATN)72 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)58 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)26 ParseTree (org.antlr.v4.runtime.tree.ParseTree)24 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)22 CharStream (org.antlr.v4.runtime.CharStream)20 TokenStream (org.antlr.v4.runtime.TokenStream)17 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)14 STGroupString (org.stringtemplate.v4.STGroupString)14 ArrayList (java.util.ArrayList)13 ATNFactory (org.antlr.v4.automata.ATNFactory)12 DFA (org.antlr.v4.runtime.dfa.DFA)12 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)12