关于使用 yacc 解析器构建符号表的问题

发布于 2024-08-12 17:39:30 字数 144 浏览 4 评论 0原文

如果我的 yacc 解析器遇到以下代码:

int foo(int a, int b)

是否应该添加 int a 和 int b 作为 foo 的属性?按照我现在的方式,它输入 a 和 b 作为单独的表条目。

If my yacc parser encounters the following code:

int foo(int a, int b)

should it add int a and int b as attributes of foo? The way I have it now, it enters a and b as separate table entries.

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

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

发布评论

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

评论(2

白日梦 2024-08-19 17:39:30

我将作为单独的条目添加到符号表中,但在 foo 节点上有一个范围子句。这将使您能够报告变量的阴影/屏蔽。因此,对于 C,您可以在顶部定义一个模块全局 int a,然后使用 a 作为参数将屏蔽全局。这是一个有用的提示,您可以向用户发出警告,避免在同一范围内重新声明 a 符号。

还有其他情况,范围规则可能需要阻止重新声明相同的符号,例如嵌套 for 循环,其中迭代器具有相同的名称。

正如 Ivan 所说,您需要根据您想要/需要检测的内容以及最简单的检查方法来进行此调用。

I would add to the symbol table as separate entries, but have a scoping clause on the foo node. This will allow you to be able to report shadowing/masking of variables. So for C you many define a module global int a at the top, then having an a as a parameter will mask the global. This is a helpful hint you can give as a warning to users verse a deal breaker of redeclaration of the a symbol in the same scope.

There are other case where scope rules may need to block the same symbol being redeclared, like nested for loops, where the iterator has the same name.

As Ivan says, you need to make this call, based on what you want/need to detect, and the simplest way to that checking.

悍妇囚夫 2024-08-19 17:39:30

你的问题很模糊。这完全取决于您稍后将使用这些数据的内容和方式。

您可以将它们用作 AST 中的单独节点,或者,正如您提到的,您可以将它们添加为 foo() 的属性。我相信,这件事的选择权在于你。

Your question is pretty vague. It all depends on what and how you'll use the data later on.

You can use them as separate nodes in your AST, or, as you mentioned, you could add them as attributes of foo(). The choice in this matter is, I believe, yours.

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