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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:34:16+00:00 2026-06-09T03:34:16+00:00

I have a rule that I would like to split as I would like

  • 0

I have a rule that I would like to split as I would like to re-use the same sub rule in other rules too. The original version works fine:

type
  : ( basicType -> basicType )
  ( '*' -> ^(TYPE_POINTER basicType)
  | '[' ']' -> ^(TYPE_DYN_ARRAY basicType)
  | '[' IntegerLiteral ']' -> ^(TYPE_STATIC_ARRAY IntegerLiteral basicType)
  | '[' IntegerLiteral '..' IntegerLiteral ']' -> ^(REF_TYPE_SLICE IntegerLiteral IntegerLiteral basicType)
  | '[' type ']' -> ^(TYPE_MAP_ARRAY type basicType)
  )?
  ;

This rule makes it possible to take a normal type (basicType, which can be a reference or a primary type, like int, char, etc..) and place under a different AST node, which makes it pointer, array, or so. This is very common thing in C like languages.

However, if I split like this:

basicType2
  : '*' -> ^(TYPE_POINTER)
  | '[' ']' -> ^(TYPE_DYN_ARRAY)
  | '[' IntegerLiteral ']' -> ^(TYPE_STATIC_ARRAY IntegerLiteral)
  | '[' IntegerLiteral '..' IntegerLiteral ']' -> ^(REF_TYPE_SLICE IntegerLiteral IntegerLiteral)
  | '[' type ']' -> ^(TYPE_MAP_ARRAY type)
  ;

type
  : ( basicType -> basicType )
    ( basicType2 -> ^(basicType2 basicType) )?
  ;

Everything looks great and the parsing will not be effected, however in the AST the basicType2’s children are missing completely. In the case of TYPE_STATIC_ARRAY, or REF_TYPE_SLICE, or TYPE_MAP_ARRAY, only the root will be copied above the type but sub nodes are missing.

I debugged the parser code, and it seems to me that the CommonTree class’ copy constructor which is called when the doesn’t copy the children, only the token and the source range information. Is there any way to bring a CommonTree node along with it’s children to the top of an other with rewrite rules?

  • 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-09T03:34:18+00:00Added an answer on June 9, 2026 at 3:34 am

    Always use a single token as the root of a tree: not another tree, as you’re doing with basicType2 in:

    type
      : ( basicType -> basicType )
        ( basicType2 -> ^(basicType2 basicType) )?
      ;
    

    Try this instead:

    grammar ...
    
    options { ... }
    
    tokens { T2; }
    
    ...
    
    type
      : ( basicType -> basicType )
        ( basicType2 -> ^(T2 basicType2 basicType) )?
      ;
    
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my GNUmakefile, I would like to have a rule that uses a temporary
I have a rule that works for one direction but, not the other. A
I would like in my GNUmakefile to have a target rule that invokes a
With at-rules, it's possible to have a ruleset for elements during that at-rule event
I would like to write a CSS selector rule that selects all elements that
I would like to know how to delete a Deny rule that makes imposible
I have a concept for a framework of controls that I would like to
I have several domains that I would like to have wildcard subdomains enabled for
I think this may be relatively straight forward. I have a rewrite rule that
I have the following rewrite rule that I need to alter in order to

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.