2
votes

I am currently working on a flex/bison project.

I need to parse multiple files in one execution, so i made a loop to run YYPARSE() multiple times.

When flex finds a lexical or syntactic error, the parsing of a file stops and the program starts parsing the next file. However, the parsing of that new file doesn't start from the beginning. Indeed, if the parsing stopped at line 8 for file 3 the parsing will start form line 8 in file 4.

How could i solve this problem ?

Thanks in advance.

Here is my main function in my bison.y file:

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


DIR* dir;

struct dirent *ent;

int val = 0;

    if ((dir = opendir ("../TpCompileHoho")) != NULL) 
    {
      // print all the files and directories within directory 
      while ((ent = readdir (dir)) != NULL) 
      { 
        if ((strcmp(ent->d_name,".") != 0) && (strcmp(ent->d_name,"..") != 0) && (strstr(ent->d_name,".txt") != NULL))
        {   
                yyin = fopen(ent->d_name,"r");

                yyparse();          
        }

      }                         
      closedir (dir);
    } 
    else 
    {
      // could not open directory 
      perror ("could not open directory");
      return EXIT_FAILURE;
    }

}

1

1 Answers

4
votes

If you need to discard the rest of the current input and start parsing a new file, you need to call yyrestart(yyin) instead of simply setting yyin to a new value. Setting yyin to a new value works fine only if the previous file was read up to the end-of-file marker.

You should also remember to close the old file. The flex-generated scanner will not do that for you.