Search in sources :

Example 16 with Rule

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

the class LeftRecursiveRule method getPrimaryAlts.

/** Return an array that maps predicted alt from primary decision
	 *  to original alt of rule. For following rule, return [0, 2, 4]
	 *
		e : e '*' e
		  | INT
		  | e '+' e
		  | ID
		  ;

	 *  That maps predicted alt 1 to original alt 2 and predicted 2 to alt 4.
	 *
	 *  @since 4.5.1
	 */
public int[] getPrimaryAlts() {
    if (recPrimaryAlts.size() == 0)
        return null;
    int[] alts = new int[recPrimaryAlts.size() + 1];
    for (int i = 0; i < recPrimaryAlts.size(); i++) {
        // recPrimaryAlts is a List not Map like recOpAlts
        LeftRecursiveRuleAltInfo altInfo = recPrimaryAlts.get(i);
        alts[i + 1] = altInfo.altNum;
    }
    return alts;
}
Also used : LeftRecursiveRuleAltInfo(org.antlr.v4.analysis.LeftRecursiveRuleAltInfo)

Example 17 with Rule

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

the class Rule method getAltLabels.

/**
	 * Get {@code #} labels. The keys of the map are the labels applied to outer
	 * alternatives of a lexer rule, and the values are collections of pairs
	 * (alternative number and {@link AltAST}) identifying the alternatives with
	 * this label. Unlabeled alternatives are not included in the result.
	 */
public Map<String, List<Pair<Integer, AltAST>>> getAltLabels() {
    Map<String, List<Pair<Integer, AltAST>>> labels = new LinkedHashMap<String, List<Pair<Integer, AltAST>>>();
    for (int i = 1; i <= numberOfAlts; i++) {
        GrammarAST altLabel = alt[i].ast.altLabel;
        if (altLabel != null) {
            List<Pair<Integer, AltAST>> list = labels.get(altLabel.getText());
            if (list == null) {
                list = new ArrayList<Pair<Integer, AltAST>>();
                labels.put(altLabel.getText(), list);
            }
            list.add(new Pair<Integer, AltAST>(i, alt[i].ast));
        }
    }
    if (labels.isEmpty())
        return null;
    return labels;
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ArrayList(java.util.ArrayList) List(java.util.List) AltAST(org.antlr.v4.tool.ast.AltAST) LinkedHashMap(java.util.LinkedHashMap) Pair(org.antlr.v4.runtime.misc.Pair)

Example 18 with Rule

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

the class BaseJavaTest method checkRuleDFA.

List<ANTLRMessage> checkRuleDFA(String gtext, String ruleName, String expecting) throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    Grammar g = new Grammar(gtext, equeue);
    ATN atn = createATN(g, false);
    ATNState s = atn.ruleToStartState[g.getRule(ruleName).index];
    if (s == null) {
        System.err.println("no such rule: " + ruleName);
        return null;
    }
    ATNState t = s.transition(0).target;
    if (!(t instanceof DecisionState)) {
        System.out.println(ruleName + " has no decision");
        return null;
    }
    DecisionState blk = (DecisionState) t;
    checkRuleDFA(g, blk, expecting);
    return equeue.all;
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) ATN(org.antlr.v4.runtime.atn.ATN) DecisionState(org.antlr.v4.runtime.atn.DecisionState) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 19 with Rule

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

the class BaseCppTest method checkRuleDFA.

List<ANTLRMessage> checkRuleDFA(String gtext, String ruleName, String expecting) throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    Grammar g = new Grammar(gtext, equeue);
    ATN atn = createATN(g, false);
    ATNState s = atn.ruleToStartState[g.getRule(ruleName).index];
    if (s == null) {
        System.err.println("no such rule: " + ruleName);
        return null;
    }
    ATNState t = s.transition(0).target;
    if (!(t instanceof DecisionState)) {
        System.out.println(ruleName + " has no decision");
        return null;
    }
    DecisionState blk = (DecisionState) t;
    checkRuleDFA(g, blk, expecting);
    return equeue.all;
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) ATN(org.antlr.v4.runtime.atn.ATN) DecisionState(org.antlr.v4.runtime.atn.DecisionState) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 20 with Rule

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

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