如何将文字从 flex 返回到 yacc?

发布于 2024-08-11 15:53:45 字数 280 浏览 1 评论 0原文

在我的 yacc 文件中,我有如下内容:

var_declaration : type_specifier ID ';'
                | type_specifier ID '[' NUM ']' ';' ;

type_specifier : INT | VOID ;

ID、NUM、INT 和 VOID 是从 flex 返回的标记,因此 yacc 识别它们没有问题。问题是上面有‘[’和‘;’之类的东西。当这些被flex识别后,应该返回什么给yacc?

In my yacc file I have things like the following:

var_declaration : type_specifier ID ';'
                | type_specifier ID '[' NUM ']' ';' ;

type_specifier : INT | VOID ;

ID, NUM, INT, and VOID are tokens that get returned from flex, so yacc has no problems recognizing them. The problem is that in the above there are things like '[' and ';'. When these are recognized by flex, what should be returned to yacc?

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

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

发布评论

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

评论(1

世俗缘 2024-08-18 15:53:45

您可以只返回字符本身。保证令牌不与 ASCII 字符冲突:

http:// www.gnu.org/software/bison/manual/html_node/Token-Decl.html

Bison会自动选择代码
彼此不冲突或
带有 ASCII 字符。

所以在你的flex文件中,

[\[\];]     { return yytext[0]; }

就可以了。

You can just return the characters themselves. Tokens are guaranteed not to conflict with ASCII characters:

http://www.gnu.org/software/bison/manual/html_node/Token-Decl.html

Bison will automatically select codes
that don't conflict with each other or
with ASCII characters.

So in your flex file,

[\[\];]     { return yytext[0]; }

is OK.

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