Search in sources :

Example 41 with Parser

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;
    }
}
Also used : NoViableAltException(org.antlr.v4.runtime.NoViableAltException) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 42 with Parser

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);
}
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 43 with Parser

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);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) ATN(org.antlr.v4.runtime.atn.ATN) Test(org.junit.Test)

Example 44 with Parser

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);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 45 with Parser

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);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)138 Grammar (org.antlr.v4.tool.Grammar)130 LexerGrammar (org.antlr.v4.tool.LexerGrammar)117 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)52 ParseTree (org.antlr.v4.runtime.tree.ParseTree)39 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)37 ATN (org.antlr.v4.runtime.atn.ATN)19 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)16 ArrayList (java.util.ArrayList)14 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)14 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)14 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)13 RecognitionException (org.antlr.v4.runtime.RecognitionException)11 Parser (org.antlr.v4.runtime.Parser)10 DecisionInfo (org.antlr.v4.runtime.atn.DecisionInfo)10 Lexer (org.antlr.v4.runtime.Lexer)9 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)9 CharStream (org.antlr.v4.runtime.CharStream)8 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)8 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)8