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 7914129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:00:45+00:00 2026-06-03T14:00:45+00:00

Lets suppose the following scenarios with 2 ANTLR grammars: 1) expr : antExp+; antExpr

  • 0

Lets suppose the following scenarios with 2 ANTLR grammars:

1)

expr     : antExp+;
antExpr  : '{' T '}' ;
T        : 'foo';

2)

expr     : antExpr; 
antExpr  : '{' T* '}' ;
T        : 'bar';

In both cases I need to know how to iterate over antExp+ and T*, because I need to generate an ArrayList of each element of them. Of course my grammar is more complex, but I think that this example should explain what I’m needing. Thank you!

  • 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-06-03T14:00:46+00:00Added an answer on June 3, 2026 at 2:00 pm

    Production rules in ANTLR can have one or more return types which you can reference inside a loop (a (...)* or (...)+). So, let’s say you want to print each of the T‘s text the antExp rule matches. This could be done like this:

    expr
     : (antExp {System.out.println($antExp.str);} )+
     ;
    
    antExpr returns [String str]
     : '{' T '}' {$str = $T.text;}
     ;
    
    T : 'foo';
    

    The same principle holds for example grammar #2:

    expr     : antExpr; 
    antExpr  : '{' (T {System.out.println($T.text);} )* '}' ;
    T        : 'bar';
    

    EDIT

    Note that you’re not restricted to returning a single reference. Running the parser generated from:

    grammar T;  
    
    parse
     : ids {System.out.println($ids.firstId + "\n" + $ids.allIds);}
     ;
    
    ids returns [String firstId, List<String> allIds]
    @init{$allIds = new ArrayList<String>();}
    @after{$firstId = $allIds.get(0);}
     : (ID {$allIds.add($ID.text);})+
     ;
    
    ID    : ('a'..'z' | 'A'..'Z')+;
    SPACE : ' ' {skip();};
    

    on the input "aaa bbb ccc" would print the following:

    aaa
    [aaa, bbb, ccc]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets suppose that I have some pages some.web/articles/details/5 some.web/users/info/bob some.web/foo/bar/7 that can call a
Let's suppose I have the following url: /valid/django/app/path/?foo=bar&spam=eggs I can simulate a request to
Lets suppose that I have the following simple query var q = from p
Lets suppose activities(A,B,C) are opened in following sequence: A -> B -> A ->
Lets suppose I have a scenario with the following model: An Animal table which
Lets suppose i have the following array submitted by a html form: array( 'firstname'
I have the following database person_id| field a | field b |friend_id Lets suppose
Suppose I have a the following in Scala object Foo { var functions: List[String
Lets suppose that I have a Category table with a column that holds the
Lets suppose i am in a local ABC with branches branch_A and branch_B .

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.