I am calling yyparse from a member function. How to access the member variables/function from the bison action.
I am currently doing as
%{
#include "myclass.h"
#include "parse.tab.hh"
MyClass *ptr=NULL;
void MyClass::evaluate(string expression)
{
开发者_如何学Pythonptr=this;
yy_scan_string(expression.c_str());
yyparse();
}
%}
%%
EXPR : EXPR PLUS EXPR {
$$ = ptr->memberFunction("+",$1,$3);
}
You can set up bison to accept parameters:
%parse-param {MyClass* self}
%lex-param {MyClass* self}
%{
void MyClass::evaluate(string expression)
{
yy_scan_string(expression.c_str());
yyparse(this); /* %parse-param allows a parameter to yyparse */
}
%}
%%
EXPR : EXPR PLUS EXPR {$$ = self->memberFunction("+",$1,$3);}
%%
int yylex(MyClass* self) /* Controlled by %lex-param */
{
return self->lex();
}
精彩评论