Search in sources :

Example 66 with Grammar

use of org.antlr.v4.tool.Grammar 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 67 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestATNParserPrediction method testAltsForLRRuleComputation.

@Test
public void testAltsForLRRuleComputation() throws Exception {
    Grammar g = new Grammar("grammar T;\n" + "e : e '*' e\n" + "  | INT\n" + "  | e '+' 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, 2, 4]", Arrays.toString(lr.getPrimaryAlts()));
    assertEquals("[0, 1, 3]", Arrays.toString(lr.getRecursiveOpAlts()));
}
Also used : LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) Test(org.junit.Test)

Example 68 with Grammar

use of org.antlr.v4.tool.Grammar in project antlr4 by antlr.

the class TestATNParserPrediction method testAltsForLRRuleComputation2.

@Test
public void testAltsForLRRuleComputation2() throws Exception {
    Grammar g = new Grammar("grammar T;\n" + "e : INT\n" + "  | e '*' 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, 3]", Arrays.toString(lr.getPrimaryAlts()));
    assertEquals("[0, 2]", Arrays.toString(lr.getRecursiveOpAlts()));
}
Also used : LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) Test(org.junit.Test)

Example 69 with Grammar

use of org.antlr.v4.tool.Grammar 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 70 with Grammar

use of org.antlr.v4.tool.Grammar 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)

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