[antlr-interest] Tree Blocks,

Martin Probst mail at martin-probst.com
Mon Aug 8 04:55:52 PDT 2005


Hi,
> 
> I am a little unsure of the how’s and whyfore’s of this construct. Is
> there some good documentation on the #BLOCK thing 

Well, there is some documentation about manual tree construction in the
ANTLR book, but this question is asked quite frequently on this list -
maybe it should go into some kind of FAQ.

> and how and what exactly is meant by 
> {#block = #([BLOCK, "block"], #block);}

This basically means the following:
#block identifies the root of AST created by a parser rule called
"block". This of course only works if you're within a rule called block.
#( A, B, ..) is the syntax to create a subtree. It means create an AST
tree with the root A having children B (, C, D etc, if supplied).
[ASTType, "text"] means create a new AST node of type ASTType (here:
BLOCK) with the text "text", where the text part is optional. This is
actually a shorthand, it should be #[], but the # can be left away
within a #()*. 
So in summary: create an AST tree where the root node is a newly created
AST node of type BLOCK with the text "block", and as a single child
whatever is currently the AST that is being created by the block rule.
Then assign this new AST to the block rules AST.

The whole idea is that this imaginary node makes it easier to identify
expression blocks, and therefore easier (and more explicit) to match
them within later parsing steps.

HTH,
Martin

* This is btw. something I can't really understand - this shorthand only
saves one character, makes the code less obvious and leads to a lot of
irritation with newbies.



More information about the antlr-interest mailing list