Hi,
I'm creating a (simple) grammar for defining state machines. When using the Python3 runtime, the parser gives the error 'TypeError: 'int' object is not callable' when one of the rules is named 'state'. I'm using version 4.3.9 (Docker on windows).
With the following grammar the error can be reproduced (statemachineproject.g4)
grammar statemachineproject;
statemachine: state ;
state : 'state' ID ;
ID : [a-z]+ ; // match lower-case identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
Parsing the grammar with Docker
docker run --rm -v "$(pwd):/work" antlr/antlr4 -Dlanguage=Python3 -o statemachineproject_py statemachineproject.g4
The python code (parser.py):
from os import name
import sys
from antlr4 import *
from statemachineproject_py.statemachineprojectLexer import statemachineprojectLexer
from statemachineproject_py.statemachineprojectParser import statemachineprojectParser
from statemachineproject_py.statemachineprojectListener import statemachineprojectListener
def main(argv):
input_stream = FileStream(argv[1])
lexer = statemachineprojectLexer(input_stream)
stream = CommonTokenStream(lexer)
parser = statemachineprojectParser(stream)
tree = parser.statemachine()
if __name__ == '__main__':
main(sys.argv)
Example file to parse (test.txt):
state closed
When callling the parser the error occures:
python3 parser.py test.txt
The class Recognizer.py contains a property named 'state' which conflicts with the method state generated by the target Python3. I consider this is a bug, but don't know a good solution for it. For now I'm renaming the rule, but 'state' is a very common name so a better solution is preferable I think.
Has anyone a better solution than renaming the rule? Or is this a bug that should be fixed?
thanks