Program Structure in COBOL COBOL

Source Format

Source programs are accepted as a sequence of lines(or records)of 80 characters or less. Each line is divided into five areas, as illustrated below.

Source Format

Source Format

The sequence number and identification areas are used for clerical and documentation purposes. They are ignored by the compiler.

The indicator area is used for denoting line continuation, comments and debugging.

Areas A and B contain the actual program according to the following rules:

  1. Division headers, section headers, paragraph headers, section-names and paragraph-names must begin in area A.
  2. The Data Division level indicators FD, SD, and CD, and level-numbers 01 and 77 must begin in area A. Other level-numbers may begin in area A or area B,although B is most often used.
  3. The keywords, DECLARATIVES and END DECLARATIVES, precede and follow, respectively, the declaratives portion of the Procedure Division. Each must appear on a line by itself and each must begin in area A, followed by a period and a space.
  4. Any other language element must begin in area B unless it immediately follows, on the same line, an element in area A

Continuation of Lines

Any sentence, entry, phrase, or clause may be continued by starting subsequent lines in area B. These subsequent lines are called continuation lines.

The line being continued is called the continued line. Any word, literal, or PICTURE character-string may be broken in such a way that part of it appears on a continuation line, according to the following rules:

  1. A hyphen in the indicator area of a line indicates that the first nonblank character in area B of the current line is the successor of the last nonblank character of the preceding line, excluding intervening comment lines or blank lines,without an intervening space.

    However, if the continued line contains a nonnumeric literal without a closing quotation mark, the first nonblank character in area B on the continuation line must be a quotation mark,and the continuation line starts with the character immediately after that quotation mark. All spaces at the end of the continued line are considered part of the literal. Area A of a continuation line must be blank. The quotation mark used to continue a nonnumeric literal must be the same quotation mark(that is,it must be a quotation mark or an apostrophe)that began the nonnumeric literal.

    Continuing a nonnumeric literal according to the previous paragraph is a deprecated feature maintained only for compatibility with older programs. Concatenation expressions are the recommended method of continuing nonnumeric literals in all new RM/COBOL programs.

  2. If there is no hyphen in the indicator area of a line, it is assumed that the last character in the preceding line is followed by a space.

Blank Lines

A blank line is one that is blank in the indicator, A and B areas. A blank line can appear anywhere in the source program.

Comment Lines

A comment line is any line with an asterisk or a slash in the indicator area of the line. A comment line may appear as any line after the Identification Division header of a source program and as any line in library text referred to by a COPY statement. Any combination of characters from the character set of the computer may be included in area A and area B of a comment line. Comment lines are reproduced on the listing but serve as documentation only.

When a comment line is indicated with an asterisk, the comment is printed on the next available line in the listing. When a comment line is indicated with a slash, page ejection occurs before the comment line is printed.

The character-strings and separators comprising pseudo-text may start in either area A or area B. If there is a hyphen in the indicator area of a line that follows the opening pseudo-text delimiter, area A of the line must be blank and the normal rules for continuation of lines apply to the formation of text words.

In-Line Comments

An in-line comment begins with the two contiguous characters *>preceded by a separator space, and ends with the last character position of the line. An in-line comment may be placed anywhere a separator space may be placed in a COBOL source program or in library text for a COBOL source program. For the purpose of evaluating library text, pseudo-text, and source text,an in-line comment has the value of a single space character. An in-line comment that is not preceded by any COBOL words or characterstrings on the same line is equivalent to a comment line, except that it may not be placed between a continued line and a continuation line if a word, literal, or PICTURE character-string is broken across the continuation.

Note: An in-line comment is not recognized as such, if it occurs in the sequence area (columns 1 to 6) or the identification area (column 73 through 80) of a source line.An in-line comment that begins in the indicator area is indistinguishable from a comment line.

Debugging Lines

A debugging line is any line with a D in the indicator area of the line. Any debugging line that consists solely of spaces from margin A to margin R is considered the same as a blank line.

The content of a debugging line must be such that a syntactically correct program is formed with or without the debugging lines being considered as comment lines.

A debugging line will be considered to have all the characteristics of a comment line if the Debug Compile Command Option is not specified and the WITH DEBUGGING MODE clause is not specified in the SOURCE-COMPUTER paragraph.

Successive debugging lines are allowed.

A debugging line is only permitted in the separately compiled program after the OBJECT-COMPUTER paragraph, or, if the OBJECT-COMPUTER paragraph is omitted, after where the OBJECT-COMPUTER paragraph would be permitted if it were present.

Statements

Source statements always begin with a keyword called a verb.There are four kinds of statements:

  1. Directive
  2. Conditional
  3. Imperative
  4. Delimited Scope

Directive Statements

A directive statement specifies action to be taken by the compiler during compilation. The directive statements are the COPY, REPLACE, and USE statements.

Conditional Statements

A conditional specifies that the truth value of a condition is to be determined and that the subsequent action of the object program is dependent on this truth value.

A conditional statement is one of the following:

  • An EVALUATE, IF, SEARCH, or RETURN statement.
  • A READ statement that specifies the AT END, NOT AT END, INVALID KEY, or NOT INVALID KEY phrase.
  • A WRITE statement that specifies the INVALID KEY, NOT INVALID KEY, END-OF-PAGE, or NOT END-OF-PAGE phrase.
  • A DELETE, REWRITE, or START statement that specifies the INVALID KEY or NOT INVALID KEY phrase.
  • An arithmetic statement (ADD, COMPUTE, DIVIDE, MULTIPLY, SUBTRACT)that specifies the ON SIZE ERROR or NOT ON SIZE ERROR phrase.
  • A RECEIVE statement that specifies the NO DATA or WITH DATA phrase.
  • A STRING or UNSTRING statement that specifies the ON OVERFLOW or NOT ON OVERFLOW phrase.
  • A CALL statement that specifies the ON OVERFLOW, ON EXCEPTION, or NOT ON EXCEPTION phrase.
  • An ACCEPT statement that specifies the ON EXCEPTION, ON ESCAPE, NOT ON EXCEPTION, or NOT ON ESCAPE phrase.

