Search in sources :

Example 21 with Rule

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

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

the class TestATNParserPrediction method checkPredictedAlt.

/** first check that the ATN predicts right alt.
	 *  Then check adaptive prediction.
	 */
public void checkPredictedAlt(LexerGrammar lg, Grammar g, int decision, String inputString, int expectedAlt) {
    Tool.internalOption_ShowATNConfigsInDFA = true;
    ATN lexatn = createATN(lg, true);
    LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn, new DFA[] { new DFA(lexatn.modeToStartState.get(Lexer.DEFAULT_MODE)) }, new PredictionContextCache());
    IntegerList types = getTokenTypesViaATN(inputString, lexInterp);
    //		System.out.println(types);
    semanticProcess(lg);
    g.importVocab(lg);
    semanticProcess(g);
    ParserATNFactory f = new ParserATNFactory(g);
    ATN atn = f.createATN();
    DOTGenerator dot = new DOTGenerator(g);
    Rule r = g.getRule("a");
    //		if ( r!=null) System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    r = g.getRule("b");
    //		if ( r!=null) System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    r = g.getRule("e");
    //		if ( r!=null) System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    r = g.getRule("ifstat");
    //		if ( r!=null) System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    r = g.getRule("block");
    //		if ( r!=null) System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    // Check ATN prediction
    //		ParserATNSimulator interp = new ParserATNSimulator(atn);
    TokenStream input = new IntTokenStream(types);
    ParserInterpreterForTesting interp = new ParserInterpreterForTesting(g, input);
    int alt = interp.adaptivePredict(input, decision, ParserRuleContext.EMPTY);
    assertEquals(expectedAlt, alt);
    // Check adaptive prediction
    input.seek(0);
    alt = interp.adaptivePredict(input, decision, null);
    assertEquals(expectedAlt, alt);
    // run 2x; first time creates DFA in atn
    input.seek(0);
    alt = interp.adaptivePredict(input, decision, null);
    assertEquals(expectedAlt, alt);
}
Also used : ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) DOTGenerator(org.antlr.v4.tool.DOTGenerator) TokenStream(org.antlr.v4.runtime.TokenStream) LexerATNSimulator(org.antlr.v4.runtime.atn.LexerATNSimulator) IntegerList(org.antlr.v4.runtime.misc.IntegerList) ATN(org.antlr.v4.runtime.atn.ATN) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) PredictionContextCache(org.antlr.v4.runtime.atn.PredictionContextCache) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 23 with Rule

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

the class TestATNInterpreter method checkMatchedAlt.

public void checkMatchedAlt(LexerGrammar lg, final Grammar g, String inputString, int expected) {
    ATN lexatn = createATN(lg, true);
    LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn, new DFA[] { new DFA(lexatn.modeToStartState.get(Lexer.DEFAULT_MODE)) }, null);
    IntegerList types = getTokenTypesViaATN(inputString, lexInterp);
    //		System.out.println(types);
    g.importVocab(lg);
    ParserATNFactory f = new ParserATNFactory(g);
    ATN atn = f.createATN();
    IntTokenStream input = new IntTokenStream(types);
    //		System.out.println("input="+input.types);
    ParserInterpreterForTesting interp = new ParserInterpreterForTesting(g, input);
    ATNState startState = atn.ruleToStartState[g.getRule("a").index];
    if (startState.transition(0).target instanceof BlockStartState) {
        startState = startState.transition(0).target;
    }
    DOTGenerator dot = new DOTGenerator(g);
    //		System.out.println(dot.getDOT(atn.ruleToStartState[g.getRule("a").index]));
    Rule r = g.getRule("e");
    //		if ( r!=null ) System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    int result = interp.matchATN(input, startState);
    assertEquals(expected, result);
}
Also used : ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) DOTGenerator(org.antlr.v4.tool.DOTGenerator) LexerATNSimulator(org.antlr.v4.runtime.atn.LexerATNSimulator) IntegerList(org.antlr.v4.runtime.misc.IntegerList) ATN(org.antlr.v4.runtime.atn.ATN) Rule(org.antlr.v4.tool.Rule) BlockStartState(org.antlr.v4.runtime.atn.BlockStartState) DFA(org.antlr.v4.runtime.dfa.DFA) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 24 with Rule

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

the class TestATNParserPrediction method testAltsForLRRuleComputation3.

@Test
public void testAltsForLRRuleComputation3() throws Exception {
    Grammar g = new Grammar("grammar T;\n" + // should have no effect
    "random : 'blort';\n" + "e : '--' e\n" + "  | e '*' e\n" + "  | e '+' e\n" + "  | 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, 5]", Arrays.toString(lr.getPrimaryAlts()));
    assertEquals("[0, 2, 3, 4]", 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 25 with Rule

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

the class TestATNSerialization method testLexerAction.

@Test
public void testLexerAction() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' {a} ;\n" + "B : 'b' ;\n" + "C : 'c' {c} ;\n");
    String expecting = "max type 3\n" + "0:TOKEN_START -1\n" + "1:RULE_START 0\n" + "2:RULE_STOP 0\n" + "3:RULE_START 1\n" + "4:RULE_STOP 1\n" + "5:RULE_START 2\n" + "6:RULE_STOP 2\n" + "7:BASIC 0\n" + "8:BASIC 0\n" + "9:BASIC 0\n" + "10:BASIC 1\n" + "11:BASIC 1\n" + "12:BASIC 2\n" + "13:BASIC 2\n" + "14:BASIC 2\n" + "rule 0:1 1\n" + "rule 1:3 2\n" + "rule 2:5 3\n" + "mode 0:0\n" + "0->1 EPSILON 0,0,0\n" + "0->3 EPSILON 0,0,0\n" + "0->5 EPSILON 0,0,0\n" + "1->7 EPSILON 0,0,0\n" + "3->10 EPSILON 0,0,0\n" + "5->12 EPSILON 0,0,0\n" + "7->8 ATOM 97,0,0\n" + "8->9 ACTION 0,0,0\n" + "9->2 EPSILON 0,0,0\n" + "10->11 ATOM 98,0,0\n" + "11->4 EPSILON 0,0,0\n" + "12->13 ATOM 99,0,0\n" + "13->14 ACTION 2,1,0\n" + "14->6 EPSILON 0,0,0\n" + "0:0\n";
    ATN atn = createATN(lg, true);
    String result = ATNSerializer.getDecoded(atn, Arrays.asList(lg.getTokenNames()));
    assertEquals(expecting, result);
}
Also used : ATN(org.antlr.v4.runtime.atn.ATN) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)50 ATN (org.antlr.v4.runtime.atn.ATN)47 Rule (org.antlr.v4.tool.Rule)47 Test (org.junit.Test)46 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)31 Grammar (org.antlr.v4.tool.Grammar)30 ATNState (org.antlr.v4.runtime.atn.ATNState)26 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)24 ArrayList (java.util.ArrayList)20 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)17 DOTGenerator (org.antlr.v4.tool.DOTGenerator)9 HashMap (java.util.HashMap)8 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)8 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)8 ActionAST (org.antlr.v4.tool.ast.ActionAST)8 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)7 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)7 DecisionState (org.antlr.v4.runtime.atn.DecisionState)7 RuleAST (org.antlr.v4.tool.ast.RuleAST)7 LinkedHashMap (java.util.LinkedHashMap)6