Pergunta

i'm search way for attaching my class to flex. If i get token, i need count it. Code below, where stateMap - other class. g++ can't compile this with "error: ‘statTable’ was not declared in this scope". How i can do this without bison?

%{
#include "mapstat.h"
...
}%
%%
{some pattern}     {    
                      int i=0,key,count;
                      while(yytext[i]!=' '){
                         lineNumber[i]=yytext[i];
                         ++i;
                      }
                      lineNumber[i]=0;
                      key=atoi(lineNumber);
                      count=statTable->find(key);
                   }
%%
int main(int argc, char *argv[]) {
       mapStat *statTable=new mapStat;
  ...
 }
Foi útil?

Solução

For this particular use case I think it's reasonable to make statTable a global variable (declare outside of main function, and initialize it before calling the yylex() function:

 %{
     #include "mapstat.h"
     extern mapStat* statTable;
     // ...
 }%

 mapStat* statTable = NULL;

 int main(int argc, char *argv[]) {
     statTable=new mapStat;
     // ...
 }

You should consider having a singleton class to enable more encapsulation of semantics and access of the statisics functionality (that's one of the rare valid use cases).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top