Building internal nodes from production rules
In this section, we will learn how to construct the tree, one node at a time, during parsing. The internal nodes of your syntax tree, all the way back up to the root, are built from the bottom up, following the sequence of reduce operations with which production rules are recognized during the parse. The tree nodes used during the construction are accessed from the value stack.
Accessing tree nodes on the value stack
For every production rule in the grammar, there is a chance to execute some code called a semantic action when that production rule is used during a parse. As you saw in Chapter 4, Parsing, in the Putting together the yacc context-free grammar section, semantic action code comes at the end of a grammar rule, before the semicolon or vertical bar that ends a rule and starts the next one.
You can put any code you want in a semantic action. For us, the main purpose of a semantic action is to build a syntax tree node...