Stats

586 Contributors: 7 Monday, July 31, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Getting started with cobol

Remarks

COBOL is the COmmon Business Oriented programming Language.

Even though it has become a pronounceable name, COBOL is still treated as an acronym by the standards committee, and COBOL is the preferred spelling by the ISO and INCITS standards bodies.

Standard Specification

The current specification is

ISO/IEC 1989:2014 Information technology – Programming languages, their environments and system software interfaces – Programming language COBOL

That document was published in May of 2014 and can be purchased from various branches of standard bodies, officially homed at

http://www.iso.org/iso/home/store/catalogue_tc/catalogue_detail.htm?csnumber=51416

Principal field of use

Business oriented. That usually means transaction processing. Banking, government agencies, and the insurance industry are major areas of COBOL application deployments. IBM mainframe systems usually have a COBOL compiler installed. There are upwards of 300 COBOL dialects in existence, with perhaps 10 or so versions taking the lion's share of deployments. Most of these compilers are proprietary systems, but free software COBOL is also available.

Category

COBOL is a procedural, imperative, compiled programming language. As of the COBOL 2002 spec, Object Oriented features were added to the standard.

By design intent, COBOL is a very verbose programming language. Although algebraic form is allowed:

COMPUTE I = R * B

the initial intent was to use full words for computational descriptions and data manipulation:

MULTIPLY INTEREST-RATE BY BALANCE GIVING CURRENT-INTEREST ROUNDED MODE IS NEAREST-EVEN

This design decision has both champions and detractors. Some feel it is too verbose, while others argue that the syntax allows for greater readability in a business environment.

Decimal Math

COBOL is designed around decimal arithmetic, unlike most languages that use a binary internal representation. The COBOL spec calls for very precise fixed point decimal calculations, an aspect of the language that has been well regarded in financial sectors. COBOL also allows for USAGE BINARY, but leans towards decimal (base-10) representations.

History

COBOL dates back to the late 1950s, with initial implementations published in 1960.

U.S. Navy Rear Admiral Grace Hopper is often associated with COBOL, and championed on behalf of the language during the early stages of development. She was not the only person involved in the design and development of COBOL, by any means, but is often referred to as the Mother of COBOL.

Due to early backing by governments and large corporations, COBOL has been in wide use for many decades. It remains a point of pride for some, and a thorn for others, who see it as outdated. The truth likely lies somewhere in between these extreme views. When applied to transaction processing, COBOL is at home. When applied to modern web screens and networking applications it may not feel as comfortable.

Structure

COBOL programs are written in four separate divisions.

  • IDENTIFICATION DIVISION
  • ENVIRONMENT DIVISION
  • DATA DIVISION
  • PROCEDURE DIVISION

Data Descriptions

Being designed to handle decimal data, COBOL allows for PICTURE based data descriptions, in grouped hierarchies.

01 record-group.
   05 balance        pic s9(8)v99.
   05 rate           pic 999v999.
   05 show-balance   pic $Z(7)9.99.

That defines balance as a signed eight digit value with two digits assumed after the decimal point. rate is three digits before and three digits after an assumed decimal point. show-balance is a numeric-edit field that will have a leading dollar sign, seven digits (zero suppressed) with at least one digit shown preceding two digits after a decimal point.

balance can be used in calculations, show-balance is only for display purposes and cannot be used in computational instructions.

Procedural statements

COBOL is a reserved keyword heavy language. MOVE, COMPUTE, MULTIPLY, PERFORM style long form words make up most of the standard specification. Over 300 keywords and 47 operational statements in the COBOL 2014 spec. Many compiler implementations add even more to the reserved word list.

Hello, world

HELLO * HISTORIC EXAMPLE OF HELLO WORLD IN COBOL
       IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLO.
       PROCEDURE DIVISION.
           DISPLAY "HELLO, WORLD".
           STOP RUN.

The days of punch card layout and uppercase only inputs are far behind. Yet most COBOL implementations still handle the same code layout. Even current implementations follow the same (often even in uppercase,) compiled and in production.

A well-formatted modern implementation might look like:

*> Hello, world
identification division.
program-id. hello.

procedure division.
display "Hello, world"
goback.
end program hello.

With some implementations of COBOL, this can be shortened to:

display "Hello, world".

This format usually requires compile time switches to put a COBOL compiler into a relaxed syntax mode, as some of the normally mandatory DIVISION statements are missing.

COBOL assumes FIXED format sources by default, even in the current specification.

Pre-2002 COBOL

ColumnArea
1-6Sequence Number Area
7Indicator Area
8-12Area A
12-72Area B
73-80Program Name Area

IBM mainframe text editors are still configured for this form in some cases.

Post 2002 and into COBOL 2014, Area A and B were merged and extended to column 255, and the Program Name Area was dropped.

ColumnArea
1-6Sequence Number Area
7Indicator Area
8-Program text Area

Column 8 thru an implementation defined column Margin R, is usually still limited to column 72, but allowed by spec to run up to column 255.

COBOL 2002 introduced FORMAT FREE source text. There is no Sequence Number Area, no Indicator Area, and source lines can be any length (up to an implementation defined Margin R limit, usually less than 2048 characters per line, commonly 255).

But the compiler starts out in FORMAT FIXED mode by default. There is usually a compilation switch or Compiler Directive Facility statement before free format source is recognized.

bbbbbb >>SOURCE FORMAT IS FREE

Where bbbbbb represents 6 blanks, or any other characters. (These are ignored as part of the initial default fixed format mode Sequence Number Area.)

Install gnu-cobol on Mac OS X

gnu-cobol is available via the homebrew system.

Open a terminal window from /Applications/Utilities/Terminal or use the keypress Command+Space and type "Terminal".

If you do not have the homebrew system installed, add it by typing, or copying and pasting into your terminal:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Once the command has finished, type:

brew install gnu-cobol

That is it, you can now compile Cobol programs on your Mac.