0

ANTLR4 allows to put code in arbitrary programming language in grammars. But is it possible to put code in different programming languages and select only those that are specified in the target programming language during code-generation?

For example if I have actions in Java and Python can I somehow select only those of them that are in Python when I generate the parsing code?

Gill Bates
  • 14,330
  • 23
  • 70
  • 138
  • 4
    No. And you sohuld avoid this feature as much as possible BTW to get a clean grammar. Use visitors on the parse tree to run your logic. – Lucas Trzesniewski Sep 18 '17 at 15:30
  • @LucasTrzesniewski, if you post it as an answer and provide some cites from authoritative sources I'll accept it. – Gill Bates Sep 18 '17 at 15:52
  • 2
    I don't have an authoritative source other than [the documented options](https://github.com/antlr/antlr4/blob/master/doc/options.md) which only let you set a single language. But as both Terence Parr and Sam Harwell are active on SO you may as well get an official answer directly from the authors. – Lucas Trzesniewski Sep 18 '17 at 16:02
  • Is this a duplicate of [this](https://stackoverflow.com/questions/46084345/antlr-for-multiple-language-generation) ? – NWS Sep 18 '17 at 19:08
  • 3
    Lucas is correct. I'm the author of the C++ target and I know what's possible and what's not. The way the target files are generated is based on a template engine called String Templates (ST4), also written by Terence Parr. These allow for conditional replacements, yes, but there's no mechanism that allows to do that based on the target language. Implementing that wouldn't be so difficult, but the general tendency is to avoid target language code in a grammar, especially when you want to share it, since it complicates adoption. – Mike Lischke Sep 19 '17 at 07:10

0 Answers0