简体   繁体   中英

Problems about yyin and yyout using in lex for lexical analysis

This is main function for lexical analysis.

If I use yyin for getting data from file, yyout execute and writes to file, but when I give codes from terminal yyout isn't working. How can I solve that?

int main(int argc, char *argv[])
{

extern FILE *yyin, *yyout; 
    char *input;
    printf("flexing there\n argc=%d\n argv[0]=%s argv[1]=%s \n",argc,argv[0],argv[1]);
    if(argc > 1 && strstr(argv[1],".g++") != NULL){
    printf("we will read your file = %s\n",argv[1]);
    
    yyin = fopen(argv[1], "r"); 
  
  
    }
    else if(argc > 1){
        printf("your file type is wrong ,must be .g++ type\n");
        return 0;
    }
    else{
        printf("enter your code please\n ");
    }
    yyout = fopen("outputlex.txt", "w"); 
    yylex();
    
    
    
   return 0;

}

I use yyout at rule section like that

%%

"("         fprintf(yyout,"OP_OP\n");

%%

In C, when you write to a file, the output is kept in a buffer until there is enough data to make it worthwhile writing, typically around 4kb. So if you just write a few bytes, you won't see anything in the file until it is closed.

By contrast, when you write to the terminal, the output is usually only buffered until a newline character is written. And if you write to stderr , the output is always immediate.

You can change the buffering for a stream by calling setvbuf right after the fopen . You can also force the stream's buffer to be sent using fflush .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM