Polyglot (computing)

From Wikipedia, the free encyclopedia

In the context of computing, a polyglot is a computer program or script written in a valid form of multiple programming languages, which performs the same operations or output independently of the programming language used to compile or interpret it.

Generally polyglots are written in a combination of C (which allows redefinition of tokens with a preprocessor) and a scripting programming language such as Lisp, Perl or sh.

The two most commonly used techniques for constructing a polyglot program are to make liberal use of languages which use different characters for comments and to redefine various tokens as others in different languages. Often good use is made of quirks of syntax. These are demonstrated in this public domain polyglot written in ANSI C, PHP and bash:

#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
#*/

Note the following:

  • A hash sign marks a preprocessor statement in C, but is a comment in both bash and PHP.
  • "//" is a comment in PHP and the root directory in bash.
  • Shell redirection is used to eliminate undesirable outputs.
  • Even on commented out lines, the "<?php" and "?>" PHP indicators still have effect.
  • The statement "function main()" is valid in both PHP and bash; C #defines are used to convert it into "main()" at compile time.
  • Comment indicators can be combined to perform various operations.
  • "if (($x))" is a valid statement in both bash and PHP.
  • printf is a bash shell builtin which is identical to the C printf except for its omission of brackets (which the C preprocessor adds if this is compiled with a C compiler).
  • The final three lines are only used by bash, to call the main function. In PHP the main function is defined but not called and in C there is no need to explicitly call the main function.

The term is sometimes applied to programs that are valid in more than one language, but do not strictly perform the same function in each. One use for this form is a file that runs as a DOS batch file, then re-runs itself in Perl:

@echo off
perl "%~dpnx0" %*
goto endofperl
@rem ';
#!perl
print "Hello, world!\n";
 :endofperl

This allows the creation of Perl scripts that can be run on DOS systems with minimal effort.

Some polyglots are quines, for example

;; (*.) = {- *) let (@@) x y = x::y let e = [] let a = (*
(letrec ((a '(
; -} -- *)
 "                                                                " @@
 "  A polyglot quine in Haskell, OCaml, Scheme                    " @@
 "                                                                " @@
 "  runhugs this_file   # http://haskell.org/hugs/                " @@
 "  ocaml this_file     # http://caml.inria.fr/                   " @@
 "  scsh -s this_file   # http://scsh.net/                        " @@
 "                                                                " @@
 "  The author hereby grants permission to use, copy, modify,     " @@
 "  and distribute this software for any purpose.                 " @@
 "                                                                " @@
 "" @@
 ";; (*.) = {- *) let (@@) x y = x::y let e = [] let a = (*" @@
 "(letrec ((a '(" @@
 "; -} -- *)" @@
 "" @@
 " e" @@
 ";; (*:) = [\" \" ++ show x ++ \" @@\" | x<-( *.)]; main = {-" @@
 "; -} sequence_ (map putStrLn (x ++ ( *:) ++ y)); (x, _:y) = {-" @@
 "; -} span p (tail (dropWhile p ( *.))); p = (/= \"\"); infixr {-" @@
 "; -} @@; (@@) = (:); e = [] {- *) let rec s = function [] -> (*" @@
 "; *) [],[] | x::y -> if x = \"\" then [],y else let a,b = s y (*" @@
 "; *) in x::a,b let b,d = s (snd (s a)) let f = String.escaped (*" @@
 "; *) let c = List.map (fun x -> \" \\\"\" ^ f x ^ \"\\\" @@\") a" @@
 ";; List.iter (fun x -> print_endline x) (b @ c @ d) (*" @@
 ")) (f (lambda (x) (if (null? x) x (if (string? (car x)) (cons (" @@
 "car x) (f (cdr x))) (f (cdr x)))))) (g (lambda (x) (if (string=?" @@
 "\"\" (car x)) (cons '() (cdr x)) (let ((y (g (cdr x)))) (cons (" @@
 "cons (car x) (car y)) (cdr y)))))) (h (lambda (x) (if (null? x)" @@
 "#f (begin (display (car x)) (newline) (h (cdr x)))))) (i (lambda" @@
 "(x) (if (null? x) #f (begin (display \" \") (write (car x)) (" @@
 "display \" @@\") (newline) (i (cdr x))))))) (let ((b (g (cdr (g" @@
 "(f a)))))) (h (car b)) (i (f a)) (h (cdr b))))" @@
 "; -} -- *)" @@
 e
;; (*:) = [" " ++ show x ++ " @@" | x<-( *.)]; main = {-
; -} sequence_ (map putStrLn (x ++ ( *:) ++ y)); (x, _:y) = {-
; -} span p (tail (dropWhile p ( *.))); p = (/= ""); infixr {-
; -} @@; (@@) = (:); e = [] {- *) let rec s = function [] -> (*
; *) [],[] | x::y -> if x = "" then [],y else let a,b = s y (*
; *) in x::a,b let b,d = s (snd (s a)) let f = String.escaped (*
; *) let c = List.map (fun x -> " \"" ^ f x ^ "\" @@") a
;; List.iter (fun x -> print_endline x) (b @ c @ d) (*
)) (f (lambda (x) (if (null? x) x (if (string? (car x)) (cons (
car x) (f (cdr x))) (f (cdr x)))))) (g (lambda (x) (if (string=?
"" (car x)) (cons '() (cdr x)) (let ((y (g (cdr x)))) (cons (
cons (car x) (car y)) (cdr y)))))) (h (lambda (x) (if (null? x)
#f (begin (display (car x)) (newline) (h (cdr x)))))) (i (lambda
(x) (if (null? x) #f (begin (display " ") (write (car x)) (
display " @@") (newline) (i (cdr x))))))) (let ((b (g (cdr (g
(f a)))))) (h (car b)) (i (f a)) (h (cdr b))))
; -} -- *)

[edit] External links

Languages