项目作者: uwol

项目描述 :
ProLeap ANTLR4-based parser for COBOL
高级语言: COBOL
项目地址: git://github.com/uwol/proleap-cobol-parser.git
创建时间: 2015-05-25T17:06:25Z
项目社区:https://github.com/uwol/proleap-cobol-parser

开源协议:MIT License

下载


ProLeap ANTLR4-based parser for COBOL

This is a COBOL parser based on an ANTLR4 grammar,
which generates an Abstract Syntax Tree (AST) and Abstract Semantic Graph (ASG) for COBOL code.
The AST represents plain COBOL source code in a syntax tree structure.
The ASG is generated from the AST by semantic analysis and provides data and control
flow information (e. g. variable access). EXEC SQL, EXEC SQLIMS and EXEC CICS
statements are extracted as texts.

The parser is developed test-driven, passes the NIST test suite and has successfully been applied to numerous COBOL files from banking and insurance. It is used by the ProLeap analyzer, interpreter & transformer for COBOL.

💫 Star if you like our work.

License: MIT
ProLeap on Twitter

Example

Input: COBOL code

  1. Identification Division.
  2. Program-ID.
  3. HELLOWORLD.
  4. Procedure Division.
  5. Display "Hello world".
  6. STOP RUN.

Output: Abstract Syntax Tree (AST)

  1. (startRule
  2. (compilationUnit
  3. (programUnit
  4. (identificationDivision Identification Division .
  5. (programIdParagraph Program-ID .
  6. (programName
  7. (cobolWord HELLOWORLD)) .))
  8. (procedureDivision Procedure Division .
  9. (procedureDivisionBody
  10. (paragraphs
  11. (sentence
  12. (statement
  13. (displayStatement Display
  14. (displayOperand
  15. (literal "Hello world")))) .)
  16. (sentence
  17. (statement
  18. (stopStatement STOP RUN))) .)))))) <EOF>)

Getting started

To include the parser in your Maven project build it and add the dependency:

  1. <dependency>
  2. <groupId>io.github.uwol</groupId>
  3. <artifactId>proleap-cobol-parser</artifactId>
  4. <version>4.0.0</version>
  5. </dependency>

Use the following code as a starting point for developing own code.

Simple: Generate an Abstract Semantic Graph (ASG) from COBOL code

  1. // generate ASG from plain COBOL code
  2. java.io.File inputFile = new java.io.File("src/test/resources/io/proleap/cobol/asg/HelloWorld.cbl");
  3. io.proleap.cobol.preprocessor.CobolPreprocessor.CobolSourceFormatEnum format = io.proleap.cobol.preprocessor.CobolPreprocessor.CobolSourceFormatEnum.TANDEM;
  4. io.proleap.cobol.asg.metamodel.Program program = new io.proleap.cobol.asg.runner.impl.CobolParserRunnerImpl().analyzeFile(inputFile, format);
  5. // navigate on ASG
  6. io.proleap.cobol.asg.metamodel.CompilationUnit compilationUnit = program.getCompilationUnit("HelloWorld");
  7. io.proleap.cobol.asg.metamodel.ProgramUnit programUnit = compilationUnit.getProgramUnit();
  8. io.proleap.cobol.asg.metamodel.data.DataDivision dataDivision = programUnit.getDataDivision();
  9. io.proleap.cobol.asg.metamodel.data.datadescription.DataDescriptionEntry dataDescriptionEntry = dataDivision.getWorkingStorageSection().getDataDescriptionEntry("ITEMS");
  10. Integer levelNumber = dataDescriptionEntry.getLevelNumber();

Complex: Generate an Abstract Semantic Graph (ASG) and traverse the Abstract Syntax Tree (AST)

  1. // generate ASG from plain COBOL code
  2. java.io.File inputFile = new java.io.File("src/test/resources/io/proleap/cobol/asg/HelloWorld.cbl");
  3. io.proleap.cobol.preprocessor.CobolPreprocessor.CobolSourceFormatEnum format = io.proleap.cobol.preprocessor.CobolPreprocessor.CobolSourceFormatEnum.TANDEM;
  4. io.proleap.cobol.asg.metamodel.Program program = new io.proleap.cobol.asg.runner.impl.CobolParserRunnerImpl().analyzeFile(inputFile, format);
  5. // traverse the AST
  6. io.proleap.cobol.CobolBaseVisitor<Boolean> visitor = new io.proleap.cobol.CobolBaseVisitor<Boolean>() {
  7. @Override
  8. public Boolean visitDataDescriptionEntryFormat1(final io.proleap.cobol.CobolParser.DataDescriptionEntryFormat1Context ctx) {
  9. io.proleap.cobol.asg.metamodel.data.datadescription.DataDescriptionEntry entry = (io.proleap.cobol.asg.metamodel.data.datadescription.DataDescriptionEntry) program.getASGElementRegistry().getASGElement(ctx);
  10. String name = entry.getName();
  11. return visitChildren(ctx);
  12. }
  13. };
  14. for (final io.proleap.cobol.asg.metamodel.CompilationUnit compilationUnit : program.getCompilationUnits()) {
  15. visitor.visit(compilationUnit.getCtx());
  16. }

Where to look next

How to cite

Please cite ProLeap COBOL parser in your publications, if it helps your research. Here is an example BibTeX entry:

  1. @misc{wolffgang2018cobol,
  2. title={ProLeap COBOL parser},
  3. author={Wolffgang, Ulrich and others},
  4. year={2018},
  5. howpublished={\url{https://github.com/uwol/proleap-cobol-parser}},
  6. }

Features

  • EXEC SQL statements, EXEC SQLIMS statements and EXEC CICS statements are extracted by the preprocessor and provided as texts in the ASG.
  • Passes the NIST test suite.
  • Rigorous test-driven development.
  • To be used in conjunction with the provided preprocessor, which executes COPY, REPLACE, CBL and PROCESS statements.

Build process

The build process is based on Maven (version 3 or higher). Building requires a JDK 17 and generates a Maven JAR, which can be used in other Maven projects as a dependency.

  • Clone or download the repository.
  • In Eclipse import the directory as a an existing Maven project.
  • To build, run:
  1. $ mvn clean package
  • The test suite executes AST and ASG tests against COBOL test code and NIST test files. NIST test files come from Koopa repo. Unit tests and parse tree files were generated by class io.proleap.cobol.TestGenerator from COBOL test files. The generator derives the COBOL line format from the containing folder name.
  • You should see output like this:
  1. [INFO] Scanning for projects...
  2. ...
  3. -------------------------------------------------------
  4. T E S T S
  5. -------------------------------------------------------
  6. Running io.proleap.cobol.ast.fixed.FixedTest
  7. Preprocessing file Fixed.cbl.
  8. Parsing file Fixed.cbl.
  9. Comparing parse tree with file Fixed.cbl.tree.
  10. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.202 sec
  11. Running io.proleap.cobol.ast.fixed.QuotesInCommentEntryTest
  12. ...
  13. Results :
  14. Tests run: 680, Failures: 0, Errors: 0, Skipped: 0
  15. [INFO] ------------------------------------------------------------------------
  16. [INFO] BUILD SUCCESS
  17. [INFO] ------------------------------------------------------------------------
  • To install the JAR in your local Maven repository:
  1. $ mvn clean install
  • To only run the tests:
  1. $ mvn clean test

Release process

License

Licensed under the MIT License. See LICENSE for details.