Cobol Code analyser

I had to analyse / review COBOL code as part of my job, and I have always found it difficult to do it from the basic ISPF browser. My friend Selva has a created a useful, simple tool to make this easier.
You can download it here. It is a jar file that will work if you have Java Runtime Environment installed.

Let me know in the comments if you find it useful, or any suggestions for improvements.

(This is only an initial version of this tool. Selva is still working to introduce much more features).

t2

Click on the “Load” button to open any cobol file that you have downloaded from mainframe. The tree structure (Code flow) present in the right side, will list all the sections / paragraphs present in the code. You can double click any section in the tree to locate that part within the code. You can use ‘Ctrl + F’ shortcut to find any part code, and use F5 to reach next occurrences of the found word.

Download

COBOL – PERFORM a SECTION

Below sample COBOL program explains how to PERFORM a SECTION.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       IDENTIFICATION DIVISION.       
       PROGRAM-ID.    KARTEST5.       
      * perform a SECTION             
       ENVIRONMENT DIVISION.          
       DATA DIVISION.                 
       PROCEDURE DIVISION.            
           PERFORM MY-SECTION.        
           GOBACK.
       MY-SECTION SECTION.            
           DISPLAY 'INSIDE MY-SECTION'
           EXIT.

Output of this program will be

INSIDE MY-SECTION

COBOL – IF ELSE ENDIF statement

Below sample COBOL program explains the IF ELSE END-IF statement. You can avoid using END-IF by using dot (‘.’) as scope terminator. But it is not a good practice. better, always code a END-IF statement as scope terminator.

‘THEN’ is optional. If you want, THEN you can use it.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       IDENTIFICATION DIVISION.           
       PROGRAM-ID.    KARTEST4.           
      * IF statement in cobol             
       ENVIRONMENT DIVISION.              
       DATA DIVISION.                     
       WORKING-STORAGE SECTION.           
        01 MY-NUMBER  PIC 9(2) VALUE 2.   
       PROCEDURE DIVISION.                
      * simple IF statement
           IF MY-NUMBER = 2               
              DISPLAY 'TWO'               
           END-IF                         
      * IF ELSE statement
           IF MY-NUMBER IS EQUAL TO 2 THEN
              DISPLAY 'TWO'               
           ELSE
              DISPLAY 'NOT TWO'
           END-IF              
      * nested IF ELSE statement
           IF MY-NUMBER = 1
              DISPLAY 'ONE'               
           ELSE
              IF MY-NUMBER = 2
                 DISPLAY 'TWO'
              ELSE
                 DISPLAY 'NEITHER ONE NOR TWO'
              END-IF
           END-IF              
           GOBACK.

Output of this program will be

TWO
TWO
TWO

Complete syntax of IF Command (COBOL)

COBOL–Working storage variables

Working Storage section comes under DATA division. Different datatypes of COBOL are explained here.

Below sample cobol program declares and Displays a working storage variable named ‘MY-STRING’.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       IDENTIFICATION DIVISION.                        
       PROGRAM-ID.    KARTEST3.                        
      * define and use a working storage variable      
       ENVIRONMENT DIVISION.                           
       DATA DIVISION.                                  
       WORKING-STORAGE SECTION.                        
        01 MY-STRING  PIC X(20) VALUE 'HELLO WORLD ! '.
       PROCEDURE DIVISION.                             
           DISPLAY MY-STRING.                          
           GOBACK.

Output of this program will be

HELLO WORLD !

COBOL – Hello World program

Below sample cobol program has the minimal set of statements required. It contains all the four divisions and PROGRAM-ID is a mandatory statement which will have the program name.

IDENTIFICATION DIVISION
ENVIRONMENT DIVISION        
DATA DIVISION              
PROCEDURE DIVISION

Divisions – > Section –> Paragraph –> Statement.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       IDENTIFICATION DIVISION. 
       PROGRAM-ID.    KARTEST2. 
       ENVIRONMENT DIVISION. 
       DATA DIVISION. 
       PROCEDURE DIVISION. 
           DISPLAY 'HELLO WORLD ! ' .
           GOBACK.

Output of this program will be

HELLO WORLD !