proleap-cobol-parser icon indicating copy to clipboard operation
proleap-cobol-parser copied to clipboard

I'm not getting the root node to get the AST. Do I need to manually print the AST ??

Open panky1998 opened this issue 2 years ago • 4 comments

Since I'm trying to get the AST elements . I'm unable to get them. If you have any suggestions please help!

panky1998 avatar Jun 19 '23 07:06 panky1998

Hi @panky1998 ,

you should be able to call getCtx on each ASG element to retrieve the corresponding AST element. So for each ASG element there should be an AST element named ctx. The AST ctx element should always be an ANTLR class.

The interface is in ASGElement.java

Best Ulrich

uwol avatar Jun 19 '23 09:06 uwol

An arbitrary example for such a call would be this line.

uwol avatar Jun 19 '23 09:06 uwol

Hi @uwol , As you said I'm calling each AST element with getCtx(). But when I'm trying to call AST elements it is returning numbers. Am I calling in improper way? Please help me as am a novice...

         // navigate on ASG
        CompilationUnit compilationUnit = program.getCompilationUnit("HelloWorld");
        ProgramUnit programUnit = compilationUnit.getProgramUnit();
        DataDivision dataDivision = programUnit.getDataDivision();
        DataDescriptionEntry dataDescriptionEntry = dataDivision
        .getWorkingStorageSection().getDataDescriptionEntry("ITEMS");
        Integer levelNumber = dataDescriptionEntry.getLevelNumber();
        
        
        System.out.println("Compilation unit => "+compilationUnit.getCtx());
        System.out.println("Program Unit => "+programUnit.getCtx());
        System.out.println("Data Division => "+dataDivision.getCtx());
        System.out.println("Data Description Entry => "+dataDescriptionEntry.getCtx());
        System.out.println("Level number =--> "+levelNumber);

I'm getting output as

Compilation unit => [1198] Program Unit => [1201 1198] Data Division => [1211 1201 1198] Data Description Entry => [3171 2286 2005 1997 1211 1201 1198] Level number =--> 1

Regards, Pankaj

panky1998 avatar Jun 21 '23 07:06 panky1998

Hi @panky1998 ,

this is correct, those are the ANTLR ParserRuleContext elements. In Eclipse debugger you can introspect them.

You can use them to navigate to children, to the parent element etc. For this the normal ANTLR documentation applies, nothing COBOL- or ProLeap-specific.

Best Ulrich

uwol avatar Jun 21 '23 07:06 uwol