连续使用 yylex() 两次

发布于 2024-12-19 12:17:06 字数 184 浏览 5 评论 0原文

所以我的问题很简单,我想它之前已经得到了回答,但我无法得到一个关于它的好线索:我可以解析一个文件一次,收集一些需要的信息来初始化我的结构,然后解析它“for真的”第二次?

当我调用 yylex() 两次时,它似乎没有执行任何操作。或者我需要重置文件的读取指针?我是一个 C 菜鸟,所以这可能是一个愚蠢的问题!无论如何,任何帮助将不胜感激。

So my question is pretty simple, I guess it has been answered before but I can't get my hands on a good thread about it: can I parse a file once, gather some needed information to initialize my structures and then parse it "for real" a second time?

When I call yylex() twice it just doesn't seem to do anything. Or is it that I need to reset the read pointer of my file? I'm a C noob so it may be a stupid question! Anyway any help will be appreciated.

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

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

发布评论

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

评论(3

俯瞰星空 2024-12-26 12:17:06

Use yyrestart(FILE *in).

But note that the flex manual suggests that instead of a two-pass scanner like this, you should build a parse tree the first time.

烟织青萝梦 2024-12-26 12:17:06

因此,如果有人感兴趣的话,这就是:

我尝试使用 rewind,这是一个通过 #include 提供的功能。尽管如此,在写了类似这样的内容之后:

yylex();
rewind(yyin);
yylex();

结果我仍然有不好的行为。这是我完全解决问题的方法:
在我的词法分析器中,我添加了:

<<EOF>>    yyterminate();

经过此修改和上面详述的代码,生成的代码按预期运行。

我希望这会有一些用处!

So here it is if it interests someone :

I tried to use rewind, a function available through #include <stdio.h>. Though, after having written something like :

yylex();
rewind(yyin);
yylex();

I still had a bad behavior as a result. Here is how I totally solved my problem :
In my lexer, I added :

<<EOF>>    yyterminate();

After this modification and the code detailed above, the resulting code ran as expected.

I hope that will be of some use !

伏妖词 2024-12-26 12:17:06

是的,您已到达文件末尾。您需要重新初始化读指针。不过,请查看此链接 可能是更好的解决方案。

Yup, you've hit the end of the file. You need to reinitialize your read pointer. However, check out this link for what would probably be a better solution.

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