Modern COBOL since 1985

COBOL is not a language that stopped in 1959. It has a standard that is revised every decade or so, and every revision added what programmers of the time were asking for: structured control flow in 1985, functions, recursion and objects in 2002, JSON and XML in 2014. What a compiler implements is a different question from what a standard says, and this lesson keeps the two apart with real builds.

Three standards

COBOL 85 is the one that shaped the language most people still write. It added EVALUATE, the explicit scope terminators (END-IF, END-PERFORM, END-READ), inline PERFORM and the reference modification of a field. A program written for it has no intrinsic functions and no objects.

COBOL 2002 added the intrinsic functions (FUNCTION TRIM, UPPER-CASE, NUMVAL, the date functions), recursive programs, object orientation, and free source format as a standard rather than a compiler extension. A large part of this course uses it: the string functions, recursion and the OO lessons would not compile under COBOL 85.

COBOL 2014 added the JSON and XML facilities, dynamic tables and a set of smaller features. Compilers implement it in parts; GnuCOBOL 3.2 builds some of it and marks other parts with -Wpending, as the JSON and XML lessons show.

The -std option

cobc -std= configures the compiler for a dialect, and the dialects it knows are not only the three standards:

cobc --help | sed -n '/std=<dialect>/,/configuration/p'

  -std=<dialect> warnings/features for a specific dialect
                        <dialect> can be one of:
                        default, cobol2014, cobol2002, cobol85, xopen,
                        ibm-strict, ibm, mvs-strict, mvs,
                        mf-strict, mf, bs2000-strict, bs2000,
                        acu-strict, acu, rm-strict, rm, gcos-strict,
                        gcos;
                        see configuration files in directory config

The default dialect is what this course uses: the language as GnuCOBOL implements it, standards plus extensions. The other names are for a program that is meant to be portable to a compiler of that family, or for a build that has to prove it uses no extension.

A 2002 program under COBOL 85

The program takes a name with spaces around it, trims it, upper-cases it and computes a factorial:

IDENTIFICATION DIVISION. PROGRAM-ID. MODERN. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NAME PIC X(10) VALUE " ada ". 01 WS-UP PIC X(10). 01 WS-N PIC 9(3) VALUE 7. 01 WS-FACT PIC 9(9). PROCEDURE DIVISION. MOVE FUNCTION UPPER-CASE(FUNCTION TRIM(WS-NAME)) TO WS-UP DISPLAY "name [" FUNCTION TRIM(WS-UP) "]" COMPUTE WS-FACT = FUNCTION FACTORIAL(WS-N) DISPLAY "factorial " WS-FACT STOP RUN.

Built with the default dialect and with COBOL 85, the same source is accepted and refused:

cobc -x -free -Wall -o modern modern.cob
./modern
cobc -x -free -Wall -std=cobol85 -o modern85 modern.cob

name [ADA]
factorial 000005040
modern.cob:12: error: FUNCTION 'TRIM' unknown
   10 |
   11 | PROCEDURE DIVISION.
  12 > MOVE FUNCTION UPPER-CASE(FUNCTION TRIM(WS-NAME)) TO WS-UP
   13 | DISPLAY "name [" FUNCTION TRIM(WS-UP) "]"
   14 | COMPUTE WS-FACT = FUNCTION FACTORIAL(WS-N)
modern.cob:13: error: FUNCTION 'TRIM' unknown
   11 | PROCEDURE DIVISION.
  12 | MOVE FUNCTION UPPER-CASE(FUNCTION TRIM(WS-NAME)) TO WS-UP
  13 > DISPLAY "name [" FUNCTION TRIM(WS-UP) "]"
   14 | COMPUTE WS-FACT = FUNCTION FACTORIAL(WS-N)
   15 | DISPLAY "factorial " WS-FACT

Two errors, one for each TRIM, and neither line says "this is a COBOL 2002 function". The message is FUNCTION 'TRIM' unknown because to a COBOL 85 compiler there is no such function: the intrinsic function library is a 2002 addition, and a 1985 program that needs the trimming writes it by hand with INSPECT and STRING. The same source under -std=cobol2002 and -std=cobol2014 builds without a word, which is the measure of what those standards added.

JSON is an extension here

The JSON lesson generated a document from a record and it worked. The same program under an explicit standard does not:

IDENTIFICATION DIVISION. PROGRAM-ID. JSONDEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUST-REC. 05 CUST-NAME PIC X(8) VALUE "Ada". 05 CUST-AGE PIC 9(3) VALUE 36. 01 JSON-OUT PIC X(100). PROCEDURE DIVISION. JSON GENERATE JSON-OUT FROM CUST-REC DISPLAY FUNCTION TRIM(JSON-OUT) STOP RUN.

cobc -x -free -Wall -o jsondef json.cob
./jsondef
cobc -x -free -Wall -std=cobol2014 -o json14 json.cob

