编译器如何构建语法树?
编译器在什么时候构建语法树?在构建可执行文件时,它如何形成树并翻译树?
At which point the compiler builds the syntax tree? How does it form the tree and translate the tree while building the executable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
构建语法树的编译器在解析步骤中这样做。它通常通过为与输入流匹配的每个语法规则生成一个树节点来实现这一点。
代码生成需要对树进行大量分析,以了解类型、操作、优化机会等。通常,直接在树上很难做到这一点,因此使用其他中间表示(三元组、静态单赋值……)。通常,甚至中间阶段也不适合机器代码生成,因此可能会构造某种机器指令的表示形式(RTL),...
重点是树并不是编译器用于生成代码的唯一表示形式。
花点时间阅读一本介绍性编译器教科书(Aho 和 Ullman,“编译器”)以获取更多详细信息是非常值得的。
A compiler that builds a syntax tree does so during the parsing step. It does so, typically by generating a tree node for each grammar rule that matches the input stream.
Code generation requires considerable analysis of the tree to understand types, operations, opportunities for optimizations, etc. Often this is hard to do well on the tree directly, so other intermediate representations are used (triples, static single assignment, ...). Often even the intermediate stages are inappropriate for machine code generations, so some kind of representation of machine intructions might be constructed (RTL), ...
The point is that trees aren't the only representation the compiler uses to generate code.
It is well worth your trouble to read an introductory compiler text book (Aho and Ullman, "Compilers") to get more details.