grammars-v4 icon indicating copy to clipboard operation
grammars-v4 copied to clipboard

[cpp] Using the cpp grammar with the python target

Open pasto9 opened this issue 2 years ago • 4 comments

I try to use the cpp grammar with the python target. With the latest fix #3603 antlr does not generate proper python code. So I tried to fix it and noticed that the new pureSpecifier implementation has the problem that python does not use the this keyword. Therefore this line in the grammer produces incorrect code in python.

I think this could be fixed if instead of this.IsPureSpecifierAllowed() $parser.IsPureSpecifierAllowed() would be used. This way it should generate correct code for python and c# (only tested this one).

I'm new to python and antlr so I hope this is not a stupid suggestion.

pasto9 avatar Jul 17 '23 10:07 pasto9

Sorry, I didn't write the port for Python3 yet. I only had time/patience to port the existing targets and add CSharp.

There is no port for the Python3 target. The way to check if a target exists is to look in the desc.xml.

Unfortunately, $parser is not sufficient either. This is because both the parser object reference and the dereference operator (".", or "->") are target specific. So, this cannot work for Cpp or PHP. But, even considering the rest of the targets, $parser is insufficient because the APIs between runtimes are so different. The currently "approved/automated" hack is to provide a base class with "transformGrammar.py" file to replace the reference and operator together.

There is a PR in Antlr to allow StringTemplates attribute replacements in actions in a grammar. This is a much cleaner solution to the whole problem.

Nevertheless, ports for specific targets will need to be written still.

kaby76 avatar Jul 17 '23 14:07 kaby76

Making a PR for the rest of the targets. https://github.com/antlr/grammars-v4/pull/3619

kaby76 avatar Jul 17 '23 16:07 kaby76

The Python3 port now works. https://github.com/antlr/grammars-v4/pull/3619

kaby76 avatar Jul 17 '23 18:07 kaby76

Thank you very much for your work. It helps a lot.

pasto9 avatar Jul 18 '23 07:07 pasto9