{"CUST-REC":{"CUST-NAME":"Ada","CUST-AGE":36}}
json.cob:12: error: syntax error, unexpected GENERATE, expecting SECTION or .
   10 |
   11 | PROCEDURE DIVISION.
  12 > JSON GENERATE JSON-OUT FROM CUST-REC
   13 | DISPLAY FUNCTION TRIM(JSON-OUT)
   14 | STOP RUN.

This is the fact that matters about dialects: the option is not a correctness switch, it is a statement about which language the source is written in. GnuCOBOL 3.2 implements JSON GENERATE as an extension of its default dialect, so a program that uses it is a GnuCOBOL program and not a COBOL 2014 program, whatever the standard says the feature belongs to. The same build under -std=cobol85 and -std=cobol2002 fails at the same line.

What 3.2 does not build

The course met four limits of this compiler and each one has a lesson with the real message. They are collected here because a reader deciding whether GnuCOBOL fits a job needs the list in one place:

JSON PARSE builds with -Wpending, parses nothing lesson 101 XML PARSE the INTO form is refused lesson 102 OO COBOL CLASS-ID, METHOD-ID and INVOKE are lessons 103 to 105 reserved words that are not supported USAGE OBJECT REF crashes the compiler with an internal lesson 105 compiler error RETURNING program RETURNING is not implemented, lesson 092 the program crashes

None of these is a reason to avoid the compiler for the work this course teaches: batch programs, indexed files, sorting, reports and subprograms are all solid. They are a reason to test a feature before a design depends on it, and to write down which compiler the program needs. That habit is older than COBOL: a program that uses an extension is a program with a dependency.

Writing for the standard

The practical rule is to pick the oldest standard that has what the program needs, and to build with -std= in the test run even when the production build uses the default. A library that compiles under -std=cobol85 runs on every COBOL compiler of the last forty years; a library that needs FUNCTION TRIM needs a compiler from this century, which in practice means any current one.

Free format is the other choice worth making on purpose. This course writes -free everywhere because the checker builds free format, but the standard form of 1985 is fixed format, and a program that has to live beside code from 1985 has to be readable in the same columns. The free and fixed lesson shows the same program both ways.

That is the end of the reading order. The last part of the course is three capstone projects that put the parts together, beginning with a fixed-width record processor and ending with a batch job and its test harness.

Exercises

  1. Rewrite the trimming and upper-casing of MODERN with INSPECT and STRING only, and build it with -std=cobol85. What does the compiler say now?
  2. Build the calculator of lesson 120 with -std=cobol85. Which statements does 1985 not have?
  3. Build one of your own programs with -std=ibm and read the warnings. Which of them are about the dialect and which about the code?
  4. Take the JSON program, remove JSON GENERATE and build it with -std=cobol2014. Does anything else on the page fail?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which standard added intrinsic functions such as FUNCTION TRIM?

    Answer

    COBOL 2002

    The 2002 build accepted the program that the COBOL 85 build refused.

  2. What does cobc -std=cobol85 on the MODERN program report?

    Answer

    FUNCTION TRIM unknown, once for each use

    Two errors, at the two lines that call TRIM.

  3. What happened to JSON GENERATE under -std=cobol2014?

    Answer

    It was a syntax error, because 3.2 implements it only in the default dialect

    The default build printed the JSON; the explicit standard refused the line.

  4. What is the default dialect of this course?

    Answer

    default: the standards plus the GnuCOBOL extensions

    Every build in the course uses no -std option, which is the default dialect.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 N PIC 9(3) VALUE 5. 01 F PIC 9(6). PROCEDURE DIVISION. COMPUTE F = FUNCTION FACTORIAL(N) DISPLAY F. STOP RUN.

    Answer

    000120

    Five factorial is a hundred and twenty.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W PIC X(9) VALUE " cobol ". PROCEDURE DIVISION. DISPLAY "[" FUNCTION UPPER-CASE(FUNCTION TRIM(W)) "]". STOP RUN.

    Answer

    [COBOL]

    TRIM removes the surrounding spaces and UPPER-CASE changes the letters.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 9(2) VALUE 3. 01 B PIC 9(2) VALUE 4. 01 C PIC 9(3). PROCEDURE DIVISION. COMPUTE C = A ** 2 + B ** 2 DISPLAY C. STOP RUN.

    Answer

    025

    Nine plus sixteen is twenty-five.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 T. 05 T-C PIC X OCCURS 5 TIMES. 01 I PIC 9(2). 01 N PIC 9(2) VALUE 0. PROCEDURE DIVISION. MOVE "COBOL" TO T. PERFORM VARYING I FROM 1 BY 1 UNTIL I IS GREATER THAN 5 IF T-C(I) = "O" ADD 1 TO N END-IF END-PERFORM. DISPLAY N. STOP RUN.

    Answer

    02

    The word COBOL holds two letter Os.

  9. Why build with -std= in a test run even when production uses the default?

    Answer

    It shows which extensions the code depends on

    The option is a statement about the language the source is written in.

  10. What is the honest status of OO COBOL in GnuCOBOL 3.2?

    Answer

    CLASS-ID, METHOD-ID and INVOKE are reserved words that are not supported

    Lessons 103 to 105 show the real messages and the compiler crash.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image