COBOL text is a stream of separated tokens: words, literals, picture strings and a few punctuation marks. There are no keywords reserved by position; a word is reserved everywhere or not at all (apart from a few context-sensitive words). The line layout comes first, on File structure.
COBOL words
A COBOL word is a run of letters, digits and hyphens. Rules common to the standards:
- Letters are A to Z and a to z, case-insensitive. Digits are 0 to 9.
- A hyphen may not be the first or last character. Underscore is accepted by GnuCOBOL,
Micro Focus and IBM Enterprise COBOL 6 and was added to the standard words in 2002.
IBM also allows
#,$and@in program names. - The length limit was 30 characters in COBOL-74 and COBOL-85, 31 in 2002 and later; compilers differ (GnuCOBOL accepts 31 by default and more with an option; Micro Focus and IBM state their own limits).
- A user-defined word (a data name, file name, paragraph name, section name, mnemonic, index, condition name,
class name, program name…) needs at least one letter, except paragraph and section names, which may be all digits
(
1000-INITis valid and0100.too). A word that starts with a digit is still a word;1000-INITis not a number. - A reserved word may not be used as a user-defined name. The lists are on Reserved words.
- A system name (
SYSIN,SYSOUT,CONSOLE,C01…) is an implementor name for a device or feature, given a mnemonic name inSPECIAL-NAMES. - A function name follows
FUNCTION; see Intrinsic functions. - A name can be made unique by qualification:
CITY OF ADDRESS OF CUSTOMER(INalso works).
Separators and punctuation
| Separator | Meaning |
|---|---|
| space (also tab and, in free format, line end) | Separates words. Any run of spaces counts as one. |
, and ; | Optional separators, always followed by a space; they mean nothing. With DECIMAL-POINT IS COMMA the comma is the decimal point, so 1,5 is one and a half and the separator is a semicolon or , with a space. |
. followed by a space or line end | The separator period that ends a sentence, a header, a paragraph name or an entry. |
( ) | Subscripts, reference modification, arguments, grouping in expressions. |
: | Reference modification NAME(start:length); host variables in embedded SQL. |
& | Concatenation of literals (2002; GnuCOBOL and Micro Focus accept it). |
== | Pseudo-text delimiter in COPY and REPLACE. |
Operators: + - * / ** (arithmetic; surrounded by spaces), = < > <= >= <>
(relations), and the words AND OR NOT, EQUAL TO, GREATER THAN, LESS THAN, XOR (2023),
B-AND B-OR B-XOR B-NOT and B-SHIFT-L and friends for booleans. A sign is attached to a numeric literal
only when there is no space between it and the digits (-5), otherwise it is an operator
(A - 5). See Expressions and conditions.
Comments
| Form | Where | Meaning |
|---|---|---|
* in column 7 | fixed format | Whole line is a comment |
/ in column 7 | fixed format | Comment, and a page eject in the listing |
*> | 2002 and later, both formats (anywhere on the line; in fixed format column 7 or later) | The rest of the line is a comment |
| text after column 72 | fixed format | Ignored (identification area) |
| columns 1 to 6 | fixed format | Ignored (sequence area) |
D in column 7, or >>D | fixed, free | A debugging line; a comment unless debugging is switched on |
comment entries after AUTHOR., INSTALLATION., DATE-WRITTEN., DATE-COMPILED., SECURITY., REMARKS. | COBOL-85 and earlier; accepted by compilers for compatibility | Free text up to the next paragraph header |
NOTE sentence | COBOL-60 to COBOL-68 | Comment to the next full stop; removed from the standard in 1974 (some compilers still take it) |
There are no block comments. The viewer colours the whole line from the sequence area for an indicator
comment, and from the *> for a trailing one.
Continuation of lines and literals
- Fixed format. A hyphen in column 7 continues the previous line. A nonnumeric literal that runs to column 72 continues on the next line, where the first non-blank character is a quotation mark of the same kind (it does not start a new literal); the text of the literal carries on right after it. Trailing spaces of the first line count when the literal reaches column 72.
- Words and numbers can be split the same way, without the second quote. GnuCOBOL warns about this and it is kept only for old programs.
- Concatenation.
"abc" & "def"joins two pieces at compile time (2002 and later; the only way in free format). - Pseudo-text may span lines without a continuation mark.
01 LONG-TEXT PIC X(60) VALUE "THIS TEXT RUNS TO COLUMN 72 AN
- "D IS CONTINUED HERE".
01 PIECES PIC X(30) VALUE "A PIECE" & " AND ANOTHER".
Literals
Nonnumeric literals
A string of characters in quotation marks "…" or apostrophes '…'. The quote
that opens it must close it; a doubled quote of the same kind inside stands for one quote character
('IT''S', "SAY ""HI"""). The compiler option or directive APOST / QUOTE (IBM, Micro Focus)
chooses which kind the figurative constant QUOTE stands for. An empty literal is not allowed in COBOL-85; later standards and compilers differ.
Prefixed forms
| Form | Meaning | Where |
|---|---|---|
X"1F2A" X'F1F2' | Hexadecimal: two hex digits give one character; the value is alphanumeric | All current compilers; 2002 standard |
Z"text" | Null-terminated: a zero byte is added after the text | IBM Enterprise COBOL, Micro Focus, GnuCOBOL |
N"text" N'text' | National (Unicode) literal; IBM holds it as UTF-16. GnuCOBOL also accepts NC"..." | 2002 standard; IBM; GnuCOBOL |
NX"00410042" | National literal in hexadecimal | 2002 standard; IBM |
G"..." G'...' | DBCS (double-byte) literal; shift-out and shift-in around double-byte characters | IBM Enterprise COBOL; GnuCOBOL accepts the prefix |
B"101" | Boolean literal, one character per bit | 2002 standard |
BX"A5" | Boolean literal in hexadecimal | 2002 standard |
L"..." | Raw C string | GnuCOBOL |
B#101 O#17 H#FF X#FF | Numeric literals in base 2, 8 and 16 | GnuCOBOL |
Numeric literals
- Fixed-point: optional sign, digits, optionally a decimal point and more digits:
7,-54,+3.14,.5. The sign must touch the digits. A literal ends before a separator period, soMOVE 1.moves the integer 1. WithDECIMAL-POINT IS COMMAthe decimal point is a comma. Length: 18 digits in COBOL-85, 31 in 2002 and later and in IBM withARITH(EXTEND); compilers vary. - Floating-point (2002): a significand with a decimal point,
E, a signed exponent:9.92E25,5.7E-14,-1.2E+03. Both the significand and exponent may carry a sign. - Integer literals also serve as level numbers (
01–49,66,77,78,88) and as segment numbers.
Figurative constants
| Word | Value |
|---|---|
ZERO ZEROS ZEROES | The value 0, or the character 0 as many times as needed |
SPACE SPACES | Space characters |
HIGH-VALUE HIGH-VALUES | The character highest in the collating sequence (hexadecimal FF in ASCII) |
LOW-VALUE LOW-VALUES | The lowest character (hexadecimal 00 in ASCII) |
QUOTE QUOTES | The quotation mark (or apostrophe, with the APOST option) |
NULL NULLS | Zero bits; a null pointer for POINTER items (2002 and several compilers) |
ALL literal | The literal repeated: ALL "AB", ALL ZERO |
| a symbolic character | A name given a character in SPECIAL-NAMES SYMBOLIC CHARACTERS |
TRUE and FALSE (2002) are conditional values, not constants, used with SET on a condition name.
A figurative constant has no length of its own, so it cannot be a CALL argument by content where a length is needed.
Pseudo-text
Text enclosed in == pairs is pseudo-text, used only in COPY … REPLACING and
REPLACE. It holds any text, including words and partial words, over any number of lines, and
nothing in it is checked until after replacement. ==== is empty pseudo-text.
COPY CUSTOMER REPLACING ==:TAG:== BY ==WS==.
REPLACE ==OLD-NAME== BY ==NEW-NAME==.
Picture strings
After PICTURE or PIC (and the optional word IS) comes a picture character-string: a run of picture
symbols with repeat counts in parentheses, such as S9(5)V99 or $$$,$$9.99CR. It
ends at the first space; a final full stop, comma or semicolon before the space is punctuation, not part of the
string. Because a picture can contain full stops, commas and currency signs, it is the one token that
is scanned character by character. Every symbol is listed on
Picture and usage.
Special registers and system words
Special registers are names the compiler provides with no declaration: RETURN-CODE, SORT-RETURN,
TALLY, LINAGE-COUNTER, LINE-COUNTER, PAGE-COUNTER, NUMBER-OF-CALL-PARAMETERS, SHIFT-IN,
SHIFT-OUT, WHEN-COMPILED, DEBUG-ITEM, EXCEPTION-OBJECT, JSON-CODE, XML-CODE
and the phrases ADDRESS OF and LENGTH OF. The viewer colours them as types.
How the viewer colours COBOL
The same scanner colours the file viewer and the code blocks in these pages. Comments, strings
(including every prefixed form and the continuation of a literal), numbers (signed, decimal, floating, radix),
picture strings, reserved words as keywords, USAGE words and special registers as types, figurative constants as literals,
compiler directives (>>, $, ?, CBL, PROCESS, COPY, REPLACE),
program, class and method names and paragraph and section names as functions, and data names as variables.
Embedded SQL and CICS blocks have their own words.
01 REC.
05 ID-NO PIC 9(5) COMP-3.
05 FILLER PIC X(3) VALUE X"414243".
05 PRICE PIC S9(5)V99 VALUE -12.50.
PROCEDURE DIVISION.
0100-MAIN.
PERFORM 0200-WORK THRU 0200-EXIT. *> paragraph names
EXEC SQL SELECT NAME INTO :WS-NAME FROM T END-EXEC.