【发布时间】:2011-11-06 10:43:15
【问题描述】:
我正在尝试使用 flex++ 和 bison 编写解析器。我需要解析器能够读取文件并在输出中写入新文件。
我有一个 yyFlexLexer 实例化如下:
yyFlexLexer lexer;
我使用它:
int main(int argc, char* argv[])
{
std::istream* in_file = new std::ifstream(argv[1])
std::ostream* out_file = new std::ofstream(argv[2])
lexer.switch_streams(in_file, out_file);
yyparse();
return 0;
}
如果我跑:
./executable foo bar
解析器正确读取了文件 foo(我可以看到它在野牛规则中进行了一些打印),但最后我发现只有一个名为“bar”的空文件,其中没有任何内容。
我也尝试过这样做:
int main(int argc, char* argv[])
{
std::istream* in_file = new std::ifstream(argv[1])
std::ostream* out_file = new std::ofstream(argv[2])
lexer.switch_streams(in_file, out_file);
while(lexer.yylex())
;
return 0;
}
但它做同样的事情。
【问题讨论】: