antlr / grammars-v4

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

[rexx] Add Python target support #2747

Closed RossPatterson closed 2 years ago

RossPatterson commented 2 years ago

The Rexx parser contains a small amount of Java code in a couple of semating predicates. There does not appear to be a way to accomplish the parse without the predicates. I would like to build a Python target, as I'm more comfortable working in Python than in Java.

I am working on a change to export the Java code to a parser base class, and to create a similar Python base class. I would prefer to make the grammar target-agnosftic, but it does not seem possible, so I'll reluctantly settle for multi-target.

@kaby76 - If your bag of tricks has something that might help me go fully agnostic, I'd love to hear about it.

kaby76 commented 2 years ago

Should be easy to make it work with all targets. There's only one predicate in the parser and lexer. Model in after the python/python/ or python/python3/ grammar.

RossPatterson commented 2 years ago

The transformGrammar.py thing is a great idea, but I don't think I'll need it. I've got a working change to the lexer that eliminates the only predicate it has, which gets me around the lack of a $lexer attribute. And I can use $parser to get around the self-vs-this problem in the Java and Python parsers. If and when Rexx gets changed to handle other targets like C++, where I'd need to replace $parser.whatever() with $parser->whatever(), then I'll certainly create a transformGrammar.py.

It would be cool if ANTLR4 had something like the syntax suggested in the Ways of keeping ANTLR4 grammar target independent StackOverflow question, or in @udif's comment on https://github.com/antlr/antlr4/issues/1045#issuecomment-1216250170 but having taken a quick look at the codegen support, I'm not going to propose anything like that, at least not yet.