[Question]
  [
A [polyglot](/questions/tagged/polyglot "show questions tagged 'polyglot'") is a program that can be run in 2 or more different programming languages.
What general tips do you have for making polyglots, or choosing languages that are easy to write polyglots for a specific task?
Please post the tips that could be applied in most situations. I.e. they shouldn't work only in polyglots of two specific languages. (You could simply post an answer to a polyglot question if you have a too specific tip.) But you can introduce features of a language that makes it easy to work with many languages, or easy to be added to any existing polyglots.
Please post one tip per answer. And feel free to suggest edit if a language-specific tip also applies to another language.
  
[Answer]
# Use two-dimensional languages
Unlike one-dimensional languages, which generally parse the entire source code and will produce syntax errors or unwanted runtime effects on things they don't understand (thus forcing you to hide other languages' code from them), two-dimensional languages tend to only parse code in the path of execution, meaning that the entire rest of the program is ignored. There's also a lot more room to split execution paths away from each other in two dimensions; you can send the instruction pointer turning in an unusual direction, like down or even left (wrapping round to the right hand side of the program), to get it out of your way very quickly. The techniques useful in one-dimensional languages also generalise to two-dimensional languages (e.g. you can skip over code with `;;` in Befunge-98, in addition to just sending the IP in a weird direction), making this mostly just a strict gain compared to a one-dimensional solution.
As a bonus, several two-dimensional languages have an entry point other than the top-left of the program, meaning that you don't need to go to any effort to split them away from other languages; they'll split themselves off from the group naturally.
[Answer]
# Exploit comment symbols
A simple way to create a two-language polyglot is to have the code divided in two parts as follows:
1. The first part does the actual work in language A, is harmless in language B (no errors), and ends in a language-A comment symbol, which hides the second part to language A.
2. The second part does the actual work in language B.
Thus
* Language A sees the first part, which does the job, and then a comment.
* Language B sees a useless first part and then the second part, which does the job.
The only difficult part here is finding a set of statements (first part) that do the job in language A while not giving errors in language B. Some suggestions for this:
* Most stack-based languages allow displaying only the top of the stack at the end of the program (sometimes this is even default, as in 05AB1E).
* Some languages ignore undefined statements (for example Golfscript).
A simple example that uses these guidelines can be found [here](https://codegolf.stackexchange.com/questions/98776/palindrome-polyglot/98781#98781). Languages A and B are [MATL](https://github.com/lmendo/MATL) and [05AB1E](https://github.com/Adriandmen/05AB1E) respectively.
[Answer]
# Know your Trues and Falses
Each language sees "true" and "false" in a slightly different way. If they have similar syntax, you can exploit this by adding a decision that the languages will handle differently.
[One example](https://codegolf.stackexchange.com/a/97812/60919) from the Trick or Treat thread uses `''`, an empty string. In Lua, this evaluates to truthy, but falsy in Python, so the following:
```
print(''and'trick'or'treat')
```
..will print a different string in each language. 
All it takes is finding a value like this. For example, you could use `'0'`, which evaluates to `false` in PHP but `true` in Python.
[Answer]
## Blockquotes in at least one language
Here's an example that works both in Python and C++
```
#include <iostream> /*
""" */
int main() {
    std::cout << "Hello World!\n";
}
/* """
print("Hello World!")
# */
```
[Luis Mendo](https://codegolf.stackexchange.com/a/101161) pitched what I think is by far the easiest solution, which is to use comments.
You look for one language that has block commenting and another language where regular syntax in the first is commenting syntax in the second.
Even easier is two languages with different block commenting styles that are interchangeably correct syntax, but I couldn't be bothered to check.
Check it out in [Python 3.5](https://repl.it/E8GQ/0) and [C++](https://repl.it/E8GS/0)
[Answer]
# Divide and conquer
When you're writing a polyglot in a large number of languages, you won't necessarily be able to separate all the language's control flows from each other immediately. Thus, you'll need to "true polyglot" some of the languages for some length of time, allowing the same code to run in each of them. There are two main rules to bear in mind while you're doing this:
* **The control flow in any two languages should either be very similar, or very different**. Trying to handle a large number of interleaved control flows is a recipe for getting confused and makes your program hard to modify. Instead, you should limit the amount of work you have to do by ensuring that all the programs that are in the same place are there for the same reason and can happily be run in parallel for as long as you need them to be. Meanwhile, if a language is very different from the others, you want its execution to move to a very different location as soon as possible, so that you don't have to try to make your code conform to two different syntactic models at once.
* **Look for opportunities to split one language, or a group of similar languages, away from each other. Work from larger groups down to smaller groups.** Once you have a group of similar languages all at a certain point in the program, you'll need to split them up at some point. At the start of the program, you might well, say, want to split the languages that use `#` as a comment marker away from languages that use some other comment marker. Later on, perhaps you have a point where all languages use `f(x)` syntax for function calls, separate commands with semicolons, and have similar syntactic similarities. At that point, you could use something much more language-specific to split them, e.g. the fact that Ruby and Perl don't process escape sequences in `''` strings, but Python and JavaScript do.
In general, the logical flow of your program should end up as a tree, repeatedly splitting into groups of languages that are more similar than each other. This puts most of the difficulty in writing the polyglot right at the start, before the first split. As the control flow branches out more and more, and the languages that are running at any given point get more and more similar, your task gets easier because you can use more advanced syntax without causing the languages involved to syntax-error.
A good example is the set {JavaScript, Ruby, Perl, Python 3}; all these languages accept function calls with parentheses and can separate statements with semicolons. They also all support an `eval` statement, which effectively allows you to do flow control in a portable way. (Perl is the best of these languages to split off from the group early, because it has a different syntax for variables from the others.)
[Answer]
# Variable or code inside string literals
Double-quoted string literals are mostly harmless in many languages. But in some languages they could also contain code.
In Bash, you can use ``...`` (it doesn't end the program):
```
"`echo Hello world! >/proc/$$/fd/1`"
```
In Tcl, you can use `[...]`:
```
"[puts {hello world!};exit]"
```
In PHP, you can use `${...}` (this generates an error in Bash so it must appear after the Bash code):
```
"${die(print(Hello.chr(32).world.chr(33)))}";
```
In Ruby, you can use `#{...}`:
```
"#{puts 'Hello world!';exit}"
```
There might be also others.
These grammars aren't compatible. That means you can put all the code of these languages in one string in a harmless location. And it will just ignore the unrecognized code in other languages and interpret them as string content.
In many cases, you could also easily comment out a double quote character there and make a more traditional polyglot.
[Answer]
# Hide code inside string literals
In most languages, a string literal on its own either does nothing, or does something that can be easily reversed (such as pushing the string onto the stack). String literal syntax is also relatively nonstandardised, especially for the alternative syntaxes that many languages use to handle strings with embedded newlines; for example, Python has `""" ... """`, Perl has `q( ... )`, and Lua has `[[ ... ]]`.
There are two main uses of these. One is to allow you to interleave sections intended for different languages via starting a string at the end of one language's first section and resuming it at the start of the second: it should be fairly easy to avoid accidentally closing the string due to the variety of string delimiters among different languages. The other is that many string delimiters happen to be meaningful as a command in other languages (often more so than comment markers), so you can do something like `x = [[4] ]`, which is a harmless assignment in languages which use JSON notation for lists, but which starts a string in Lua (and thus allows you to split the Lua code from the rest, given that it effectively "jumps" to the next `]]`).
[Answer]
# Variable Aliasing
This is probably one of the simplest yet (IMO) most important tricks to use, especially since it can reach so many languages.
Example:
```
print=alert;print("Hello World!")
```
This will work in not only Javascript, but also Python, Ruby, etc. More examples later when I think of some others. Of course, comment suggestions/post edits are welcome.
[Answer]
## `#`-based comments
*This tip is a subset of [Exploit comment symbols](https://codegolf.stackexchange.com/a/101161/21487) and [Blockquotes in at least one language](https://codegolf.stackexchange.com/a/101168/21487)*
When creating polyglots with many languages, especially production-ready languages as opposed to esolangs, it can be useful to look at the languages which use `#` in block or single-line comments.
* There are many languages with block comment syntaxes starting with `#`, and there's a lot of variety in the chars following the `#`.
* Most of these languages also allow a single `#` as a line comment, which means that something which might start a block comment in one language is just an ordinary comment in another, making it easy to fit in.
Here's a quick summary list of languages which use `#` in a block comment (not exhaustive):
```
Language            Start       End      Single-line #?     Notes
------------------------------------------------------------------------------------------
Agena               #/          /#             ✓
AutoIt              #cs         #ce
Brat                #*          *#             ✓
C                   #if 0       #endif                      Not actually a comment
CoffeeScript        ###         ###            ✓            Needs to be on separate line
Common Lisp         #|          |#
Julia               #=          =#             ✓
Lily                #[          ]#             ✓
Objeck              #~          ~#             ✓
Perl 6              #`{         }#             ✓            Any bracketing chars will do
Picolisp            #{          }#             ✓
Scheme              #|          |#
```
For more examples, see [Rosetta Code](https://www.rosettacode.org/wiki/Comments).
Here's a quick and easy example, as a demonstration:
```
#|
###
#`[
print("Julia")
#=
|#
(format t "Common Lisp")
#|
###
alert("CoffeeScript")
###
]#
say "Perl 6"
#`[
...
# ]# # ### # |# ; =#
```
[Answer]
# Ending the program
You can end the program abruptly in one language so that it will ignore the code in another language.
So basically this format can be used
```
code_in_language1 end_program_in_language1 code_for_language2 end_program_in_language2 ...
```
where `end_program_in_languageN` is the command for ending the program.
For example, in my answer in [What will you bring for Thanksgiving?](https://codegolf.stackexchange.com/questions/100470/what-will-you-bring-for-thanksgiving/100483#100483), I ended the program in Dip, and then I wrote code for another language, V, so that the Dip interpreter would ignore it.
```
"turkey"e#"corn"??"gravy"p&Ssalad
"turkey"e#"corn"??"gravy"                 
                         p&            # print stack and exit program (Dip) 
                           Ssalad      # Now that the program ended in Dip,
                                       # I can write V code that would otherwise
                                       # have caused errors in Dip
```
But then, not all languages have a command that can end the program just like that. However, if such a language has the feature, it should be used wisely.
As @LuisMendo suggested, you can create an error (if it is allowed) to end the program if the language does not already have an "end program" builtin.
[Answer]
## Arithmetic operator discrepancies
For similar languages or simple polyglots, sometimes it's useful to look for differences in how the languages perform arithmetic. This is because most (non-esoteric) languages have infix arithmetic operators and arithmetic can be a quick and easy way to introduce a difference.
For example:
* `^` is bitwise XOR in some languages and exponentiation in others
* `/` is integer division in some languages and floating point division in others
	+ For the integer division languages, `-1/2` is `-1` in some languages (round down) and `0` in others (round to zero)
* `-1%2` is `-1` in some languages and `1` in others
* `--x` is a no-op in some languages (double negation) and pre-decrement in others
* `1/0` gives infinity in some languages and errors out in others
* `1<<64` gives 0 in some languages (overflow) and `36893488147419103232` in others
[Answer]
# Use Brainfuck
Pretty much all BF implementations cast out chars that aren't `+-<>[].,`, which just so happens to work in our favor!
BF is probably one of the easiest languages to work into a polyglot because of this feature, **as long as you write the BF part first.** Once you have your BF code written out, it's just a matter of modeling whatever other code you have *around* the BF structure.
Here's a really simple example:
```
.+[.+]
```
This pretty much increments and charcode-outputs "forever" (depending on runtime settings). Now if you wanted to write a random piece of code, say, in JS, you could do:
```
x=>"asdf".repeat(+x)[x*Math.random()*2+1|0]
```
Notice how the JS is molded around the BF.
Be sure to know that this works best if you are really set on starting with BF; it's decently harder to start with another language and try to incorporate BF.
[Answer]
# Use languages in which most characters don't matter
This is a generalization of [Mama Fun Roll's point about BF](https://codegolf.stackexchange.com/a/101171/16766). An esolang that ignores most characters is very useful in polyglots. Also useful: an esolang in which a large set of characters are interchangeable. Some examples:
* [Whitespace](http://esolangs.org/wiki/Whitespace) ignores everything that isn't space, tab, or newline.
* [Brain-Flak](http://esolangs.org/wiki/Brain-Flak) basically ignores everything besides `()[]{}<>`. (`@` sometimes causes an error when the interpreter tries to parse it as the start of a debug flag.)
* [oOo CODE](http://esolangs.org/wiki/OOo_CODE) ignores everything except letters. Furthermore, all lowercase letters are interchangeable, as are all uppercase letters.
* [Wierd](http://esolangs.org/wiki/Wierd) only distinguishes between whitespace and non-whitespace characters.
* In [Wordy](http://esolangs.org/wiki/Wordy), some punctuation characters are ignored, and all letters are interchangeable.
* Both [Parenthetic](http://esolangs.org/wiki/Parenthetic) and [Parenthesis Hell](http://esolangs.org/wiki/Parenthesis_Hell) ignore everything except parentheses.
[Answer]
# Be Aware of Nested Block Comments
Sometimes multiple languages will use the same syntax for block comments, which is more often than not a deal breaker for creating a polyglot with the two languages. Very occasionally however, one of the languages will allow nested block comments, which can be abused to create separate code paths. 
For example, consider this polyglot: 
```
#[#[]#print("Lily")#]#echo"Nim"
```
Nim and Lily both use `#[` and `]#` to begin and end block comments, but only Nim allows nested block comments. 
Lily considers the second `#[` as part of the singular block comment and the first `]#` as terminating the block comment. (The `#` following Lily’s print statement is a line comment that hides Nim’s code.)
Nim alternatively, sees the `#[]#` as a nested (albeit empty) block comment and `print("Lily")#` as the outer block comment. 
[Answer]
# Call nonexistent functions, then exit while evaluating their arguments
Many programming languages are capable of parsing an arbitrary identifier followed by a pair of parentheses with an expressions inside:
```
identifier(1 + 1)
```
Sometimes, the form of the identifier in question might be fixed, due to being needed to give code to a different language you're using. That might at first seem to cause trouble, if the identifier doesn't correspond to a function that the language actually has.
However, many programming languages will evaluate a function's arguments before they check to see if the function itself actually exists (e.g. Lua), and so you can use this sort of construct anyway; all you need is to exit the program somewhere inside the function's arguments.
Here's an example, a dc/Lua polyglot:
```
c2pq(1 + #os.exit(print(3)))
```
`c2pq` is a dc program to print 2 and exit; Lua sees this as the name of a function, but Lua can be prevented from erroring via placing an exit command in its argument. The big advantage of this construction is that unlike an assignment (`c2pq =`), it's not automatically incompatible with languages in which variable names start with a sigil; function name syntax is much more consistent across languages than variable name syntax is.
[Answer]
Not sure if this counts, but...
# Use a shebang line to turn everything into a valid `perl` program
According to [this answer](https://askubuntu.com/a/850575) and the Perl documentation, if you pass any file that starts with a shebang line to `perl`, it invokes the appropriate program to run it. For instance, this
```
#!/usr/bin/python
for i in range(6):
    print i**2
```
gets executed by the Python interpreter if you call `perl filename.py`.
]