NoneType 对象不可调用制作词法分析器

问题描述

我的代码

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","-",TokenDef("aterisk","*",TokenDef("slash","/",TokenDef("left_paren","(",TokenDef("right_paren",")",TokenDef("integer",re.compile("[0-9]+"),int),TokenDef("whitespace",re.compile("[ \t]+"),]

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_,start + len(token_text)))
TypeError: 'nonetype' object is not callable

为什么会这样?

我正在尝试制作词法分析器,我从教程开始,但我找不到我的错误在哪里。我在 jupyter notebook 上,使用 windows 10 pro,我的 python 版本是 3.9.0(anaconda 环境)。什么是 nonetype 对象? idk 这很奇怪,但是 idk。

解决方法

我看到了你的问题,你有两个独立的东西,叫做 token

token = namedtuple("Token",("type","value","slice"))

是您想要用作调用的东西,将另一个令牌重命名为 my_token 之类的东西

更改以下几行:

token = None
token = token(type_,match_value,slice(start,start + len(token_text)))
return token

my_token = None
my_token = token(type_,start + len(token_text)))
return my_token