Clang : What does AST (abstract syntax tree) look like?

自古美人都是妖i 提交于 2019-12-03 02:51:14

There is a small confusion between the various options available:

  • -ast-print will pretty-print the current AST, that is, it will render the code it understood as closely as possible to what it parsed (but making some things explicit, like the apparition of the this)
  • -ast-dump will generate a lisp-like representation of the current AST

The pretty printer can be useful to check that the AST is lossless (ie, preserved the const-ness of such expression, etc...) but is not really about development.

If you want to hack on the compiler, you need -ast-dump, which will generate an output that maps directly the in-memory representation of the code that was parsed.

The AST is a linked structure in memory ("tree" does not make justice to the complexity of the thing, but it's the name people use). What -ast-print produces is a textual representation of the AST. Since the human who set the option is already familiar with C/C++-like syntax, it is printed in a representation that follows that syntax. This is a design choice, not a happy coincidence.

If you want to see what the AST looks like when it's not printed on purpose in a familiar syntax, you could for instance look at GIMPLE, GCC's internal representation.

And if you want to play with GIMPLE, you could even use GCC MELT for that purpose. MELT is a high-level domain specific language to deal with GIMPLE!

And inside compilers, the internal representation are often not trees, but somehow circular structures. In GCC, a basic block knows it gimple-s, but the gimple-s may know their basic blocks.... (it is a bit more complex, but you've got the idea).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!