antlr / grammars-v4

Grammars written for ANTLR v4; expectation that the grammars are free of actions.
MIT License
10.15k stars 3.7k forks source link

Inquiry about the purpose and use of Cobol85Preprocessor.g4 parser #3716

Open momah opened 1 year ago

momah commented 1 year ago

I want to create a COBOL visitor to translate a COBOL source code into another language using the visitor generated by ANTLR4. I have been going through the repository and I've noticed that there are two ANTLR grammars present: Cobol85.g4 and Cobol85Preprocessor.g4. While the purpose of Cobol85.g4 seems to be related to parsing COBOL code itself, I would like to better understand the utility of Cobol85Preprocessor.g4.

Usually, when working with source code using ANTLR, a visitor is generated from the grammar. So, what would be the process for working in ANTLR4 in this case? If I run both grammars, will they generate two visitors? In that case, which one do I use between these two visitors? Or is it that one visitor prepares the COBOL sources and preprocesses them to generate a different type of COBOL, which will be the input for the second grammar? If so, how would that be done? I apologize for so many questions, but it's due to my limited knowledge of the framework. Thank you in advance."

https://github.com/antlr/grammars-v4/blob/master/cobol85/Cobol85.g4 https://github.com/antlr/grammars-v4/blob/master/cobol85/Cobol85Preprocessor.g4

kaby76 commented 1 year ago

As far as using the grammars, see https://stackoverflow.com/a/50438417/4779853. The author gives a link to code that uses both grammars.

momah commented 1 year ago

Thank you very much for the response. I have reviewed the ProLeap COBOL parser repository with the implementation. However, I want to create my own implementation based on your COBOL grammar.

The issue is that I still don't understand the process followed to generate the visitor from the two grammars, the main grammar Cobol.g4, and the preprocessing grammar Cobol85Preprocessor.g4.

What would be the workflow for working in ANTLR4 with both grammars? Will they generate two visitors in parallel? Or is it that one visitor converts the COBOL sources to generate a different type of COBOL, which will be the input for the second grammar and generate the final visitor? How would that be done?