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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:07:53+00:00 2026-05-26T20:07:53+00:00

I am trying to write an antlr script where in, rule1 has a subrule

  • 0

I am trying to write an antlr script where in, rule1 has a subrule , rule2. I am using StringTemplate on rule 1.

What I want to do, is restructure the text matched by rule2 before it is consumed/used by rule1. How do I do that ?

options{
output=template;
}

rule1 :
  begin sub-rule2 end ';' -> meth(body={rule1.text})

sub-rule2 :
   sub-rule3
 | sub-rule4
 | sub-rule5;

here “meth” is the stringtemplate call

If say sub-rule 4 matches “select * from dual;”, I would like this to be passed to rule1 “#sql (select * from dual);”.

Here is my Actual code I would like the statements matched by the select_statement rule to be wrapped in ‘#sql()’ and to be passed in “stats” list to the “body” attribute of the “meth” template :

body 
@init {
      List stats = new ArrayList();
    }   :   
    BEGIN s=statement{ stats.add($s.text); } SEMI ( s=statement{ stats.add($s.text); } SEMI | pragma SEMI )*
    ( EXCEPTION exception_handler+ )? END ID? -> method(modifiers={"public"},returnType={"void"},name={"execute"},body={stats})
    ;


statement :
    label*
    ( assign_or_call_statement
    | case_statement
    | close_statement
    | continue_statement
    | basic_loop_statement
    | execute_immediate_statement
    | exit_statement
    | fetch_statement
    | for_loop_statement
    | forall_statement
    | goto_statement
    | if_statement
    | null_statement
    | open_statement
    | plsql_block
    | raise_statement
    | return_statement
    | sql_statement 
    | while_loop_statement
    ) 
    ;

    sql_statement
    : (commit_statement
    | delete_statement
    | insert_statement
    | lock_table_statement
    | rollback_statement
    | savepoint_statement
    | select_statement 
    | set_transaction_statement
    | update_statement )
    ;


    select_statement :
        SELECT swallow_to_semi 
    ;

    SELECT  :   'select';

    swallow_to_semi :
        ~( SEMI )+
    ;
  • 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-26T20:07:54+00:00Added an answer on May 26, 2026 at 8:07 pm

    You can specifically define what a rule can return like this:

    sub_rule2 returns [String x] 
      :  sub_rule3 {$x = ... }
      |  sub_rule4 {$x = "#sql (" + $sub_rule4.text + ");";}
      |  sub_rule5 {$x = ... }
      ;
    

    Now sub_rule2 returns a String x which you can use like this:

    rule1
      :  ... sub_rule2 ... -> meth(body={sub_rule2.x})
      ;
    

    Note the sub_rule2.x.

    EDIT

    You could also create a custom method that checks if the text to be added to the List starts with "select " like this:

    grammar YourGrammarName;
    
    options{
      output=template;
    }
    
    @parser::members {
      private void addStat(String stat, List<String statList>) {
        // 1. if `stat` starts with "select ", wrap "#sql(...)" around it.
        // 2. add `stat` to `statList`
      }
    }
    
    body 
    @init {
      List<String> stats = new ArrayList<String>();
    }   
      :  BEGIN s=statement { addStat($s.text, stats); } SEMI 
         ( s=statement     { addStat($s.text, stats); } SEMI 
         | pragma                                      SEMI
         )*
         (EXCEPTION exception_handler+)? END ID? 
    
         -> method(modifiers={"public"},returnType={"void"},name={"execute"},body={stats})
      ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple interactive (using System.in as source) language using antlr,
I am trying to write an ANTLR parser rule that matches a list of
I am trying to write a comment matching rule in ANTLR, which is currently
I'm trying to write an ANTLR3 grammar that generates HTML output using StringTemplate. To
I'm trying write a Ruby script that checks if user credentials are valid using
Trying to write a windows speech recognition macro. Written using XML and scripting language
Trying to write a simple C# script to copy file to directory one level
Trying to write Unit test for Silverlight 4.0 using Moq 4.0.10531.7 public delegate void
Im trying to write a simple script that slides the next 'slide' div in
I'm trying write a simple perl script that reads some fields from a password

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.