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]
If you want to remove the
DOT‘s from the tree and only keep the tokensa,bandcfromimport a.b.c;(withPACKAGEas root, of course) try:or with the
DOT‘s, simply remove the rewrite rule:By the way, your
packageDeclarationhas an error in it:qualifiedIdentifier+should bequalifiedIdentifierinstead.That is not possible.
Unless you do not use
qualifiedIdentifierinsidepackageDeclaration, in which case you could do something like:EDIT
Regarding your comments:
From the rule:
(^(ID INT))+means there are one or moreID‘s and one ore moreINT‘s, and only then you can use+in the rewrite rule (everything to the right of ->).You on the other hand have:
there’s only one
qualifiedIdentifierin there, so you can only use that sinlequalifiedIdentifierin your rewrite rule (no+!)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:
and you want the first
IDto become the right child. Doing:will place the first ID as the left child. To make it the right child, do:
EDIT II
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 (
IDENTis such a token, just asDOTis). 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.