END OF FILE token with flex and bison (only works without it)

我们两清 提交于 2019-12-02 06:14:25

It's a little hard to tell what's going on here without seeing your code (and I don't really want to wade through it, anyway), but I'll hazard a guess: my guess is that you're replacing the standard yylex EOF indication (i.e., returning 0) with your END token. If the bison parser never sees an EOF, it never finishes the parse.

In effect, bison creates a special production all of its own:

__parse__: __start__ $;

parse is the (actually unnamed) production, and __start__ is whatever you've declared as %start (or the first non-terminal, if you don't declare it explicitly). In your case, I suppose it's overallexpr. $ is the symbol conventionally used to indicated the EOF mark.

Now, when do bison parser actions happen? Although in some cases, they can happen where you think they will (i.e. immediately after the last token in the production), they usually don't happen until the parser takes a peek at the following token. It's allowed to do that; that's why it's called an LALR(1) parser: the 1 is the number of future tokens it's allowed to look at before deciding exactly what to do with the ones it's already got. It almost always needs this information, and often works as though it did even if it seems to you and me that it doesn't.

So in all probability, the parser will not actually do the overallexpr reduction -- or, in other words, it won't execute the action associated with the overallexpr rule -- until it convinces itself that the end-of-file marker is the next token.

Now, if you leave your END token out of the rule and the lexer actually returns EOF, then bison do the reduction when it sees the EOF.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!