7.2 COBOL


As pointed out in Chapter 1, the majority of legacy systems are implemented in COBOL or FORTRAN. Because many programmers today don't know anything about COBOL, we provide a short overview of the language according to the COBOL 85 standard, highlighting some of the challenges of analyzing COBOL code.

History

The history of the COBOL programming language begins in 1960, when the Conference on Data Systems Languages (CODASYL) produced a report that described the common business-oriented language (COBOL 60). After some intermediary revisions ”COBOL 61, COBOL 61 Extended, and COBOL 65 ”the American National Standards Institute (ANSI) gave formal definitions in 1968 and 1974. The latest major revision ”ANSI COBOL 85 ”attempted to end "spaghetti coding" by introducing new language constructs that support a structured style of programming while remaining backward compatible. However, because these constructs were introduced to COBOL only in 1985, older programs are usually not well structured. In addition, other COBOL language constructs make it easy to produce spaghetti code. Thus, structured COBOL programming requires discipline and rigid guidelines.

Several compiler vendors have recently updated the language to include constructs for object orientation (OO COBOL). The International COBOL Committee (ISO WG4) is developing the next major revision of the language that includes support for internationalization, object-oriented programming, exception handling, better arithmetic, and other modern language capabilities. [1]

[1] For more details on the final text to ISO see http://www.sei.cmu.edu/cbs/mls/links.html#dkuug.

General Structure

A COBOL program consists of one or more files containing source code. A source code file that can be compiled is called a program element. Additional types of source files, such as source code fragments , can be copied into a program element during compilation ”similar to include files in C or C++. A collection of these elements makes up the overall program. A program element can be invoked directly from the runtime environment or can be called from another program element.

COBOL program elements were designed to resemble natural-language documents to be understandable by nonprogrammers. This results in programs that are often wordier than those written in other programming languages. Compare, for example, the assignment x := 1 in Pascal to MOVE 1 TO X or, even worse , COMPUTE X = 1 END-COMPUTE in COBOL.

In general, a COBOL program consists of DIVISION s that are composed of SECTION s. A section can be divided into paragraphs consisting of sentences. These sentences are the statements of COBOL and are terminated by a period.

In the course of introducing some structure into COBOL, COBOL 85 allowed program sentences to consist of multiple statements to make nested statements possible.

In the following sample program, variables a and b are assigned values, the values are swapped, and the results are displayed on the screen:

 IDENTIFICATION DIVISION.    PROGRAM-ID. Example DATA DIVISION.   WORKING STORAGE SECTION.     01 A PIC 9.     01 B PIC 9.     01 Temp PIC 9. PROCEDURE DIVISION.   MAIN SECTION.     MOVE 1 TO a     MOVE 2 TO b     PERFORM swap     DISPLAY a     DISPLAY b.   STOP RUN.   swap SECTION.     MOVE a TO Temp     MOVE b TO a     MOVE Temp TO b. END PROGRAM. 

In the following alternative formulation of the procedure division, the PERFORM statement is used to execute a sequence of paragraphs:

 PROCEDURE DIVISION.    MAIN SECTION.     MOVE 1 TO a     MOVE 2 TO b     PERFORM moveA THRU moveTemp     DISPLAY a     DISPLAY b.   STOP RUN.   moveA.     MOVE a TO Temp.   moveB.     MOVE b TO a.   moveTemp.     MOVE Temp TO b. END PROGRAM. 

The statements in the procedure division can be grouped into paragraphs. A paragraph starts with a paragraph name and ends before the next paragraph name or with the end of the section or procedure division. Paragraphs look like labels scattered throughout the source code. Not surprisingly, these labels can be used as destinations for GOTO statements.

Paragraphs themselves may be grouped into sections, which begin with a section name and the keyword SECTION . The end of a section is either the beginning of the next section or the end of the procedure division.

A procedure division can be composed of paragraphs without sections or of sections that may contain paragraphs. If the procedure division contains sections, every paragraph of this division must be contained in a section.

Sections, paragraphs, and sequences of paragraphs are executed by PERFORM statements. Control is transferred to the first statement in the called section or paragraph. Then all statements in the section or paragraphs are executed, and control returns to the next statement after the PERFORM statement. It is not possible to pass parameters with this mechanism.

Arithmetic

One of COBOL's built-in features is signed, fixed-point arithmetic with overflow/underflow handling. Other programming languages must emulate this, so COBOL has an advantage here. COBOL's original arithmetic capabilities were extremely limited. Since then, many COBOL vendors have added floating-point extensions to handle the numeric applications found in computational finance, insurance, and other fields.

Variables

In general, a COBOL program works on data records. Variables are called data items in COBOL. Simple data items are either alphanumeric with a fixed length or numeric in a fixed-point format. Combining simple data items forms compound data items. Combining simple and compound items forms even more complex, compound data items. This results in a tree structure with simple items as the leaves , similar to nested structures in C. Substructures of complex data items are referenced by qualified names . Parts of a compound data item may be redefined, resulting in a construct similar to unions in C.

Simple items are stored by default as a sequence of characters , even for numeric items. No type checking is required of the compiler. Therefore, it is possible to extract substrings from numbers , which is perfectly legal with some compilers, or to perform calculations with non-numeric items, which usually crashes the program. In addition, numeric values can be stored in binary format.

In COBOL, a variable declaration consists of a line in the DATA DIVISION that contains a level number; a data-name, or identifier; and a picture clause. The following example illustrates some variable declarations:

 01 Person    02 SSN PIC 9(9)    02 Name        03 FirstName PIC X(15)        03 MiddleInitial PIC X(1)        03 LastName PIC X(30)    02 DateOfBirth        03 Day PIC 99        03 Month PIC 99        03 Year PIC 9(4)    02 dummy REDEFINES DateOfBirth.        03 NumDate PIC 9(8) 

