Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 957
Ananth Avatar asked Feb 27 '26 23:02

Ananth


1 Answers

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.

like image 176
Ira Baxter Avatar answered Mar 02 '26 04:03

Ira Baxter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!