use of org.antlr.v4.tool.Grammar 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.tool.Grammar 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.tool.Grammar in project antlr4 by antlr.
the class TestATNParserPrediction method testAltsForLRRuleComputation3.
@Test
public void testAltsForLRRuleComputation3() throws Exception {
Grammar g = new Grammar("grammar T;\n" + // should have no effect
"random : 'blort';\n" + "e : '--' e\n" + " | e '*' e\n" + " | e '+' e\n" + " | e '--'\n" + " | ID\n" + " ;\n" + "ID : [a-z]+ ;\n" + "INT : [0-9]+ ;\n" + "WS : [ \\r\\t\\n]+ ;");
Rule e = g.getRule("e");
assertTrue(e instanceof LeftRecursiveRule);
LeftRecursiveRule lr = (LeftRecursiveRule) e;
assertEquals("[0, 1, 5]", Arrays.toString(lr.getPrimaryAlts()));
assertEquals("[0, 2, 3, 4]", Arrays.toString(lr.getRecursiveOpAlts()));
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestATNSerialization method testLexerAction.
@Test
public void testLexerAction() throws Exception {
LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' {a} ;\n" + "B : 'b' ;\n" + "C : 'c' {c} ;\n");
String expecting = "max type 3\n" + "0:TOKEN_START -1\n" + "1:RULE_START 0\n" + "2:RULE_STOP 0\n" + "3:RULE_START 1\n" + "4:RULE_STOP 1\n" + "5:RULE_START 2\n" + "6:RULE_STOP 2\n" + "7:BASIC 0\n" + "8:BASIC 0\n" + "9:BASIC 0\n" + "10:BASIC 1\n" + "11:BASIC 1\n" + "12:BASIC 2\n" + "13:BASIC 2\n" + "14:BASIC 2\n" + "rule 0:1 1\n" + "rule 1:3 2\n" + "rule 2:5 3\n" + "mode 0:0\n" + "0->1 EPSILON 0,0,0\n" + "0->3 EPSILON 0,0,0\n" + "0->5 EPSILON 0,0,0\n" + "1->7 EPSILON 0,0,0\n" + "3->10 EPSILON 0,0,0\n" + "5->12 EPSILON 0,0,0\n" + "7->8 ATOM 97,0,0\n" + "8->9 ACTION 0,0,0\n" + "9->2 EPSILON 0,0,0\n" + "10->11 ATOM 98,0,0\n" + "11->4 EPSILON 0,0,0\n" + "12->13 ATOM 99,0,0\n" + "13->14 ACTION 2,1,0\n" + "14->6 EPSILON 0,0,0\n" + "0:0\n";
ATN atn = createATN(lg, true);
String result = ATNSerializer.getDecoded(atn, Arrays.asList(lg.getTokenNames()));
assertEquals(expecting, result);
}
use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.
the class TestATNSerialization method testLexerNotSetWithRange2.
@Test
public void testLexerNotSetWithRange2() throws Exception {
LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "ID : ~('a'|'b') ~('e'|'p'..'t')\n ;");
String expecting = "max type 1\n" + "0:TOKEN_START -1\n" + "1:RULE_START 0\n" + "2:RULE_STOP 0\n" + "3:BASIC 0\n" + "4:BASIC 0\n" + "5:BASIC 0\n" + "rule 0:1 1\n" + "mode 0:0\n" + "0:'a'..'b'\n" + "1:'e'..'e', 'p'..'t'\n" + "0->1 EPSILON 0,0,0\n" + "1->3 EPSILON 0,0,0\n" + "3->4 NOT_SET 0,0,0\n" + "4->5 NOT_SET 1,0,0\n" + "5->2 EPSILON 0,0,0\n" + "0:0\n";
ATN atn = createATN(lg, true);
String result = ATNSerializer.getDecoded(atn, Arrays.asList(lg.getTokenNames()));
assertEquals(expecting, result);
}
Aggregations