The variable Person , at level 01 , is composed of three variables at level 02 : SSN , Name , and DateOfBirth . The REDEFINES clause applies a new name to a segment of memory already set aside. In this case, the data in DateOfBirth can also be accessed by using the name NumDate . SSN is a numeric variable of length 9, as specified by PIC 9(9) . FirstName is an alphanumeric variable of length 15 as specified by PIC X(15) and is a part of the variable Name . Some assignment examples based on these variable declarations are shown in Table 7-1.

Table 7-1. Examples of Assignments in COBOL

Operation

Comment

MOVE 22 TO Day OF DateOfBirth OF Person

Complete qualification.

MOVE 5 TO Month OF Person

Incomplete qualification is permitted if unique.

MOVE "22051965" TO DateOfBirth OF Person

Now Day is 22 , Month is 5, Year is 1965 , and NumDate is 22051965 , owing to the REDEFINES clause.

MOVE "year" TO Year OF Person ADD 1 TO Year OF Person

The add operation crashes with "Illegal decimal operand" or a similar message.

Data items defined in a program element are, by default, visible only in this program element. Data items can also be specified as global to make them visible to other program elements. It is not possible to define variables that are local to a paragraph or a section.

Calling and Parameter Passing

Program elements invoke other program elements by using the COBOL CALL statement. The CALL statement transfers control to another program in the executable image. The destination program name is specified as a string literal or a data item containing the string.

The CALL statement can pass data items as parameters to the called program element with the USING clause. Parameters are passed by reference ”the default ”or by value. The called program element specifies the names of its formal parameters in the PROCEDURE DIVISION header with a USING clause as well. The structure of the parameter items is specified in the LINKAGE SECTION of the DATA DIVISION that precedes the PROCEDURE DIVISION .

In a program element PE0815 , for example, we can call PE4711 as follows :

 CALL "PE4711" USING Data-1, Data-2 

The subroutine in PE4711 is declared as follows:

 DATA DIVISION.      LINKAGE SECTION.       01 Para-1.         02 Parm-1         02 Parm-2       01 Para-2.         02 Parm-3         02 Parm-4 PROCEDURE DIVISION USING Para-1, Para-2. 

One weakness is that no type checking is performed on the parameters by the compiler. Analyzing call hierarchies in COBOL programs is especially difficult if a variable rather than a string literal is used for the name of the called program. A called program element can also return a data item as the result of the call.

COBOL 85 introduced nested program elements that contain multiple procedure divisions with local data divisions. This makes COBOL a block-structured language; unfortunately , however, this feature is not in widespread use.

Composing Source Files

COBOL includes a mechanism to compose program elements from various source files at compilation time. The programmer can include arbitrary fragments of COBOL source into a program element by using the COPY statement. In addition to including another file, the COPY statement can also apply textual modifications to the included source ( COPY REPLACING ). For example, you can put a lengthy variable declaration into a separate file and include this into every program element that uses this data item. COPY REPLACING is a useful aspect of COBOL for producing multilingual applications.

Obsolete Language Features

Some COBOL features are no longer found in programming languages but rather in libraries or separate tools. These features include file access for sequential, random, and indexed files; report generation; and screen descriptions of forms for input/output (I/O) on a text terminal. In a system modernization, these functions are usually completely redesigned.

Standards

COBOL X3.23-1968

This is the American National Standards Committee on Computers and Information Processing (X3) standard, based on the original COBOL specification. The standard defines COBOL as a nucleus and eight functional modules: Table Handling, Sequential I/O, Random I/O, Random Processing, Sort, Report Writer, Segmentation, and Library.

COBOL X3.23-1974

This standard revises and expands the functional modules from eight to eleven. Random I/O and Random Processing are replaced by the new modules Relative I/O, Indexed I/O, Debug, Inter-program Communication, and Communication.

ANSI COBOL X3.23-1985

This standard introduces a revised core language to support structured programming by changing COBOL into a block-structured language.

Products

Because COBOL has a long history, many different COBOL compilers and runtime environments are available for various platforms. Our case study uses compiler environments on Solaris and Unisys platforms.

ASCII COBOL

The COBOL source language implemented by the OS 2200 ASCII COBOL compiler system includes all the features of American National Standard COBOL X3.23-1974. In addition, ASCII COBOL includes numerous compatibility features for American National Standard COBOL X3.23-1968, as well as a random processing ”multitasking ”capability based on the work of the CODASYL COBOL committee [Unisys 98].

Universal Compiling System (UCS) COBOL

The COBOL source language implemented by UCS COBOL, commonly known as UCOB, includes all the required features of ANSI COBOL X3.23-1985, as well as numerous compatibility features for migrating from ASCII COBOL to UCS COBOL [Unisys 99].

Object-Oriented COBOL

The object-oriented COBOL environment is a member of the Unisys OS 2200 UCS family. The object-oriented COBOL compiler includes the features specified by the ANSI COBOL X3.23-1985, as well as object orientation and other features included in the emerging COBOL standard.

Micro Focus COBOL

Micro Focus COBOL is part of the Server Express product for UNIX and MS Windows environments. Server Express includes the Micro Focus COBOL compiler, Animator for debugging, and File Handling. [2]

[2] A further description of the product can be found at: http://www.sei.cmu.edu/cbs/mls/links.html#microfocus.



Modernizing Legacy Systems
Modernizing Legacy Systems: Software Technologies, Engineering Processes, and Business Practices
ISBN: 0321118847
EAN: 2147483647
Year: 2003
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net