antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.22k stars 3.29k forks source link

Generate python3 parser file contains a syntax error? #4438

Open ldorigo opened 1 year ago

ldorigo commented 1 year ago

I tried generating parsers/lexers using the files at https://github.com/ethereum/solidity/blob/develop/docs/grammar/SolidityLexer.g4 and https://github.com/ethereum/solidity/blob/develop/docs/grammar/SolidityParser.g4 using target=Python3.

The generated Python parser contains syntax errors (exclamation marks before variables and "true" instead of "True"?), e.g.:

                    elif la_ == 4:
                        self.state = 392
[SolidityParser.py.txt](https://github.com/antlr/antlr4/files/12861363/SolidityParser.py.txt)

                        if not !localctx.visibilitySet:
                            from antlr4.error.Errors import FailedPredicateException
                            raise FailedPredicateException(self, "!$visibilitySet")
                        self.state = 393
                        self.match(SolidityParser.Public)
                        localctx.visibilitySet =  true
                        pass

I attach the generated file for reference. This is with antlr 4.13.1.

ldorigo commented 1 year ago

SolidityParser.py.txt Forgot the attachement

ldorigo commented 1 year ago

For reference, the exact commands I used are:

antlr4 -Dlanguage=Python3 SolidityLexer.g4
antlr4 -Dlanguage=Python3 SolidityParser.g4
kaby76 commented 1 year ago

That grammar, and in particular SolidityParser.g4, contains locals and actions, both of which are in a target specific language. While it is not declared with a language option, it is most likely Java. Python3 does not have type declarations, and True and False are the literals in the language, not true and false. You will need to make a port of the grammar for Python3. There is a target-independent grammar in grammars-v4, but it may be quite old.