[antlr-interest] manual AST construction: what am I doing wrong?

Jorge Scandaliaris j_scandaliaris at yahoo.es
Sun Jun 15 04:26:35 PDT 2003


Hi,
	How can I generate for a rule a single AST node, using manual
tree construction? 
	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.
	I am doing a special use of AST, basically I am not going to
walk my tree, I just use the nodes as "supertokens" that can hold the
information I collect while parsing. 
	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?). 
I tried also to suffix all sub rules but one, leaving automatic tree
construction on. This works, but it has two bad points: 
1. The node for the rule is created from the sub rule I didn't suffix,
including the information it carries (which I might not want). That
forces me to "reset" the node before putting in it the info I want.
2. In some rules say 
	rule: (sub_rule)* 
or
	rule: (sub_rule)+
I am forced to change the grammar so as to create only one AST node,
adding complexity and loosing the readability that characterizes antlr
grammars.

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?

Thanks,

Jorge




 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list