General Information
Tutorials
Reference Manuals
Libraries
Translation Tasks
Tools
Administration
|
Pattern-based Text GeneratorSome Useful TechniquesThis chapter describes some techniques that solve common tasks in PTG applications.
Output of Data ItemsUsually data items like numbers, identifiers, or strings which are computed by the translation process or taken from the input are to be inserted into the output text at certain positions. This is best achieved by using typed insertion points as described in See Typed Insertion Points.
It is often necessary to convert single data items into a Number: $ int String: $ string StringLit: "\"" $ string "\""Such patterns are applied by calls like PTGNumber(5) ,
PTGString("+") , PTGStringLit("Hello!")
producing the text items 5 , + , and "Hello!" respectively.
Predefined patterns for the conversion of single data items to
This module also solves the problem of generating a
Generating IdentifiersCommon techniques for producing identifiers are recommended especially for translation into programming languages. In the simplest case identifiers are just reproduced from the input as shown above. The following set of patterns allow to add prefixes or postfixes to the original identifiers (see See Typed Insertion Points):
PointerId: "PTR_" $ string ValueId: "VAL_" $ string UniqueId: $ string $ intPatterns like the first two may be used to generate different output identifiers from one input identifier. The last pattern allows to attach a number to an identifier, e.g. to guarantee uniqueness in the output in cases where the source and the target language have different scope rules. Note: PTG does not add any white space between pattern items. Hence, patterns where identifiers or numbers may be inserted have to ensure that the component tokens are separated, e.g.
Decl: $ /* Type */ " " $ /* Ident */
Output of SequencesThe construction of output text often requires to compose arbitrary long sequences of items, e.g tokens, statements, or procedures. Some simple techniques for those tasks are described here. The examples can easily be generalized for similar applications. A generally applicable pattern for constructing sequences is
Seq: $ $An application
PTGSeq (PTGString("."), PTGSeq (PTGString("."), PTGString(".")))produces a sequence of 3 dots, assuming pattern String is
suitably defined.
The following C loop computes a sequence of
PTGNode dots = PTGNULL; int i; for (i=0; i<n; i++) dots = PTGSeq (dots, PTGString(".")); Sequences where the items are separated by a certain string, e.g. a comma or a space character can be specified by
CommaSeq: $ {", "} $An application
PTGCommaSeq (PTGNumber (1), PTGNumber (2))produces 1, 2 , assuming pattern Number be
suitably defined.
The following C loop computes a comma separated sequence of the
numbers 1 to
PTGNode numseq = PTGNULL; int i; for (i=1; i<=n; i++) numseq = PTGCommaSeq (numseq, PTGNumber (i));
|