I am trying to compile a simple program using COBOL and the gnuCOBOL cobc compiler. However, I cannot seem to get any sort of output for my program, error or otherwise. So I wrote a barebones "Hello World" program, and even that doesn't output as expected:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
AUTHOR. Myself
PROCEDURE DIVISION.
DISPLAY "HELLO WORLD".
STOP RUN.
I am trying to copile this by running cobc -x hello_world.cob, which generates an executable, but the executable produces no output when run. What am I missing?
GNUCOBOL version 2.2.0
Your code starts at position 12 (or so it seems). GNUCOBOL expects it to start at position 8. You may either alter your code, or use the '-free' compiler flag.
The issue is rooted in the AUTHOR paragraph, but only in combination with the rest...
The AUTHOR paragraph is one of the comment paragraphs.
With fixed-form reference-format (which is where they commonly are found) those end whenever the next code starts in area a - and because the code is indented to column 12 there is no match and the complete rest of the program is skipped as "comment".
Note that this is identical with other compilers, too (I've checked with Micro Focus and ACUCOBOL).
Removing the comment line therefore "fixes" the issue, as well as the "real fix" that moves either all or all (COBOL2002+) or all but the statements (older reference-format) to column 8.
With free-form reference-format there is no "area a" and those comment-paragraphs therefore end with the start of the next line (which is the reason that compiling with --free "fixes" this issue as well).
Note: the comment paragraphs were removed with COBOL 2002, and because of the "special area-a handling" they are obsolete with GnuCOBOL (and only worked if "used correctly, as far as COBOL2002+ is concerned its support is an extension). With COBOL 85, where the comment paragraphs were already obsolete, the program would not compile because it enforces area a.
You can see this when compiling with the dialect and warning options:
$ cobc -xj hi.cob # compiles and runs, nothing happens
$ cobc -xj hi.cob -Wall
hi.cob:3: warning: AUTHOR is obsolete in GnuCOBOL [-Wobsolete]
$ cobc -xj hi.cob -std=cobol85 -Wall
hi.cob:3: warning: AUTHOR is obsolete in COBOL 85 [-Wobsolete]
hi.cob:1: error: 'IDENTIFICATION DIVISION' should start in Area A
$ cobc -xj hi.cob -std=cobol2002
hi.cob:3: error: AUTHOR does not conform to COBOL 2002
Note: area a enforcement depending on dialects is a GnuCOBOL 3.2 feature, this was not available back when the original question was asked.
Remove the line containing AUTHOR. Myself and it should work.
Related
Is it possible to get and display the current line number in the Cobol program?
For example, C allows do it by the next way:
...
printf("Current line = %d\n", __LINE__);
...
Short answer: No.
There is no portable COBOL way in doing this, especially not in all places like __LINE__ does.
Long answer with potential alternatives:
COBOL 2002 added intrinsic functions for exception handling. Using these you can get the location where the last error happened, which checks are activated.
You could hack something by raising a non-fatal exception and ideally in the same line use that function...
From the standard:
The EXCEPTION-LOCATION function returns an alphanumeric character string, part of which is the implementor-defined location of the statement associated with the last exception status.
So this may provide you with the line number, as the returned value depends on the implementation, additional it seems that - at the time of writing - neither IBM nor MicroFocus nor Fujitsu compilers support that intrinsic function at all.
The GnuCOBOL implementation returns a semicolon-separated list with the last entry being the line number.
The upcoming COBOL standard added the MODULE-NAME intrinsic function - but this will only give the name, not the line reference.
If you are free to choose which implementation you use, then an addition of an extra register COB_SOURCE_LINE / COB_SOURCE_FILE in GnuCOBOL should be relative easy to add...
If the intend is a tracing of some kind: many compilers have an extension READY TRACE/ RESET TRACE. With those two statements (and possibly compiler directives / options) they will at least show the name of sections and paragraphs reached, some may also show the line number. Often this could be redirected to a file and will otherwise go to the default error stream.
If you use GnuCOBOL and compile with -ftrace-all you can also use that for line or statement tracing with self-defined format as specified in COB_TRACE_FORMAT [which can also be adjusted within the COBOL program and limited to the line number].
Q: Is it possible to get and display the current line number in the Cobol program?
There was a feature through COBOL 85 called the DEBUG module. The feature was made obsolete in COBOL 85 and subsequently removed in COBOL 2002. While DEBUG lines were available in the 2002 standard, the DEBUG module was removed from the standard.
NOTE: The DEBUG module may still be available in current compilers.
The feature requires debugging mode in the source-computer paragraph. If the line is removed, source lines with a D or d in column 7 are treated as comments.
Declaratives must be added to access debug-line which is the standard name for the source line number.
I have coded the source such that the source line number of wherever I place perform show-line will be displayed. Notice that show-line doesn't do anything.
Source:
program-id. dbug.
environment division.
source-computer. computer-name debugging mode.
object-computer. computer-name.
data division.
working-storage section.
01 char pic x.
procedure division.
declaratives.
ddebug section.
duse for debugging show-line.
d display "Source-line: " debug-line.
end declaratives.
main-line.
begin.
display "Before"
d perform show-line
display "After"
accept char
stop run.
dshow-line.
end program dbug.
Each implementor has their own means for activating the feature. For the system I use, it's a switch parameter (+D) on the command line. Without the switch parameter the line number will not show. (For GnuCOBOL 3.2 it is, apparently, the environment variable COB_SET_DEBUG with a value of 'Y', 'y' or '1'. ;-))
Command line:
dbug (+D)
Display:
Before
Source-line: 17
After
I'm watching COBOL tutorials, they use the "erase" statement to clean the screen and it doesn't work for me. The compiler indicates "syntax error, unexpected ERASE"
The code is:
DISPLAY "1.- CALC, 2.- CLOSE" ERASE.
It is my mistake?
I am using opencobol on ubuntu
Sorry about my English, it's not my native language
As noted by #user207421 ERASE is not a statement, it is a clause for the DISPLAY statement, and it even is standardized - but (standard-wise) it needs a specification what you want to erase:
ERASE [END] [OF] LINE
[END] [OF] SCREEN
EOL
EOS
The syntax you have shown is actually the very non-standard, outdated Microsoft-COBOL DISPLAY statement with ERASE phrase.
If you still use open-cobol then the package is heavily outdated, there should be a new gnucobol package available in Ubuntu (otherwise you could build from source). Using GnuCOBOL 2.2 you get a nicer error message:
error: syntax error, unexpected ., expecting LINE or SCREEN
And then you can decide if you want the old ms-cobol variant (that's supported in GnuCOBOL, but only with the pos-specifier) or the standard variant (ERASE EOS would be the compatible version and is supported by many compilers).
I am running Ubuntu and trying to learn COBOL. I have dabbled in a few online tutorials but have had inconsistent results with certain programs.
I prefer to use vim in a bash shell; leading me to OpenCOBOL (cobc)
Is there a decent tutorial that will teach me the basics? I have been working through this one.
http://www2.southeastern.edu/Academics/Faculty/kyang/Cmps401/P2Cobol/Resources/Teach%20Yourself%20Cobol%20In%2021%20Days%20%282nd%20Ed%29.pdf
My issue is that when running some of the example source code, the compiler returns an error when trying to use a "*". It says it is expecting an end of file.
Here is my source code:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
ENVIRONMENT DIVISION.
*comment here
DATA DIVISION.
PROCEDURE DIVISION.
DISPLAY 'HELLO WORLD!'.
STOP RUN.
Here is the command I am running:
cobc -x -free -o helloworld helloworld.cbl
Here is the error returned
helloworld.cbl:4: Error: syntax error, unexpected '*', expecting "end of file"
If you are using only the '*' comment, you need to use fixed form Cobol.
That means all of your division headers would start in "Area A", from columns 8-12, and your Display and Stop Run statements would start in "Area B", from column 12 - 72.
Or you could change the comment to a free form one, using '*>' and then it should work.
One of the best online COBOL learning resources is by Michael Coughlan, University of Limerick. http://www.csis.ul.ie/cobol/
Most, if not all the samples will work with GnuCOBOL, if you change the compiler directives to standard.
$ SET SOURCEFORMAT"FREE"
becomes
>>SOURCE FORMAT IS FREE
and change all the * column one comment markers to *>. If you like Vim, then those comment fixes are pretty easy
%s/^\*/\*>/gc
With those simple changes, the samples should compile clean with cobc. Michael has written one of the best beginner through advanced tutorials available on the net. Umm, that's a personal opinion.
I am trying to call a paragraph N times using an online compiler which is OpenCOBOL v1.1.0 This is what I am doing :
IDENTIFICATION DIVISION.
PROGRAM-ID. PerformNTimes.
PROCEDURE DIVISION.
PERFORM 3 TIMES
DISPLAY 'IN A-PARA'
END-PERFORM
PERFORM B-PARA 3 TIMES.
STOP RUN.
B-PARA.
DISPLAY 'IN B-PARA'
Why it is not calling B-PARA?
It will probably be much more convenient for you to get your own compiler. OpenCOBOL is now known as GnuCOBOL, and is available, in more up-to-date versions, from SourceForge.Net.
Your example may not be compiling. You don't show a final full-stop/period after the DISPLAY in B-PARA.
There's full support for the product and assistance with COBOL in the discussion groups at the GnuCOBOL area: https://sourceforge.net/p/open-cobol/discussion/?source=navbar
Finally, I solved it. The point is each line should end with CRLF. Added a period after the DISPLAY in B-PARA and ended each line with CRLF. You can refer here to see how you can achieve this.
I'm trying to make a very simple COBOL program. I've gotten my directory, configurations, etc. all set up, but when I go to compile it I get this error:
sampleCOBOL.cbl: In paragraph 'Main-Paragraph' :
sampleCOBOL.cbl:9: Error: syntax error, unexpected "end of file"
("sampleCOBOL.cbl" is my file name)
This is what my file contains:
Identification Division.
Program-ID. sampleCOBOL.
Data Division.
Procedure Division.
Main-Paragraph.
Display "Hello World!"
Stop Run.
I know that the error is occurring on Line#9 ("Stop Run."). However, why?
There is support for GNU COBOL (formerly OpenCOBOL) at SourgeForge.
From there, here is answer for the same error message: https://sourceforge.net/p/open-cobol/discussion/109660/thread/cdfe04a5/#0996
You can have you COBOL program obey the traditional fixed-column starts/ends, our you can put this, >>SOURCE FORMAT IS FREE in line one, column 12 of your program. You can then code without reference to column numbers.
If using column numbers, columns 1-6 are not used for code, column seven is for the comment, debugging, or new-page marker, or, rarely, continuing a literal which cannot fit on the previous line.
Code then either starts in columns 8-11 (aka "area a") or columnn 12-71 ("area b").
You do not need a full-stop/period in the PROCEDURE DIVISION except to end the PROCEDURE DIVISION header, before a paragraph/SECTION name and before the end of the program. In the distant past, you used to need lots of full-stops/periods, but not needed for many years (although many still code them).
Seeing your comment on the other answer and NealB's comment on your question, if you scroll down the linked-to discussion:
I have used Notepad++ for a lot of my own coding. You can set the EOL
to use UNIX instead of windows or UTF encoding. This will also resolve
EOF issues. Also, you will need to ensure you set "Use Spaces" when
tabbing. cobc has an issue when tabs are used from windows editors.
Putting that together, you are using Windows, tabs, and a version of OpenCOBOL which doesn't like tabs in source. You have two things to do directly to get it working, and you may want to get the latest version of GNU COBOL when it is convenient for you.
I suggest you go here, http://sourceforge.net/p/open-cobol/discussion/2526793/. Join, if you don't have a SourceForge account, or login if you do, and post in Help getting started. There are people there using Windows (which I don't) who should be able to help. The reason for login/join is that otherwise you will wait hours for your question to be "moderated" first, and you'll appear as Annonymous.
Figured out what was wrong. I had an extra line in between "Identification Division" and "Program-ID."
I have no idea how I missed that.
Boy, do I feel stupid.
The I of IDENTIFICATION must be at the column 8 ( 7 spaces before ).
---- sampleCOBOL.cob -------------------------
* Sample COBOL program
IDENTIFICATION DIVISION.
PROGRAM-ID. sampleCOBOL.
PROCEDURE DIVISION.
DISPLAY "Hello World!".
STOP RUN.
----------------------------------------
I faced the same problem recently when I just started learning COBOL. The point is each line should end with CRLF. You can refer here to see how you can achieve this.