语法与语义分析

发布于 2024-09-14 01:33:42 字数 198 浏览 5 评论 0原文

我想知道语法规范和语义分析之间的界限在哪里。哪个更好:使用详细的语法描述或将细节留给语义阶段?例如:想象像 C# 这样具有枚举类型的 OO 语言,它可以从原始类型“派生”

enum X : int { a = 1 }

现在,枚举的基本类型的正确性(在本例中:原始类型或非原始类型)是否应该成为语法检查还是语义分析?

I am wondering where is the borderline between grammar specification and semantic analysis. What is better: to use a detailed grammar description or leave the details for semantic phase? For example: imagine an OO-language like C# with enum type, that can "derive" from a primitive type

enum X : int { a = 1 }

Now, should the correctness (in this case: primitive or non-primitive type) of base-type of enum be a subject of grammar check or semantic analysis?

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

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

发布评论

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

评论(1

豆芽 2024-09-21 01:33:42

这是一个极其广泛的问题。还有很多其他考虑因素,因此很难简单地说一种策略在所有情况下都比另一种策略更好。这取决于您的目的和语言其余部分的定义。如果语言的其他部分本质上在语法上是不明确的(并且必须使用语义信息来消除歧义),那么显然有必要将执行任务作为语义分析的一个步骤。

也就是说,我喜欢尽早发现所有错误。如果您的语言和目标允许您解析输入的完全明确的语义表示,而不需要任何语义分析来拒绝无效程序,那么我相信这将是迄今为止最好的方法。

基本上,您肯定会在尽早拒绝错误程序的能力与语法和解析器的复杂性以及语法描述和语义分析阶段之间的重复工作之间进行平衡。我认为不可能说以一种方式做事就比另一种方式更好。

This is an extremely broad question. There are so many other considerations such that it would be very difficult to simply say that one tactic is better than another in all situations. It depends on your purpose and the definition of the rest of the language. If other parts of the language are inherently syntactically ambiguous (and must be disambiguated using semantic information), then it's apparent that it's necessary to perform tasks as a step of semantic analysis.

That said, I am a fan of catching all errors as early as possible. If your language and target permits you to parse a completely unambiguous semantic representation of your input without requiring any semantic analysis to reject invalid programs, then I believe that is going to be the best approach by far.

Basically, you are definitely balancing your ability to reject erroneous programs as early as possible with the complexity of your grammar and parser and duplication of effort between the grammar description and the semantic analysis phase. I don't believe it's ever possible to say that doing things in one way is better than another.

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