0
votes

Is it possible to generate the antlr lexer and parser (from a given g4 grammar) directly within the code be it with the Antlr 4 runtime directly from the Python or C# code?

I think it would be much more convenient that calling the external tool everytime I need.

[EDIT]

It looks that I am looking for something similar to an in memory antlr feature with C# or Python:

1
If I've understood you correctly, you're currently manually running the ANTLR tool whenever you change the grammar and your main motivation for wanting to process the grammar file at runtime is that you want to avoid that manual step in the build process? If so, a much simpler alternative would be to simply integrate ANTLR into your build system, so the ANTLR tool is automatically run when you compile and/or run the project and the grammar file has changed since the last time.sepp2k

1 Answers

3
votes

The code to parse ANTLR4 grammars and convert them to an ATN + generating the target files is written in Java. This tool code is not translated to the target language (only the runtime is), so it is not possible to do the same job in other languages. That inmenantlr project only uses the Java code from ANTLR4 in its own Java code to do the same thing, except for the need to run it as an external jar.

The only way to make your wish possible would be to translate all the tool code also to the target language.

However, depending on your needs there's a way to generate a parser interpreter for your target language. I have done this in my vscode-antlr4 extension, where users can debug their ANTLR4 grammars. For that I added an export feature of the data required for the interpreter to ANTLR4 (it's available there since 4.7.2). This data can then be used to set up the lexer + parser interpreters (which are translated to the target language) to parse a file with that grammar. These interpreters use the same prediction engine as the generated parsers, but do not keep parse contexts, variables etc.