开发者

How to access class member from bison action

开发者 https://www.devze.com 2023-01-31 22:21 出处:网络
I am calling yyparse from a member function. How to access the member variables/function from the bison action.

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();
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号