Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does COBOL have to be indented? [closed]

Why does COBOL have to be indented, as in, have additional spacing in each sourcefile?

Consider this code (note the additional whitespace):

  IDENTIFICATION DIVISION.
  PROGRAM-ID. HELLO-WORLD.
  PROCEDURE DIVISION.
      DISPLAY 'Hello, world'.
      STOP RUN.

A similar formatting can be seen in Fortran code:

   program hello
      print *, "Hello World!"
   end program hello

But why do COBOL and Fortran need this whitespace? What's the reason?

like image 348
hiobs Avatar asked Jan 22 '12 06:01

hiobs


1 Answers

Cobol no longer has to be indented. AFAIK, all most modern compilers support format free Cobol source.

The original reason was dealing with punch cards. Cobol kept the first 6 positions for a line sequence number. Column 7 was a continuation / comment / debug / form-feed. Area "A", or Columns 8-11, indicated certain special language artifacts like 01 levels, section or paragraph names, et al. Area "B", or Columns 12 - 72, was for open code. Columns 73 - 80 were for OS sequence numbers.

The two languages you mention, Cobol and Fortran, were both written before automatic parser generation existed. And they had no real prior art to draw on for good and bad ideas of how to create parse-able source text. So some of the things -- like Area "A" for special section headers -- made the task of manually writing parsers easier. Modern languages tend to use context free grammars to make parser generation simple. But that postdates Cobol.

like image 71
Joe Zitzelberger Avatar answered Oct 29 '22 20:10

Joe Zitzelberger