[antlr-interest] manual AST construction: what am I doing wrong?
Ric Klaren
klaren at cs.utwente.nl
Mon Jun 16 07:43:40 PDT 2003
On Sun, Jun 15, 2003 at 01:26:35PM +0200, Jorge Scandaliaris wrote:
> How can I generate for a rule a single AST node, using manual
> tree construction?
In a nutshell:
myrule ! : st:SOME_TOKEN
{ ## = #([MY_IMAG_AST_NODE, st->getText()]);
##->setSomeAttrib( sumthin );
}
;
Alternatively you can also use #myrule in stead of ##.
> I have created created a custom AST class. I have defined the
> option ASTLabelType = "RefMyAST"; It is sort of working. I say
> this 'cause I came over some issues and I am not sure if it is ANTLR'
> expected behaviour or I did something wrong.
> In view of this I decided that I don't need to generate all the
> nodes corresponding to all sub rules, but that I only need the node
> corresponding to the rule.
> I tried using ! at the rule and sub rules levels, but here is
> where problems appear. If I use say:
> rule!
> : .....
> { ## = #[##];
> #->addSrc(...); // addSrc() is defined for MyAST
> }
> I have a run-time error in addSrc(). I think this is due to a missing
> astFactory->addChild(...)in the generated code compared to when
> automatic AST building is done (what is the purpose of this anyway? And
> why it is only there for automatic tree building?).
The addChild stuff is used to generate the tree e.g. to link sibling/child
nodes together. If you turn it off you have to do this manually via the
#( .. ) syntax. If you have rules with multiple parts which may return AST
trees you may have to label them and then put them together yourself.
e.g.
some_rule: p1:other_rule p2:ignore_rule p3:great_other_rule
{
## = #( #p1, #p3 ); // only link up p1 and p3
};
Hmmm lemme refer to this old post of Mike Barnett for some more stuff:
http://wwwhome.cs.utwente.nl/~klaren/antlr/treebuilding.txt
> Please, if someone can enlighten me on this, or point me to some
> resources (I didn't find a lot). Maybe I got the semantics of AST
> completely wrong?
There might be some jewels in the archives, maybe even still something
usefull in the FAQ site.
Cheers,
Ric
--
-----+++++*****************************************************+++++++++-------
---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893722 ----
-----+++++*****************************************************+++++++++-------
"You can't expect to wield supreme executive power just because some
watery tot throws a sword at you!"
--- Monty Python and the Holy Grail
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list