ANTLR4 v4.7.1 CPP target- visitor bad_cast error

33 views
Skip to first unread message

Rachana Koneru

unread,
Mar 14, 2018, 11:50:22 AM3/14/18
to antlr-discussion
Hi,

I'm trying to use a custom visitor class for a simple expression grammar.

// .h
class MyVisitor: public MyParserBaseVisitor ...

// .cpp  
 
Any MyVisitor::visitExpr(MyParser::ExprContext *ctx) {
   
auto result = visitChildren(ctx);
    std
::cout<< result.as<int>() << std::endl;
   
return result;
 
}

  Any MyVisitor::visitInteger(MyParser::IntegerContext *ctx) {
   
return atoi(ctx->start->toString().c_str());
  }



result.as<int>() throws a bad_cast exception and result.isNull() always return true. What am I doing wrong here? 

Also, can I use anltrcpp::Any to return a pointer to a custom class object or does it only work with primitives?
Reply all
Reply to author
Forward
0 new messages