What code compiles in the most number of languages?

29

7

Excluding trivial programs, what code compiles in the most number of languages?

(By "trivial" I mean to exclude answers such as the empty program or text that will be echoed directly.)

The following code apparently compiles in all of the following programming languages (and prints something different in each one): C, C++, Perl, TeX, LaTeX, PostScript, sh, bash, zsh and Prolog.

%:/*:if 0;"true" +s ||true<</;#|+q|*/include<stdio.h>/*\_/
{\if(%)}newpath/Times-Roman findfont 20 scalefont setfont(
%%)pop 72 72 moveto(Just another PostScript hacker,)show((
t)}. t:-write('Just another Prolog hacker,'),nl,halt. :-t.
:-initialization(t). end_of_file. %)pop pop showpage(-: */
int main(){return 0&printf("Just another C%s hacker,\n",1%
sizeof'2'*2+"++");}/*\fi}\csname @gobble\endcsname{\egroup
\let\LaTeX\TeX\ifx}\if00\documentclass{article}\begin{doc%
ument}\fi Just another \LaTeX\ hacker,\end{document}|if 0;
/(J.*)\$sh(.*)"/,print"$1Perl$2$/"if$_.=q # hack the lang!
/
sh=sh;test $BASH_VERSION &&sh=bash;test $POSIXLY_CORRECT&&
sh=sh;test  $ZSH_VERSION && sh=zsh;awk 'BEGIN{x="%c[A%c[K"
printf(x,27,27)}';echo "Just another $sh hacker," #)pop%*/

That's 10 different languages. I found it via pts oldalai (who also has a magnificent Christmas poem written in C, C++, Perl and TeX). Can anyone do better?

shamp00

Posted 2012-01-02T11:11:33.350

Reputation: 1 221

Question was closed 2016-01-31T03:49:51.310

Coming to your no empty file restriction, IIRC an empty file doesn't compile in GCC. – Reinstate Monica - ζ-- – 2012-10-24T11:32:22.593

7There are languages (e.g. Whitespace, BrainF**k and Perl), where pretty much any character combination is a valid program. They can be claimed by any program. – ugoren – 2012-11-18T19:29:32.910

1

SO's original polyglot: http://meta.stackexchange.com/questions/28625/whats-the-joke-in-the-stack-overflow-404-page-code

– It'sNotALie. – 2013-11-25T22:07:32.043

8

The technical term being Polyglot

– st0le – 2012-01-02T11:55:07.033

8

Here's 16 language polyglot

– st0le – 2012-01-02T11:56:16.613

7If sh, bash, and zsh count as different languages even when it's essentially only using sh then I think you need to specify precisely what you count as different languages. E.g. Perl 4 vs Perl 5.10 have some significant differences. – Peter Taylor – 2012-01-02T13:14:19.153

@PeterTaylor: Point taken, and many languages are likely to be similar, e.g., C/C++, Tex/LaTeX. Perhaps that's the nature of the game - to win you have to target the language that has the most number of near-variants. – shamp00 – 2012-01-02T15:15:39.550

@st0le: Thanks for that, I failed to find the name for this type of program (and I am surprised to find that this is the first use of the polyglot tag on this site). – shamp00 – 2012-01-02T15:15:58.957

1

@shamp00, infact when you try to access a deleted question on stackoverflow they show you a polyglot :D see here

– st0le – 2012-01-03T03:43:14.263

Answers

9

3 Languages - C, C++, and Python

#ifdef _cplusplus
    #include <iostream>
    #define print() int main(){cout << "Hello world! -- from C++" << endl;}
#elif (defined __STDC__) || (defined __STDC_VERSION__)
    #include <stdio.h>
    #define print() int main(){printf("Hello world! -- from C\n");}
#else
import builtins
print = lambda : builtins.print("Hello world! -- from Python")
#endif

print()

Something different is printed in each language. In C & C++, lines starting with '#' are preprocessing directives, but those same lines are comments in Python.

golfer9338

Posted 2012-01-02T11:11:33.350

Reputation: 411

4

5 languages- Thue, Brainf***, Boolf***, Treehugger, and Javascript

/*::=
alert::=~This is Thue!
::=
-><[[--->+<]>-.[---->+++++<]>-.+.++++++++++.+[---->+<]>+++.-[--->++<]>-.++++++++++.+[---->+<]>+++.+[->++<]>.---[----->+<]>-.+++[->+++<]>++.++++++++.+++++.--------.---[->+++<]>+...---------.[-]]
^^[[--->+^]>-.[---->+++++^]>-.+.++++++++++.+[---->+^]>+++.-[--->++^]>-.++++++++++.+[---->+^]>+++.>-[--->+^]>-.-[--->+^]>+.-------------..+++.[--->+^]>---.++[->+++^]>++..--.+++++++++++++.[--->+^]>-----.[-]]
-+[+;;+;+;+;+;+;+;;;;+;+;+;;+;+;+;;+;+;+;;+;+;;+;;+;;;+;;;;;;+;+;;+;+;;+;+;+;;+;+;;+;;+;;;+;;;;;;+;+;;;+;+;;;;+;+;+;;;;+;+;;+;+;;;;+;+;;+;;;+;;+;+;;+;;+;;+;;+;;+;;+;+;+;+;+;+;;;+;+;+;+;+;+;;;+;+;+;+;+;+;;+;+;;;;+;+;;]
*/alert("This is Javascript!")

Note that the Treehugger portion times out in the web-based implementation, unfortunately, so you should use a different Treehugger interpreter.

SuperJedi224

Posted 2012-01-02T11:11:33.350

Reputation: 11 342