1
votes

I've got an antlr grammar that has a rule like so:

rule:
ID (COMMA ID)*
;

where ID is a lexer rule for matching a typical variable name and comma is a lexer rule that matches a comma. So the rule matches one or more comma separated variables

I'd like to create a tree that looks like so:

ITEM 
    ID
ITEM
    ID
....

where ITEM is an imaginary node that I insert in front of each ID that is found.

How would I do such a thing with the tree rewrite syntax?

Typically if I wanted to gather all the ids I'd just use

rule:
ID (COMMA ID)*  -> ^(ITEM ID+)
;

but this produces the tree:

ITEM
    ID 
    ID
    .... 

I'm using C# if it matters as the implementation langauge and ANTLR 3.1.3

1

1 Answers

1
votes

I came to my senses quickly after asking the question.

To get the tree shape I'd like I need to use the rewrite syntax like so:

rule:
ID (COMMA ID)*  -> ^(ITEM ID)+
;