[antlr-interest] Custom AST Node
Henry Butowsky
henryb at ntlworld.com
Fri Aug 4 04:18:38 PDT 2006
Hi Guys,
I using Antlr 2.7.5-6 and the cpp runtime.
I wanted to add line number to my tree parsing error messages so I
created my own AST node called ncoAST ( following the C++ notes in
http://www.antlr.org/doc/cpp-runtime.html#_bb1 )
I have a tree parsing function called "statements" --this calls other
tree parsing including "out" which is my c like expression evaluator
However in order to get the program to compile I have had to add
casts where the AST nodes refer to a child or sibling e.g
run((RefncoAST)(blk->getFirstChild()));
var=out((RefncoAST)(exp->getFirstChild()));
I would like to sort out ncoAST so I don't have to add casts as code
like above is doted throughout my tree parser -- Im no expert in C++
Pleae help
Many thx
Regards Henry
statements
{
var_sct *var;
}
: blk:BLOCK {
run((RefncoAST)(blk->getFirstChild()));
}
| exp:EXPR {
if(exp->getFirstChild()->getType() == ASSIGN)
cout << "Type ASSIGN " <<
exp->getFirstChild()->getFirstChild()->getText() <<endl;
var=out((RefncoAST)(exp->getFirstChild()));
var=nco_var_free(var);
}
| iff:IF {
bool br;
var_sct *var1;
RefncoAST ex;
//Calculate logical expression
var1= out( iff->getFirstChild());
br=ncap_var_lgcl(var1);
var1=nco_var_free(var1);
if(br) {
run(iff->getFirstChild()->getNextSibling() );
}else{
// See if else exists
ex=iff->getFirstChild()->getNextSibling()->getNextSibling();
if(ex && ex->getType()==ELSE ) run(ex->getFirstChild());
}
var=(var_sct*)NULL;
}
| ELSE {
}
| def:DEFDIM {
const char *dmn_nm;
long sz;
dmn_nm=def->getFirstChild()->getText().c_str();
var=out(def->getFirstChild()->getNextSibling());
var=nco_var_cnf_typ(NC_INT,var);
(void)cast_void_nctype(NC_INT,&var->val);
sz=*var->val.lp;
var=(var_sct*)nco_var_free(var);
(void)ncap_def_dim(dmn_nm,sz,prs_arg);
}
| NULL_NODE {
}
;
More information about the antlr-interest
mailing list