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

The Archive Base Latest Questions

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

I have the following grammar (i’m showing only the important sectons) packageDeclaration : PACKAGE

  • 0

I have the following grammar (i’m showing only the important sectons)

packageDeclaration
    :   PACKAGE qualifiedIdentifier SEMI -> ^(PACKAGE qualifiedIdentifier+) 
    ;

qualifiedIdentifier
    :   (   IDENT               ->  IDENT
        )
        (   DOT ident=IDENT     ->  ^(DOT $qualifiedIdentifier $ident)
        )*
    ;

and let’s say i have a package decleration of “package a.b.c” (Java parser btw)

What i get now is something of the form
(package (. (. a b) c))

what i want is to inline the package so that i get
(package a.b.c)

I would prefer not to change the rewriting for the qualifiedIdentifier, but only for the packageDeclaration.

How can i do this.

I understand what you mean by alias and + now. But i’m still unclear about the difference between qualifiedIdentifier and $qualifiedIdentifier in the rewrite rule.

As for my second question, what i mean is that i removed the rewrite rule for qualifiedIdentifier and for the package i have the following:

packageDeclaration
    :   PACKAGE ident=qualifiedIdentifier SEMI -> ^(PACKAGE $ident+) 
    ;

What i get as a result of this is nested tokens as in:

 (package
    (a) [end:a]

    (.) [end:.]

    (b) [end:b]

    (.) [end:.]

    (c) [end:c]
  ) [end:package]

Each token is represented as "(<token's text property>) [end: <token's text property>]"

I hope it’s clear in the output above but i have one parent token (package) with 5 children. Now they are in the correct order and all that. What i would like is the same parent with only one child as in:

 (package
    (a.b.c) [end:a.b.c]
  ) [end:package]
  • 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:18:43+00:00Added an answer on May 20, 2026 at 1:18 am

    If you want to remove the DOT‘s from the tree and only keep the tokens a, b and c from import a.b.c; (with PACKAGE as root, of course) try:

    qualifiedIdentifier
      :  IDENT (DOT IDENT)* -> IDENT+
      ;
    

    or with the DOT‘s, simply remove the rewrite rule:

    qualifiedIdentifier
      :  IDENT (DOT IDENT)*
      ;
    

    By the way, your packageDeclaration has an error in it:

    packageDeclaration
      :  PACKAGE qualifiedIdentifier SEMI -> ^(PACKAGE qualifiedIdentifier+)
      ;
    

    qualifiedIdentifier+ should be qualifiedIdentifier instead.

    Coder wrote:

    I would prefer not to change the rewriting for the qualifiedIdentifier, but only for the packageDeclaration.

    That is not possible.

    Unless you do not use qualifiedIdentifier inside packageDeclaration, in which case you could do something like:

    packageDeclaration
      :  PACKAGE IDENT (DOT IDENT)* SEMI -> ^(PACKAGE IDENT+)
      ;
    

    EDIT

    Regarding your comments:

    Coder wrote:

    I’m a little confused about the meaning of + in the rewrite rule. The documentation here antlr.org/wiki/display/ANTLR3/Tree+construction says that you can break apart trees into sequences “a : (^(ID INT))+ -> INT+ ID+ ; // break apart trees into sequences “.

    From the rule:

    a 
      :  (^(ID INT))+ -> INT+ ID+
      ; 
    

    (^(ID INT))+ means there are one or more ID‘s and one ore more INT‘s, and only then you can use + in the rewrite rule (everything to the right of ->).

    You on the other hand have:

    packageDeclaration 
      :  PACKAGE qualifiedIdentifier SEMI
      ; 
    

    there’s only one qualifiedIdentifier in there, so you can only use that sinle qualifiedIdentifier in your rewrite rule (no +!)

    Coder wrote:

    I’m also confused about the difference between specifying an alias in the rewrite rule as in “PACKAGE ident:qualifiedIdentifier -> $ident” vs, using quanlifiedIdentifier vs $quanlifiedIdentifier

    An alias can be used if it’s not apparent which not apparent which rule should be placed where in the tree or if you want more control over how the children are placed. For example, given the rule:

    name
      :  ID '.' ID
      ;
    

    and you want the first ID to become the right child. Doing:

    name
      :  ID '.' ID > ^(NAME ID ID)
      ;
    

    will place the first ID as the left child. To make it the right child, do:

    name
      :  a=ID '.' b=ID > ^(NAME $b $a)
      ;
    

    EDIT II

    Coder wrote:

    What i get as a result of this is nested tokens as in:

    (SNIP)

    Each token is represented as …

    Okay, I see what you mean. You must understand that the parser “feeds of” the lexer. The lexer chops up the input of characters and creates tokens from these characters (IDENT is such a token, just as DOT is). These tokens are then passed to the parser. The parser can’t just create or merge tokens. So the answer is: what you want is not easily done, it certainly is not possible to do in “ANTLR syntax” inside your grammar.

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

Sidebar

Related Questions

I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have following situation. A main table and many other tables linked together with
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following text in a file 23456789 When I tried to replace the
I have following Code Block Which I tried to optimize in the Optimized section
I have following classes. class A { public: void fun(); } class B: public
Suppose I have following string: String asd = this is test ass this is

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.