Polyglot program

from Wikipedia, the free encyclopedia

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 exitterminate 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

# include <stdio.h> /*                  
eval "echo 'Hello, world!'; exit";
sub echo { print "@_\n" }; __END__ */
main() { printf ("Hello, world!\n"); }

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.

# include <stdio.h> /*
eval "echo 'Hello, world!'; exit";
sub echo { print "@_\n" }; __END__ */
main() { printf ("Hello, world!\n"); }

C
The text enclosed by /*and */(here colored green) is a comment in C and is ignored. The rest is a variant of the Hello World program in C .

# include <stdio.h> /*
eval "echo 'Hello, world!'; exit";
sub echo { print "@_\n" }; __END__ */
main() { printf ("Hello, world!\n"); }

Unix shells
The first line (green) is a comment line and is ignored. When the evalstatement has been executed, the program terminates because of the exit; the rest (gray) is no longer read in by the interpreter and is therefore not checked for correct syntax .

# include <stdio.h> /*
eval "echo 'Hello, world!'; exit";
sub echo { print "@_\n" }; __END__ */
main() { printf ("Hello, world!\n"); }

Perl
The first line is comment. The evalstatement is only executed after the subroutine has been echo defined in line 3 . The remainder (gray) following the keyword __END__ is read by the Perl interpreter, but ignored.

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 #definelines 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:

Web links

(All sources cited here are in English.)