弯曲++切换流

发布于 2024-12-14 02:39:44 字数 839 浏览 5 评论 0原文

我正在尝试使用 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;
}

但它做了同样的事情。

I'm trying to write a parser using flex++ and bison. I need the parser to be able to read from a file and write a new file in output.

I have an yyFlexLexer instantiated as follows:

yyFlexLexer lexer;

and I use it:

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;
}

If I run:

./executable foo bar

the parser correctly read the file foo (I can see it making some printing in bison rules) but at the end I found only an empty file called "bar" without anything in it.

I have also tried to do this:

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;
}

but it does the same thing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗地喜欢 2024-12-21 02:39:44

您需要解析器的操作(在 .y 文件中)来构建已解析输入的一些抽象语法树,并且需要一些例程将该树漂亮地打印到输出。

flexbison 仅进行解析(不多)。剩下的事情你应该做。

我不明白你想达到什么目的。

You need the parser's actions (in your .y file) to build some abstract syntax tree of the parsed input, and you need some routine to pretty print that tree to the output.

flex and bison do only the parsing (which is not much). You should do the rest.

I don't understand what you want to achieve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文