Python 不确定我做错了什么…-| | |蟒蛇|||

Python 不确定我做错了什么…-| | |蟒蛇|||,python,programming-languages,Python,Programming Languages,我正在做一件事,同时遵循一个教程。我想我做的每件事都是正确的,但是当启动程序时,我遇到了一个错误 以下是我的文件代码: 1) 主文件-frs.py from parser import Parser from lexer import Lexer def main(): filename = 'hello.frs' file = open(filename, 'r') lexer = Lexer(file) parser = Parser(le

我正在做一件事,同时遵循一个教程。我想我做的每件事都是正确的,但是当启动程序时,我遇到了一个错误

以下是我的文件代码:

1) 主文件-frs.py

from parser import Parser
from lexer import Lexer

def main():
    filename = 'hello.frs'
    file     = open(filename, 'r')
    lexer    = Lexer(file)
    parser   = Parser(lexer.tokens)

    lexer.tokenizer()
    print ("TOKENS:")
    print (lexer.tokens, "\n")

    parser.build_AST()
    print ("AST:")
    print (parset.AST, "\n")


if __name__ == "__main__":
    main()
2) Lexer类-Lexer.py

class Lexer:

    def __init__(self, data):
        self.data = data
        self.tokens = []
        self.keywords = [
            'tosay'
        ]

    def tokenizer(self):
        for loc in self.data:
            tmp = []
            tid = ''

            for l in loc:
                if l == '"' and tid == '':
                    tid = 'char'
                    tmp = []
                elif l == '"' and tid == 'char':
                    self.tokens.append({'id': tid, 'value': ''.join(tmp)})
                    tid = ''
                    tmp = []
                elif l == ':':
                    self.tokens.append({'id': 'label', 'value': ''.join(tmp)})
                    tmp = []
                elif ''.join(tmp) in self.keywords:
                    self.tokens.append({'id': 'keyword', 'value': ''.join(tmp)})
                    tmp = []
                elif l == ' ' and tid != 'char':
                    continue
                else:
                    tmp.append(l)
3) 解析器类-Parser.py

class Parser:
    def __init__(self, tokens):
        self.tokens = tokens
        self.AST    = []

    def add_node(self, parent, node):
        for a in self.AST:
            if parent in a:
                a[parent].append(node)

    def build_AST(self):
        saved   = {}
        parent  = {}
        collect = False

        for token in self.tokens:
            if token['id'] == 'label':
                t = {token['value']: []}

                if parent != t:
                    parent = token['value']
                    self.AST.append(t)

            elif token['id'] == 'keyword':
                if token['value'] == 'stop':
                    t = {token['value']: 0}
                    self.add_node(parent, t)
                else:
                    if collect ==  False:
                        saved = token
                        collect = True
                    else:
                        t = {saved['value']: token[:value]}
                        self.add_node(parent, t)
                        collect = False

            elif token['id'] == 'char':
                if collect = False:
                    saved = token
                    collect = True
                else:
                    t = {saved['value']: token['value']}
                    self.add_node(parent, t)
                    collect = False
4) 该文件使用我自己的语言编写,是本教程的目标-hello.frs:

commence:
    tosay "Hello World"
stop
基本上,在我从解析器导入解析器添加
之前,一切都正常。但添加后,我收到以下错误消息:

Traceback (most recent call last):
 File "frs.py", line 1, in <module>
  from parser import Parser
ImportError: cannot import name 'Parser'
回溯(最近一次呼叫最后一次):
文件“frs.py”,第1行,在
从解析器导入解析器
ImportError:无法导入名称“Parser”
我试着重新命名这个类,但仍然不起作用。 请帮帮我!
提前感谢您。

您的文件中有两个错误

1) 文件parser.py:

更改:

if collect = False:
print (parset.AST, "\n")

2) 文件frs.py

更改:

if collect = False:
print (parset.AST, "\n")
致:

经过上述更正后,我的输出

TOKENS:
[{'id': 'label', 'value': 'commence'}, {'id': 'keyword', 'value': 'tosay'}, {'id': 'char', 'value': 'Hello World'}] 

AST:
[{'commence': [{'tosay': 'Hello World'}]}] 

parser.py有一个错误。如果collect=False:
更改为
如果collect==False:
。frs.py具有eerror。将
print(parset.AST,“\n”)
更改为`print(parset.AST,“\n”)`。我修复了“parset”和“collect=”stuff,但它仍然说我无法导入它。