ProLeap COBOL Parser

repository·main·Indexed 41 days ago

https://github.com/uwol/proleap-cobol-parser

An ANTLR4-based COBOL parser that generates an Abstract Syntax Tree (AST) for syntax traversal and an Abstract Semantic Graph (ASG) for semantic analysis, including data and control flow. Version 4.0.0 requires JDK 17 and Maven 3+. It supports extracting EXEC SQL, EXEC SQLIMS, and EXEC CICS statements as text and provides a CobolBaseVisitor for AST traversal.

Tokens
1.8K
Snippets
4
Records
6
Agent score
3%

What's inside ProLeap COBOL Parser

  1. How AST and ASG differ in ProLeap COBOL

    main

    The parser produces two distinct representations of COBOL code:

    1. Abstract Syntax Tree (AST): Represents the plain COBOL source code in a hierarchical syntax tree structure based on the ANTLR4 grammar. It is used for structural traversal.
    2. Abstract Semantic Graph (ASG): Generated from the AST via semantic analysis. It provides higher-level information such as data and control flow (e.g., variable access). It also contains extracted text for EXEC SQL, EXEC SQLIMS, and EXEC CICS statements.

    Developers typically use the AST for syntax-based traversal (via CobolBaseVisitor) and the ASG for semantic analysis and data-flow queries.

  2. Build the ProLeap COBOL parser from source

    main

    The project requires JDK 17 and Maven 3 or higher.

    To build the project and generate a Maven JAR:

    $ mvn clean package

    To install the JAR into your local Maven repository:

    $ mvn clean install

    To run only the test suite:

    $ mvn clean test
  3. Install the ProLeap COBOL parser via Maven

    main

    To use the parser in your Maven project, add the following dependency to your pom.xml file:

    <dependency>
      <groupId>io.github.uwol</groupId>
      <artifactId>proleap-cobol-parser</artifactId>
      <version>4.0.0</version>
    </dependency>
  4. Generate an Abstract Semantic Graph (ASG) from COBOL code

    main

    You can generate an ASG by using CobolParserRunnerImpl().analyzeFile(). This method performs semantic analysis on the COBOL source code, providing access to data and control flow information (e.g., variable access).

    Note that EXEC SQL, EXEC SQLIMS, and EXEC CICS statements are extracted as text and included in the ASG. You must specify a CobolSourceFormatEnum (e.g., TANDEM) to define the source format.

    // generate ASG from plain COBOL code
    java.io.File inputFile = new java.io.File("src/test/resources/io/proleap/cobol/asg/HelloWorld.cbl");
    io.proleap.cobol.preprocessor.CobolPreprocessor.CobolSourceFormatEnum format = io.proleap.cobol.preprocessor.CobolPreprocessor.CobolSourceFormatEnum.TANDYEM;
    io.proleap.cobol.asg.metamodel.Program program = new io.proleap.cobol.asg.runner.impl.CobolParserRunnerImpl().analyzeFile(inputFile, format);
    
    // navigate on ASG
    io.proleap.cobol.asg.metamodel.CompilationUnit compilationUnit = program.getCompilationUnit("HelloWorld");
    io.proleap.cobol.asg.metamodel.ProgramUnit programUnit = compilationUnit.getProgramUnit();
    io.proleap.cobol.asg.metamodel.data.DataDivision dataDivision = programUnit.getDataDivision();
    io.proleap.cobol.asg.metamodel.data.datadescription.DataDescriptionEntry dataDescriptionEntry = dataDivision.getWorkingStorageSection().getDataDescriptionEntry("ITEMS");
    Integer levelNumber = dataDescriptionEntry.getLevelNumber();
  5. Traverse the Abstract Syntax Tree (AST) using a Visitor

    main

    To traverse the AST, extend CobolBaseVisitor<T>. Within your visitor methods, you can retrieve the corresponding ASG element from the Program instance using the getASGElementRegistry().getASGElement(ctx) method, where ctx is the parser context. This allows you to bridge the gap between the syntax tree (AST) and the semantic model (ASG).

    // generate ASG from plain COBOL code
    java.io.File inputFile = new java.io.File("src/test/resources/io/proleap/cobol/asg/HelloWorld.cbl");
    io.proleap.cobol.preprocessor.CobolSourceFormatEnum format = io.proleap.cobol.preprocessor.CobolSourceFormatEnum.TANDEM;
    io.proleap.cobol.asg.metamodel.Program program = new io.proleap.cobol.asg.runner.impl.CobolParserRunnerImpl().analyzeFile(inputFile, format);
    
    // traverse the AST
    io.proleap.cobol.CobolBaseVisitor<Boolean> visitor = new io.proleap.cobol.CobolBaseVisitor<Boolean>() {
      @Override
      public Boolean visitDataDescriptionEntryFormat1(final io.proleap.cobol.CobolParser.DataDescriptionEntryFormat1Context ctx) {
        io.proleap.cobol.asg.metamodel.data.datadescription.DataDescriptionEntry entry = (io.proleap.cobol.asg.metamodel.data.datadescription.DataDescriptionEntry) program.getASGElementRegistry().getASGElement(ctx);
        String name = entry.getName();
    
        return visitChildren(ctx);
      }
    };
    
    for (final io.proleap.cobol.asg.metamodel.CompilationUnit compilationUnit : program.getCompilationUnits()) {
      visitor.visit(compilationUnit.getCtx());
    }
  6. Use the Index interface in the ASG metamodel

    main

    The Index interface represents an index description within the Abstract Semantic Graph (ASG). It extends CobolDivisionElement and NamedElement, meaning it is a named element belonging to a COBOL division. It is used to manage and retrieve IndexCall objects associated with the index.

    Key methods:

    • addCall(IndexCall call): Associates a new IndexCall with this index.
    • getCalls(): Returns a List<IndexCall> representing all calls associated with this index.
    // Example usage of the Index interface
    Index myIndex = ...;
    IndexCall myCall = ...;
    
    // Add a call to the index
    myIndex.addCall(myCall);
    
    // Retrieve all calls associated with the index
    List<IndexCall> calls = myIndex.getCalls();