flex lexer - passing values to yylex -


so want avoid global variables want use flex tokenize input. want know if possible pass value yylex can rid of global s.

right have this

 %{ #include  #include  #include  #include  #include "lex.h" %}  %option noyywrap  digit       [0-9] alpha       [a-za-z] alphanum    {alpha}|{digit}|"_"  %% [\t\n ]                 printf("ws:\n"); {alpha}{alphanum}*      printf("symbol: %s\n",yytext); {digit}+                printf("int: %s\n",yytext); {digit}+"."{digit}      printf("float: %s\n",yytext); "\"".*"\""              printf("litral: %s\n",yytext); "+"                     printf("op: %s\n",yytext); "-"                     printf("op: %s\n",yytext); "*"                     printf("op: %s\n",yytext); "/"                     printf("op: %s\n",yytext); "%"                     printf("op: %s\n",yytext); "="                    printf("op: %s\n",yytext); ""                     printf("op: %s\n",yytext); "=="                    printf("op: %s\n",yytext); "!="                    printf("op: %s\n",yytext); "("                     printf("op: %s\n",yytext); ")"                     printf("op: %s\n",yytext); ","                     printf("op: %s\n",yytext); "="                     printf("op: %s\n",yytext); %%  void lexinit() {     tokens = malloc(sizeof(tokenstream));     tokens->size=0; }  void lexpush(const char* str) {     size_t size = strlen(str);     char* newstr = malloc(size*sizeof(char));     realloc(tokens->tokens,++tokens->size*sizeof(char*));  }  void lex(const char* filepath) {   lexinit();   yyin = fopen(filepath,"r");   yylex(); }  

you should read article related reentrant , extra-type options.

    /* example of overriding yy_extra_type. */      %{      #include       #include       %}      %option reentrant      %option extra-type="struct stat *"      %%       __filesize__     printf( "%ld", yyextra->st_size  );      __lastmod__      printf( "%ld", yyextra->st_mtime );      %%      void scan_file( char* filename )      {          yyscan_t scanner;          struct stat buf;          file *in;           in = fopen( filename, "r" );          stat( filename, &buf );           yylex_init_extra( buf, &scanner );          yyset_in( in, scanner );          yylex( scanner );          yylex_destroy( scanner );           fclose( in );     } 

http://www.cse.yorku.ca/tdb/_doc.php/userg/man_g/file/flex/node/extra%20data


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -