Rust的语法语法是上下文无关的还是上下文敏感的? [英] Is Rust's syntactical grammar context-free or context-sensitive?
问题描述
几乎没有任何编程语言的语法语法是规则的,因为它们允许任意深度嵌套的括号. Rust也这样做:
let x = ((((()))));
但是Rust的句法语法是否至少与上下文无关?如果不是,什么元素使语法对上下文敏感?还是语法甚至可以递归枚举,例如 C ++的语法语法?/p>
相关: Rust的词汇语法是规则的,上下文无关的还是上下文敏感的?
Rust包含一个宏处理器,其操作与上下文有关.
您可以尝试通过仅进行语法分析直至(但不包括)宏扩展来解决该问题-可能,但不是特别有用-或假设宏扩展是由某些中间工具完成的,免费通行证,以使它成为图灵完整版.
但是我倾向于说这只是意味着Rust语言是可递归枚举的.
对宏定义的有效性有很多限制,即使您决定不将宏扩展作为语法分析的一部分,也可能使语言(至少)对上下文敏感.
这并不意味着上下文无关的语法不能作为Rust语法分析的一部分使用.这可能是必不可少的,使用解析器生成器(例如bison或Antlr)(甚至存在两者的示例)甚至可能会很有用.像大多数编程语言一样,Rust有一个简单的超集,它没有上下文,并且可以使用上下文无关的语法工具进行有用的分析.但是,最后,即使它们是CF超集的一部分,仍需要在编译时将其视为无效的文本.
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 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.
这篇关于Rust的语法语法是上下文无关的还是上下文敏感的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!