当人们说C ++有“不可判定的语法”时,人们是什么意思? [英] What do people mean when they say C++ has "undecidable grammar"?

查看:110
本文介绍了当人们说C ++有“不可判定的语法”时,人们是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当他们这样说时,人们的意思是什么?对于程序员和编译器有什么影响?

What do people mean when they say this? What are the implications for programmers and compilers?

推荐答案

这与C ++的模板系统完成。这意味着(理论上)你可以在编译时使用任何其他图灵完整语言或系统的模板计算任何东西。

This is related to the fact that C++'s template system is Turing complete. This means (theoretically) that you can compute anything at compile time with templates that you could using any other Turing complete language or system.

这有副作用,有效的C ++程序不能编译;编译器将永远不能决定程序是否有效。如果编译器可以决定所有程序的有效性,它将能够解决暂停问题

This has the side effect that some apparently valid C++ programs cannot be compiled; the compiler will never be able to decide whether the program is valid or not. If the compiler could decide the validity of all programs, it would be able to solve the Halting problem.

注意这与C ++语法的模糊无关。

编辑: Josh Haberman在下面的评论中指出了一个 blog post 有一个很好的例子,构建一个C ++的解析树实际上是不可判定的。由于语法的模糊性,不可能将语法分析与语义分析分开,并且由于语义分析是不可判定的,所以语法分析也是如此。

Josh Haberman pointed out in the comments below and in a blog post with an excellent example that constructing a parse tree for C++ actually is undecideable. Due to the ambiguity of the grammar, it's impossible to separate syntax analysis from semantic analysis, and since semantic analysis is undecideable, so is syntax analysis.

Josh的帖子):

  • C++ grammar: the type name vs object name issue
  • C++ grammar: type vs object and template specializations

这篇关于当人们说C ++有“不可判定的语法”时,人们是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