Polyglot program
A polyglot program ( polyglot = multilingual) is a computer program whose source text is valid in more than one programming language ; d. In other words, the respective interpreter or compiler can execute or translate any of the languages provided for it without errors. Usually it produces the same output when executed in the different languages . Such a program has no use for the general public, its creation is just a demanding exercise for the programmer .
functionality
While it may be almost impossible to formulate a polyglot text in natural language, creating a polyglot program is less difficult than it initially appears. The following facts and methods are used to develop polyglot programs:
- Parts of the code can be valid in several programming languages, e.g. For example, "
printf ("...")
" creates textual output in both Perl and C. - Jump commands can be used to skip the parts of the source text that are invalid in the respective language or to
exit
terminate the program execution with "in good time" before invalid codes appear. - A program line can represent executable code in one programming language and a comment in the other . For example, “
# include ...
” in C is a preprocessor instruction , in many scripting languages - because of the character “#
” at the beginning of a line - a comment line. - In some programming languages (e.g. in C with a preprocessor instruction) you can redefine tokens or redefine existing ones and thus a token that is valid in a foreign programming language also in your own - u. U. in a different meaning - let become valid.
A simple example
The following example is far less spectacular than the one referenced in the web links; it was specially designed with the aim of making it easier to understand, even for the layperson. It makes use of the first three of the techniques mentioned above.
Source code | Explanation |
---|---|
|
The source code opposite is valid in C, Perl and many Unix shells (Bourne Shell, Korn Shell , Bash, C Shell, Z Shell). The program outputs the text “ Hello, world! " out. |
|
C |
|
Unix shells |
|
Perl |
A more complex example
The following source code is valid in C, PHP and Bash. All of the methods described above are used; in particular, for compatibility with C, the new or redefinition of character strings is necessary in many cases. These are made in the #define
lines beginning with " ", which are comment lines for the two script languages.
#define a /* #<?php echo "\010Hello, world!\n"// 2> /dev/null > /dev/null \ ; // 2> /dev/null; x=a; $x=5 // 2> /dev/null \ ; if (($x)) // 2> /dev/null; then return 0; // 2> /dev/null; fi #define e ?> #define b */ #include <stdio.h> #define main() int main() #define printf printf( #define true ) #define function function main() { printf "Hello, world!\n"true/* 2> /dev/null | grep -v true*/; return 0; } #define c /* main #*/ |
MS-DOS and Perl
Hello, world!
The following program can through the Perl interpreter or as a DOS - batch file to run. (Explanations for each line to the right of the source text.)
Source code | DOS | Pearl |
---|---|---|
@rem = ' @echo Hello, world! @goto END '; print substr "@rem", 7, 13; __END__ : END |
Kommentar Ausgabe Sprung ans Ende wird übersprungen wird übersprungen wird übersprungen Ende Code |
Definition des Arrays @rem Inhalt des Arrays @rem Inhalt des Arrays @rem Ende der Definition von @rem Ausgabe Teilstring aus @rem Ende Code wird ignoriert |
DOS wrapper for Perl
A DOS batch file for calling a Perl program can be created in a similar way: The DOS batch file contains the Perl code and executes itself (in line 10) with the Perl interpreter. However, it takes some effort to collect the command line arguments and pass them to Perl:
@rem = ' ---------- BEGIN DOS ------------------ @echo off set PROG=%0.bat : ARG LOOP if "%1" == "" goto EXEC set ARGS=%ARGS% %1 shift goto ARG LOOP : EXEC perl %PROG% %ARGS% goto END '; # ================ BEGIN PERL ================== print "Perl here with these arguments: @ARGV\n"; __END__ # ========= END PERL =================== : END set PROG= set ARGS= :: ---------------- END DOS -------------------- |
See also
More examples of useless but educational programs:
- Hello World Program - the output of "Hello, World" in a wide variety of programming languages
- Quine - programs that output their own source code
- Just another Perl hacker - creative Perl programs that output "Just another Perl hacker" in an original way
- Programs in esoteric programming languages
- Program contributions for "Obfuscated Contests" (e.g. Obfuscated Perl Contest , International Obfuscated C Code Contest )
Web links
(All sources cited here are in English.)
- An 8-fold polyglot program (executable in Unix Shell , C , COBOL , Fortran , Pascal , Perl , PostScript and x86 assembler )
- A 7-fold polyglot program (executable in Bash , Befunge , C , Pascal, Perl, PostScript and TeX )
- A 6-fold polyglot program (executable in Brainfuck , Befunge , C, Perl, Unix-Shell and Whitespace )
- A 4-fold polyglot program (executable in ANSI C , Perl, Tcl and Unix Shell)
- A compilation of 14 polyglot programs