Sunday, December 07, 2008

OpenCobol Compiler

I was looking for a COBOL compiler to use compile a matrix multiplication program. I found and downloaded openCobol using Synaptic.

I wrote a simple "Hello World" display program but it would not complie for lack of lncurses. I cursed and installed libncurses5-dev. The program compiled and ran just fine.

OpenCobol translates COBOL to C and then compiles C program using Gnome C compiler. COBOL code is compiled using "cobc -x pgmname.cob" and if you want to see how your COBOL program looks in C try "cobc -C pgmname.cob"

Detailed instructions are available in user manual on openCobol.org


Sample Hello World COBOL program
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLOW.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
DISPLAY "HELLO WORLD".
STOP RUN.

C Program from Cobol program
/* Generated from HELLOW.cob by cobc version 0.33 patch level 0 */

#define __USE_STRING_INLINES 1
#include
#include
#include
#include
#include

#define COB_SOURCE_FILE "HELLOW.cob"
#define COB_PACKAGE_VERSION "0.33"
#define COB_PATCH_LEVEL 0

/* function prototypes */
static int HELLOW_ (const int);

int HELLOW (void);

/* functions */
int
HELLOW ()
{
return HELLOW_ (0);
}
/* end functions */
static int

HELLOW_ (const int entry)
{
#include "HELLOW.c.h" /* local variables */
static int initialized = 0;
static cob_field *cob_user_parameters[COB_MAX_FIELD_PARAMS];
static cob_module module = { NULL, NULL, NULL, NULL, cob_user_parameters, 0, '.', '$', ',', 1, 1, 1, 0, 0};
/* perform frame stack */
int frame_index;
struct frame {
int perform_through;
void *return_address;
} frame_stack[255];

/* Start of function code */

if (unlikely(entry < 0)) {
if (!initialized) {
return 0;
}
initialized = 0;
return 0;
}

module.next = cob_current_module;
cob_current_module = &module;

if (unlikely(initialized == 0))
{
if (!cob_initialized) {
cob_fatal_error (COB_FERROR_INITIALIZED);
}
cob_check_version (COB_SOURCE_FILE, COB_PACKAGE_VERSION, COB_PATCH_LEVEL);
if (module.next)
cob_set_cancel ((const char *)"HELLOW", (void *)HELLOW, (void *)HELLOW_);
(*(int *) (b_1)) = 0;
(*(int *) (b_2)) = 0;
(*(int *) (b_3)) = 0;
initialized = 1;
}

/* initialize frame stack */
frame_index = 0;
frame_stack[0].perform_through = -1;

/* initialize number of call params */
(*(int *) (b_3)) = cob_call_params;
cob_save_call_params = cob_call_params;

goto l_2;

/* PROCEDURE DIVISION */

/* HELLOW: */
l_2:;
/* HELLOW.cob:6: DISPLAY */
{
cob_new_display (0, 1, 1, &c_1);
}
/* HELLOW.cob:7: STOP */
{
cob_stop_run ((*(int *) (b_1)));
}
cob_current_module = cob_current_module->next;
return (*(int *) (b_1));
}

/* end function stuff */

So much for a two line code
#include
main()
{
printf("Hello world\n");
}

No comments: