我的代码:
import re
from collections import namedtuple
class TokenDef(namedtuple("TokenDef", ("name", "pattern", "value_filter"))):
def __repr_(self):
return "TokenType." + self.name
class TokenType(object):
_defs = [
TokenDef("plus", "+", None),
TokenDef("minus", "-", None),
TokenDef("aterisk", "*", None),
TokenDef("slash", "/", None),
TokenDef("left_paren", "(", None),
TokenDef("right_paren", ")", None),
TokenDef("integer", re.compile("[0-9]+"), int),
TokenDef("whitespace", re.compile("[ \t]+"), None),
]
for def_ in TokenType._defs:
setattr(TokenType, def_.name, def_)
token = namedtuple("Token", ("type", "value", "slice"))
def first_token(text, start=0):
match_text = text[start:]
token = None
token_text = None
for type_ in TokenType._defs:
name, pattern, value_filter = type_
if pattern is None:
continue
elif isinstance(pattern, str):
if not match_text.startswith:
continue
match_value = pattern
else:
match = pattern.match(match_text)
if not match:
continue
match_value = match.group(0)
if token_text is not None and len(token_text) >= len(match_value):
continue
token_text = match_value
if value_filter is not None:
match_value = value_filter(match_value)
token = token(type_, match_value, slice(start, start + len(token_text)))
return token
first_token("6")
我的错误:
Traceback (most recent call last):
File ".\lexer.py", line 64, in <module>
first_token("6")
File ".\lexer.py", line 60, in first_token
token = token(type_, match_value, slice(start, start + len(token_text)))
TypeError: 'NoneType' object is not callable
为什么会这样
我正在试着做一个词汇量表,我正在做一个教程,但是我找不到我的错误在哪里。我在jupyter笔记本上,使用Windows10Pro,我的python版本是3.9.0(anaconda环境)。什么是非类型对象?这很奇怪,但我知道
我看到你的问题了,你有两个不同的东西叫做
token
如果要将其用作调用,请将另一个令牌重命名为类似于my_令牌的内容
更改以下行:
到
相关问题 更多 >
编程相关推荐