I'm learning on how to use Antlr4 to parse COBOL source codes. Currently, I'm following the steps, exactly as demonstrated by Enam Biswas in his Youtube video.
Basically, I've downloaded antlr-4.7.1-complete.jar
and placed it in C:\Javalib
. Yes, I've also include the path into my Windows environment and created the antlr.bat
and grun.bat
files.
For the grammar files, I'm using Cobol85.g4 and Cobol85Preprocessor.g4 which were taken from Ulrich Wolffgang github. On the same time, I use HellowWorl.cbl sample source code to see how the parsing works.
After running the antlr.bat
, I executed the command below:
C:\Users\ffa\Desktop\COBOL>grun Cobol85Preprocessor startRule HellowWorld.cbl
As the result, I received the error message as shown below:
Warning: TestRig moved to org.antlr.v4.gui.TestRig; calling automatically
Can't load Cobol85.g4 as lexer or parser
As I'm not sure why I can't get it parsed as shown in the video, I also attempted below commands:
C:\Users\ffa\Desktop\COBOL>grun Cobol85 startRule HellowWorld.cbl
and
C:\Users\ffa\Desktop\COBOL>grun Cobol85* startRule HellowWorld.cbl
End up, I still get the same error message. So, I did my search through Google and found a suggestion to download antlr-runtime-4.7.1.jar
. So, I downloaded the file and placed it in the same directory which is located at C:\Javalib
.
When I executed the commands above, this time, I received a different message
Error: Could not find or load main class org.antlr.v4.runtime.misc.TestRig
Could anyone please assist me to parse the COBOL source code with Antlr4? It would also be good if someone could explain the difference between Cobol85.g4
and Cobol85Preprocessor.g4
.
Disclaimer: I am the author of these COBOL ANTLR4 grammar files.
The parser generated from grammar Cobol85.g4
has to be provided with COBOL source code, which has been preprocessed with a COBOL preprocessor. Cobol85Preprocessor.g4
is at the core of this preprocessor and enables parsing of statements such as COPY REPLACE
, EXEC SQL
etc.
Cobol85Preprocessor.g4
is meant to be augmented with quite extensive additional logic, which is not included in the grammar files and enables normalization of line formats, line breaks, comment lines, comment entries, EXEC SQL, EXEC CICS and so on.
The ProLeap COBOL parser written by me implements all of this in Java based on the files Cobol.g4 and Cobol85Preprocessor.g4.