Jonathan. Frech’s WebBlog

Generic C / Python Polyglot (#184)

Jonathan Frech,

A polyglot — com­ing from the Greek words πολύς (many) and γλώττα (tongue) — is a piece of source code which can run in mul­ti­ple languages, often per­form­ing lan­guage-de­pen­dent tasks.
Even though such a source code’s fea­ture may not seem particularily useful or possible with certain lan­guage combinations, trying to bend not one but mul­ti­ple lan­guage’s syn­tac­tic rules and overall behavior is still interesting.

An example of a rather simple polyglot would be a Python 2 / Python 3 polyglot — if one counts those as two sep­a­rate languages. Because of their enormous similarities, one can pick out differences and use those to de­ter­mine which lan­guage runs the source code.

if 1/2>0:print("Python 3")
else:print("Python 2")

Utilizing Python’s version difference re­gard­ing in­te­ger division and real division is one of the most com­mon ways to tell them apart, as it can also be used to on­ly control a specific part of a pro­gram in­stead of having to write two nearly identical programs (increases the pro­gram’s style and cuts on bytes — an important con­sid­er­a­tion if one golfs a polyglot).

How­ev­er, polyglots combining languages that are not as similar as Python 2 and Python 3 re­quire more effort. The following is a gen­er­al Python 2 / C polyglot, meaning that nearly all C and Python 2 programs can be mixed using this template (there are a few rules both languages need to abide which will come apparent later).

#define _\
"""
main(){printf("C");}
#define _"""
#define/*
print"Python 2"
#*/_f

In the above code, main(){printf("C");} can be nearly any C code and print"Python 2" can be nearly any Python 2 code.
Lan­guage determination is exclusively done via syntax. A C com­pil­er⁠¹ sees a #define statement and a line continuation \, another two #define statements with a block com­ment in be­tween and actual compilable C source code (view first emphasis).Python, on the oth­er hand, treats all octothorps, #, as comments, ignoring the line continuation, and triple-quoted strings, """...""", as strings rather than statements and thus on­ly sees the Python code (view second emphasis).

My first ever polyglot used this exact syntactical lan­guage differentiation and solved a task titled Life and Death of Trees (link to my an­swer).


[1][2020-08-06] More specifically, a C preprocessor.