Generative programming

from Wikipedia, the free encyclopedia

The automatic programming is a programming paradigm in the methodical software development . The automatic generation of program code by a generator is characteristic of generative programming .

How a program generator works

A program generator can best be understood like an ordinary program based on the EVA principle . Based on certain input parameters, the program generator generates a certain output, the so-called Generat. However, the output of a generative program is in turn a program code, namely the code that is to be executed for the specific situation.

The basis for automatically generated code is the abstraction of frequently occurring program constructs in formal models . The programming is divided into three phases:

  1. The programming of a specific program generator
  2. The parameterization or addition and configuration of the formal model to a specific model specification
  3. Calling the program generator with the specific input parameters, which then creates the specific target program

A program generator or code generator is accordingly specialized in a generic application and program class. It refers to a specific, underlying generic program model from which it generates the target code after concretizing parameterization . This can be a source code , intermediate code or binary code .

While a normal, functionally programmed program covers the variance of the tasks exclusively with data variables, generative programming also works with variable program code , which is only clearly expressed with regard to the target code.

This approach is particularly suitable for problem solutions that occur in a correspondingly large number of variations in practice, since a considerable amount of effort has to be planned for creating the model and the generator. This effort can be amortized due to the higher quality of the program code and the shorter development time. The target programs are often only generated temporarily for one-time use and then deleted again. As a result, the program code persistently present at a certain point in time, e.g. B. measured using the number of lines of code, possibly reduced by a few powers of ten.

Generative programming makes sense wherever certain parts of code are to be combined into a multitude of target programs in the same way as variable text modules. Generative programming also allows the creation of target programs whose target parameters are not yet known at the time the program generator is coded.

Persistent target code

The target code generated by a program code can

  • generated once, stored persistently and then used permanently or
  • generated and executed dynamically as required and then deleted again.

If a target code is generated once and then kept persistent, the program generation and the execution of the target program can take place at separate times. The program generation and the execution of the target program are only dependent on each other insofar as the generation takes place before the execution of the target code. The code generation is then typically initiated by the programmer or a system administrator during the software installation, i.e. typically not by the end user.

Example: A program generator (code wizard) for creating the basic code of a program class queries various parameters such as class name, number, name and type of class properties, number and names of class methods and then creates the program code of the class.

A regeneration is only necessary if there are changes to the generation parameters.

Dynamically generated target code

In the second case, the program generation and the execution of the target code are usually initiated directly by the end user. The program generation ideally takes place so quickly that the end user does not even notice that the program part he is using was programmed automatically only a fraction of a second ago. The sequence of this dynamic process should be traced in individual steps:

  1. The user makes a selection of the input parameters, e.g. B. the name of a database table.
  2. The program generator takes the table name from the end user, reads the fields, field types and foreign key relationships from the data dictionary of the database and uses these control parameters to generate the program target code of a search form for displaying data for the database table specified by the user.
  3. The program target code is now briefly compiled and then executed by the program generator with a dynamic call.

The last step places certain requirements on the programming language used:

  • It must be possible in the program generator to call a routine whose name is specified as a variable and is not necessarily known in the context of the program generator (e.g. specification of the routine to be called by a string variable, late binding).
  • The necessary flexibility in program generation requires an interpreted language, i. H. As a rule, an interpreter code is generated as the target code and not a machine code . In principle, however, the target code can be anything, i. H. a source code , intermediate code or binary code .

The dynamically generated target code is usefully often coded in the same language as the code-generating program module. A program generator is accordingly a code generator specialized in a generic application and program class . It refers to a specific, underlying generic program model, from which it generates the target code after concretizing parameterization . A complex parameterization can e.g. B. be done using a table model in a database, which controls the flexible code generation.

Application examples

UML

UML allows the creation of a software architecture in the form of a diagram. From this code can then be generated automatically, which then usually has to be completed “by hand”. More demanding development environments also allow simultaneous work on the UML and source code level. You can either change the UML or the source code and the development environment then automatically creates the other representation of the program. Either UML code is generated from the source code or vice versa.

With XML and XSLT

Automatic code generation is also possible with XSLT . The desired model is represented in an XML document, the syntax of which you can declare yourself. Then you create an XSLT script that matches the XML document and generates the program code. This can also be done in a multi-step process, e.g. For example, a batch file , a shell script or a makefile with a list of further XSLT processing steps or other commands is generated with a first XSLT script .

Form generators

Using a list-like description of the table structure, a screen template is created from specifically specified tables. The structure and functionality of the form are fixed. However, the various tables differ in terms of the type and number of fields, field names, field types and foreign key relationships.

Compiler-compiler

The syntax of a programming language is e.g. B. specified in EBNF notation . On the basis of this formal language definition, a compiler-compiler generates the compiler or a parser generator generates the parser for the specified language. See also: Coco / R and yacc .

Product configuration

Generative programming can also be used to process parts lists with variable parts list items. As part of customizing or installation, generative programs can bring the variant configuration of complex software applications to the desired target form.

Rapid control prototyping

When rapid control prototyping is in control technology and for the from block diagrams controller custom code generated so errors in the implementation of block diagrams in tax-device-specific code as possible no longer appear.

See also

literature

  • Czarnecki, Krzysztof, Ulrich W. Eisenecker: Generative Programming: Methods, Tools, and Applications . Addison-Wesley, 2000, ISBN 0-201-30977-7 .
  • Olaf Zwintzscher: Component-based & generative software development . W3L, 2003, ISBN 3-937137-50-5 .
  • Peter Rechenberg, Hanspeter Mössenböck: A compiler generator for microcomputers. Basics, application, programming in Modula-2 . Hanser, 1988, ISBN 3-446-15350-0 .
  • Michael Klar: Simply generate. Generative programming understandable and practical . Hanser, 2006, ISBN 3-446-40448-1 .
  • Claude Gomez, Tony Scott: Maple programs for generating efficient FORTRAN code for serial and vectorised machines. In: Computer Physics Communications . Volume 115, Number 2-3, 1998, pp. 548-562, doi : 10.1016 / S0010-4655 (98) 00114-3 .
  • TC Scott, MB Monagan, IP Grant, VR Saunders: Numerical computation of molecular integrals via optimized (vectorized) FORTRAN code. In: Nuclear Instruments and Methods in Physics Research Section A: Accelerators, Spectrometers, Detectors and Associated Equipment . Volume 389, Number 1-2, 1997, pp 117-120, doi : 10.1016 / S0168-9002 (97) 00059-4 .
  • TC Scott, Wenxing Zhang: Efficient hybrid-symbolic methods for quantum mechanical calculations. In: Computer Physics Communications . Volume 191, 2015, pp. 221-234, doi : 10.1016 / j.cpc.2015.02.009 .