use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestATNInterpreter method testAmbigAltChooseFirst.
@Test
public void testAmbigAltChooseFirst() throws Exception {
LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n" + "D : 'd' ;\n");
Grammar g = new Grammar("parser grammar T;\n" + // first alt
"a : A B | A B ;");
checkMatchedAlt(lg, g, "ab", 1);
checkMatchedAlt(lg, g, "abc", 1);
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestATNInterpreter method testMustTrackPreviousGoodAlt.
@Test
public void testMustTrackPreviousGoodAlt() 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" + "a : A | A B ;");
checkMatchedAlt(lg, g, "a", 1);
checkMatchedAlt(lg, g, "ab", 2);
checkMatchedAlt(lg, g, "ac", 1);
checkMatchedAlt(lg, g, "abc", 2);
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestATNInterpreter method testSimpleLoop.
@Test
public void testSimpleLoop() throws Exception {
LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n" + "D : 'd' ;\n");
Grammar g = new Grammar("parser grammar T;\n" + "a : A+ B ;");
checkMatchedAlt(lg, g, "ab", 1);
checkMatchedAlt(lg, g, "aab", 1);
checkMatchedAlt(lg, g, "aaaaaab", 1);
checkMatchedAlt(lg, g, "aabd", 1);
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestATNInterpreter method testRecursiveLeftPrefix.
@Test
public void testRecursiveLeftPrefix() throws Exception {
LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n" + "LP : '(' ;\n" + "RP : ')' ;\n" + "INT : '0'..'9'+ ;\n");
Grammar g = new Grammar("parser grammar T;\n" + "tokens {A,B,C,LP,RP,INT}\n" + "a : e B | e C ;\n" + "e : LP e RP\n" + " | INT\n" + " ;");
checkMatchedAlt(lg, g, "34b", 1);
checkMatchedAlt(lg, g, "34c", 2);
checkMatchedAlt(lg, g, "(34)b", 1);
checkMatchedAlt(lg, g, "(34)c", 2);
checkMatchedAlt(lg, g, "((34))b", 1);
checkMatchedAlt(lg, g, "((34))c", 2);
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestATNInterpreter method testMustTrackPreviousGoodAlt3WithEOF.
@Test(expected = NoViableAltException.class)
public void testMustTrackPreviousGoodAlt3WithEOF() throws Exception {
LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n" + "D : 'd' ;\n");
Grammar g = new Grammar("parser grammar T;\n" + "a : (A B | A | A B C) EOF;");
checkMatchedAlt(lg, g, "a", 2);
checkMatchedAlt(lg, g, "ab", 1);
checkMatchedAlt(lg, g, "abc", 3);
try {
checkMatchedAlt(lg, g, "abd", 1);
} catch (NoViableAltException re) {
assertEquals(2, re.getOffendingToken().getTokenIndex());
assertEquals(4, re.getOffendingToken().getType());
throw re;
}
}
Aggregations