antlr / grammars-v4

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

[cpp] Using the cpp grammar with the python target #3618

Open pasto9 opened 1 year ago

pasto9 commented 1 year ago

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.

kaby76 commented 1 year ago

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 commented 1 year ago

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

kaby76 commented 1 year ago

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

pasto9 commented 1 year ago

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