Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6205021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:11:53+00:00 2026-05-24T05:11:53+00:00

In ANTLR I want to define a rule like this: rule : ( a

  • 0

In ANTLR I want to define a rule like this:

rule : ( a b c | a c b | b a c | b c a | c a b | c b a );

But in my case I have 10 rules instead of three, that I want to permute so it gets very impractical.
Is there any way of expressing this in ANTLR without having to write all the permutations?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:11:55+00:00Added an answer on May 24, 2026 at 5:11 am

    I would just match any a, b or c once or more:

    rule
     : ( a | b | c )+
     ;
    

    and then, after parsing, traversing the parse tree and checking if a, b and c all matched exactly once.

    But Yes, it is possible in the grammar itself by using predicates where needed.

    A demo:

    grammar Permutation;
    
    parse
      :  permutation[5] {System.out.println("parsed: " + $permutation.text);} EOF
      ;
    
    permutation[final int n]
    @init{
      java.util.Set set = new java.util.HashSet();
      int counter = n;
    }
      :  (
           {counter > 0}?=> token   // keep matching a `token` as long as `counter > 0`
           {                        //
             set.add($token.text);  // add the contents of `token` to `set`
             counter--;             // decrease `counter`
           }                        //
         )+ 
         {set.size() == n}?         // if `set.size() != n`, an exception is thrown
      ;
    
    token
      :  A
      |  B
      |  C
      |  D
      |  E
      ;
    
    A : 'A';
    B : 'B';
    C : 'C';
    D : 'D';
    E : 'E';
    
    Space : ' ' {skip();};
    

    The demo grammar above uses 2 different types of predicates: 1) a gated semantic predicate i to make sure that the permutation rule matches no more than the parameter final int n tokens, and 2) a validating semantic predicate i to ensure that the set holds exactly the final int n elements to ensure that it’s a proper permutation of the 5 tokens.

    More info about semantic predicates can be found here: What is a 'semantic predicate' in ANTLR?

    You can test the grammar with the following class:

    import org.antlr.runtime.*;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        PermutationLexer lexer = new PermutationLexer(new ANTLRStringStream(args[0]));
        PermutationParser parser = new PermutationParser(new CommonTokenStream(lexer));
        parser.parse();
      }
    }
    
    java -cp antlr-3.3.jar org.antlr.Tool Permutation.g 
    javac -cp antlr-3.3.jar *.java
    
    java -cp .:antlr-3.3.jar Main "A B C D E"
    parsed: ABCDE
    
    java -cp .:antlr-3.3.jar Main "B D C E A"
    parsed: BDCEA
    
    java -cp .:antlr-3.3.jar Main "A B C D B"
    line 1:9 rule permutation failed predicate: {set.size() == n}?
    parsed: null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I want to match beer, but don't care about case sensitivity.
i have problem with @members rule in ANTLR. I want overwrite default error handling,
I want to parser a simple matlab-like for-loop, using ANTLR. The loop is like
I have an antlr generated Java parser that uses the C target and it
I'm using ANTLR 3 to create an AST. I want to have two AST
It seems that sometimes the Antlr lexer makes a bad choice on which rule
I want to generate an antlr lexer at runtime -- that is, generate the
I want to parse ISO 8601 dates in my ANTLR grammar. 2001-05-03 I have
I am trying to write an ANTLR parser rule that matches a list of
I am using antlr to generate my parser, but I want to override some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.