udakarajd
udakarajd

Reputation: 114

Antlr 4 C++ target grammar rule returning lambda, missing attribute access on rule reference error

I'm a new to both C++ and Antlr, so pardon my ignorance.

I need millions of values to be derived based on several rules.

Eg rule1:-  Value = ob.field1 * ob.field2 //the user defines the rule
Eg rule2:-  Value = 4* ob.field4 < 3* ob.field1 ? 5 : 0b.field6 

so, I need to parse the rules only once and generate functors (or lambdas) so I can keep them in a map and call them any time. Ony ob instance is different each time.

This is the simple sample I came up with, k is a double value I'm passing as a parameter for this sample, and it would be an object later.

grammar calculator;


start: expr EOF;

expr returns [std::function<double(double)> exprEval]
    : left=expr op=('+'|'-') right=expr {$exprEval= [](double k)->double { return $left.exprEval(k) + $right.exprEval(k); }; }
    | left=expr op=('*'|'/') right=expr {$exprEval= [](double k)->double { std::cout<<2*k<<std::endl;  return -2*k; }; }
    | '(' expr ')'                      {$exprEval= [](double k)->double { std::cout<<k<<std::endl;    return -1*k; }; }
    | numb                              {$exprEval= [](double k)->double { std::cout<<-1*k<<std::endl; return k;    }; }
    ;

numb 
    :DOUBLE
    |INT 
    ;

INT 
    : [0-9]+
    ;

DOUBLE
    : [0-9]+'.'[0-9]+
    ;

WS
   : [ \r\n\t] + -> channel (HIDDEN)
   ;

It produces the following errors. I think I'm referencing them wrong.

error(67): calculator.g4:6:152: missing attribute access on rule reference left in $left
error(67): calculator.g4:6:172: missing attribute access on rule reference right in $right

following don't work either.

$left.ctx.exprEval(k) //compilation error :  in lambda, localctx is not captured.
ctx.$left.exprEval(k) //compilation error : ctx was not declared in this scope

How do I access the "left" and "right" expression contexts from inside lambda? Or isn't this the best approach? Is there a better way?

I think parsing the rules everytime is not a good idea since there are millions of records.

Upvotes: 1

Views: 222

Answers (1)

Mike Lischke
Mike Lischke

Reputation: 53317

You probably could get away by adjusting the capture in your lambdas, but I strongly recommend to change your approach. Don't write all the code in your grammar, but instead create a listener (or visitor if you need to evaluate expressions) and implement all that there. Much easier to maintain and you avoid trouble like this.

Upvotes: 1

Related Questions