Rule name 'state' python3 gives error ('int' object is not callable)

61 views
Skip to first unread message

Jorg Visch

unread,
Dec 12, 2021, 8:46:37 AM12/12/21
to antlr-discussion
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

eric vergnaud

unread,
Dec 31, 2021, 8:58:12 AM12/31/21
to antlr-discussion
Hi,

there is ongoing conversation on this topic amongst antlr authors.
This is NOT a bug, rather a decision made 25 years ago, that forces grammar writers to avoid keywords and other reserved words.
With the proliferation of targets (a good thing!) there is no foreseeable solution on the short term without breaking thousands of implementation relying on antlr.
So for now, the only way is to use non-colliding rule names.

Jorg Visch

unread,
Jan 10, 2022, 9:50:21 AM1/10/22
to antlr-discussion
Hi,

Thanks for your reply, renaming is ok for me. Is it possible to add the keyword 'state' to the list of reserved words? It took me quite a while before I realized the error 'int object is not callable' wasn't caused by my grammar (I'm new to Anltr and grammars).

thanx,
Jorg
Reply all
Reply to author
Forward
0 new messages