Polyglots are programs that are valid in multiple programming languages simultaneously. Most such polyglots are written in such a way that certain constructs of one language are interpreted as comments of another language (e.g. #define in C being interpreted as a comment in several scripting languages).

I am curious to see if it is possible to make a non-trivial polyglot which contains no comments, but also immediately changes when you remove any non-whitespace character, I therefore challenge you to come up with such a program.

The concrete rules are as follows:

(Output). Your program must produce some output on the console under each of your languages. That is, your program is not permitted to simply exit without printing anything.

(Variance). As a relaxation of the standard polyglot definition, the program's output may vary between languages.

(Errors). Your program must not produce any errors (broadly defined) under any of your languages. For most languages, this is defined as returning a non-zero exit code from the compiler and/or interpreter.

(Restriction). The removal of any single non-whitespace character from your code should cause your program to change its behaviour under every one of your languages. The program may "change" by becoming invalid for that language, or by changing the output that is produced.

This is a code challenge. Winner is the program which is valid in the most programming languages. Ties will be broken in favor of shorter program length.

The restriction rule doesn't apply to the removal of several characters. That is, it is fine if removing several characters simultaneously results in no change for one of your languages.

Observe that the restriction rule implies that you cannot use Whitespace as one of your languages, as removing any non-whitespace character won't change the behaviour of the Whitespace program.

Here's a simple example of a program that fulfills all the above restrictions, for the languages Python 2 and Python 3:

print("Hello World!")

Removing any character in print will cause both languages to throw a NameError; removing any bracket or quote will throw a SyntaxError, and removing any of the string characters will change the output in both languages. (Note that print("hello", "world") is a more subtle, but still valid program under the above rules).

This example is a bit lame because Python 2 and Python 3 are very similar, so I won't accept any other solutions that only use different versions of the same language (especially Python 2 and Python 3).

It's a little annoying that the simple answers (like mine) to a [polyglot] tend to be able to have so many languages.
– JustinSep 28 '14 at 19:40

5

To avoid the myriad language variants or languages with similar syntax, it would be more interesting to require that two languages count as different only if the program produces different output.
– GillesSep 29 '14 at 12:09

GNU Octave, version 3.6.4
Copyright (C) 2013 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. For details, type `warranty'.
Octave was configured for "x86_64-redhat-linux-gnu".
Additional information about Octave is available at http://www.octave.org.
Please contribute if you find this software useful.
For more information, visit http://www.octave.org/get-involved.html
Read http://www.octave.org/bugs.html to learn how to submit bug reports.
For information about changes from previous versions, type `news'.

And a set of fixes to make it satisfy

(Restriction). The removal of any single non-whitespace character from your code should cause your program to change its behaviour under every one of your languages. The program may "change" by becoming invalid for that language, or by changing the output that is produced.

From Javascript side:

Changing int, main or puts will crash with reading of undeclared variable. But bool and Hi from C++! can be safely changes. Let's fix it:

puts=function(){}

puts=function(s){s=='Hi from C++!'?bool:nope}

If strings are equal, it checks existance of bool, otherwice it crashes with undeclared nope.

Now, there are 2 trailing semicolons that can be removed. The first is fixed esyly - just remove newline before int:

C and C++

#include <stdio.h>
int main() { puts("Hello!"); return 0; }

C and C++ are different languages that are almost compatible with each other - contrary to what you might hear C++ is not a superset of C. Look at http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B for some differences. The example above is not idiomatic C++ but it does work and produces the same output in both C and C++.

@Gilles: If the main function executes a return that specifies no value, the termination status returned to the host environment is undefined. (The C89 Draft - 2.1.2.2 Hosted environment) That doesn't make the program invalid nor does it change its output, so for the purposes of this question, the 0 can be removed.
– Dennis♦Sep 29 '14 at 13:37

@Dennis I think an unspecified exit code counts as making the program invalid as per the rules of this question. If it doesn't, make that a C89 implementation where this program returns a nonzero exit code, such as gcc -ansi on x86 (or many other architectures: what happens is that the return value of puts ends up being in the register where the runtime reads the value to return from the main function).
– GillesSep 29 '14 at 17:18

@Gilles: My interpretation of rule 3 is that the compiler must return 0, not the program.
– Dennis♦Sep 29 '14 at 17:24

Your Answer

If this is an answer to a challenge…

…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.

…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.

…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.

More generally…

…Please make sure to answer the question and provide sufficient detail.

…Avoid asking for help, clarification or responding to other answers (use comments instead).