A COBOL Source Program [ Micro Focus COBOL Language Reference ] MPE/iX 5.0 Documentation
Micro Focus COBOL Language Reference
A COBOL Source Program
A COBOL source program is a syntactically correct set of COBOL
statements.
Organization
With the exception of COPY and REPLACE statements and the END PROGRAM
header (ANS85) , the statements, entries, paragraphs, and sections of a
COBOL source program are grouped into four divisions which are sequenced
in the following order:
1. The Identification Division
2. The Environment Division
3. The Data Division
4. The Procedure Division
The end of a COBOL source program is indicated by either the END PROGRAM
header, if specified, or by (ANS85) the absence of further source program
lines.
Structure
The following gives the general format and order of presentation of the
entries and statements which constitute a COBOL source program.
Format 1.
Format 2.
Syntax Rules.
All formats.
1. The generic terms Identification Division, Environment Division,
Data Division , Procedure Division and END PROGRAM header (ANS85)
represent a COBOL identification division, a COBOL environment
division, a COBOL data division, a COBOL procedure division and a
COBOL END PROGRAM header (ANS85) respectively.
For MF only: Format 2.
2. Procedure-division-header must conform to the rules defined for a
format 2 PROCEDURE DIVISION header defined later in this chapter.
3. Entry-statement must conform to the rules defined for a format 2
ENTRY statement defined later in this chapter.
4. Although for clarity, they are marked as required in the format 2
General Format, the rules for including or omitting the
IDENTIFICATION DIVISION, DATA DIVISION and LINKAGE SECTION
headers are the same for a format 2 PROGRAM STRUCTURE as they are
for format 1.
5. All COBOL clauses, phrases and statements other than those listed
in Format 2 will be syntax checked but treated as documentary.
6. A format 2 COBOL source program must not follow a format 1 source
program.
General Rules.
All Formats.
1. The beginning of a division in a program is indicated by the
appropriate division header. The end of a division is indicated
by one of the following:
a. The division header of a succeeding division in that
program.
b. That physical position after which no more source program
lines occur.
c. For ANS85 only: The END PROGRAM header.
For MF only: Format 2.
2. Format 2 of the PROGRAM STRUCTURE creates a call prototype that
will be used during syntax checking to determine the validity and
nature of CALL literal statements as described elsewhere in this
chapter.
For ANS85 only: Nested Source Programs
A format 1 COBOL source program can contain other format 1 COBOL source
programs, and these contained programs can reference some of the
resources of the programs in which they are contained.
When a program, program B, is contained in another program, program A, it
can be directly or indirectly contained. Program B is directly contained
in program A if program A doesn't contain a program that also contains
program B. Program B is indirectly contained in program A if program A
contains a program that also contains program B.
Syntax Rule.
End-program-header must be present if:
a. The COBOL source program contains one or more nested COBOL source
programs, or:
b. The COBOL source program is contained in another COBOL source
program.
General Rules.
1. The beginning of a division in a program is indicated by the
appropriate division header. The end of a division is indicated
by one of the following:
a. The division header of a succeeding division in that
program.
b. An Identification Division header which indicates the start
of another source program.
c. The END PROGRAM header.
d. That physical position after which no more source program
lines occur.
2. A COBOL source program which is directly or indirectly contained
in another program is considered in these specifications as a
separate program that can additionally reference certain resources
defined in the containing program.
3. The object code that results from passing a source program
contained in another program through your COBOL system is
considered to be inseparable from the object code produced from
the containing program.
For ANS85 only: END PROGRAM Header
Function.
The END PROGRAM header indicates the end of the named COBOL source
program.
General Format.
END PROGRAM program-name.
Syntax Rules.
1. The program-name must conform to the rules for forming a
user-defined word.
2. The program-name must be identical to the program-name declared in
the PROGRAM-ID paragraph. (See the section The PROGRAM-ID
Paragraph in this chapter.)
3. For MF and VSC2 only: The program-name can be a nonnumeric
literal. The content of the literal must follow the rules for
formation of program-names.
4. If a PROGRAM-ID paragraph declaring a specific program-name is
stated between the PROGRAM-ID paragraph and the END PROGRAM header
declaring and referencing, respectively, another program-name, the
END PROGRAM header referencing the former program-name must
precede that referencing the latter program-name.
General Rules.
1. The END PROGRAM header must be present in every program which
either contains or is contained in another program.
2. The END PROGRAM header indicates the end of the specified COBOL
source program.
3. If the program terminated by the END PROGRAM header is contained
in another program, the next statement must be either an
IDENTIFICATION DIVISION header or another END PROGRAM header which
terminates the containing program.
4. If the program terminated by the END PROGRAM header is not
contained in another program and if the next source statement is a
COBOL statement, it must be the IDENTIFICATION DIVISION header of
a program to be compiled separately from that program terminated
by the END PROGRAM header.
For ANS85 only: Initial State of a Program
A program is in the initial state:
1. the first time the program is called in a run unit.
2. the first time the program is called after the execution of a
CANCEL statement referencing the program or a CANCEL statement
referencing a program that directly or indirectly contains the
program.
3. every time the program is called, if it possesses the initial
attribute.
4. the first time the program is called after the execution of a CALL
statement referencing a program that possesses the initial
attribute, and that directly or indirectly contains the program.
For ANS85 only: Characteristics of a Program in its Initial State.
1. The program's internal data contained in the Working-Storage
Section and the Communication Section is initialized. If a VALUE
clause is used in the description of the data item, the data item
is initialized to the defined value. If a VALUE clause is not
associated with a data item, the initial value of the data item is
undefined.
For MF only: All such data items are filled with the value
found in the DEFAULTBYTE directive.
2. Files with internal file connectors associated with the program
are not in the open mode.
3. The control mechanisms for all PERFORM statements contained in the
program are set to their initial states.
4. A GO TO statement referred to by an ALTER statement contained in
the same program is set to its initial state.
For MF only: CALL prototypes
Function.
A CALL prototype is a skeleton program which serves to define the
characteristics of a called subprogram. These characteristics include
the number of parameters required, the type of these parameters and the
calling convention. If a program contains a CALL statement referring to
a subprogram for which a prototype exists, the CALL statement is checked
against the prototype. If there is an explicit mismatch, an error is
given. If the CALL statement leaves some characteristics (such as the
call convention) undefined, this is derived from the prototype.
The prototype is defined as a complete program in which the EXTERNAL
clause is specified in the PROGRAM-ID paragraph. The program structure
should consist only of an IDENTIFICATION DIVISION with a PROGRAM-ID
paragraph, a DATA DIVISION with a LINKAGE SECTION, a PROCEDURE DIVISION
header, and optional ENTRY statements. These EXTERNAL programs are
placed before the real source programs in a similar way to multi-program
source files.
Note that you must include a copy of the CALL prototype for every
subprogram to which you want your CALL statements checked before the
IDENTIFICATION DIVISION header of your program containing the CALL
statements. Similarly, you must copy in or include the call prototype
for a subprogram in every call program for which you want the CALL
statements validated.
For ANS85 only: File Connector
A file connector is a storage area which contains information about a
file and is used as the linkage between a file-name and a physical file
and between a file-name and its associated record area.
For ANS85 only: Global Names and Local Names
A data-name names a data item. A file-name names a file connector.
These names are classified as either global or local.
A global name can be used to refer to the object with which it is
associated either from within the program in which the global name is
declared, or from within any other program which is contained in the
program which declares the global name.
A local name, however, can be used only to refer to the object with which
it is associated from within the program in which the local name is
declared. Some names are always global; other names are always local;
some other names are either local or global depending upon specifications
in the program in which the names are declared.
A record-name is global if the GLOBAL clause is specified in the record
description entry by which the record-name is declared or, in the case of
record description entries in the File Section, if the GLOBAL clause is
specified in the file description entry for the file-name associated with
the record description entry.
A data-name is global if the GLOBAL clause is specified either in the
data description entry by which the data-name is declared or in another
entry to which that data description entry is subordinate.
A condition-name declared in a data description entry is global if that
entry is subordinate to another entry in which the GLOBAL clause is
specified. However, specific rules sometimes prohibit specification of
the GLOBAL clause for certain data description, file description, or
record description entries.
A file-name is global if the GLOBAL clause is specified in the file
description entry for that file-name.
If a data-name, a file-name, or a condition-name declared in a data
description entry is not global, the name is local.
Global names are transitive across programs contained within other
programs.
For ANS85 only: External Objects and Internal Objects
Accessible data items usually require that certain representations of
data be stored. File connectors usually require that certain information
concerning files be stored. The storage associated with a data item or a
file connector can be external or internal to the program in which the
object is declared.
A data item or file connector is external if the storage associated with
that object is associated with the run unit rather than with any
particular program within the run unit. An external object can be
referenced by any program in the run unit which describes the object.
References to an external object from different programs using separate
descriptions of the objects are always to the same object. In a run
unit, there is only one representation of an external object.
An object is internal if the storage associated with that object is
associated only with the program which describes the object.
External and internal objects can have either global or local names.
A data record described in the Working-Storage Section is given the
external attribute by the presence of the EXTERNAL clause in its data
description entry. Any data item described by a data description entry
subordinate to an entry describing an external record also attains the
external attribute. If a record or data item does not have the external
attribute, it is part of the internal data of the program in which it is
described.
A file connector is given the external attribute by the presence of the
EXTERNAL clause in the associated file description entry. If the file
connector does not have the external attribute, it is internal to the
program in which the associated file-name is described.
The data record described subordinate to a file description entry which
does not contain the EXTERNAL clause or a sort-merge file description
entry, and any data items described subordinate to the data description
entries for such records, are always internal to the program
describingthe file-name. If the EXTERNAL clause is included in the file
description entry, the data records and the data items attain the
external attribute.
Data records, subordinate data items, and various associated control
information described in the Linkage, Communication, and Report Sections
of a program are always considered to be internal to the program
describing that data. Special considerations apply to data described in
the Linkage Section whereby an association is made between the data
records described and other data items accessible to other programs.
Record Sequential Input/Output
Record sequential I/O allows the programmer to access recordsof a file in
an established sequence. The sequence is established as a result of
writing the records to the file.
Organization of Line and (MF) Record Sequential Files.
Sequential filesare organizedsuch that each record in the file except the
first has a unique predecessor record, and each record except the last
has a unique successor record. These predecessor-successor relationships
are established by the order of WRITE statements when the file is
created. Once established, the predecessor-successor relationships do
not change except in the case where records are added to the end of the
file
For MF only: Line Sequential Input/Output.
Line sequential I/O allows the programmer to access records of a text
file in an established sequence. Line sequential files are identical in
format to those files produced by your operating system editor. The
records are stored with trailing spaces removed.
If the words sequential file or sequential organization are used in this
chapter without specifying LINE or RECORD, then the sentence applies to
both forms. The default behavior for sequential files is sensitive to
the SEQUENTIAL directive. See your COBOL System Reference for details.
Access Mode.
The only access available for sequential filesis sequentialaccess mode;
the sequence in which records are accessed is the order in which the
records were originally written.
Relative Input-Output
Relative I/Oallows the programmer to access records within a mass storage
file in either a random or sequential manner. Each record in a relative
file is identified by an integer value greater than zero which specifies
the record's ordinal position in the file.
Organization of Relative Files.
Relative fileorganization is permitted only on disk devices. A relative
file consists of records which are identified by relative record numbers.
The file can be thought of as being composed of a serial string of areas,
each capable of holding a logical record. Each of these areas has a
relative record number. Records are stored and retrieved via this
number. For example, ten denotes the tenth record area.
Access Mode.
In sequential access mode, records are accessed in the ascending order of
the relative record numbers of those records which currently exist within
the file.
In random access mode, the programmer controls the sequence in which
records are accessed. The desired record is accessed by placing its
relativerecord number in a relative key data item.
In dynamic access mode, the programmer can change at will between
sequential access and randomaccess, using the appropriate forms of
input-output statements.
Indexed Input-Output
Indexedinput-outputallows the programmer to accessrecords within a mass
storage file in either a random or sequential manner. Each record in an
indexed file is identified by the value of one or more keys within that
record.
Organization of Indexed Files.
An indexed fileis a mass storage
file in which data records can be accessed by the value of a key. A
record description can include one or more key
data items, each of which is associated with an index. Each index
provides a logical path to the data records, according to the contents of
a data item within each record which is the record key for that index.
The data item named in the RECORD KEY clause
of the file control entry for a file is the prime record keyfor that
file. For purposes of inserting, updatingand deleting recordsin a file,
each record is identified solely by the value of its prime record key.
This value should, therefore, be unique and must not be changed when
updating the record. See your COBOL System Reference for further
details.
The data item named in the ALTERNATE RECORD KEY clause of the file
control entry for a file is an alternative record key for that file.
The value of an alternative record key can be non-unique if the
DUPLICATESphrase is specified. These keys provide alternativeaccess
paths for retrieval of records from the file.
For MF only: Micro Focus provides an extension which allows the key
field to be a split key. A split keyis a key comprising two or more data
items, which may or may not be contiguous, in the record description.
Access Mode.
In sequential access mode, records are accessed in the ascending order of
the record key values. Records within a set of records which have
duplicate record keyvalues are retrieved in the order in which the
records were written into the set.
In random access mode, the programmer controls the sequence in which
records are accessed. The desired record is accessed by placing the
value of its record key in the record key data item.
In dynamicaccess mode, the programmer can change at will between
sequential access and random access, using appropriate forms of
input-output statements.
MPE/iX 5.0 Documentation