Conditional Phrases

A conditional phrase specifies the action to be taken upon determination of the truth value of a condition resulting from execution of a conditional statement.

A conditional phrase is one of the following:

  • The AT END or NOT AT END phrase in a READ statement.
  • The INVALID KEY or NOT INVALID KEY phrase in a DELETE, READ, REWRITE, START, or WRITE statement.
  • The END-OF-PAGE or NOT END-OF-PAGE phrase in a WRITE statement.
  • The ON SIZE ERROR or NOT ON SIZE ERROR phrase in an ADD, COMPUTE, DIVIDE, MULTIPLY, or SUBTRACT statement.
  • The NO DATA or WITH DATA phrase in a RECEIVE statement.
  • The ON OVERFLOW or NOT ON OVERFLOW phrase in a STRING or UNSTRING statement.
  • The ON OVERFLOW, ON EXCEPTION, or NOT ON EXCEPTION phrase in a CALL statement.
  • The ON EXCEPTION, ON ESCAPE, NOT ON EXCEPTION, or NOT ON ESCAPE phrase in an ACCEPT statement.

Imperative Statements

An imperative statement begins with an imperative verb and specifies an unconditional action to be taken by the object program, or is a conditional statement that is delimited by its explicit scope terminator (delimited scope statement). An imperative statement may consist of a sequence of imperative statements,each possibly separated from the next by a separator or the word THEN.

The imperative verbs are listed below

Wherever imperative-statement appears in the general format of statements it refers to that sequence of consecutive imperative statements that must be ended by a period or by any phrase associated with a statement containing imperative-statement.

Imperative Verbs

Imperative Verbs

Delimited Scope Statements

A delimited scope statement is any statement that includes its explicit scope terminator.The explicit scope terminators are the following:

Delimited Scope Statements

Scope of Statements

Scope terminators delimit the scope of certain Procedure Division statements. Statements that include their explicit scope terminators are called delimited scope statements. The scope of statements that are contained within statements (nested) may also be implicitly terminated.

When statements are nested within other statements, a separator period that ends the sentence implicitly terminates all nested statements.

When any statement is contained within another statement, the next phrase of the containing statement following the contained statement terminates the scope of any unterminated contained statement.

When statements are nested within other statements that allow optional conditional phrases, any optional conditional phrase encountered is considered to be the next phrase of the nearest preceding unterminated statement with which that phrase is permitted to be associated but with which no such phrase has already been associated. An unterminated statement is one that has not been previously terminated either explicitly or implicitly.

When a delimited scope statement is nested within another delimited scope statement with the same verb, each explicit scope terminator terminates the statement started by the most recently preceding, and as yet unterminated, occurrence of that verb.

Sentences

A sentence is a sequence of one or more statements terminated by the period separator. There are three kinds of sentences:

  1. A directive sentence may contain only a single directive statement.
  2. A conditional sentence is a conditional statement, optionally preceded by an imperative statement, terminated by the separator period.
  3. An imperative sentence is an imperative statement terminated by the separator period.

Clauses and Entries

An entry is an item of descriptive or declaratory nature made up of consecutive clauses. Each clause specifies an attribute of the entry. Clauses are separated by space, comma, or semicolon separators. The entry is terminated by a period separator.

Paragraphs

A paragraph is a sequence of zero, one, or more sentences or entries.In the Identification and Environment Divisions, each paragraph begins with a reserved word called a paragraph header. In the Procedure Division, each paragraph begins with a userdefined paragraph-name.

Sections

A section is a sequence of zero, one, or more paragraphs in the Environment and Procedure Divisions and a sequence of zero, one, or more entries in the Data Division. In the Environment and Data Divisions, each section begins with a section header that is made up of reserved words.In the Procedure Division, each section begins with a user-defined section-name.

Divisions

With the exception of COPY and REPLACE statements and the end program header, the statements, entries, paragraphs, and sections of a source program are grouped into four divisions which are sequenced in the following order:

  1. Identification Division
  2. Environment Division
  3. Data Division
  4. Procedure Division

The end of a source program is indicated either by the end program header,if specified,or by the absence of additional source program lines.

Source Program General Format

The following gives the general format and order of presentation of the entries and statements that constitute a source program. The generic terms identification-division, environment-division, data-division, procedure-division, nested-source-program, and end-program-header represent an Identification Division, an Environment Division,a Data Division, a Procedure Division, a nested source program, and an end program header.

Source Program General Format

end-program-header must be present if either of the following circumstances exists:

  • The source program contains one or more other source programs.
  • The source program is contained within another source program.

General Rules

  • 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:
    • The division header of a succeeding division in that program.
    • An Identification Division header that indicates the start of another source program.
    • The end program header.
    • That physical position after which no more source program lines occur.
  • A source program directly or indirectly contained within another program is considered in these specifications as a separate program that may additionally reference certain resources defined in the containing program.
  • The object code, resulting from compiling a source program contained within another program, is considered in these specifications to be inseparable from the object code resulting from compiling the containing program.
  • All separately compiled source programs in a sequence of programs must be terminated by an end program header except for the last program in the sequence.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

COBOL Topics