连续使用 yylex() 两次
所以我的问题很简单,我想它之前已经得到了回答,但我无法得到一个关于它的好线索:我可以解析一个文件一次,收集一些需要的信息来初始化我的结构,然后解析它“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 yyrestart(FILE *in)。
但请注意 flex 手册 建议您应该第一次构建一个解析树,而不是像这样的两遍扫描器。
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.
因此,如果有人感兴趣的话,这就是:
我尝试使用
rewind
,这是一个通过#include
提供的功能。尽管如此,在写了类似这样的内容之后:结果我仍然有不好的行为。这是我完全解决问题的方法:
在我的词法分析器中,我添加了:
经过此修改和上面详述的代码,生成的代码按预期运行。
我希望这会有一些用处!
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 :I still had a bad behavior as a result. Here is how I totally solved my problem :
In my lexer, I added :
After this modification and the code detailed above, the resulting code ran as expected.
I hope that will be of some use !
是的,您已到达文件末尾。您需要重新初始化读指针。不过,请查看此链接 可能是更好的解决方案。
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.