(preliminary) B2CTrans example translated file

Basic to C Translator (B2CTrans)

B2CTrans developed from my earliest gaming interests back in the 1980s when games where written and distributed in listing-format in various flavors of the BASIC programming language.

Back then an old friend of mine, Dave, and I spent many hours laboriously typing in these programs from paper listings of the source code. OCR wasn’t a reality for us yet. Both Dave and I worked on mainframe computers so games were pretty rare. So after many hours of cramped hands we got to play our games. They were trivial but fun.

However, BASIC has gone from an interesting alternative to my commonly used languages of Fortran and Cobol (yuk!), at the time, to a nuisance — especially after getting used to structured modern languages.

The idea of translating these programs into C appealed to me but the process didn’t so I decided to let my university education kick in and I built a Lex/Yacc based translator to convert the source code from Basic to C. It seemed to work really well. The days and weeks it took to develop B2CTrans was well offset by the 1/10sec it took to translate these BASIC programs ;-).

If you are a programmer you’ll understand that the more sophisticated compiler programming was a lot more rewarding than the original games so I lost a little bit of my interest in the games themselves.

The B2CTrans project got to the point where I wanted to convert the BASIC spaghetti code into a structured C instead of doing a straight line-for-line translation. But I got bogged down in trying to translate my parse tree in place and put the project down for a while.

When I tripped across a project called BNFC (Backus-Naur Form Converter) this reignited my interest in B2CTrans as it makes the front end considerably easier to re-write from scratch. Or at least I thought it was going much better but then I recognized that the extra burden of learning BNFC and the limitations that it imposed didn’t offset the tedium of writing YACC grammars. Oh, well!

Currently I’m busy writing a game for Android and when I get finished that I’ll go back and work on these translators some more.

Once I get this working again I’d like to continue doing my Fortran-to-C translator because I’ve got the Empire, Galaxy and Star Trek sources I’d like to translate and get working again too.

Basic to C Translator (B2CTrans)

  • Basilio says:

    I need to translate some old unstructured BASIC code into C or any other currently used language.
    Can you share your B2CTrans project?
    Thanks in advance

    • Alan Angold says:

      Hi Basilio,

      Sorry, to take so long to respond…

      Unfortunately, I put the b2ctrans project in limbo for a while so I could work on other projects. I actually started building the translator because I had a number of Basic programs I wanted to convert, including some from an old book “Basic Computer Games” http://www.vintage-basic.net/games.html

      As I remember it was doing quite well translating them statement-to-statement (one Basic statement to one C statement(roughly)). However, I wanted to convert Basic programs into structured C which is where I got bogged down.

      I’m not sure about the state of the project right now so can’t offer you much.

      What type of programs and what flavour of Basic were you wanting to translate? B2Ctrans does some varieties micro basic.

      Take care,

  • Jay says:

    I was wondering how were you able to convert vintage basic to c, specifically with goto and gosub statements? I am currently trying to translate the Star trek game from the “BASIC Computer Games.”


    • Alan Angold says:

      Hi Jay,

      The C language has statements that are almost identical to the Basic statements. For instance for the Basic “GOTO NNNN \n…\n NNNN ” has an equivalent in C “goto Lbl_NNNN \n…\nLbl_NNNN: ” so you just have to prefix your Basic line numbers (the ones that are used for GOTO or GOSUB targets) with “Lbl_” or something similar. You can see this in some of the images of listings above.

      A Basic GOSUB is just a call to a subroutine. So the Basic “GOSUB NNNN \n…\n NNNN \n…\n END” can be replaced in C with “routine_NNNN \n…\n void routine_NNNN(){ \n … \n }”. But beware Basic subroutines can have multiple entry points. For instance “NNNN … MMMM … END” where the NNNN and MMMM are line numbers and the “\n” represents newlines. My first attempt at this problem just copies the MMMM chunk of code to a completely separate routine. This will duplicate a number of lines of code but it was easy to implement. The hard part in this case is to determine where overlapping subroutines occur.

      This was one advantage of b2c because it hunts down all the “active” line numbers (ones used by GOTOs, GOSUBs etc) and traces the flow of control in cases where you have overlapping subroutines (which seems to happen quite often).

      The b2c program is written in C++ and uses Flex/Bison to parse the old Basic program which produces an AST (Abstract Syntax Tree) in memory and then runs through the AST to output the equivalent C statements.

      BTW, there are a bunch of StarTrek games that seem to have been spawned from, or in parallel with, this initial version.

      Take care,

Leave a Reply

Your email address will not be published. Required fields are marked *