next up previous
Next: Tree construction and computations Up: Describing a Complete Compiler Previous: Command line analysis

Creation and output of structured text

We create structured text by pasting together text fragments. Each fragment can be created by invoking a routine that constructs the fragment from its component parts. Usually there is a hierarchical relationship among the fragments, with some fragments being constructed from basic data types like integers and strings and others being constructed from previously-constructed fragments.

The format string in a printf call is a declarative description of a text fragment. It specifies certain constant parts, and certain places where computed information can be inserted. A similar technique, describing each text fragment as a combination of strings and other fragments, can be used in a declarative specification of the output module for a compiler:

While:
  "  JMP " $4 "\n"
  $3 ": " $2
  $4 ": " $1

This specification might describe the assembly language corresponding to a C while statement. Constant parts are described by C strings, other fragments by $ signs. The digit identifies a particular fragment. A tool called PTG will translate this description into a C routine named PTGWhile that takes four text fragments as arguments and delivers one text fragment as its result. The first argument to PTGWhile would be the translation of the test of the while statement, and the second would be the translation of the body. If the test yielded true, the code would transfer control to a generated label supplied as the third argument to PTGWhile. Finally, the fourth argument to PTGWhile would be a new generated label.

The digits may be omitted in the specification if each component fragment is inserted exactly once, and the order of the arguments is the same as the order of the component fragments. All of the examples in Section 4.2 take that form.

A structured text module generated from a PTG specification exports one function for each named text fragment. Each of these functions returns a data structure called a PTGNode that represents the completed fragment, which is the root of a directed acyclic graph of fragments. This PTGNode can then be passed to other routines of the generated module for use as components of other fragments, stored in the definition table as properties of entities, or output as text.


next up previous
Next: Tree construction and computations Up: Describing a Complete Compiler Previous: Command line analysis
2007-05-18