BASIC to C converter based on M4


The good old M4 macro processor is not used very often nowadays, though it can be found on virtually every Unix and Linux distribution. It *does* take some effort to master the concepts of M4, but once the main concepts are grasped, it can be of great help in your daily Unix programming tasks.

There is a lot of documentation about the capabilities of M4, but good examples are missing. To train myself in M4 I have tried to implement a simple BASIC to C converter loosely based on the syntax of my BaCon project. The results can be found below.

The converter is work in progress but already fairly complete. The implementation is published here to fill the gap of good M4 examples, and also in the hope that this will improve the general understanding of M4. Furthermore it can be used in small Unix distributions, or in case of slow remote connections where a script is too slow to solve a problem and a binary needs to be created quickly to carry out the task at hand.

For the allowed BASIC syntax, please refer to the example BASIC programs down below this page.


Using a macro processor to implement a converter reveals some limitations:

In order to keep this converter small, the M4BASIC-to-C converter relies on the following techniques:

Real parsing needs higher level capabilities. It seems that a programming language is needed to create a programmming language ☺


Alternatively, the script m4basic performs all the steps above. Simply run './m4basic prog.m4b' to convert and compile.

The M4 program

BASIC test programs

The 'Makefile' for the above programs is here.

Last update: October 7, 2017