Rust的语法语法是无上下文还是上下文敏感?(Is Rust's syntactical grammar context

编程入门 行业动态 更新时间:2024-10-26 15:28:43
Rust的语法语法是无上下文还是上下文敏感?(Is Rust's syntactical grammar context-free or context-sensitive?)

几乎没有任何编程语言的语法语法是规则的,因为它们允许任意深度嵌套的括号。 Rust也是这样做的:

let x = ((((()))));

但Rust的语法语法至少是无上下文的吗? 如果没有,哪个元素使语法上下文敏感? 或者语法甚至是递归可枚举的,就像C ++的语法语法一样 ?


相关 : Rust的词汇语法是常规的,无上下文的还是上下文敏感的?

The syntactical grammar of hardly any programming language is regular, as they allow arbitrarily deeply nested parenthesis. Rust does, too:

let x = ((((()))));

But is Rust's syntactical grammar at least context-free? If not, what element makes the grammar context-sensitive? Or is the grammar even recursively enumerable, like C++'s syntactical grammar?


Related: Is Rust's lexical grammar regular, context-free or context-sensitive?

最满意答案

Rust包含一个宏处理器,其操作具有高度上下文相关性。

您可以尝试通过语法分析来解决这个问题,但不包括宏扩展 - 可能,但不是特别有用 - 或者假设宏扩展是由一些中间工具完成的,该工具被赋予一个免费的传递给让它成为图灵完整的。

但我倾向于说它只是意味着Rust语言是递归可枚举的。

宏定义的有效性存在许多限制,这可能会使语言(至少)对上下文敏感,即使您决定不执行宏扩展作为语法分析的一部分。

这并不意味着无上下文语法不能用作Rust语法分析的一部分。 它可能是必不可少的,使用诸如bison或Antlr之类的解析器生成器(以及两者的示例)甚至可能是有用的。 像大多数编程语言一样,有一个简单的Rust超集,它是无上下文的,可以使用无上下文语法工具进行有用的分析; 但是,最终有些文本需要在编译时被拒绝为无效,即使它们是CF超集的一部分。

Rust includes a macro processor, whose operation is highly context-sensitive.

You could attempt to skate around this issue by only doing syntactic analysis up to but not including macro expansion -- possible, but not particularly useful -- or by assuming that the macro expansion is done by some intermediate tool which is given a free pass to allow it to be Turing complete.

But I'm inclined to say that it simply means that the Rust language is recursively enumerable.

There are a number of restrictions on the validity of macro definitions which probably make the language (at least) context-sensitive, even if you settle for not performing the macro expansions as part of syntactic analysis.

This doesn't mean that a context-free grammar cannot be useful as part of the syntactic analysis of Rust. It's probably essential, and it could even be useful to use a parser generator such as bison or Antlr (and examples of both exist). Like most programming languages, there is a simple superset of Rust which is context-free, and which can be usefully analysed with context-free grammar tools; however, in the end there are texts which will need to be rejected at compile-time as invalid even though they are part of the CF superset.

更多推荐

本文发布于:2023-08-03 00:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382451.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法   上下文   下文   敏感   Rust

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!