AST,抽象语法树,是由语言定义的还是由前端定义的?

Is the AST, abstract syntax tree, defined by the language or by the frontend?

在过去的几周里,我一直在试验 AST 和 Clang,尤其是 clang-tidy。

Clang 提供了一些 类 和与 AST 交互的方式,但我不明白的是,我经常使用的 clang::VarDecl 是否是由Clang,或语言的创造者。

谁决定将其命名为 VarDecl?

我的意思是,AST(及其所有元素)是否出自语言发明者的想法,各种前端只是创建 类 以 [=19= 编写的文档命名] ] 或者每个前端都可能创建给定源代码的 AST,因此 Clang 和 GCC 是不同的?

Is the AST, abstract syntax tree, defined by the language

不完全。 C++ 语言标准中的每个定义都带有一个简短的 syntax notation and there is a informative annex with grammar summary. But the annex notes https://eel.is/c++draft/gram :

This summary of C++ grammar is intended to be an aid to comprehension. It is not an exact statement of the language. In particular, the grammar described here accepts a superset of valid C++ constructs. [...]

标准变量声明中的语法中没有 VarDecl,只有 simple-declaration 的一种解释。

or by the frontend?

编译器的内部结构,如果它有前端,或者没有,它有 3 个或 1000 个阶段,都是编译器实现的一部分。从语言的角度来看,编译器可以以任何它想要的方式实现,只要它能正确地翻译有效的程序。一般而言,语言指定什么时候应该发生什么,而不是如何发生。

所以要回答这个问题,AST(如果以任何形式使用的话)是由编译器定义的。

Who decided that was to be called VarDecl?

我很可能怀疑 Chris Lattner 在 https://github.com/llvm/llvm-project/commit/a11999d83a8ed1a2661feb858f0af786f2b829ad .

that came from the mind of the inventor of the language and the various frontends just creates classes named after a document written by him/her or every frontend potentially creates its AST of a given source code and so Clang's and GCC's are different?

它们当然会受到标准中的内容的影响,但每个编译器都有自己的内部结构。好吧,简而言之,Clang VarDecl 和 GCC VAR_DECL 是不同的,它们在概念上也不同 - 假设 GCC 使用 switch(...) case VAR_DECL: 而 Clang 使用 类 clang::VarDecl .