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

I would like to checkout a single file from a remote git-repository. Is there
I would like to have a stored procedure that will update values in a
In my application, I would like to fetch a set of entities from the
I realize that they are different beast used to solve different problems, but I
Through a comedy of noob errors I find myself in the following situation in
While I realize there is a similar question ( How to serialize an Exception
I declared a global function in a .cpp file void functionA() . I would
I have a method that takes an NSMutableArray as a parameter, and I would
I'm using the CheckStyle plugin for Eclipse. It's great at finding things I didn't
If I have an active System.Threading.Timer and I set it to null, is it

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.