Reputation: 61
I am trying to use lexer.py and parser.py in python , but it has error in this code :
import antlr3
import antlr3.tree
import traceback
from test22Lexer import test22Lexer
from test22Parser import test22Parser
char_stream = antlr3.ANTLRStringStream("input.txt")
lexer = test22Lexer(char_stream)
tokens = antlr3.CommonTokenStream(lexer)
parser = test22Parser(tokens)
parser.block()
and the error is :
class block_return(ParserRuleReturnScope):
NameError: name 'ParserRuleReturnScope' is not defined
I need help :D
parser :
class block_return(ParserRuleReturnScope):
def __init__(self):
super(test22Parser.block_return, self).__init__()
self.tree = None
Upvotes: 6
Views: 1666
Reputation: 1576
I just had the exact same problem. I went through the egg looking for the class (ParserRuleReturnScope), and it did not appear to exist. This was using version 3.0.1. I was then able to find the 3.1.3 runtime, and the associated version of the ANTLR generator JAR file. Once I generated using 3.1.3, the problem went away. So, I would recommend installing 3.1.3, here are the relevant links I used:
ANTLR generator: https://github.com/antlr/website-antlr3/blob/gh-pages/download/antlr-3.1.3.jar
Download the .jar file, and then run: java -jar path/to/jar/file myGrammar.g
Once your parser/lexer are generated using 3.1.3, use the same version runtime: https://github.com/antlr/website-antlr3/blob/gh-pages/download/Python/antlr_python_runtime-3.1.3-py2.5.egg
You can install that, e.g. with easy_install.
Now the module loads (syntax is correct), but I'm still working through runtime errors. Progress!
Upvotes: 1
Reputation: 2390
Honestly, I know nothing of ANTLR, but you should have more faith in Python's error message:
NameError: name 'ParserRuleReturnScope' is not defined
It clearly indicates that it's not defined, so you forgot to include it. As you can see in Python ANTLR page, they suggest including it with a wildcard, like:
from antlr3 import *
which would make it work because, if I'm not mistaken, ParserRuleReturnScope
is defined in the antlr3
package. If you don't want to import everything in antlr
, you could import it as you do and prefix all objec by the package, like: altlr3.ParserRuleReturnScope
.
As I wrote, I know nothing of ANTLR, sorry if it doesn't work, I tried to help :)
Upvotes: 0