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

  • SEARCH
  • Home
  • 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 3949992
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:33:33+00:00 2026-05-20T01:33:33+00:00

I would like to realize a branch in ANTLR3. I figured using branch[boolean is_a]

  • 0

I would like to realize a “branch” in ANTLR3.

I figured using

branch[boolean is_a]
    : ({ $is_a}? => a)
    | ({!$is_a}? => b);

would do the trick, but I get the compiling errors “cannot find symbol” and “illegal start of type”, because in the in the generated source i.e. DFA45.specialStateTransition(...) does not have a parameter is_a.

I tried omitting the =>¹, and/or omitting the $ of $is_a.

The FIRST sets of a and b are not disjoint.

In fact b is of type ((C) => c) | a.

¹) as I don’t understand the difference between {...}? => ... and {...}? ...

  • 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-20T01:33:34+00:00Added an answer on May 20, 2026 at 1:33 am

    I’m not 100% sure why you get that error: I’d need to see your entire grammar for that. Anyway, there is no need to check for both is_a and !is_a. And both $is_a and is_a are valid.

    Let’s say you’re parsing a list of numbers, and every 4th number, you want to handle through a different “branch”. A grammar for that would look like:

    grammar T;
    
    parse
    @init{int n = 1;}
      :  (number[n\%4 == 0] {n++;})+ EOF
      ;
    
    
    number [boolean multipleOf4]
      :  {multipleOf4}?=> Int {System.out.println("branch A -> " + $Int.text);}
      |                   Int {System.out.println("branch B :: " + $Int.text);}
      ;
    
    Int
      :  '0'..'9'+
      ;
    
    Space
      :  (' ' | '\t' | '\r' | '\n') {skip();}
      ;
    

    (note that the % is a reserved character inside ANTLR grammars (not inside String literals and comments though), so it needs escaping with a backslash)

    And can be tested with the class:

    import org.antlr.runtime.*;
    
    public class Main {
        public static void main(String[] args) throws Exception {
            ANTLRStringStream in = new ANTLRStringStream("11 22 33 44 55 66 77 88 99");
            TLexer lexer = new TLexer(in);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            TParser parser = new TParser(tokens);
            parser.parse();
        }
    }
    

    Now generate a parser/lexer (A), compile all source files (B) and run the main class (C):

    java -cp antlr-3.2.jar org.antlr.Tool T.g // A
    javac -cp antlr-3.2.jar *.java            // B
    java -cp .:antlr-3.2.jar Main             // C
    

    (on Windows, run it by doing java -cp .;antlr-3.2.jar Main)

    which produces the following output:

    branch B :: 11
    branch B :: 22
    branch B :: 33
    branch A -> 44
    branch B :: 55
    branch B :: 66
    branch B :: 77
    branch A -> 88
    branch B :: 99
    

    So, yes, you needed a “gated semantic predicate” ({boolean}?=>) in this case, not a “validating semantic predicate” ({boolean}?). The difference between the two predicates is explained in this previous SO Q&A: What is a ‘semantic predicate’ in ANTLR?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would like to get a list of advantages and disadvantages of using Stored Procedures.
I would like to realize array objects initialization by using the initialization statement as
I would like to quickly send email from the command line. I realize there
i want to realize a construct in MS SQL that would look like this
I would like to use the WebClient (or there is another better option?) but
I would like dynamically (via JavaScript) hide the address bar. I realize this can
I realize that this question is pretty well discussed, however I would like to
I would like to create an animation of an object using an elliptical path.
I Would like to realize the following loading animation with java swing : The
I would like to make my application somewhat REST compliant. I am using Rails

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.