Introduction
COBOL, the Common Business-Oriented Language, was designed in 1959 for business data processing, with statements close to English. It is still used for banking, insurance and government systems, much of it on IBM mainframes. The language is an ISO standard, most recently revised in 2023.
Source files on Windows, Linux and Unix are usually named .cbl or
.cob. .cpy is a copybook, a shared piece of source, most often
a record layout, pulled into programs with the COPY statement. On a
mainframe, source is kept in members of data sets, in EBCDIC, and gains an extension
only when it is moved elsewhere.
These pages describe the language as it appears in a source file, in the standards from COBOL-60 to COBOL 2023 and in the main compilers: IBM Enterprise COBOL for z/OS, Micro Focus Visual COBOL and its ancestors, GnuCOBOL, ACUCOBOL-GT, RM/COBOL, Fujitsu NetCOBOL and HP NonStop COBOL85, and COBOL for .NET and the JVM. The viewer colours a COBOL file in either reference format, with embedded SQL and CICS.
- File structure – encodings, divisions, programs in a file, fixed and free format, extensions
- Lexical structure – words, separators, comments, continuation and every literal form
- Data division – sections, level numbers, data description clauses, tables
- Picture and usage – every picture symbol and every USAGE
- Statements – every verb, scope terminators and conditional phrases
- Expressions and conditions – arithmetic, conditions and MOVE rules
- File handling – SELECT, organisations, I/O statements, file status codes
- Intrinsic functions – the functions by group
- Compiler directives – COPY, REPLACE, >> directives, CBL cards, $SET
- Object-oriented COBOL – classes, methods, functions, managed COBOL
- Embedded SQL and CICS – EXEC blocks and their precompilers
- Report Writer and screens – report groups and the screen section
- Reserved words – the words the viewer colours
- Dialects and versions – the standards, the compilers and JCL
Structure
A program has up to four divisions, in order: IDENTIFICATION names it,
ENVIRONMENT ties it to files and hardware, DATA declares its
storage and PROCEDURE holds the code. Sentences end with a full stop.
Words are not case-sensitive and may be joined with hyphens.
Traditional fixed format gives each column of an 80-column card a job:
- columns 1 to 6: sequence number, ignored by the compiler
- column 7: indicator, with
*for a comment and-for a continuation - columns 8 to 11: Area A, for division, section and paragraph names and
01level numbers - columns 12 to 72: Area B, for statements
- columns 73 to 80: ignored
The 2002 standard added a free format with no column rules, which many compilers take
with a switch or the directive >>SOURCE FORMAT IS FREE.
Data items are declared with a level number and a PIC clause that gives
their shape: PIC X(10) is ten characters and PIC 9(5)V99 is a
number with five digits, an implied decimal point and two more digits. Higher level
numbers nest inside lower ones to build records.
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NAME PIC X(10) VALUE "WORLD".
PROCEDURE DIVISION.
DISPLAY "HELLO, " WS-NAME.
STOP RUN.
Not described
Known, not yet covered
- Reference: the text of the standards was not read. ANSI X3.23-1985, ISO/IEC 1989:2002, 2014 and 2023 and the CODASYL reports could not be downloaded (the sources I tried refused or had moved), so every statement, clause and function list is from the GnuCOBOL Programmer’s Guide, a summary of the versions, and general knowledge. Reserved-word lists per standard and the 2023 function list are the main gaps (Reserved words, Intrinsic functions).
- Reference: IBM Enterprise COBOL, Micro Focus, ACUCOBOL-GT, RM/COBOL, Fujitsu NetCOBOL and HP COBOL85 manuals were not read (IBM’s and Micro Focus’s pages gave only an empty page or an error). Their compiler options, directive lists, file formats, status codes of the 9x class and extra words are not described (Dialects and versions, Compiler directives).
- Reference: a full syntax diagram of every statement, with every phrase, in each dialect; the Report Writer and screen sections are described by their main clauses only.
- Viewer: the Micro Focus terminal format (indicator in column 1) and EBCDIC files are not read as COBOL; an EBCDIC file is shown as bytes.
- Viewer: the words of each dialect are one shared list: a word reserved in one compiler only is coloured everywhere, and a word that a program uses as a name may be coloured as a keyword. Names used before they are declared are not coloured as variables; only declarations are.
- Viewer: the SQL and CICS words are a short list, and other EXEC blocks (such as DLI, HTML) are shown plain between the markers; managed COBOL (.NET and JVM) statements beyond try, catch, declare and throw are not coloured.
- Viewer: inactive branches of
>>IFare not dimmed, andCOPYfiles are not opened.
Unknown
- Reference: what produces the
.cbx,.ccpand.scbfiles: no public documentation was found. The viewer reads them as COBOL source. - Reference and Viewer: the
H"..."hexadecimal numeric literal and the$-in-column-7 directive are coloured as Micro Focus forms from memory, unchecked against its documentation. - Viewer: nothing was run on a COBOL compiler; the colouring was tested on hand-written cases and the few COBOL files found on this Mac (one GnuCOBOL sample, one EBCDIC file).
Links
Software
- GnuCOBOL – free compiler that translates COBOL to C (Windows, macOS, Linux)
- IBM Enterprise COBOL – IBM’s compiler for its mainframes (z/OS)
- Visual Studio Code – free code editor with COBOL extensions (Windows, macOS, Linux)
Reference
- Enterprise COBOL for z/OS documentation – IBM’s language reference and programming guides
- GnuCOBOL Programmer’s Guide – free, detailed guide to the language and the GnuCOBOL compiler, with the reserved-word and function lists
- ISO/IEC 1989 – the international standard for COBOL