1//1;"""Polyglot JS/Python program. /*""" """How can we make a JS/Python polyglot program, i.e. something that parses in both languages? One approach is, of course, to use the common subset of the syntax of both languages. But this is actually pretty small: simple string literals, numbers, and function calls. For real polyglot flexibility, you want the code for each language to be contained in the strings or comments of the other, so that you can put anything into the code that doesn't interfere with the string or comment syntax. Essentially you want to keep the tokenizers of the two languages desynchronized, in "opposite" states, until the end of the program. It turns out that this is a pretty easy situation to maintain once you have it set up; you can use the sequences demonstrated below to alternate between Python code/JS comment and JS code/Python string: """ js_code = """ /**/ function f() { return 3 + 4; } console.log(f()); /* """+"*"+"/" if __name__ == '__main__': print js_code """But how do you get them desynchronized in the first place without producing syntax errors in either language? You need some kind of slight mismatch in the lexical syntax, for a lexeme that is part of a valid sentence in both languages. r'' has promising backslashing rules, but won't work because you can't just juxtapose two expressions on JS. # basically isn't valid at all in JS. JS /* can't, I think, be part of valid Python code. // can --- it's a division operator. Maybe something like 1 // 1; ''' """ # And then to resynchronize at the end of the program: # */