COBOL

Table of Contents

1 COBOL简介

COBOL (an acronym for COmmon Business-Oriented Language) is a compiled English-like computer programming language designed for business use.

COBOL was designed in 1959 by the Conference on Data Systems Languages (CODASYL), COBOL is still widely used in legacy applications deployed on mainframe computers.

Three ANSI standards for COBOL have been produced: in 1968, 1974 and 1985.

By 2001, around 300 dialects had been created. Micro Focus COBOL Compiler can compile many different COBOL dialect variations.

Reference:
http://en.wikipedia.org/wiki/COBOL
http://www.csis.ul.ie/cobol/course/COBOLIntro.htm
http://www.tutorialspoint.com/cobol/index.htm
http://www-01.ibm.com/support/knowledgecenter/SS6SG3_5.2.0/com.ibm.cobol52.ent.doc/PGandLR/rlpre.html?lang=en
http://supportline.microfocus.com/Documentation/books/sx60/lrpubb.htm

2 COBOL基本语法

Traditionally, COBOL programs were written on coding forms and then punched on to punch cards.

cobol_coding_form.jpg

Figure 1: Ancient COBOL coding form

COBOL源码每行可分为几个区域,不同区域的作用不同。

cobol_source_line_format.gif

Figure 2: COBOL source line format

Table 1: COBOL源码格式
所在列号 区域名 描述
01 - 06 顺序号区 一般用来标记行号,会被编译器忽略,这几列也可用来写简短注释
07 标示域 星号"*"表示整行是注释;连接符"-"表示本行是上一行的续行;字母"D"表示查错
08 - 11 A区 有些内容必须从A区开始,如Division headers, Section headers, Paragraph headers or paragraph names, Level indicators (FD and SD) or level-numbers (01 and 77)
12 - 72 B区 有些内容必须从B区开始,如Entries, sentences, statements, clauses, Continuation lines (上一行MOVE只写了MO,可在下一行标示域中写连接符"-",再从12列开始写VE两字符)

参考:
http://www-01.ibm.com/support/knowledgecenter/SS6SG3_5.2.0/com.ibm.cobol52.ent.doc/PGandLR/ref/rlfmt.html?lang=en

2.1 Structure of COBOL Program

COBOL程序接近于日常英语,类似于英文散文的结构,将程序分为Division, Section, Paragraph, Sentence, Statement等。

cobol_source_structure.png

Figure 3: COBOL程序结构

2.1.1 Divisions

A division is a block of code, usually containing one or more sections, that starts where the division name is encountered and ends with the beginning of the next division or with the end of the program text.

2.1.2 Sections

A section is a block of code usually containing one or more paragraphs. A section begins with the section name and ends where the next section name is encountered or where the program text ends.

Section names are devised by the programmer, or defined by the language. A section name is followed by the word SECTION and a period.

2.1.3 Paragraphs

A paragraph is a block of code made up of one or more sentences. A paragraph begins with the paragraph name and ends with the next paragraph or section name or the end of the program text.

A paragraph name is devised by the programmer or defined by the language, and is followed by a period.
See the two example names below

PrintFinalTotals.
PROGRAM-ID.

2.1.4 Sentences and statements

A sentence consists of one or more statements and is terminated by a period.
For example:

MOVE .21 TO VatRate
MOVE 1235.76 TO ProductCost
COMPUTE VatAmount = ProductCost * VatRate.

2.2 4 Divisions

整个COBOL程序从上往下,由4个部分组成——标识部、环境部、数据部、过程部。

2.2.1 Identification Division

The IDENTIFICATION DIVISION supplies information about the program to the programmer and the compiler.

IDENTIFICATION DIVISION.
PROGRAM-ID. 程序名.
[AUTHOR.   作者名.]
...

2.2.2 Environment Division

The ENVIRONMENT DIVISION is used to describe the environment in which the program will run. 环境部是COBOL程序中唯一与硬件设备有关的部分。

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. 源计算机名.
OBJECT-COMPUTER. 目标计算机名.
...
INPUT-OUTPUT SECTION.
FILE-CONTROL.
...

2.2.3 Data Division

DATA DIVISION provides descriptions of the data-items processed by the program.

DATA DIVISION.
FILE SECTION.
...
WORKING-STORAGE SECTION.
...
LINKAGE SECTION.
...

2.2.4 Procedure Division

The PROCEDURE DIVISION contains the code used to manipulate the data described in the DATA DIVISION. It is here that the programmer describes his algorithm.

2.2.5 Simple Program

下面实例程序来自:http://www.csis.ul.ie/cobol/course/COBOLIntro.htm

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SequenceProgram.
       AUTHOR. Michael Coughlan.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 Num1 PIC 9 VALUE ZEROS.
       01 Num2 PIC 9 VALUE ZEROS.
       01 Result PIC 99 VALUE ZEROS.

       PROCEDURE DIVISION.
       CalculateResult.
          ACCEPT Num1.
          ACCEPT Num2.
          MULTIPLY Num1 BY Num2 GIVING Result.
          DISPLAY "Result is = ", Result.
          STOP RUN.

2.3 Hello World

下面演示COBOL版本的hello world程序:

       IDENTIFICATION DIVISION.
       PROGRAM-ID.  TestCobol.

       PROCEDURE DIVISION.
       SayHello.
          DISPLAY "Hello world".
          STOP RUN.

3 Tips

3.1 COBOL过程调用默认为“By Reference”

Parameters can be passed by reference, by content (where a copy is passed by reference) or by value (but only if a prototype is available).

COBOL中过程调用默认为“By Reference”。子过程对参数的修改在过程返回后在主程序中仍然有效。

参考:
http://www.tutorialspoint.com/cobol/cobol_subroutines.htm
http://www-01.ibm.com/support/knowledgecenter/SS6SG3_5.2.0/com.ibm.cobol52.ent.doc/PGandLR/ref/rlpscall.html


Author: cig01

Created: <2013-03-10 Sun 00:00>

Last updated: <2015-11-08 Sun 21:12>

Creator: Emacs 25.1.1 (Org mode 9.0.7)