Page 2 of 2

Re: EVALUATE/WHEN in COBOL

PostPosted: Wed May 07, 2014 3:20 pm
by enrico-sorichetti
I don't think there will be a limit for nesting of statements (including IF) because the compiler will just generate code with branches,

the problem will not be the CODE GENERATION but the SHIFT/REDUCE parsing logic
very rough approach ... but enough to clarify
IF <logical expression>
<true process>
ELSE
<false process>


if one of the inside processes contains an IF construct
the outer IF cannot be reduced until the inner IF is

and keep nesting... keep nesting
and the compiler will accumulate things to be reduces until the deepest IF can be reduced

as I said ... VERY ROUGH explanation

Re: EVALUATE/WHEN in COBOL

PostPosted: Wed May 07, 2014 3:31 pm
by BillyBoyo
Well, I've never seen a recorded limit for nesting with an IBM COBOL.

I may have a play later. I'll generate:

IF A EQUAL TO B
IF A EQUAL TO B
.... about 999,987 of them
IF A EQUAL TO B
    MOVE B TO A
END-IF


Compile it with NOOPTiMIZE. See what happens :-)

Re: EVALUATE/WHEN in COBOL

PostPosted: Wed May 07, 2014 3:43 pm
by enrico-sorichetti
my comment about parsing was pretty general

MIGHT not even apply to the specific paring logic used by IBM

the IF part never gave me problems
it was the ELSE part that fouled things up
( using Yacc and bison )

Re: EVALUATE/WHEN in COBOL

PostPosted: Wed May 07, 2014 3:51 pm
by Aki88
Ditto Enricho; I have seen the ELSE giving trouble, rarely an IF.
And Billy, nested IF/THEN/ELSE, ohh I've seen those, a guy had coded this huge construct, went some 20+ layers down, and to complicate things, there were couple of sub-layers which again had few IF/THEN/ELSE. He had actually coded a copybook for this whole construct.
Never seen that big an EVALUATE though; can expriment just to see what the compiler throws up :lol:

Thanks.

Re: EVALUATE/WHEN in COBOL

PostPosted: Wed May 07, 2014 4:03 pm
by BillyBoyo
OK, I'll stick an ELSE in and cut the number of IF statements in half. I don't think it'll be a problem - we'll see :-)

Re: EVALUATE/WHEN in COBOL

PostPosted: Thu May 08, 2014 5:22 am
by BillyBoyo
Well, not as clear-cut as I thought.

First problem is on the listing. The nesting indicator is only two digits, so greater than 99 levels of nesting and it prints as "**".

However, more than 99 levels of nesting work.

However, nowhere near as many as I thought.

It seems like 16380 is either the number of levels of nesting you can have, or 32760 is the maximum number of statements you can include in the nest. I'm not going to look further to find out which :-) Beyond that point the compiler goes to pot, with a number of weird errors.

So, yes, there is an undocumented limit to the number of levels of nesting, at least for an IF and presumably for anything else that can be nested.

However, except for a small possibility for generated code, the limit is so far from approachable as to not matter. Even the 99 levels of nesting would be enormously difficult to code, understand or change.

At 16380 levels the program does still work :-) At 16381, it just doesn't compile, with multiple messages entirely undrelated to the nested code.

Out of interest, here's how I generated the program, using DFSORT.

//SYSIN    DD *
  OPTION COPY
  OUTFIL FNAMES=OUT1,INCLUDE=(1,1,CH,EQ,C'A'),REMOVECC,
       HEADER1=(8:'ID DIVISION.',/,
       8:'PROGRAM-ID. NESTING.',/,
       8:'DATA DIVISION.',/,
       8:'WORKING-STORAGE SECTION.',/,
       8:'01  A PIC X VALUE "A".',/,
       8:'01  B PIC X VALUE "B".',/,
       8:'01  C PIC X(8)BX(8) VALUE "NEW".',/,
       8:'PROCEDURE DIVISION.')
                                                       
  OUTFIL FNAMES=OUT2,INCLUDE=(1,1,CH,EQ,C'A'),REMOVECC,
       TRAILER1=(12:'MOVE B TO A',/,
           12:'.',/,
       12:'MOVE WHEN-COMPILED TO C',/,
       12:'DISPLAY C',/,
           12:'DISPLAY ">" A "<"',/,
           12:'GOBACK',/,
           12:'.')
  OUTFIL REPEAT=16380
                                                       
                                                       
//SORTIN   DD *
           IF A EQUAL TO B CONTINUE ELSE


I then concatenated the three output datasets (OUT1, SORTOUT, OUT2) as input to the COBOL compiler.

On executing the program, the value "B" is displayed, which is the expected result. I turned the OPTimizer off, of course. Mmmm.... perhaps I should try with it on as well, to see what happens...

Re: EVALUATE/WHEN in COBOL

PostPosted: Thu May 08, 2014 5:35 am
by BillyBoyo
The OPTimizer does not attempt to optimise my 16000+ levels of nesting.

Re: EVALUATE/WHEN in COBOL

PostPosted: Thu May 08, 2014 10:44 am
by Aki88
Just one word for that wow, and I mean 'W O W'; drat, I was using a nested PERFORM to generate my statements, lest the remaining stuff, I am keeping that SORT card in my tool library right away ;)

This is good info; thanks Billy, this will come really handy. We have an :ugeek: :P

Cheers.

Re: EVALUATE/WHEN in COBOL

PostPosted: Thu May 08, 2014 1:57 pm
by BillyBoyo
Well, it was some fun. Learned some stuff myself.

Be aware that normally I only need one OUTFIL for a generated program. However, the HEADER1 and TRAILER1 are reporting functions, and REPEAT cannot be used with reporting functions on the same OUTFIL.

Of course, it doesn't hurt to have them as three datasets and concatenate.