我将 Antlr4 与 Python3 结合使用。
我有一个 getTerminalPos
方法,它返回给定终端的行号和列号的元组。为此,我首先使用 .getSymbol()
获取 token ,然后使用 .getLine()
和 .getCharPositionInLine()
方法获得职位。
def getTerminalPos(self, terminal):
token = terminal.getSymbol()
return (token.getLine(), token.getCharPositionInLine())
在 antlr 访问者内部调用 getTerminalPos
的示例:
def visitAtom(self, ctx):
if ctx.LPAREN():
return self.visitExpr(ctx.expr())
if ctx.INT():
return nodes.Number(int(ctx.INT().getText()), getTerminalPos(ctx.INT()))
if ctx.FLOAT():
return nodes.Number(float(ctx.FLOAT().getText()), getTerminalPos(ctx.FLOAT()))
当我运行代码时,收到以下错误消息:
File ".../py-antlr4-lmaspl/AntlrVisitor.py", line 55, in getTerminalPos
return (token.getLine(), token.getCharPositionInLine())
AttributeError: 'CommonToken' object has no attribute 'getLine'
根据 Antlr4 Java 运行时,存在以下方法:https://www.antlr.org/api/Java/org/antlr/v4/runtime/CommonToken.html
根据 Antlr3 Python 运行时,存在以下方法:https://www.antlr3.org/api/Python/classantlr3_1_1_common_token.html
那么,它们也应该存在于 Antlr4 Python 运行时中吗?
如何修复此错误?我应该使用一组不同的方法来获取行号和列号吗?
编辑:我的意思是我在这里发现了类似的问题:https://github.com/antlr/antlr4/issues/1529 。它被标记为错误,但暂时关闭...
最佳答案
如果我查看 Python 3 运行时的源代码,我会看到 CommonToken
like this :
class CommonToken(Token):
# An empty {@link Pair} which is used as the default value of
# {@link #source} for tokens that do not have a source.
EMPTY_SOURCE = (None, None)
def __init__(self, source = EMPTY_SOURCE, type = None, channel=Token.DEFAULT_CHANNEL, start=-1, stop=-1):
super(CommonToken, self).__init__()
self.source = source
self.type = type
self.channel = channel
self.start = start
self.stop = stop
self.tokenIndex = -1
if source[0] is not None:
self.line = source[0].line
self.column = source[0].column
else:
self.column = -1
...
和 token
如下所示:
class Token (object):
...
def __init__(self):
self.source = None
self.type = None # token type of the token
self.channel = None # The parser ignores everything not on DEFAULT_CHANNEL
self.start = None # optional; return -1 if not implemented.
self.stop = None # optional; return -1 if not implemented.
self.tokenIndex = None # from 0..n-1 of the token object in the input stream
self.line = None # line=1..n of the 1st character
self.column = None # beginning of the line at which it occurs, 0..n-1
self._text = None # text of the token.
...
所以,我猜这应该适合你:
return (token.line, token.column)
关于Python3 Antlr4属性错误: 'CommonToken' object has no attribute 'getLine' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55414958/