野牛文件结尾

发布于 2024-08-12 08:55:53 字数 275 浏览 7 评论 0原文

如果我忘记在任何文件的末尾放置一个空行,我的程序就会出现语法错误。问题是我的语法期望用换行符来结束当前行。由于换行符不存在,野牛会生成语法错误,因为它没有完成规则。

我该如何解决这个问题?我尝试让 <> 返回 MY_EOF 但是当我这样做时 lex 崩溃了,可怕的死亡。我猜想它的默认 EOF 中有我没有调用的代码。我不知道它们可能有什么功能。使用 EOF 创建错误使用符号 EOF,但未定义为令牌且没有规则

If I forget to put an empty line at the end of any of my files my program gets a syntax error. The problem is my grammar expects a newline to end the current line. Since a newline doesn't exist bison generates a syntax error because it does not finish the rule.

How do I solve this? I tried making <<EOF>> return MY_EOF BUT when I do that lex crashes a horrible death. I guess there's code in its default EOF that I am not calling. I have no idea what functions they may be. Using EOF create the error symbol EOF is used, but is not defined as a token and has no rules

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

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

发布评论

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

评论(5

烟燃烟灭 2024-08-19 08:55:53

您可以使用 flex EOF 规则将换行符附加到输入:

<<EOF>> { static int once = 0; return once++ ? 0 : '\n' }

You could use a flex EOF rule to append a newline to the input:

<<EOF>> { static int once = 0; return once++ ? 0 : '\n' }
胡大本事 2024-08-19 08:55:53

在您的 lex 文件中

#define yyterminate() return token::END

在您的 yacc 文件中

%token END 0 "end of file"

In your lex file

#define yyterminate() return token::END

In your yacc file

%token END 0 "end of file"
魔法少女 2024-08-19 08:55:53

事实上,要捕获 lex|flex 中的文件结尾,您可以使用 yywrap() 函数,如果到达输入文件结尾,则词法分析器将调用该函数。

lex 和 flex 都可以使用此解决方案。yywrap() 的回调指示 EOF,因此您可以重新实现此函数并在最后注入您需要执行的工作您的输入流。

In fact to catch end of file in lex|flex you can use the yywrap() function which is called by lexical analyzer if end of the input file is reached.

this solution is available by both lex and flex.the callback of yywrap() indicates the EOFso you can reimplement this function and inject the work you need to do at the end of your input stream.

埖埖迣鎅 2024-08-19 08:55:53

以前的作品对我来说很好。

如果您使用 C Bizon (不是 C++),只需使用 END 作为 token::END 并在 yacc 文件 %token END

之后还有另一个问题,如果宏返回不是 YY_NULL,它永远不会终止(无限循环)

可以这样解决:

bool term = false;
#define yyterminate() return (term = !term)?END : YY_NULL

Previous works fine for me.

If you use C Bizon (not C++), just use END for token::END and in yacc file %token END

Had another issue after that, if the macros return not YY_NULL, it never terminates (infinite loop)

It can be solved like this:

bool term = false;
#define yyterminate() return (term = !term)?END : YY_NULL
伴随着你 2024-08-19 08:55:53

另一种方法是重组语法,使其末尾不需要换行符。只要您的语言允许空行(通常是这种情况),您就可以使用换行符作为行分隔符而不是行终止符来编写语法。

input: line | input '\n' line ;
line: /* empty */
    | ... various other rules ...

现在,如果您输入末尾确实有换行符,这会被视为换行符后面的空行,这可能没问题。

An alternative approach would be to restructure your grammar to not need a newline at the end. As long as your language allows blank lines (usually the case), you can write your grammar using newline as a line separator rather than a line terminator

input: line | input '\n' line ;
line: /* empty */
    | ... various other rules ...

Now if you do have a newline at the end of the input, this gets treated as a blank line after that newline, which is probably fine.

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