GithubHelp home page GithubHelp logo

albertmcma / cobol85parser Goto Github PK

View Code? Open in Web Editor NEW

This project forked from uwol/proleap-cobol-parser

0.0 1.0 0.0 6.48 MB

ProLeap ANTLR4-based parser for COBOL

Home Page: http://proleap.io

License: MIT License

ANTLR 0.29% Java 9.24% COBOL 90.47%

cobol85parser's Introduction

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.

๐Ÿ’ซ Star if you like our work.

Build Coverage License: MIT ProLeap on Twitter

Example

Input: COBOL code

 Identification Division.
 Program-ID.
  HELLOWORLD.
 Procedure Division.
  Display "Hello world".
  STOP RUN.

Output: Abstract Syntax Tree (AST)

(startRule
	(compilationUnit
		(programUnit
			(identificationDivision Identification Division .
				(programIdParagraph Program-ID .
					(programName
						(cobolWord HELLOWORLD)) .))
			(procedureDivision Procedure Division .
				(procedureDivisionBody
					(paragraphs
						(sentence
							(statement
								(displayStatement Display
									(displayOperand
										(literal "Hello world")))) .)
						(sentence
							(statement
								(stopStatement STOP RUN))) .)))))) <EOF>)

Getting started

To include the parser in your Maven project edit your pom.xml file as follows

<repositories>
	<repository>
		<id>maven.proleap.io</id>
		<url>http://maven.proleap.io</url>
	</repository>
</repositories>
<dependency>
	<groupId>io.github.uwol</groupId>
	<artifactId>cobol85parser</artifactId>
	<version>2.4.0</version>
</dependency>

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

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

// 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.TANDEM;
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();

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

// 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.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.Cobol85BaseVisitor<Boolean> visitor = new io.proleap.cobol.Cobol85BaseVisitor<Boolean>() {
  @Override
  public Boolean visitDataDescriptionEntryFormat1(final io.proleap.cobol.Cobol85Parser.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());
}

Where to look next

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 8 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:
$ 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:
[INFO] Scanning for projects...
...
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running io.proleap.cobol.ast.fixed.FixedTest
Preprocessing file Fixed.cbl.
Parsing file Fixed.cbl.
Comparing parse tree with file Fixed.cbl.tree.
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.202 sec
Running io.proleap.cobol.ast.fixed.QuotesInCommentEntryTest
...
Results :

Tests run: 680, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
  • To install the JAR in your local Maven repository:
$ mvn clean install
  • To only run the tests:
$ mvn clean test

Release process

  • Releases and snapshots are published in the MAVEN repository maven.proleap.io.
  • Milestones of the grammar are published in the ANTLR grammars repo.

License

Licensed under the MIT License. See LICENSE for details.

cobol85parser's People

Contributors

uwol avatar slesa avatar httpdigest avatar sebdei avatar

Watchers

albertmcma avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.