Topic
  • 7 replies
  • Latest Post - ‏2013-01-04T13:26:08Z by markevans
SystemAdmin
SystemAdmin
6195 Posts

Pinned topic simple for-loop in recursive function not terminating

‏2012-11-18T10:35:28Z |
Hello IBM-EGL-inventors,
shouldn't it be possible to have a simple for-loop within a recursive function working properly for all recursive calls. Please have a look at my program recursiveFunction and it's output down here.

The rucursion is terminating like expected but the for loop with the first instance not, thus leading to an endless loop.

Please try the program out and tell me what's wrong with my program or with EGL. I would be very greatfull to get this example running for my University exam which is a tool for my company PKS.
Kind regards
Andreas Gronert

package work;
program recursiveFunction type BasicProgram{}
function main()
recFunc(0);
end

function recFunc(recTiefe int in)
recTiefe = recTiefe + 1;
i int = 0;
if(recTiefe <= 2)
for(i from 1 to 1)
syslib.writeStdout("recTiefe " :: recTiefe ::
" Schleifenzähler " :: i :: " - vor recCall");
recFunc(recTiefe);
syslib.writeStdout("recTiefe " :: recTiefe ::
" Schleifenzähler " :: i :: " - nach recCall");
end
Syslib.writeStdout("recTiefe " :: recTiefe::" - nach Schleife");
end
end
end

