Is it possible to parse compressed files in flex?
可以用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?
yyin是类型文件*的指针。所以我想做这样的事情:创建一个压缩文件的管道并设置yyin到它?
1 个解决方案
#1
2
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.
使用flex,您可以定义宏YY_INPUT(buf,result,maxlen)来更改flex获取输入的方式。宏必须在大多数maxlen字节中读入buf,并返回存储在结果中的实际字节数,或者将结果设置为YY_NULL以表示EOF。
For example, using the convenience interface of zlib
, you could insert something like the following into your flex file:
例如,使用zlib的便利接口,您可以在flex文件中插入如下内容:
%{
#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.)
(可以使用gzdopen来使用打开的文件描述符(比如管道)创建一个gzfile。)
#1
2
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.
使用flex,您可以定义宏YY_INPUT(buf,result,maxlen)来更改flex获取输入的方式。宏必须在大多数maxlen字节中读入buf,并返回存储在结果中的实际字节数,或者将结果设置为YY_NULL以表示EOF。
For example, using the convenience interface of zlib
, you could insert something like the following into your flex file:
例如,使用zlib的便利接口,您可以在flex文件中插入如下内容:
%{
#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.)
(可以使用gzdopen来使用打开的文件描述符(比如管道)创建一个gzfile。)