title: Antlr 4 C++ target grammar rule returning lambda, missing attribute access on rule reference error
body: <p>I'm a new to both C++ and Antlr, so pardon my ignorance.</p>
  
  <p>I need millions of values to be derived based on several rules.</p>
  
  <pre><code>Eg rule1:-  Value = ob.field1 * ob.field2 //the user defines the rule
  Eg rule2:-  Value = 4* ob.field4 &lt; 3* ob.field1 ? 5 : 0b.field6 
  </code></pre>
  
  <p>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. </p>
  
  <p>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. </p>
  
  <pre><code>grammar calculator;
  
  
  start: expr EOF;
  
  expr returns [std::function&lt;double(double)&gt; exprEval]
      : left=expr op=('+'|'-') right=expr {$exprEval= [](double k)-&gt;double { return $left.exprEval(k) + $right.exprEval(k); }; }
      | left=expr op=('*'|'/') right=expr {$exprEval= [](double k)-&gt;double { std::cout&lt;&lt;2*k&lt;&lt;std::endl;  return -2*k; }; }
      | '(' expr ')'                      {$exprEval= [](double k)-&gt;double { std::cout&lt;&lt;k&lt;&lt;std::endl;    return -1*k; }; }
      | numb                              {$exprEval= [](double k)-&gt;double { std::cout&lt;&lt;-1*k&lt;&lt;std::endl; return k;    }; }
      ;
  
  numb 
      :DOUBLE
      |INT 
      ;
  
  INT 
      : [0-9]+
      ;
  
  DOUBLE
      : [0-9]+'.'[0-9]+
      ;
  
  WS
     : [ \r\n\t] + -&gt; channel (HIDDEN)
     ;
  </code></pre>
  
  <p>It produces the following errors. I think I'm referencing them wrong. </p>
  
  <pre><code>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
  </code></pre>
  
  <p>following don't work either. </p>
  
  <pre><code>$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
  </code></pre>
  
  <p>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?</p>
  
  <p>I think parsing the rules everytime is not a good idea since there are millions of records. </p>