Implement each of your SDD”s of Exercise 5.4.4 as a recursive-descent parser in the style of Section 5.5.2.
Exercise 5.4.4 : Write L-attributed SDD”s analogous to that of Example 5.19 for the following productions, each of which represents a familiar flow-of-control construct, as in the programming language C. You may need to generate a three-address statement to jump to a particular label L, in which case you should generate goto L.
Note that any statement in the list can have a jump from its middle to the next statement, so it is not sufficient simply to generate code for each statement in order.
5.5.2 On-The-Fly Code Generation
The construction of long strings of code that are attribute values, as in Example 5.20, 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 non-terminals, a main attribute. For convenience, we shall assume that the main attributes are all string valued. In Example 5.20, the attributes S. code and C. code are main attributes; the
other attributes are not.
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 non-terminals 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 L1 and L2.
(b) The main attributes of non-terminals appear in the rule in the same order as the non-terminals 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 non-terminals in a production body to emit the value of their main attribute incrementally.