/*Ausgabe
recTiefe 1 Schleifenzähler 1 - vor recCall
recTiefe 2 Schleifenzähler 1 - vor recCall
recTiefe 2 Schleifenzähler 1 - nach recCall
recTiefe 2 - nach Schleife
recTiefe 1 Schleifenzähler 1 - nach recCall
recTiefe 1 Schleifenzähler 1 - vor recCall
The debug session was terminated.
*/
/* Debug nach letzter obiger Anweisung abgebrochen, weil Anfang der Endlosschleife */
Updated on 2013-01-04T13:26:08Z at 2013-01-04T13:26:08Z by markevans
  • markevans
    markevans
    3034 Posts

    Re: simple for-loop in recursive function not terminating

    ‏2012-11-19T01:04:49Z  
    Andreas,

    Is this a problem in COBOL Gen, Java Gen, Javascript, or the debugger (or all)?

    thanks.
  • SystemAdmin
    SystemAdmin
    6195 Posts

    Re: simple for-loop in recursive function not terminating

    ‏2012-11-19T10:43:07Z  
    • markevans
    • ‏2012-11-19T01:04:49Z
    Andreas,

    Is this a problem in COBOL Gen, Java Gen, Javascript, or the debugger (or all)?

    thanks.
    This got me interested. When targeting COBOL the code performs just as expected. I could reproduce the problem when debugging the program in RBD 7.5.1.7. What is interesting and probably a clue to where the defect lies, is that when I changed to function recFunc to the following format
    
    function recFunc(recTiefe 
    
    int in) recTiefe = recTiefe + 1; 
    
    if(recTiefe <= 2) 
    
    for(i 
    
    int from 1 to 1) syslib.writeStdout(
    "recTiefe " :: recTiefe :: 
    " Schleifenzähler " :: i :: 
    " - vor recCall"); recFunc(recTiefe); syslib.writeStdout(
    "recTiefe " :: recTiefe :: 
    " Schleifenzähler " :: i :: 
    " - nach recCall"); end Syslib.writeStdout(
    "recTiefe " :: recTiefe::
    " - nach Schleife"); end end
    

    the debugger throws exception
    
    com.ibm.etools.egl.interpreter.infrastructure.InternalDebuggerException: Could not resolve the runtime part 
    
    for the variable named i
    

    and when code is run as a Java application, it results in another exception:
    
    EGL0100E An unhandled error occurred. Error: java.lang.StringIndexOutOfBoundsException. EGL0002I The error occurred in EVVK28 processing the main function.
    

    In fact, the following much simpler code should create an infinite loop but it crashes with the same errors both when run in debugger and when run as a Java application.
    
    function main() 
    
    for(i 
    
    int from 1 to 1) syslib.writeStdout(i); main(); end end
    

    It seems that the problem lies in creating and maintaining loop variable: if it is defined within the loop it cannot be found, and if it is defined outside the loop execution fails.

    For once it is nice to see a case in which COBOL generation is correct but Java generation is not. :)
  • SystemAdmin
    SystemAdmin
    6195 Posts

    Re: simple for-loop in recursive function not terminating

    ‏2012-11-19T16:48:57Z  
    Hello Mark,
    in my case it was the debugger and I didn't tried with COBOL Gen, Java Gen or Javascript yet.
    Kind regards
    Andreas Gronert
  • dan_darnell
    dan_darnell
    973 Posts

    Re: simple for-loop in recursive function not terminating

    ‏2012-11-19T21:09:10Z  
    Hello Mark,
    in my case it was the debugger and I didn't tried with COBOL Gen, Java Gen or Javascript yet.
    Kind regards
    Andreas Gronert
    Always fun.

    Using RBD 8.5 here with a General Project targeted to Java.

    Debugging in the IDE, I get the issue exactly as Andreas has reported it.

    When I "Run As..." a Java program, the program runs correctly. No errors and the following output:

    recTiefe 1 Schleifenzähler 1 - vor recCall
    recTiefe 2 Schleifenzähler 1 - vor recCall
    recTiefe 2 Schleifenzähler 1 - nach recCall
    recTiefe 2 - nach Schleife
    recTiefe 1 Schleifenzähler 1 - nach recCall
    recTiefe 1 - nach Schleife

    So, at least in 8.5, conceivably you have an issue with Debug but not with generated Java code.

    --Dan
  • SystemAdmin
    SystemAdmin
    6195 Posts

    Re: simple for-loop in recursive function not terminating

    ‏2012-11-22T07:50:49Z  
    Always fun.

    Using RBD 8.5 here with a General Project targeted to Java.

    Debugging in the IDE, I get the issue exactly as Andreas has reported it.

    When I "Run As..." a Java program, the program runs correctly. No errors and the following output:

    recTiefe 1 Schleifenzähler 1 - vor recCall
    recTiefe 2 Schleifenzähler 1 - vor recCall
    recTiefe 2 Schleifenzähler 1 - nach recCall
    recTiefe 2 - nach Schleife
    recTiefe 1 Schleifenzähler 1 - nach recCall
    recTiefe 1 - nach Schleife

    So, at least in 8.5, conceivably you have an issue with Debug but not with generated Java code.

    --Dan
    I could not help trying infinite recursion on with RBD 8.5 and I can confirm Dan's observation: run as Java correctly produces stack overflow whereas the debugger cannot find the variable i inside the loop
    for(i int from 1 to 1)
            syslib.writeStdout(i);
            main();
    end
    

    Hence, behaviour is identical in RBD 7.5 and RBD 8.5.
    Updated on 2014-03-25T04:38:09Z at 2014-03-25T04:38:09Z by iron-man
  • SystemAdmin
    SystemAdmin
    6195 Posts

    Re: simple for-loop in recursive function not terminating

    ‏2013-01-04T08:55:09Z  
    Conclusion:

    Problem only with debugger.

    This makes analysis of problems much mor complicated, as I'm encountering different problems with and without debugger, thus that I'm asking myself if it wouldn't be better to analyze problems allways without debugger in old fashioned way by inserting console messages.
  • markevans
    markevans
    3034 Posts

    Re: simple for-loop in recursive function not terminating

    ‏2013-01-04T13:26:08Z  
    Conclusion:

    Problem only with debugger.

    This makes analysis of problems much mor complicated, as I'm encountering different problems with and without debugger, thus that I'm asking myself if it wouldn't be better to analyze problems allways without debugger in old fashioned way by inserting console messages.
    Andreas,

    I can understand the frustration and our goal is to

    a.) Make the debugger, Java Gen, and COBOL gen consistent in behavior

    or

    b.) Document it in the EGL Reference Manual when there are differences
    Therefore, I ask that you open problems if you find that either is not the case so that we can investigate it and try to correct it.

    Thanks for your understanding.