[antlr-interest] How-to: Tree grammar when AST rewrite rules involve parameters?

Gavin Lambert antlr at mirality.co.nz
Sat Sep 13 19:27:55 PDT 2008


At 12:50 14/09/2008, =?ISO-8859-2?Q?M._Utku_Karata=BA?= wrote:
 >// GRAMMAR with AST output:...................
 >expression
 >     //  MESSAGE is imaginary token. operand is passed to the
 >messageChain rule
 >     : operand (messageChain[(CommonTree)$operand.tree])* ->
 >^(MESSAGE
 >messageChain*);
 >
 >messageChain [CommonTree o]
 >      :  unaryMessageChain binaryMessageChain ->
 >^({(CommonTree)$o}
 >unaryMessageChain binaryMessageChain)
 >      | binaryMessage binaryMessageChain -> ^({(CommonTree)$o}
 >binaryMessage binaryMessageChain)
 >      ;
 >
 >// FAILING TREE GRAMMAR for the tree walker (ANTLR simply 
rejects
 >and variations don't helpneither):...................
 >expression
 >     //  MESSAGE is imaginary token. operand is passed to the
 >messageChain rule
 >     : ^(MESSAGE messageChain*);
 >
 >messageChain [CommonTree o]
 >      :  ^({(CommonTree)$o} unaryMessageChain 
binaryMessageChain)
 >      |  ^({(CommonTree)$o} binaryMessage binaryMessageChain)

Well, that's definitely not valid syntax.  It would help if you 
knew what token type that root node is.  Failing that, though, you 
could always just use this:

messageChain
     : ^(. (unaryMessageChain binaryMessageChain | binaryMessage 
binaryMessageChain))
     ;



More information about the antlr-interest mailing list