hp
toc

Generic C / Python Polyglot

2017-11-04, post № 184

C, programming, Python, #recreational

A polyglot — coming from the Greek words πολύς (many) and γλώττα (tongue) — is a piece of source code which can run in multiple languages, often performing language-dependent tasks.
Even though such a source code’s feature may not seem particularily useful or possible with certain language combinations, trying to bend not one but multiple language’s syntactic 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 separate languages. Because of their enormous similarities, one can pick out differences and use those to determine which language runs the source code.

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

Utilizing Python’s version difference regarding integer division and real division is one of the most common ways to tell them apart, as it can also be used to only control a specific part of a program instead of having to write two nearly identical programs (increases the program’s style and cuts on bytes — an important consideration if one golfs a polyglot).

However, polyglots combining languages that are not as similar as Python 2 and Python 3 require more effort. The following is a general 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.
Language determination is exclusively done via syntax. A C compiler [1] sees a #define statement and a line continuation \, another two #define statements with a block comment in between and actual compilable C source code (view first emphasis).Python, on the other hand, treats all octothorps, #, as comments, ignoring the line continuation, and triple-quoted strings, """...""", as strings rather than statements and thus only sees the Python code (view second emphasis).

My first ever polyglot used this exact syntactical language differentiation and solved a task titled Life and Death of Trees (link to my answer).

Footnotes

  1. [2020-08-06] More specifically, a C preprocessor.
Jonathan Frech's blog; built 2024/08/31 22:59:44 CEST