Search in sources :

Example 41 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class TestATNParserPrediction method testEmptyInput.

@Test
public void testEmptyInput() 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 | ;");
    int decision = 0;
    checkPredictedAlt(lg, g, decision, "a", 1);
    checkPredictedAlt(lg, g, decision, "", 2);
    // After matching these inputs for decision, what is DFA after each prediction?
    String[] inputs = { "a", "" };
    String[] dfa = { "s0-'a'->:s1=>1\n", "s0-EOF->:s2=>2\n" + "s0-'a'->:s1=>1\n" };
    checkDFAConstruction(lg, g, decision, 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 42 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class TestATNParserPrediction method testRuleRefxory.

@Test
public void testRuleRefxory() 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 : x | y ;\n" + "x : A ;\n" + "y : B ;\n");
    int decision = 0;
    checkPredictedAlt(lg, g, decision, "a", 1);
    checkPredictedAlt(lg, g, decision, "b", 2);
    // After matching these inputs for decision, what is DFA after each prediction?
    String[] inputs = { "a", "b", "a" };
    String[] dfa = { "s0-'a'->:s1=>1\n", "s0-'a'->:s1=>1\n" + "s0-'b'->:s2=>2\n", // don't change after it works
    "s0-'a'->:s1=>1\n" + "s0-'b'->:s2=>2\n" };
    checkDFAConstruction(lg, g, decision, 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 LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

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 44 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class TestATNParserPrediction method checkDFAConstruction.

public void checkDFAConstruction(LexerGrammar lg, Grammar g, int decision, String[] inputString, String[] dfaString) {
    // Tool.internalOption_ShowATNConfigsInDFA = true;
    ATN lexatn = createATN(lg, true);
    LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn);
    semanticProcess(lg);
    g.importVocab(lg);
    semanticProcess(g);
    ParserInterpreterForTesting interp = new ParserInterpreterForTesting(g, null);
    for (int i = 0; i < inputString.length; i++) {
        // Check DFA
        IntegerList types = getTokenTypesViaATN(inputString[i], lexInterp);
        System.out.println(types);
        TokenStream input = new IntTokenStream(types);
        try {
            interp.adaptivePredict(input, decision, ParserRuleContext.emptyContext());
        } catch (NoViableAltException nvae) {
            nvae.printStackTrace(System.err);
        }
        DFA dfa = interp.getATNSimulator().atn.decisionToDFA[decision];
        assertEquals(dfaString[i], dfa.toString(g.getVocabulary(), g.rules.keySet().toArray(new String[g.rules.size()])));
    }
}
Also used : TokenStream(org.antlr.v4.runtime.TokenStream) NoViableAltException(org.antlr.v4.runtime.NoViableAltException) LexerATNSimulator(org.antlr.v4.runtime.atn.LexerATNSimulator) IntegerList(org.antlr.v4.runtime.misc.IntegerList) ATN(org.antlr.v4.runtime.atn.ATN) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 45 with LexerGrammar

use of org.antlr.v4.tool.LexerGrammar in project antlr4 by tunnelvisionlabs.

the class TestATNParserPrediction method testOptionalRuleChasesGlobalFollow.

@Test
public void testOptionalRuleChasesGlobalFollow() 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" + "tokens {A,B,C}\n" + "a : x B ;\n" + "b : x C ;\n" + "x : A | ;\n");
    int decision = 0;
    checkPredictedAlt(lg, g, decision, "a", 1);
    checkPredictedAlt(lg, g, decision, "b", 2);
    checkPredictedAlt(lg, g, decision, "c", 2);
    // After matching these inputs for decision, what is DFA after each prediction?
    String[] inputs = { "a", "b", "c", "c" };
    String[] dfa = { "s0-'a'->:s1=>1\n", "s0-'a'->:s1=>1\n" + "s0-'b'->:s2=>2\n", "s0-'a'->:s1=>1\n" + "s0-'b'->:s2=>2\n" + "s0-'c'->:s3=>2\n", "s0-'a'->:s1=>1\n" + "s0-'b'->:s2=>2\n" + "s0-'c'->:s3=>2\n" };
    checkDFAConstruction(lg, g, decision, 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)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)444 Test (org.junit.Test)410 Grammar (org.antlr.v4.tool.Grammar)140 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)120 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)100 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)86 ATN (org.antlr.v4.runtime.atn.ATN)72 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)58 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)43 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)26 ParseTree (org.antlr.v4.runtime.tree.ParseTree)24 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)22 CharStream (org.antlr.v4.runtime.CharStream)20 TokenStream (org.antlr.v4.runtime.TokenStream)17 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)14 STGroupString (org.stringtemplate.v4.STGroupString)14 ArrayList (java.util.ArrayList)13 ATNFactory (org.antlr.v4.automata.ATNFactory)12 DFA (org.antlr.v4.runtime.dfa.DFA)12 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)12