Pergunta

Is it possible to parse compressed files in flex?

yyin is a pointer of type FILE*. So I would like to do something like this: create a pipe of compressed file and set yyin to it?

Foi útil?

Solução

With flex, you can define the macro YY_INPUT(buf,result,maxlen) to change how flex obtains input. The macro must read at most maxlen bytes into buf, and return the actual number of bytes stored in result, or set result to YY_NULL to indicate EOF.

For example, using the convenience interface of zlib, you could insert something like the following into your flex file:

 %{

 #include <zlib.h>
 gzFile gz_yyin;
 #define YY_INPUT(buf,result,maxlen) do {  \
    int n = gzread(gz_yyin, buf, maxlen);  \
    if (n < 0) { /* handle the error */ }  \
    result = n > 0 ? n : YY_NULL;     \
 } while (0)

 %}

 // lots of stuff skipped

 int main(int argc, char** argv) {
   gz_yyin = gzopen(argv[1], "rb");
   if (gz_yyin == NULL) { /* handle the error */ }
   /* Start parsing */
   // ...

(You can use gzdopen to create a gzfile using an open file descriptor, such as a pipe.)

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