Search in sources :

Example 31 with Parser

use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.

the class TestATNParserPrediction 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" + "  ;");
    int decision = 0;
    checkPredictedAlt(lg, g, decision, "34b", 1);
    checkPredictedAlt(lg, g, decision, "34c", 2);
    checkPredictedAlt(lg, g, decision, "((34))b", 1);
    checkPredictedAlt(lg, g, decision, "((34))c", 2);
    // After matching these inputs for decision, what is DFA after each prediction?
    String[] inputs = { "34b", "34c", "((34))b", "((34))c" };
    String[] dfa = { "s0-INT->s1\n" + "s1-'b'->:s2=>1\n", "s0-INT->s1\n" + "s1-'b'->:s2=>1\n" + "s1-'c'->:s3=>2\n", "s0-'('->s4\n" + "s0-INT->s1\n" + "s1-'b'->:s2=>1\n" + "s1-'c'->:s3=>2\n" + "s4-'('->s5\n" + "s5-INT->s6\n" + "s6-')'->s7\n" + "s7-')'->s1\n", "s0-'('->s4\n" + "s0-INT->s1\n" + "s1-'b'->:s2=>1\n" + "s1-'c'->:s3=>2\n" + "s4-'('->s5\n" + "s5-INT->s6\n" + "s6-')'->s7\n" + "s7-')'->s1\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 32 with Parser

use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.

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)

Example 33 with Parser

use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.

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 34 with Parser

use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.

the class TestATNDeserialization method test3Alts.

@Test
public void test3Alts() throws Exception {
    Grammar g = new Grammar("parser grammar T;\n" + "a : A | A B | A B C ;");
    checkDeserializationIsStable(g);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 35 with Parser

use of org.antlr.v4.runtime.Parser in project antlr4 by antlr.

the class TestATNDeserialization method testPEGAchillesHeel.

@Test
public void testPEGAchillesHeel() throws Exception {
    Grammar g = new Grammar("parser grammar T;\n" + "a : A | A B ;");
    checkDeserializationIsStable(g);
}
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