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);
}
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);
}
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));
}
}
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)");
}
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));
}
Aggregations