This is an approximate grammar and parser for Cobol 85, which generates an Abstract Syntax Tree (AST) and Abstract Semantic Graph (ASG) for COBOL 85 code.
The AST represents plain COBOL source code in a syntax tree structure. The ASG is generated from the AST by a semantic analysis and provides data and control flow information (e. g. variable access).
The grammar is akin but neither copied from nor identical to the Cobol.jj, Cobol.kg and VS COBOL II grammars.
-
Passes NIST test suite.
-
To be used in conjunction with the provided preprocessor, which executes COPY and REPLACE statements.
Identification Division.
Program-ID.
HELLOWORLD.
Procedure Division.
Display "Hello world".
STOP RUN.
(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>)
io.proleap.cobol.parser.applicationcontext.CobolParserContextFactory.configureDefaultApplicationContext();
java.io.File inputFile = new java.io.File("src/test/resources/io/proleap/cobol/gpl/parser/HelloWorld.cbl");
io.proleap.cobol.preprocessor.CobolPreprocessor.CobolSourceFormatEnum format = io.proleap.cobol.preprocessor.CobolPreprocessor.CobolSourceFormatEnum.TANDEM;
io.proleap.cobol.parser.metamodel.Program program = io.proleap.cobol.parser.applicationcontext.CobolParserContext.getInstance().getParserRunner().analyzeFile(inputFile, format);
io.proleap.cobol.parser.metamodel.CompilationUnit compilationUnit = program.getCompilationUnit("HelloWorld");
io.proleap.cobol.parser.metamodel.ProgramUnit programUnit = compilationUnit.getProgramUnit();
io.proleap.cobol.parser.metamodel.data.DataDivision dataDivision = programUnit.getDataDivision();
io.proleap.cobol.parser.metamodel.data.datadescription.DataDescriptionEntry dataDescriptionEntry = dataDivision.getWorkingStorageSection().getDataDescriptionEntry("ITEMS");
Integer levelNumber = dataDescriptionEntry.getLevelNumber();
io.proleap.cobol.parser.applicationcontext.CobolParserContextFactory.configureDefaultApplicationContext();
java.io.File inputFile = new java.io.File("src/test/resources/io/proleap/cobol/gpl/parser/HelloWorld.cbl");
io.proleap.cobol.preprocessor.CobolPreprocessor.CobolSourceFormatEnum format = io.proleap.cobol.preprocessor.CobolPreprocessor.CobolSourceFormatEnum.TANDEM;
io.proleap.cobol.parser.metamodel.Program program = io.proleap.cobol.parser.applicationcontext.CobolParserContext.getInstance().getParserRunner().analyzeFile(inputFile, format);
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.parser.metamodel.data.datadescription.DataDescriptionEntry entry = (io.proleap.cobol.parser.metamodel.data.datadescription.DataDescriptionEntry) io.proleap.cobol.parser.applicationcontext.CobolParserContext.getInstance().getASGElementRegistry().getASGElement(ctx);
String name = entry.getName();
return visitChildren(ctx);
}
};
for (final io.proleap.cobol.parser.metamodel.CompilationUnit compilationUnit : program.getCompilationUnits()) {
visitor.visit(compilationUnit.getCtx());
}
- The build process is based on Maven (version 3 or higher). Building requires a JDK 8.
- To build, run:
$ mvn clean package
- The test suite executes tests against NIST, CICS and some GPLed Cobol 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 (e.g. org/cobol85/gpl/fixed).
- To only run the tests:
$ mvn clean test
- You should see output like this:
[INFO] Scanning for projects...
...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running io.proleap.cobol.gpl.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.gpl.fixed.QuotesInCommentEntryTest
...
Results :
Tests run: 622, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
- To install the JAR in your local Maven repository:
$ mvn clean install
- To use it in your Maven project, add following dependency to your pom.xml:
<dependency>
<groupId>io.github.uwol</groupId>
<artifactId>cobol85parser</artifactId>
<version>2.0.0</version>
</dependency>
- For parsing large Cobol source code files, VM args have to be set: -Xmx2048m -XX:MaxPermSize=256m
- Intellij Plugin for ANTLR 4 has to be provided with those VM args in file idea.vmoptions.
- Milestones are published in the ANTLR grammars repo.
Licensed under the BSD 3-Clause License. See LICENSE for details.
Patches accepted, or create an issue! I'd love, if you could send me a note in which context you're using the grammar. Thank you!
This fork is actively modified. Sometimes a test may fail. To skip tests, so as to debug via a client of this , skip tests during installation:
mvn clean install -DskipTests
-- Lou Mauget, [email protected]