On-The-Fly Code Generation
Introduction: The construction of long strings of code that are attribute values, is undesirable for several reasons, including the time it could take to copy or move long strings. In common cases such as our running code generation example, we can instead incrementally generate pieces of the code into an array or output file by executing actions in an SDT.
The elements we need to make this technique work are:
1. There is, for one or more nonterminals, a main attribute. For convenience, we shall assume that the main attributes are all string valued.
2. The main attributes are synthesized.
3. The rules that evaluate the main attribute (s) ensure that
(a) The main attribute is the concatenation of main attributes of nonterminals appearing in the body of the production involved, perhaps with other elements that are not main attributes, such as the string label or the values of labels LI and L2.
(b) The main attributes of nonterminals appear in the rule in the same order as the nonterminals themselves appear in the production body. As a consequence of the above conditions, the main attribute can be constructed by emitting the non-main-attribute elements of the concatenation. We can rely on the recursive calls to the functions for the nonterminals in a production body to emit the value of their main attribute incrementally.