uwol / proleap-cobol-parser

ProLeap ANTLR4-based parser for COBOL
MIT License
134 stars 73 forks source link

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

Open panky1998 opened 1 year ago

panky1998 commented 1 year ago

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

uwol commented 1 year ago

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 commented 1 year ago

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

panky1998 commented 1 year ago

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

uwol commented 1 year ago

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