Take the 2-minute tour ×

I'm building an AST using ANTLR. I want to write a production that matches this string:


In my grammar file I have:


This works fine. I'm using my own adaptor to emit tree nodes. The rewrite rule used creates for me two nodes: one for NODE_VAR_REFERENCE and one for IDENT.

What I want to do is create only one node (for the NODE_VAR_REFERENCE token), and this node must have the IDENT token in its "token" field.

Is this possible using a rewrite rule? Thanks.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Well, to let IDENT be the token of the node NODE_VAR_REFERENCE would mean there isn't any NODE_VAR_REFERENCE at all. A token consists of a type (NODE_VAR_REFERENCE or IDENT) and some text this token matched. To let a tree node's token become IDENT would mean both the type and text would be that of IDENT (losing the NODE_VAR_REFERENCE token, or type).

What you probably mean is to have single node with type NODE_VAR_REFERENCE and the text of IDENT, in which case you could do something like this:

share|improve this answer
that's what I mean. Thanks. –  Antonio Nov 21 '12 at 9:35
You're welcome @Antonio. –  Bart Kiers Nov 21 '12 at 22:58
add comment

Your Answer


