![]() | ![]() | ![]() | Syntax Tree Organization |
As discussed in class, there is a significant difference between the internal nodes of a syntax tree and its leaves. Within the internal nodes, one must store a phrase type and pointers to sub-trees. The leaves, on the other hand, hold information about identifiers and constants. In fact, in class I have suggested that rather than actually having separate nodes for the leaves, one could use symbol table entries for leaf nodes.
We will not actually do this in the compilers you build. The reason is a simple, practical one. To generate good error messages, one needs to keep information about where in the source program the text that corresponds to each sub-tree of the syntax tree can be found. We will do this by storing in each node the line number on which the first token that belonged to the phrase the node represents was found. This can not be done for identifiers if all occurrences of an identifier are represented by a single symbol table entry. So, we will instead represent identifiers by nodes that contain the line number on which they were found and a pointer to the appropriate symbol table entry. Similar nodes will be used for constants.
![]() | ![]() | ![]() | Syntax Tree Organization |