2011-01-26 6 views
0

だからグローバル変数を避けたいが、Flexを使って入力をトークン化したい。私はそれがグローバルsを取り除くことができるようにyylexに値を渡すことが可能かどうかを知りたい。yylexに値を渡す

今の私は、あなたが再入可能と余分なタイプのオプションに関連する記事をお読みください。この


%{ 
#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(); 
} 

答えて

1

を持っています。

/* An 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