Hello I am trying to implement a parser for mathematical expression using antlr4, using c++ as the target.
I used the example calculator grammar as a starting poing, and I am currently parsing and eventually evaluating correctly simple expressions. Now I started addressing using math functions such as sqrt, log, sin, etc... Grammar is mine, but I would like to avoid a sequence of if/elseif in the visitor implementation.
the generated code for the context is:
class OneArgFunctionNameContext : public antlr4::ParserRuleContext {
public:
OneArgFunctionNameContext(antlr4::ParserRuleContext *parent, size_t invokingState);
virtual size_t getRuleIndex() const override;
antlr4::tree::TerminalNode *SQRT();
antlr4::tree::TerminalNode *EXP();
antlr4::tree::TerminalNode *LOG();
antlr4::tree::TerminalNode *LN();
antlr4::tree::TerminalNode *SIN();
antlr4::tree::TerminalNode *COS();
antlr4::tree::TerminalNode *TAN();
antlr4::tree::TerminalNode *ACOS();
antlr4::tree::TerminalNode *ASIN();
antlr4::tree::TerminalNode *ATAN();
antlr4::tree::TerminalNode *ABS();
virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;
};
then, given the examples I had the chance to see and understand, I could to the following:
{
if (ctx->SQRT())
{
... // perform sqrt
}
else if (ctx->EXP())
{
... // perform exp
}
else if (...)
{
...
}
...
}
what I would like to have is a switch statement rather than a sequence of if/elseif, but I cant find how to identify which type is the child terminal node. Something like the following:
switch (ctx->{SOMETHING})
{
case SQRT:
...
break;
case SQRT:
...
break;
...
}
Can it be done this way? I have this feeling that the answer (wether positive or negative) should be pretty obvious, but I cant reach out to grasp it.
Thanks in Advance