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

M. Utku Karataş m.utku.k at gmail.com
Sat Sep 13 17:50:30 PDT 2008


Hi all,

After practically inhaling the Definitive Antlr Reference (kudos to
Terence Parr for this great piece) for a few days, I started to build
a little smalltalk variant for practising and here goes my first show
stopper:

I am trying to generate a walker for the following grammar with no
avail. Note that my main aim is that i want to push the "operand" leaf
into the "messageChain" AST rather than having "operand" leaf in the
"expression" AST. Any ideas on how to create a correct tree grammar
appreciated.

Here is an excerpt of the grammars:

// 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)

TIA,
Utku.


More information about the antlr-interest mailing list