use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.
the class TestATNInterpreter method testAmbigAltChooseFirst2WithEOF.
@Test(expected = NoViableAltException.class)
public void testAmbigAltChooseFirst2WithEOF() 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 B | A B C) EOF;");
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;
}
}
use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.
the class TestATNParserPrediction method testPEGAchillesHeel.
@Test
public void testPEGAchillesHeel() 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 ;");
checkPredictedAlt(lg, g, 0, "a", 1);
checkPredictedAlt(lg, g, 0, "ab", 2);
checkPredictedAlt(lg, g, 0, "abc", 2);
String[] inputs = { "a", "ab", "abc" };
String[] dfa = { "s0-'a'->s1\n" + "s1-EOF->:s2=>1\n", "s0-'a'->s1\n" + "s1-EOF->:s2=>1\n" + "s1-'b'->:s3=>2\n", "s0-'a'->s1\n" + "s1-EOF->:s2=>1\n" + "s1-'b'->:s3=>2\n" };
checkDFAConstruction(lg, g, 0, inputs, dfa);
}
use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.
the class TestATNSerialization method testEOFInSet.
@Test
public void testEOFInSet() throws Exception {
Grammar g = new Grammar("parser grammar T;\n" + "a : (A|EOF) ;");
String expecting = "max type 1\n" + "0:RULE_START 0\n" + "1:RULE_STOP 0\n" + "2:BASIC 0\n" + "3:BASIC 0\n" + "4:BASIC 0\n" + "rule 0:0\n" + "0:EOF, A..A\n" + "0->2 EPSILON 0,0,0\n" + "2->3 SET 0,0,0\n" + "3->1 EPSILON 0,0,0\n";
ATN atn = createATN(g, true);
String result = ATNSerializer.getDecoded(atn, Arrays.asList(g.getTokenNames()));
assertEquals(expecting, result);
}
use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.
the class TestATNConstruction method testAorB.
@Test
public void testAorB() throws Exception {
Grammar g = new Grammar("parser grammar P;\n" + "a : A | B {;} ;");
String expecting = "RuleStart_a_0->BlockStart_5\n" + "BlockStart_5->s2\n" + "BlockStart_5->s3\n" + "s2-A->BlockEnd_6\n" + "s3-B->s4\n" + "BlockEnd_6->RuleStop_a_1\n" + "s4-action_0:-1->BlockEnd_6\n" + "RuleStop_a_1-EOF->s7\n";
checkRuleATN(g, "a", expecting);
}
use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.
the class TestATNConstruction method testAorEpsilon.
@Test
public void testAorEpsilon() throws Exception {
Grammar g = new Grammar("parser grammar P;\n" + "a : A | ;");
String expecting = "RuleStart_a_0->BlockStart_4\n" + "BlockStart_4->s2\n" + "BlockStart_4->s3\n" + "s2-A->BlockEnd_5\n" + "s3->BlockEnd_5\n" + "BlockEnd_5->RuleStop_a_1\n" + "RuleStop_a_1-EOF->s6\n";
checkRuleATN(g, "a", expecting);
}
Aggregations