How does a compiler build the syntax tree?
At which point the compiler 开发者_如何学编程builds the syntax tree? How does it form the tree and translate the tree while building the executable?
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.
精彩评论