2016-05-19 Brainfuck to COBOL compiler

..and also Brainfuck to C, Python and Pascal, see bottom of page

what? why?

Inspired by my Brainfuck interpreter in COBOL (TL;DR: at work we do automatic conversions from one programming language to another) I decided to implement a program which converts a Brainfuck sourcecode into a COBOL source-code.

Brainfuck is, due to its simplicity, a language that can very easily be translated into something else. Most of the code of the Python program I wrote for doing so consists of boilerplate code.

running it

The Python program expects the Brainfuck code via standard input (stdin) (this is different from the brainfuck interpreter). The output goes to your screen or to a file if you redirect the output.

cat program.b | python compile-brainfuck-to-cobol.py > program.cbl
Please let me know if you got the result to work on a real mainframe!


Download link: compile-brainfuck-to-cobol.py


I verified the working of this compiler by converting this brainfuck program (which calculates the Mandelbrot fractal(!)) into COBOL and then compiling that COBOL program into a native Linux native binary using GnuCOBOL.

speed optimalization tip

TIP: this tip is not specific to using the COBOL programs generated by this compiler program but may apply to all other programs compiled with the GnuCOBOL compiler:
I got a 16,8% speed improvement by letting GnuCOBOL produce a .c-file (using -C) and then compile & link that with the GNU C-compiler (instead of letting GnuCOBOL do all by itself):

cobc -O2 -fimplicit-init -C mandelbrot.cbl
gcc -O3 -march=native -mtune=native -fomit-frame-pointer -lcob mandelbrot.c -o mb2
NOTE: you need to manually add a 'main()' function to the .c-file first.

speed comparison

compiled and linked by cobc (cobc -O2)
real    11m19.063s
user    11m15.768s
sys     0m1.084s

translated to c and then compiled with gnu-c
real    9m25.608s
user    9m24.040s
sys     0m0.492s

executed original code using beef
real    11m9.350s
user    10m29.464s
sys     0m1.920s


conversions to other languages

Check this page for python, pascal, c and others.