After the problem about AST rewrite rule with devide group technique at AST rewrite rule with " * +" in antlr.
I have a trouble with AST generating in ANTLR, again :).Here is my antlr code :
start : noun1+=n (prep noun2+=n (COMMA noun3+=n)*)*
-> ^(NOUN $noun1) (^(PREP prep) ^(NOUN $noun2) ^(NOUN $noun3)*)*
;
n : 'noun1'|'noun2'|'noun3'|'noun4'|'noun5';
prep : 'and'|'in';
COMMA : ',';
Now, with input : “noun1 and noun2, noun3 in noun4, noun5”, i got following unexpected AST:

Compare with the “Parse Tree” in ANLRwork:

I think the $noun3 variable holding the list of all “n” in “COMMA noun3+=n”. Consequently, AST parser ^(NOUN $noun3)* will draw all “n” without sperating which “n” actually belongs to the “prep”s.
Are there any way that can make the sepration in “(^(PREP prep) ^(NOUN $noun2) ^(NOUN $noun3))“. All I want to do is AST must draw exactly, without token COMMA, with “Parse Tree” in ANTLRwork.
Thanks for help !
Getting the separation that you want is easiest if you break up the
startrule. Here’s an example (without writingCOMMAs to the AST):A
prepphraseis a noun that may be followed by a preposition with another noun. Thestartrule looks for comma-separatedprepphrases.The output appears like the parse tree image, but without the commas.
If you prefer explicitly writing out ASTs with
->or if you don’t like syntax likeCOMMA!, you can write thestartrule like this instead. The two different forms are functionally equivalent.