Search in sources :

Example 76 with Grammar

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);
}
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 77 with Grammar

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);
}
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 78 with Grammar

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);
}
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 79 with Grammar

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);
}
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 80 with Grammar

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

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)305 Test (org.junit.Test)304 Grammar (org.antlr.v4.tool.Grammar)183 ATN (org.antlr.v4.runtime.atn.ATN)65 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)62 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)59 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)53 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)47 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)43 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)31 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)29 Rule (org.antlr.v4.tool.Rule)25 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)21 STGroupString (org.stringtemplate.v4.STGroupString)21 ArrayList (java.util.ArrayList)18 ATNState (org.antlr.v4.runtime.atn.ATNState)18 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)17 ST (org.stringtemplate.v4.ST)17 ParseTree (org.antlr.v4.runtime.tree.ParseTree)15 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)15